-
Notifications
You must be signed in to change notification settings - Fork 751
add a scope class to manage the context of interaction with Python an… #381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
8e0b1a2
72003aa
efd3798
9f50e25
add5ba8
c15555c
b0d57e9
ceaaef0
dec39c7
e117d60
904d9ed
5484451
2e063c2
dd492f4
df6a49a
f35d75b
497d7aa
60ce28b
ebf5a2b
2c3db3d
30543eb
b9dcdac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,15 +44,16 @@ internal PyScope(string name) | |
InitVariables(); | ||
} | ||
|
||
//To compitable with the python module | ||
//To compitable with the python module. | ||
//Enable locals() and glocals() usage in the python script. | ||
private void InitVariables() | ||
{ | ||
SetVariable("locals", (Func<PyDict>)Variables); | ||
SetVariable("globals", (Func<PyDict>)Variables); | ||
} | ||
|
||
/// <summary> | ||
/// the dict for local variables | ||
/// the dict for scope variables | ||
/// </summary> | ||
internal IntPtr variables { get; private set; } | ||
|
||
|
@@ -96,23 +97,23 @@ public void ImportScope(PyScope scope) | |
} | ||
|
||
/// <summary> | ||
/// Import Method | ||
/// ImportModule Method | ||
/// </summary> | ||
/// <remarks> | ||
/// The import .. as .. statement in Python. | ||
/// Import a module ,add it to the local variable dict and return the resulting module object as a PyObject. | ||
/// Import a module ,add it to the variables dict and return the resulting module object as a PyObject. | ||
/// </remarks> | ||
public PyObject ImportModule(string name) | ||
{ | ||
return ImportModule(name, name); | ||
} | ||
|
||
/// <summary> | ||
/// Import Method | ||
/// ImportModule Method | ||
/// </summary> | ||
/// <remarks> | ||
/// The import .. as .. statement in Python. | ||
/// Import a module ,add it to the local variable dict and return the resulting module object as a PyObject. | ||
/// Import a module ,add it to the variables dict and return the resulting module object as a PyObject. | ||
/// </remarks> | ||
public PyObject ImportModule(string name, string asname) | ||
{ | ||
|
@@ -126,6 +127,13 @@ public PyObject ImportModule(string name, string asname) | |
return module; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here, imported modules are global names. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the responses below. |
||
} | ||
|
||
/// <summary> | ||
/// Execute method | ||
/// </summary> | ||
/// <remarks> | ||
/// Execute a Python ast and return the result as a PyObject. | ||
/// The ast can be either an expression or stmts. | ||
/// </remarks> | ||
public PyObject Execute(PyObject script) | ||
{ | ||
Check(); | ||
|
@@ -156,9 +164,9 @@ public T ExecuteVariable<T>(string name) | |
PyObject script = GetVariable(name); | ||
return Execute<T>(script); | ||
} | ||
|
||
/// <summary> | ||
/// Evaluate a Python expression | ||
/// Eval method | ||
/// </summary> | ||
/// <remarks> | ||
/// Evaluate a Python expression and return the result as a PyObject | ||
|
@@ -193,7 +201,7 @@ public T Eval<T>(string code) | |
/// Exec Method | ||
/// </summary> | ||
/// <remarks> | ||
/// Evaluate a Python script and save its local variables in the current local variable dict. | ||
/// Exec a Python script and save its local variables in the current local variable dict. | ||
/// </remarks> | ||
public void Exec(string code) | ||
{ | ||
|
@@ -214,13 +222,13 @@ private void Exec(string code, IntPtr _globals, IntPtr _locals) | |
} | ||
Runtime.XDecref(ptr); | ||
} | ||
|
||
/// <summary> | ||
/// SetLocal Method | ||
/// SetVariable Method | ||
/// </summary> | ||
/// <remarks> | ||
/// Add a new variable to local variable dict if it not exists | ||
/// or set the value of the local variable if it exists. | ||
/// Add a new variable to the variables dict if it not exists | ||
/// or update its value if the variable exists. | ||
/// </remarks> | ||
public void SetVariable(string name, object value) | ||
{ | ||
|
@@ -238,10 +246,10 @@ public void SetVariable(string name, object value) | |
} | ||
|
||
/// <summary> | ||
/// DelLocal Method | ||
/// RemoveVariable Method | ||
/// </summary> | ||
/// <remarks> | ||
/// Remove a variable from the local variable dict. | ||
/// Remove a variable from the variables dict. | ||
/// </remarks> | ||
public void RemoveVariable(string name) | ||
{ | ||
|
@@ -257,10 +265,10 @@ public void RemoveVariable(string name) | |
} | ||
|
||
/// <summary> | ||
/// Exists Method | ||
/// ContainsVariable Method | ||
/// </summary> | ||
/// <remarks> | ||
/// Returns true if the variable appears in the local variable dict or the global variable dict. | ||
/// Returns true if the variable exists in the variables dict. | ||
/// </remarks> | ||
public bool ContainsVariable(string name) | ||
{ | ||
|
@@ -270,13 +278,13 @@ public bool ContainsVariable(string name) | |
return Runtime.PyMapping_HasKey(variables, pyKey.obj) != 0; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// GetVariable Method | ||
/// </summary> | ||
/// <remarks> | ||
/// Returns the value of the variable, local variable first. | ||
/// If the variable is not exists, return null. | ||
/// If the variable is not exists, throw an Exception. | ||
/// </remarks> | ||
public PyObject GetVariable(string name) | ||
{ | ||
|
@@ -304,7 +312,7 @@ public PyObject GetVariable(string name) | |
} | ||
|
||
/// <summary> | ||
/// GetVariable Method | ||
/// TryGetVariable Method | ||
/// </summary> | ||
/// <remarks> | ||
/// Returns the value of the variable, local variable first. | ||
|
@@ -380,7 +388,6 @@ public override bool TrySetMember(SetMemberBinder binder, object value) | |
return true; | ||
} | ||
|
||
// Currently, AsManagedObject method cannot accept 'dynamic' for the T parameter | ||
private object ToManagedObject<T>(PyObject pyObj) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cannot agree more. I had planed to replace it for a long time. Since you mentioned it now, a new As method was added to the PyObject. |
||
{ | ||
if(typeof(T) == typeof(PyObject) || typeof(T) == typeof(object)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As said before, I'm strongly against this. I don't see any advantage in this over separating
globals
andlocals
as done in the Python API.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the responses below.