@@ -29,18 +29,15 @@ public class PyScope : DynamicObject, IDisposable
29
29
/// <summary>
30
30
/// the python Module object the scope associated with.
31
31
/// </summary>
32
- internal IntPtr obj ;
33
- internal BorrowedReference Reference => new BorrowedReference ( obj ) ;
32
+ readonly PyObject obj ;
33
+ internal BorrowedReference Reference => obj . Reference ;
34
34
35
35
/// <summary>
36
- /// the variable dict of the scope.
36
+ /// the variable dict of the scope. Borrowed.
37
37
/// </summary>
38
38
internal readonly IntPtr variables ;
39
39
internal BorrowedReference VarsRef => new BorrowedReference ( variables ) ;
40
40
41
- private bool _isDisposed ;
42
- private bool _finalized = false ;
43
-
44
41
/// <summary>
45
42
/// The Manager this scope associated with.
46
43
/// It provides scopes this scope can import.
@@ -65,7 +62,7 @@ internal PyScope(ref NewReference ptr, PyScopeManager manager)
65
62
throw new PyScopeException ( "object is not a module" ) ;
66
63
}
67
64
Manager = manager ?? PyScopeManager . Global ;
68
- obj = ptr . DangerousMoveToPointer ( ) ;
65
+ obj = ptr . MoveToPyObject ( ) ;
69
66
//Refcount of the variables not increase
70
67
variables = Runtime . PyModule_GetDict ( Reference ) . DangerousGetAddress ( ) ;
71
68
PythonException . ThrowIfIsNull ( variables ) ;
@@ -81,7 +78,6 @@ internal PyScope(ref NewReference ptr, PyScopeManager manager)
81
78
/// <summary>
82
79
/// return the variable dict of the scope.
83
80
/// </summary>
84
- /// <returns></returns>
85
81
public PyDict Variables ( )
86
82
{
87
83
Runtime . XIncref ( variables ) ;
@@ -136,7 +132,7 @@ public dynamic Import(string name, string asname = null)
136
132
/// </remarks>
137
133
public void Import ( PyScope scope , string asname )
138
134
{
139
- this . Set ( asname , scope . obj ) ;
135
+ this . SetPyValue ( asname , scope . obj . Handle ) ;
140
136
}
141
137
142
138
/// <summary>
@@ -335,11 +331,11 @@ private void Exec(string code, BorrowedReference _globals, BorrowedReference _lo
335
331
public void Set ( string name , object value )
336
332
{
337
333
IntPtr _value = Converter . ToPython ( value , value ? . GetType ( ) ) ;
338
- Set ( name , _value ) ;
334
+ SetPyValue ( name , _value ) ;
339
335
Runtime . XDecref ( _value ) ;
340
336
}
341
337
342
- private void Set ( string name , IntPtr value )
338
+ private void SetPyValue ( string name , IntPtr value )
343
339
{
344
340
Check ( ) ;
345
341
using ( var pyKey = new PyString ( name ) )
@@ -507,31 +503,16 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
507
503
508
504
private void Check ( )
509
505
{
510
- if ( _isDisposed )
506
+ if ( this . obj . IsDisposed )
511
507
{
512
508
throw new PyScopeException ( $ "The scope of name '{ Name } ' object has been disposed") ;
513
509
}
514
510
}
515
511
516
512
public void Dispose ( )
517
513
{
518
- if ( _isDisposed )
519
- {
520
- return ;
521
- }
522
- _isDisposed = true ;
523
- Runtime . XDecref ( obj ) ;
524
514
this . OnDispose ? . Invoke ( this ) ;
525
- }
526
-
527
- ~ PyScope ( )
528
- {
529
- if ( _finalized || _isDisposed )
530
- {
531
- return ;
532
- }
533
- _finalized = true ;
534
- Finalizer . Instance . AddFinalizedObject ( ref obj ) ;
515
+ this . obj . Dispose ( ) ;
535
516
}
536
517
}
537
518
0 commit comments