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

Clear weakref list when reflected object is destroyed#1758

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
filmor merged 1 commit intopythonnet:masterfromlosttech:weakref-support
Apr 9, 2022
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
10 changes: 10 additions & 0 deletionssrc/embed_tests/TestInstanceWrapping.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,6 +34,16 @@ public void OverloadResolution_UnknownToObject()
}
}

[Test]
public void WeakRefIsNone_AfterObjectIsGone()
{
using var makeref = Py.Import("weakref").GetAttr("ref");
var ub = new UriBuilder().ToPython();
using var weakref = makeref.Invoke(ub);
ub.Dispose();
Assert.IsTrue(weakref.Invoke().IsNone());
}

class Base {}
class Derived: Base { }

Expand Down
2 changes: 2 additions & 0 deletionssrc/runtime/Runtime.Delegates.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -78,6 +78,7 @@ static Delegates()
PyObject_RichCompareBool = (delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference, int, int>)GetFunctionByName(nameof(PyObject_RichCompareBool), GetUnmanagedDll(_PythonDll));
PyObject_IsInstance = (delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference, int>)GetFunctionByName(nameof(PyObject_IsInstance), GetUnmanagedDll(_PythonDll));
PyObject_IsSubclass = (delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference, int>)GetFunctionByName(nameof(PyObject_IsSubclass), GetUnmanagedDll(_PythonDll));
PyObject_ClearWeakRefs = (delegate* unmanaged[Cdecl]<BorrowedReference, void>)GetFunctionByName(nameof(PyObject_ClearWeakRefs), GetUnmanagedDll(_PythonDll));
PyCallable_Check = (delegate* unmanaged[Cdecl]<BorrowedReference, int>)GetFunctionByName(nameof(PyCallable_Check), GetUnmanagedDll(_PythonDll));
PyObject_IsTrue = (delegate* unmanaged[Cdecl]<BorrowedReference, int>)GetFunctionByName(nameof(PyObject_IsTrue), GetUnmanagedDll(_PythonDll));
PyObject_Not = (delegate* unmanaged[Cdecl]<BorrowedReference, int>)GetFunctionByName(nameof(PyObject_Not), GetUnmanagedDll(_PythonDll));
Expand DownExpand Up@@ -361,6 +362,7 @@ static Delegates()
internal static delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference, int, int> PyObject_RichCompareBool { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference, int> PyObject_IsInstance { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, BorrowedReference, int> PyObject_IsSubclass { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, void> PyObject_ClearWeakRefs { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, int> PyCallable_Check { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, int> PyObject_IsTrue { get; }
internal static delegate* unmanaged[Cdecl]<BorrowedReference, int> PyObject_Not { get; }
Expand Down
12 changes: 12 additions & 0 deletionssrc/runtime/Runtime.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -993,6 +993,18 @@ internal static int PyObject_Compare(BorrowedReference value1, BorrowedReference

internal static int PyObject_IsSubclass(BorrowedReference ob, BorrowedReference type) => Delegates.PyObject_IsSubclass(ob, type);

internal static void PyObject_ClearWeakRefs(BorrowedReference ob) => Delegates.PyObject_ClearWeakRefs(ob);

internal static BorrowedReference PyObject_GetWeakRefList(BorrowedReference ob)
{
Debug.Assert(ob != null);
var type = PyObject_TYPE(ob);
int offset = Util.ReadInt32(type, TypeOffset.tp_weaklistoffset);
if (offset == 0) return BorrowedReference.Null;
Debug.Assert(offset > 0);
return Util.ReadRef(ob, offset);
}


internal static int PyCallable_Check(BorrowedReference o) => Delegates.PyCallable_Check(o);

Expand Down
6 changes: 6 additions & 0 deletionssrc/runtime/Types/ClassBase.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -346,6 +346,12 @@ public static void tp_dealloc(NewReference lastRef)

public static int tp_clear(BorrowedReference ob)
{
var weakrefs = Runtime.PyObject_GetWeakRefList(ob);
if (weakrefs != null)
{
Runtime.PyObject_ClearWeakRefs(ob);
}

if (TryFreeGCHandle(ob))
{
IntPtr addr = ob.DangerousGetAddress();
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp