-
Notifications
You must be signed in to change notification settings - Fork 773
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 |
|---|---|---|
|
|
@@ -38,14 +38,14 @@ public class PyScope : DynamicObject, IDisposable | |
| internal readonly PyScopeManager Manager; | ||
|
|
||
| public event Action<PyScope> OnDispose; | ||
| public PyScope(IntPtr ptr, PyScopeManager manager) | ||
|
|
||
| internal PyScope(IntPtr ptr, PyScopeManager manager) | ||
| { | ||
| if (Runtime.PyObject_Type(ptr) != Runtime.PyModuleType) | ||
| { | ||
| throw new PyScopeException("object is not a module"); | ||
| } | ||
| if(manager == null) | ||
| if (manager == null) | ||
| { | ||
| manager = PyScopeManager.Global; | ||
| } | ||
|
|
@@ -80,19 +80,23 @@ public PyScope NewScope() | |
| public dynamic Import(string name, string asname = null) | ||
| { | ||
| Check(); | ||
| if (asname == null) | ||
| if (String.IsNullOrEmpty(asname)) | ||
| { | ||
| asname = name; | ||
| } | ||
| var scope = Manager.TryGet(name); | ||
| if(scope != null) | ||
| PyScope scope; | ||
| Manager.TryGet(name, out scope); | ||
| if (scope != null) | ||
| { | ||
| Import(scope, asname); | ||
| return scope; | ||
| } | ||
| PyObject module = PythonEngine.ImportModule(name); | ||
| Import(module, asname); | ||
| return module; | ||
| else | ||
| { | ||
| PyObject module = PythonEngine.ImportModule(name); | ||
| Import(module, asname); | ||
| return module; | ||
| } | ||
| } | ||
|
|
||
| public void Import(PyScope scope, string asname) | ||
|
|
@@ -109,7 +113,7 @@ public void Import(PyScope scope, string asname) | |
| /// </remarks> | ||
| public void Import(PyObject module, string asname = null) | ||
| { | ||
| if (asname == null) | ||
| if (String.IsNullOrEmpty(asname)) | ||
| { | ||
| asname = module.GetAttr("__name__").ToString(); | ||
| } | ||
|
|
@@ -118,14 +122,18 @@ public void Import(PyObject module, string asname = null) | |
|
|
||
| public void ImportAll(string name) | ||
| { | ||
| var scope = Manager.TryGet(name); | ||
| if(scope != null) | ||
| PyScope scope; | ||
| Manager.TryGet(name, out scope); | ||
| if (scope != null) | ||
| { | ||
| ImportAll(scope); | ||
| return; | ||
| } | ||
| PyObject module = PythonEngine.ImportModule(name); | ||
| ImportAll(module); | ||
| else | ||
| { | ||
| PyObject module = PythonEngine.ImportModule(name); | ||
| ImportAll(module); | ||
| } | ||
| } | ||
|
|
||
| public void ImportAll(PyScope scope) | ||
|
|
@@ -323,27 +331,13 @@ public bool ContainsVariable(string name) | |
| /// </remarks> | ||
| public PyObject GetVariable(string name) | ||
| { | ||
| Check(); | ||
| using (var pyKey = new PyString(name)) | ||
| PyObject scope; | ||
| var state = TryGetVariable(name, out scope); | ||
| if(!state) | ||
| { | ||
| if (Runtime.PyMapping_HasKey(variables, pyKey.obj) != 0) | ||
| { | ||
| IntPtr op = Runtime.PyObject_GetItem(variables, pyKey.obj); | ||
| if (op == IntPtr.Zero) | ||
| { | ||
| throw new PythonException(); | ||
| } | ||
| if (op == Runtime.PyNone) | ||
| { | ||
| return null; | ||
| } | ||
| return new PyObject(op); | ||
| } | ||
| else | ||
| { | ||
| throw new PyScopeException(String.Format("'ScopeStorage' object has no attribute '{0}'", name)); | ||
| } | ||
| throw new PyScopeException($"The scope of name '{Name}' has no attribute '{name}'"); | ||
| } | ||
| return scope; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -367,6 +361,7 @@ public bool TryGetVariable(string name, out PyObject value) | |
| } | ||
| if (op == Runtime.PyNone) | ||
| { | ||
| Runtime.XDecref(op); | ||
| value = null; | ||
| return true; | ||
| } | ||
|
|
@@ -401,11 +396,18 @@ public bool TryGetVariable<T>(string name, out T value) | |
| { | ||
| value = default(T); | ||
| return false; | ||
| } | ||
| } | ||
| if (pyObj == null) | ||
| { | ||
| value = default(T); | ||
| return true; | ||
| if(typeof(T).IsValueType) | ||
| { | ||
| throw new PyScopeException($"The value of the attribute '{name}' is None which cannot be convert to '{typeof(T).ToString()}'"); | ||
| } | ||
| else | ||
| { | ||
| value = default(T); | ||
| return true; | ||
| } | ||
| } | ||
|
Member
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. This means that if someone does |
||
| value = pyObj.As<T>(); | ||
| return true; | ||
|
|
@@ -427,7 +429,7 @@ private void Check() | |
| { | ||
| if (isDisposed) | ||
| { | ||
| throw new PyScopeException("'ScopeStorage' object has been disposed"); | ||
| throw new PyScopeException($"The scope of name '{Name}' object has been disposed"); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -484,7 +486,7 @@ public PyScope Create(string name) | |
| } | ||
| if (name != null && NamedScopes.ContainsKey(name)) | ||
|
Member
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.
Contributor
Author
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. Sorry, I'm not quite understand?
Member
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. You already defined a member function |
||
| { | ||
| throw new PyScopeException($"PyScope '{name}' has existed"); | ||
| throw new PyScopeException($"A scope of name '{name}' does already exist"); | ||
| } | ||
| var scope = this.NewScope(name); | ||
| scope.OnDispose += Remove; | ||
|
|
@@ -507,14 +509,12 @@ public PyScope Get(string name) | |
| { | ||
| return NamedScopes[name]; | ||
| } | ||
| throw new PyScopeException($"PyScope '{name}' not exist"); | ||
| throw new PyScopeException($"There is no scope named '{name}' registered in this manager"); | ||
| } | ||
|
|
||
| public PyScope TryGet(string name) | ||
| public bool TryGet(string name, out PyScope scope) | ||
| { | ||
| PyScope value; | ||
| NamedScopes.TryGetValue(name, out value); | ||
| return value; | ||
| return NamedScopes.TryGetValue(name, out scope); | ||
| } | ||
|
|
||
| public void Remove(PyScope scope) | ||
|
|
||
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.
Use
.As<string>()instead?ToStringis much too forgiving here.