🌐 AI搜索 & 代理 主页
Skip to content

Commit d8abce6

Browse files
committed
simplify PyScope by delegating ownership to PyObject instance
1 parent 32fdc9c commit d8abce6

File tree

2 files changed

+10
-28
lines changed

2 files changed

+10
-28
lines changed

src/runtime/pyobject.cs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@ public class PyScope : DynamicObject, IDisposable
2929
/// <summary>
3030
/// the python Module object the scope associated with.
3131
/// </summary>
32-
internal IntPtr obj;
32+
readonly PyObject obj;
3333

3434
/// <summary>
35-
/// the variable dict of the scope.
35+
/// the variable dict of the scope. Borrowed.
3636
/// </summary>
3737
internal readonly IntPtr variables;
3838

39-
private bool _isDisposed;
40-
private bool _finalized = false;
41-
4239
/// <summary>
4340
/// The Manager this scope associated with.
4441
/// It provides scopes this scope can import.
@@ -63,9 +60,9 @@ internal PyScope(IntPtr ptr, PyScopeManager manager)
6360
throw new PyScopeException("object is not a module");
6461
}
6562
Manager = manager ?? PyScopeManager.Global;
66-
obj = ptr;
63+
obj = new PyObject(ptr);
6764
//Refcount of the variables not increase
68-
variables = Runtime.PyModule_GetDict(obj);
65+
variables = Runtime.PyModule_GetDict(obj.Handle);
6966
PythonException.ThrowIfIsNull(variables);
7067

7168
int res = Runtime.PyDict_SetItem(
@@ -79,7 +76,6 @@ internal PyScope(IntPtr ptr, PyScopeManager manager)
7976
/// <summary>
8077
/// return the variable dict of the scope.
8178
/// </summary>
82-
/// <returns></returns>
8379
public PyDict Variables()
8480
{
8581
Runtime.XIncref(variables);
@@ -134,7 +130,7 @@ public dynamic Import(string name, string asname = null)
134130
/// </remarks>
135131
public void Import(PyScope scope, string asname)
136132
{
137-
this.Set(asname, scope.obj);
133+
this.SetPyValue(asname, scope.obj.Handle);
138134
}
139135

140136
/// <summary>
@@ -333,11 +329,11 @@ private void Exec(string code, IntPtr _globals, IntPtr _locals)
333329
public void Set(string name, object value)
334330
{
335331
IntPtr _value = Converter.ToPython(value, value?.GetType());
336-
Set(name, _value);
332+
SetPyValue(name, _value);
337333
Runtime.XDecref(_value);
338334
}
339335

340-
private void Set(string name, IntPtr value)
336+
private void SetPyValue(string name, IntPtr value)
341337
{
342338
Check();
343339
using (var pyKey = new PyString(name))
@@ -505,31 +501,16 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
505501

506502
private void Check()
507503
{
508-
if (_isDisposed)
504+
if (this.obj.IsDisposed)
509505
{
510506
throw new PyScopeException($"The scope of name '{Name}' object has been disposed");
511507
}
512508
}
513509

514510
public void Dispose()
515511
{
516-
if (_isDisposed)
517-
{
518-
return;
519-
}
520-
_isDisposed = true;
521-
Runtime.XDecref(obj);
522512
this.OnDispose?.Invoke(this);
523-
}
524-
525-
~PyScope()
526-
{
527-
if (_finalized || _isDisposed)
528-
{
529-
return;
530-
}
531-
_finalized = true;
532-
Finalizer.Instance.AddFinalizedObject(ref obj);
513+
this.obj.Dispose();
533514
}
534515
}
535516

0 commit comments

Comments
 (0)