@@ -22,15 +22,9 @@ public class PyGILAttribute : Attribute
2222 }
2323
2424 [ PyGIL ]
25- public class PyScope : DynamicObject , IDisposable
25+ public class PyScope : PyObject
2626 {
27- public readonly string Name ;
28-
29- /// <summary>
30- /// the python Module object the scope associated with.
31- /// </summary>
32- readonly PyObject obj ;
33- internal BorrowedReference Reference => obj . Reference ;
27+ public string Name { get ; }
3428
3529 /// <summary>
3630 /// the variable dict of the scope. Borrowed.
@@ -49,20 +43,24 @@ public class PyScope : DynamicObject, IDisposable
4943 /// </summary>
5044 public event Action < PyScope > OnDispose ;
5145
52- /// <summary>
53- /// Constructor
54- /// </summary>
55- /// <remarks>
56- /// Create a scope based on a Python Module.
57- /// </remarks>
58- internal PyScope ( ref NewReference ptr , PyScopeManager manager )
46+ /// <summary>Create a scope based on a Python Module.</summary>
47+ internal PyScope ( ref NewReference reference , PyScopeManager manager )
48+ : this ( reference . DangerousMoveToPointer ( ) , manager ) { }
49+ /// <summary>Create a scope based on a Python Module.</summary>
50+ internal PyScope ( BorrowedReference reference , PyScopeManager manager )
51+ : this ( reference . DangerousGetAddress ( ) , manager )
5952 {
60- if ( ! Runtime . PyType_IsSubtype ( Runtime . PyObject_TYPE ( ptr ) , Runtime . PyModuleType ) )
53+ Runtime . XIncref ( reference . DangerousGetAddress ( ) ) ;
54+ }
55+
56+ /// <summary>Create a scope based on a Python Module.</summary>
57+ private PyScope ( IntPtr ptr , PyScopeManager manager ) : base ( ptr )
58+ {
59+ if ( ! Runtime . PyType_IsSubtype ( Runtime . PyObject_TYPE ( Reference ) , Runtime . PyModuleType ) )
6160 {
6261 throw new PyScopeException ( "object is not a module" ) ;
6362 }
6463 Manager = manager ?? PyScopeManager . Global ;
65- obj = ptr . MoveToPyObject ( ) ;
6664 //Refcount of the variables not increase
6765 variables = Runtime . PyModule_GetDict ( Reference ) . DangerousGetAddress ( ) ;
6866 PythonException . ThrowIfIsNull ( variables ) ;
@@ -72,7 +70,8 @@ internal PyScope(ref NewReference ptr, PyScopeManager manager)
7270 Runtime . PyEval_GetBuiltins ( )
7371 ) ;
7472 PythonException . ThrowIfIsNotZero ( res ) ;
75- this . Name = this . Get < string > ( "__name__" ) ;
73+ using var name = this . Get ( "__name__" ) ;
74+ this . Name = name . As < string > ( ) ;
7675 }
7776
7877 /// <summary>
@@ -118,7 +117,7 @@ public dynamic Import(string name, string asname = null)
118117 }
119118 else
120119 {
121- PyObject module = PythonEngine . ImportModule ( name ) ;
120+ var module = PyModule . Import ( name ) ;
122121 Import ( module , asname ) ;
123122 return module ;
124123 }
@@ -132,7 +131,7 @@ public dynamic Import(string name, string asname = null)
132131 /// </remarks>
133132 public void Import ( PyScope scope , string asname )
134133 {
135- this . SetPyValue ( asname , scope . obj . Handle ) ;
134+ this . SetPyValue ( asname , scope . Handle ) ;
136135 }
137136
138137 /// <summary>
@@ -169,7 +168,7 @@ public void ImportAll(string name)
169168 }
170169 else
171170 {
172- PyObject module = PythonEngine . ImportModule ( name ) ;
171+ var module = PyModule . Import ( name ) ;
173172 ImportAll ( module ) ;
174173 }
175174 }
@@ -503,16 +502,20 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
503502
504503 private void Check ( )
505504 {
506- if ( this . obj . IsDisposed )
505+ if ( this . obj == IntPtr . Zero )
507506 {
508507 throw new PyScopeException ( $ "The scope of name '{ Name } ' object has been disposed") ;
509508 }
510509 }
511510
512- public void Dispose ( )
511+ protected override void Dispose ( bool disposing )
513512 {
513+ if ( this . obj == IntPtr . Zero )
514+ {
515+ return ;
516+ }
517+ base . Dispose ( disposing ) ;
514518 this . OnDispose ? . Invoke ( this ) ;
515- this . obj . Dispose ( ) ;
516519 }
517520 }
518521
0 commit comments