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

Commit7fa3e8e

Browse files
committed
clear weak reference list when an extension type is destroyed
According to official documentation for defining extension types, if they are to support weak references, they must clear weak reference list associated with the instance in tp_clear.Extension types were missed inpythonnet#1758 (50da522 )
1 parent50da522 commit7fa3e8e

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

‎src/embed_tests/ExtensionTypes.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
usingSystem;
2+
3+
usingNUnit.Framework;
4+
5+
usingPython.Runtime;
6+
7+
namespacePython.EmbeddingTest;
8+
9+
publicclassExtensionTypes
10+
{
11+
[OneTimeSetUp]
12+
publicvoidSetUp()
13+
{
14+
PythonEngine.Initialize();
15+
}
16+
17+
[OneTimeTearDown]
18+
publicvoidDispose()
19+
{
20+
PythonEngine.Shutdown();
21+
}
22+
23+
[Test]
24+
publicvoidWeakrefIsNone_AfterBoundMethodIsGone()
25+
{
26+
usingvarmakeref=Py.Import("weakref").GetAttr("ref");
27+
varboundMethod=newUriBuilder().ToPython().GetAttr(nameof(UriBuilder.GetHashCode));
28+
varweakref=makeref.Invoke(boundMethod);
29+
boundMethod.Dispose();
30+
Assert.IsTrue(weakref.Invoke().IsNone());
31+
}
32+
}

‎src/runtime/Types/ExtensionType.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ public unsafe static void tp_dealloc(NewReference lastRef)
8686

8787
publicstaticinttp_clear(BorrowedReferenceob)
8888
{
89+
varweakrefs=Runtime.PyObject_GetWeakRefList(ob);
90+
if(weakrefs!=null)
91+
{
92+
Runtime.PyObject_ClearWeakRefs(ob);
93+
}
94+
8995
if(TryFreeGCHandle(ob))
9096
{
9197
booldeleted=loadedExtensions.Remove(ob.DangerousGetAddress());

‎src/runtime/Types/MetaType.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@ public static NewReference mp_subscript(BorrowedReference tp, BorrowedReference
273273
/// </summary>
274274
publicstaticvoidtp_dealloc(NewReferencelastRef)
275275
{
276+
varweakrefs=Runtime.PyObject_GetWeakRefList(lastRef.Borrow());
277+
if(weakrefs!=null)
278+
{
279+
Runtime.PyObject_ClearWeakRefs(lastRef.Borrow());
280+
}
281+
276282
// Fix this when we dont cheat on the handle for subclasses!
277283

278284
varflags=PyType.GetFlags(lastRef.Borrow());

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp