Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1ab9cb1

Browse files
committed
simplify PyScope by delegating ownership to PyObject instance
1 parent0f5e781 commit1ab9cb1

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+
internalboolIsDisposed=>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,18 +29,15 @@ public class PyScope : DynamicObject, IDisposable
2929
/// <summary>
3030
/// the python Module object the scope associated with.
3131
/// </summary>
32-
internalIntPtrobj;
33-
internalBorrowedReferenceReference=>newBorrowedReference(obj);
32+
readonlyPyObjectobj;
33+
internalBorrowedReferenceReference=>obj.Reference;
3434

3535
/// <summary>
36-
/// the variable dict of the scope.
36+
/// the variable dict of the scope. Borrowed.
3737
/// </summary>
3838
internalreadonlyIntPtrvariables;
3939
internalBorrowedReferenceVarsRef=>newBorrowedReference(variables);
4040

41-
privatebool_isDisposed;
42-
privatebool_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
thrownewPyScopeException("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
publicPyDictVariables()
8682
{
8783
Runtime.XIncref(variables);
@@ -136,7 +132,7 @@ public dynamic Import(string name, string asname = null)
136132
/// </remarks>
137133
publicvoidImport(PyScopescope,stringasname)
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
publicvoidSet(stringname,objectvalue)
336332
{
337333
IntPtr_value=Converter.ToPython(value,value?.GetType());
338-
Set(name,_value);
334+
SetPyValue(name,_value);
339335
Runtime.XDecref(_value);
340336
}
341337

342-
privatevoidSet(stringname,IntPtrvalue)
338+
privatevoidSetPyValue(stringname,IntPtrvalue)
343339
{
344340
Check();
345341
using(varpyKey=newPyString(name))
@@ -507,31 +503,16 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
507503

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

516512
publicvoidDispose()
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(refobj);
515+
this.obj.Dispose();
535516
}
536517
}
537518

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp