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

Weakref support#1267

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

Closed
amos402 wants to merge13 commits intopythonnet:masterfromamos402:weakref-support
Closed
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
d6c9716
Support weakref for CLR types
amos402Sep 24, 2020
5d57e5a
Merge branch 'master' into weakref-support
amos402Oct 22, 2020
f08813c
Apply unified mechanism for `GetManagedObjectType`
amos402Oct 22, 2020
b014ce1
Merge branch 'master' into weakref-support
filmorDec 10, 2020
e7f97a9
Merge branch 'master' into weakref-support
amos402Dec 21, 2020
b68f0a4
* Remove magic offset
amos402Dec 21, 2020
b4b1cff
Merge branch 'master' into weakref-support
amos402Dec 21, 2020
ff35e6d
Merge branch 'master' into weakref-support
amos402Feb 10, 2021
ea3d47a
Merge branch 'master' into weakref-support
amos402Feb 19, 2021
958a8eb
Fix Shutdown crash
amos402Feb 19, 2021
53ad8d9
Use PyTypeType.tp_basicsize as size of PyHeapTypeObject directly inst…
amos402Feb 19, 2021
647bc8a
Merge branch 'master' into weakref-support
amos402Feb 21, 2021
f474039
* Use C# 9.0 for Python.EmbeddingTest.csproj
amos402Feb 21, 2021
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
2 changes: 1 addition & 1 deletionpythonnet/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,7 +63,7 @@ def unload():
global _RUNTIME
if _LOADER_ASSEMBLY is not None:
func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Shutdown"]
if func(b"") != 0:
if func(b"full_shutdown") != 0:
raise RuntimeError("Failed to call Python.NET shutdown")

if _RUNTIME is not None:
Expand Down
1 change: 1 addition & 0 deletionssrc/embed_tests/Python.EmbeddingTest.csproj
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
<LangVersion>9.0</LangVersion>
<AssemblyOriginatorKeyFile>..\pythonnet.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
Expand Down
78 changes: 78 additions & 0 deletionssrc/embed_tests/TestClass.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
using System;
using System.Runtime.InteropServices;

using NUnit.Framework;

using Python.Runtime;

using PyRuntime = Python.Runtime.Runtime;

namespace Python.EmbeddingTest
{
public class TestClass
{
public class MyClass
{
}

[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void WeakRefForClrObject()
{
var obj = new MyClass();
using var scope = Py.CreateScope();
scope.Set("clr_obj", obj);
scope.Exec(@"
import weakref
ref = weakref.ref(clr_obj)
");
using PyObject pyobj = scope.Get("clr_obj");
ValidateAttachedGCHandle(obj, pyobj.Handle);
}

[Test]
public void WeakRefForSubClass()
{
using (var scope = Py.CreateScope())
{
scope.Exec(@"
from Python.EmbeddingTest import TestClass
import weakref

class Sub(TestClass.MyClass):
pass

obj = Sub()
ref = weakref.ref(obj)
");
using (PyObject pyobj = scope.Get("obj"))
{
IntPtr op = pyobj.Handle;
IntPtr type = PyRuntime.PyObject_TYPE(op);
IntPtr clrHandle = Marshal.ReadIntPtr(op, ObjectOffset.magic(type));
var clobj = (CLRObject)GCHandle.FromIntPtr(clrHandle).Target;
Assert.IsTrue(clobj.inst is MyClass);
}
}
}

private static void ValidateAttachedGCHandle(object obj, IntPtr op)
{
IntPtr type = PyRuntime.PyObject_TYPE(op);
IntPtr clrHandle = Marshal.ReadIntPtr(op, ObjectOffset.magic(type));
var clobj = (CLRObject)GCHandle.FromIntPtr(clrHandle).Target;
Assert.True(ReferenceEquals(clobj.inst, obj));
}
}
}
39 changes: 16 additions & 23 deletionssrc/runtime/classbase.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;

namespace Python.Runtime
{
Expand DownExpand Up@@ -354,44 +352,39 @@ public static IntPtr tp_repr(IntPtr ob)
public static void tp_dealloc(IntPtr ob)
{
ManagedType self = GetManagedObject(ob);
tp_clear(ob);
Runtime.PyObject_GC_UnTrack(self.pyHandle);
Runtime.PyObject_GC_Del(self.pyHandle);
if (Runtime.PyType_SUPPORTS_WEAKREFS(Runtime.PyObject_TYPE(ob)))
{
Runtime.PyObject_ClearWeakRefs(ob);
}
RemoveObjectDict(ob);
Runtime.Py_CLEAR(ref self.tpHandle);
Runtime.PyObject_GC_UnTrack(ob);
Runtime.PyObject_GC_Del(ob);
self.FreeGCHandle();
}

public static int tp_clear(IntPtr ob)
{
ManagedType self = GetManagedObject(ob);
if (!self.IsTypeObject())
{
ClearObjectDict(ob);
}
self.tpHandle = IntPtr.Zero;
ClearObjectDict(ob);
return 0;
}

protected override void OnSave(InterDomainContext context)
{
base.OnSave(context);
if (pyHandle != tpHandle)
{
IntPtr dict = GetObjectDict(pyHandle);
Runtime.XIncref(dict);
context.Storage.AddValue("dict", dict);
}
IntPtr dict = GetObjectDict(pyHandle);
Runtime.XIncref(dict);
Runtime.XIncref(tpHandle);
context.Storage.AddValue("dict", dict);
}

protected override void OnLoad(InterDomainContext context)
{
base.OnLoad(context);
if (pyHandle != tpHandle)
{
IntPtr dict = context.Storage.GetValue<IntPtr>("dict");
SetObjectDict(pyHandle, dict);
}
IntPtr dict = context.Storage.GetValue<IntPtr>("dict");
SetObjectDict(pyHandle, dict);
gcHandle = AllocGCHandle();
Marshal.WriteIntPtr(pyHandle,TypeOffset.magic(), (IntPtr)gcHandle);
Marshal.WriteIntPtr(pyHandle,ObjectOffset.magic(tpHandle), (IntPtr)gcHandle);
}


Expand Down
3 changes: 2 additions & 1 deletionsrc/runtime/classmanager.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,7 @@
using System.Runtime.InteropServices;
using System.Security;
using System.Linq;
using System.Diagnostics;

namespace Python.Runtime
{
Expand DownExpand Up@@ -270,7 +271,7 @@ private static void InitClassBase(Type type, ClassBase impl)

// Finally, initialize the class __dict__ and return the object.
var dict = new BorrowedReference(Marshal.ReadIntPtr(tp, TypeOffset.tp_dict));

Debug.Assert(!dict.IsNull);

if (impl.dotNetMembers == null)
{
Expand Down
8 changes: 2 additions & 6 deletionssrc/runtime/clrobject.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,10 +30,6 @@ internal CLRObject(object ob, IntPtr tp)
tpHandle = tp;
pyHandle = py;
inst = ob;

// Fix the BaseException args (and __cause__ in case of Python 3)
// slot if wrapping a CLR exception
Exceptions.SetArgsAndCause(py);
}

protected CLRObject()
Expand All@@ -49,7 +45,7 @@ static CLRObject GetInstance(object ob, IntPtr pyType)
static CLRObject GetInstance(object ob)
{
ClassBase cc = ClassManager.GetClass(ob.GetType());
return GetInstance(ob, cc.tpHandle);
return GetInstance(ob, cc.pyHandle);
}

internal static NewReference GetInstHandle(object ob, BorrowedReference pyType)
Expand All@@ -67,7 +63,7 @@ internal static IntPtr GetInstHandle(object ob, IntPtr pyType)
internal static IntPtr GetInstHandle(object ob, Type type)
{
ClassBase cc = ClassManager.GetClass(type);
CLRObject co = GetInstance(ob, cc.tpHandle);
CLRObject co = GetInstance(ob, cc.pyHandle);
return co.pyHandle;
}

Expand Down
26 changes: 20 additions & 6 deletionssrc/runtime/exceptions.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -85,6 +85,13 @@ internal static Exception ToException(IntPtr ob)
}
return Runtime.PyUnicode_FromString(message);
}

public static int tp_init(IntPtr ob, IntPtr args, IntPtr kwds)
{
Exceptions.SetArgsAndCause(ob);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Please, move the original comment here.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

What comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

NIT: You seem to have moved this fromCLRObject, and removed

// Fix the BaseException args (and __cause__ in case of Python 3)// slot if wrapping a CLR exception

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The comment onSetArgsAndCause won't be enough?

return 0;
}

}

/// <summary>
Expand DownExpand Up@@ -180,16 +187,23 @@ internal static void SetArgsAndCause(IntPtr ob)
args = Runtime.PyTuple_New(0);
}

Marshal.WriteIntPtr(ob, ExceptionOffset.args, args);

int baseOffset = OriginalObjectOffsets.Size;
Runtime.Py_SETREF(ob, baseOffset + ExceptionOffset.args, args);

if (e.InnerException != null)
{
// Note: For an AggregateException, InnerException is only the first of the InnerExceptions.
IntPtr cause = CLRObject.GetInstHandle(e.InnerException);
Marshal.WriteIntPtr(ob, ExceptionOffset.cause, cause);
IntPtr cause = GetExceptHandle(e.InnerException);
Runtime.Py_SETREF(ob, baseOffset + ExceptionOffset.cause, cause);
}
}

internal static IntPtr GetExceptHandle(Exception e)
{
IntPtr op = CLRObject.GetInstHandle(e);
SetArgsAndCause(op);
return op;
}

/// <summary>
/// Shortcut for (pointer == NULL) -&gt; throw PythonException
/// </summary>
Expand DownExpand Up@@ -289,7 +303,7 @@ public static void SetError(Exception e)
return;
}

IntPtr op =CLRObject.GetInstHandle(e);
IntPtr op =GetExceptHandle(e);
IntPtr etype = Runtime.PyObject_GetAttr(op, PyIdentifier.__class__);
Runtime.PyErr_SetObject(new BorrowedReference(etype), new BorrowedReference(op));
Runtime.XDecref(etype);
Expand Down
2 changes: 1 addition & 1 deletionsrc/runtime/extensiontype.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -55,7 +55,7 @@ void SetupGc ()
/// </summary>
public static void FinalizeObject(ManagedType self)
{
ClearObjectDict(self.pyHandle);
RemoveObjectDict(self.pyHandle);
Runtime.PyObject_GC_Del(self.pyHandle);
// Not necessary for decref of `tpHandle`.
self.FreeGCHandle();
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp