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

Simplify PyScope by delegating ownership to PyObject instance#1367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionssrc/runtime/pyobject.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -157,6 +157,7 @@ public T As<T>()
return (T)AsManagedObject(typeof(T));
}

internal bool IsDisposed => obj == IntPtr.Zero;

/// <summary>
/// Dispose Method
Expand Down
37 changes: 9 additions & 28 deletionssrc/runtime/pyscope.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,18 +29,15 @@ public class PyScope : DynamicObject, IDisposable
/// <summary>
/// the python Module object the scope associated with.
/// </summary>
internal IntPtr obj;
internal BorrowedReference Reference =>new BorrowedReference(obj);
readonly PyObject obj;
internal BorrowedReference Reference => obj.Reference;

/// <summary>
/// the variable dict of the scope.
/// the variable dict of the scope. Borrowed.
/// </summary>
internal readonly IntPtr variables;
internal BorrowedReference VarsRef => new BorrowedReference(variables);

private bool _isDisposed;
private bool _finalized = false;

/// <summary>
/// The Manager this scope associated with.
/// It provides scopes this scope can import.
Expand All@@ -65,7 +62,7 @@ internal PyScope(ref NewReference ptr, PyScopeManager manager)
throw new PyScopeException("object is not a module");
}
Manager = manager ?? PyScopeManager.Global;
obj = ptr.DangerousMoveToPointer();
obj = ptr.MoveToPyObject();
//Refcount of the variables not increase
variables = Runtime.PyModule_GetDict(Reference).DangerousGetAddress();
PythonException.ThrowIfIsNull(variables);
Expand All@@ -81,7 +78,6 @@ internal PyScope(ref NewReference ptr, PyScopeManager manager)
/// <summary>
/// return the variable dict of the scope.
/// </summary>
/// <returns></returns>
public PyDict Variables()
{
Runtime.XIncref(variables);
Expand DownExpand Up@@ -136,7 +132,7 @@ public dynamic Import(string name, string asname = null)
/// </remarks>
public void Import(PyScope scope, string asname)
{
this.Set(asname, scope.obj);
this.SetPyValue(asname, scope.obj.Handle);
}

/// <summary>
Expand DownExpand Up@@ -335,11 +331,11 @@ private void Exec(string code, BorrowedReference _globals, BorrowedReference _lo
public void Set(string name, object value)
{
IntPtr _value = Converter.ToPython(value, value?.GetType());
Set(name, _value);
SetPyValue(name, _value);
Runtime.XDecref(_value);
}

private voidSet(string name, IntPtr value)
private voidSetPyValue(string name, IntPtr value)
{
Check();
using (var pyKey = new PyString(name))
Expand DownExpand Up@@ -507,31 +503,16 @@ public override bool TrySetMember(SetMemberBinder binder, object value)

private void Check()
{
if (_isDisposed)
if (this.obj.IsDisposed)
{
throw new PyScopeException($"The scope of name '{Name}' object has been disposed");
}
}

public void Dispose()
{
if (_isDisposed)
{
return;
}
_isDisposed = true;
Runtime.XDecref(obj);
this.OnDispose?.Invoke(this);
}

~PyScope()
{
if (_finalized || _isDisposed)
{
return;
}
_finalized = true;
Finalizer.Instance.AddFinalizedObject(ref obj);
this.obj.Dispose();
}
}

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp