Skip to content

Commit 1ab9cb1

Browse files
committed
simplify PyScope by delegating ownership to PyObject instance
1 parent 0f5e781 commit 1ab9cb1

File tree

2 files changed

+10
-28
lines changed

2 files changed

+10
-28
lines changed

src/runtime/pyobject.cs

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public T As<T>()
157157
return (T)AsManagedObject(typeof(T));
158158
}
159159

160+
internal bool IsDisposed => obj == IntPtr.Zero;
160161

161162
/// <summary>
162163
/// Dispose Method

src/runtime/pyscope.cs

+9-28
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,15 @@ public class PyScope : DynamicObject, IDisposable
2929
/// <summary>
3030
/// the python Module object the scope associated with.
3131
/// </summary>
32-
internal IntPtr obj;
33-
internal BorrowedReference Reference => new BorrowedReference(obj);
32+
readonly PyObject obj;
33+
internal BorrowedReference Reference => obj.Reference;
3434

3535
/// <summary>
36-
/// the variable dict of the scope.
36+
/// the variable dict of the scope. Borrowed.
3737
/// </summary>
3838
internal readonly IntPtr variables;
3939
internal BorrowedReference VarsRef => new BorrowedReference(variables);
4040

41-
private bool _isDisposed;
42-
private bool _finalized = false;
43-
4441
/// <summary>
4542
/// The Manager this scope associated with.
4643
/// It provides scopes this scope can import.
@@ -65,7 +62,7 @@ internal PyScope(ref NewReference ptr, PyScopeManager manager)
6562
throw new PyScopeException("object is not a module");
6663
}
6764
Manager = manager ?? PyScopeManager.Global;
68-
obj = ptr.DangerousMoveToPointer();
65+
obj = ptr.MoveToPyObject();
6966
//Refcount of the variables not increase
7067
variables = Runtime.PyModule_GetDict(Reference).DangerousGetAddress();
7168
PythonException.ThrowIfIsNull(variables);
@@ -81,7 +78,6 @@ internal PyScope(ref NewReference ptr, PyScopeManager manager)
8178
/// <summary>
8279
/// return the variable dict of the scope.
8380
/// </summary>
84-
/// <returns></returns>
8581
public PyDict Variables()
8682
{
8783
Runtime.XIncref(variables);
@@ -136,7 +132,7 @@ public dynamic Import(string name, string asname = null)
136132
/// </remarks>
137133
public void Import(PyScope scope, string asname)
138134
{
139-
this.Set(asname, scope.obj);
135+
this.SetPyValue(asname, scope.obj.Handle);
140136
}
141137

142138
/// <summary>
@@ -335,11 +331,11 @@ private void Exec(string code, BorrowedReference _globals, BorrowedReference _lo
335331
public void Set(string name, object value)
336332
{
337333
IntPtr _value = Converter.ToPython(value, value?.GetType());
338-
Set(name, _value);
334+
SetPyValue(name, _value);
339335
Runtime.XDecref(_value);
340336
}
341337

342-
private void Set(string name, IntPtr value)
338+
private void SetPyValue(string name, IntPtr value)
343339
{
344340
Check();
345341
using (var pyKey = new PyString(name))
@@ -507,31 +503,16 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
507503

508504
private void Check()
509505
{
510-
if (_isDisposed)
506+
if (this.obj.IsDisposed)
511507
{
512508
throw new PyScopeException($"The scope of name '{Name}' object has been disposed");
513509
}
514510
}
515511

516512
public void Dispose()
517513
{
518-
if (_isDisposed)
519-
{
520-
return;
521-
}
522-
_isDisposed = true;
523-
Runtime.XDecref(obj);
524514
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();
535516
}
536517
}
537518

0 commit comments

Comments
 (0)