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

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion 2 pythonnet/__init__.py
Original file line number Diff line number Diff 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
14 changes: 5 additions & 9 deletions 14 src/runtime/classbase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,20 +354,16 @@ 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);
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;
}

Expand Down
3 changes: 2 additions & 1 deletion 3 src/runtime/classmanager.cs
Original file line number Diff line number Diff 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 Down Expand 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
2 changes: 1 addition & 1 deletion 2 src/runtime/extensiontype.cs
Original file line number Diff line number Diff 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
10 changes: 10 additions & 0 deletions 10 src/runtime/managedtype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ protected virtual void OnSave(InterDomainContext context) { }
protected virtual void OnLoad(InterDomainContext context) { }

protected static void ClearObjectDict(IntPtr ob)
{
IntPtr dict = GetObjectDict(ob);
if (dict == IntPtr.Zero)
{
return;
}
Runtime.PyDict_Clear(dict);
}

protected static void RemoveObjectDict(IntPtr ob)
{
IntPtr dict = GetObjectDict(ob);
if (dict == IntPtr.Zero)
Expand Down
6 changes: 6 additions & 0 deletions 6 src/runtime/metatype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ public static void tp_dealloc(IntPtr tp)
NativeCall.Void_Call_1(op, tp);
}

public static int tp_clear(IntPtr ob)
{
ClearObjectDict(ob);
return 0;
}

private static IntPtr DoInstanceCheck(IntPtr tp, IntPtr args, bool checkType)
{
var cb = GetManagedObject(tp) as ClassBase;
Expand Down
2 changes: 1 addition & 1 deletion 2 src/runtime/methodobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public static IntPtr tp_repr(IntPtr ob)
{
var self = (MethodObject)GetManagedObject(ob);
self.ClearMembers();
ClearObjectDict(ob);
RemoveObjectDict(ob);
self.Dealloc();
}

Expand Down
32 changes: 23 additions & 9 deletions 32 src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,15 +503,19 @@ private static void PyDictTryDelItem(BorrowedReference dict, string key)
private static void MoveClrInstancesOnwershipToPython()
{
var objs = ManagedType.GetManagedObjects();
var copyObjs = objs.ToArray();
foreach (var entry in copyObjs)
var copyObjs = new KeyValuePair<ManagedType, ManagedType.TrackTypes>[objs.Count];
{
ManagedType obj = entry.Key;
if (!objs.ContainsKey(obj))
int i = 0;
foreach (var entry in objs)
{
System.Diagnostics.Debug.Assert(obj.gcHandle == default);
continue;
ManagedType obj = entry.Key;
XIncref(obj.pyHandle);
copyObjs[i++] = entry;
}
}
foreach (var entry in copyObjs)
{
ManagedType obj = entry.Key;
if (entry.Value == ManagedType.TrackTypes.Extension)
{
obj.CallTypeClear();
Expand All @@ -522,11 +526,21 @@ private static void MoveClrInstancesOnwershipToPython()
PyObject_GC_Track(obj.pyHandle);
}
}
if (obj.gcHandle.IsAllocated)
}
foreach (var entry in copyObjs)
{
ManagedType obj = entry.Key;
if (!objs.ContainsKey(obj))
{
System.Diagnostics.Debug.Assert(obj.gcHandle == default);
continue;
}
if (obj.RefCount > 1)
{
obj.gcHandle.Free();
obj.FreeGCHandle();
Marshal.WriteIntPtr(obj.pyHandle, ObjectOffset.magic(obj.tpHandle), IntPtr.Zero);
}
obj.gcHandle = default;
XDecref(obj.pyHandle);
}
ManagedType.ClearTrackedObjects();
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.