@@ -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.
@@ -55,14 +49,13 @@ public class PyScope : DynamicObject, IDisposable
5549 /// <remarks>
5650 /// Create a scope based on a Python Module.
5751 /// </remarks>
58- internal PyScope ( ref NewReference ptr , PyScopeManager manager )
52+ internal PyScope ( ref NewReference ptr , PyScopeManager manager ) : base ( ptr . DangerousMoveToPointer ( ) )
5953 {
60- if ( ! Runtime . PyType_IsSubtype ( Runtime . PyObject_TYPE ( ptr ) , Runtime . PyModuleType ) )
54+ if ( ! Runtime . PyType_IsSubtype ( Runtime . PyObject_TYPE ( Reference ) , Runtime . PyModuleType ) )
6155 {
6256 throw new PyScopeException ( "object is not a module" ) ;
6357 }
6458 Manager = manager ?? PyScopeManager . Global ;
65- obj = ptr . MoveToPyObject ( ) ;
6659 //Refcount of the variables not increase
6760 variables = Runtime . PyModule_GetDict ( Reference ) . DangerousGetAddress ( ) ;
6861 PythonException . ThrowIfIsNull ( variables ) ;
@@ -72,7 +65,8 @@ internal PyScope(ref NewReference ptr, PyScopeManager manager)
7265 Runtime . PyEval_GetBuiltins ( )
7366 ) ;
7467 PythonException . ThrowIfIsNotZero ( res ) ;
75- this . Name = this . Get < string > ( "__name__" ) ;
68+ using var name = this . Get ( "__name__" ) ;
69+ this . Name = name . As < string > ( ) ;
7670 }
7771
7872 /// <summary>
@@ -118,7 +112,7 @@ public dynamic Import(string name, string asname = null)
118112 }
119113 else
120114 {
121- PyObject module = PythonEngine . ImportModule ( name ) ;
115+ var module = PyModule . Import ( name ) ;
122116 Import ( module , asname ) ;
123117 return module ;
124118 }
@@ -132,7 +126,7 @@ public dynamic Import(string name, string asname = null)
132126 /// </remarks>
133127 public void Import ( PyScope scope , string asname )
134128 {
135- this . SetPyValue ( asname , scope . obj . Handle ) ;
129+ this . SetPyValue ( asname , scope . Handle ) ;
136130 }
137131
138132 /// <summary>
@@ -169,7 +163,7 @@ public void ImportAll(string name)
169163 }
170164 else
171165 {
172- PyObject module = PythonEngine . ImportModule ( name ) ;
166+ var module = PyModule . Import ( name ) ;
173167 ImportAll ( module ) ;
174168 }
175169 }
@@ -503,16 +497,20 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
503497
504498 private void Check ( )
505499 {
506- if ( this . obj . IsDisposed )
500+ if ( this . obj == IntPtr . Zero )
507501 {
508502 throw new PyScopeException ( $ "The scope of name '{ Name } ' object has been disposed") ;
509503 }
510504 }
511505
512- public void Dispose ( )
506+ protected override void Dispose ( bool disposing )
513507 {
508+ if ( this . obj == IntPtr . Zero )
509+ {
510+ return ;
511+ }
512+ base . Dispose ( disposing ) ;
514513 this . OnDispose ? . Invoke ( this ) ;
515- this . obj . Dispose ( ) ;
516514 }
517515 }
518516
0 commit comments