Skip to content
Open
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
17 changes: 14 additions & 3 deletions src/runtime/Finalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ internal void AddFinalizedObject(ref IntPtr obj, int run
obj = IntPtr.Zero;
}

internal void AddDerivedFinalizedObject(ref IntPtr derived, int run)
internal void AddDerivedFinalizedObject(ref IntPtr derived, int run, object? deferred = null, bool keptReference = false)
{
if (derived == IntPtr.Zero)
throw new ArgumentNullException(nameof(derived));
Expand All @@ -164,7 +164,10 @@ internal void AddDerivedFinalizedObject(ref IntPtr derived, int run)
return;
}

var pending = new PendingFinalization { PyObj = derived, RuntimeRun = run };
var pending = new PendingFinalization
{
PyObj = derived, RuntimeRun = run, Deferred = deferred, KeptReference = keptReference,
};
derived = IntPtr.Zero;
_derivedQueue.Enqueue(pending);
}
Expand Down Expand Up @@ -254,7 +257,7 @@ internal nint DisposeAll()
}

#pragma warning disable CS0618 // Type or member is obsolete. OK for internal use
PythonDerivedType.Finalize(derived.PyObj);
PythonDerivedType.Finalize(derived.PyObj, derived.Deferred, derived.KeptReference);
#pragma warning restore CS0618 // Type or member is obsolete

collected++;
Expand Down Expand Up @@ -367,6 +370,14 @@ struct PendingFinalization
public IntPtr PyObj;
public BorrowedReference Ref => new(PyObj);
public int RuntimeRun;
/// <summary>
/// The .NET object of a Python derived type whose finalization waits until this is handled
/// </summary>
public object? Deferred;
/// <summary>
/// <see cref="Deferred"/> holds a reference to <see cref="PyObj"/>
/// </summary>
public bool KeptReference;
#if TRACE_ALLOC
public string StackTrace;
#endif
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public ModulePropertyAttribute()
[Flags]
public enum TypeFlags: long
{
/// <summary>Python 3.7 calls tp_finalize only when set</summary>
HaveFinalize = (1 << 0),
HeapType = (1 << 9),
BaseType = (1 << 10),
Ready = (1 << 12),
Expand Down Expand Up @@ -188,6 +190,9 @@ internal static ThunkInfo GetThunk(Delegate @delegate)
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void N_V(NewReference ob);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void B_V(BorrowedReference ob);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int BPP_I32(BorrowedReference ob, IntPtr a1, IntPtr a2);
}
Expand Down
1 change: 1 addition & 0 deletions src/runtime/Native/ITypeOffsets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ interface ITypeOffsets
int tp_descr_set { get; }
int tp_dict { get; }
int tp_dictoffset { get; }
int tp_finalize { get; }
int tp_flags { get; }
int tp_free { get; }
int tp_getattro { get; }
Expand Down
1 change: 1 addition & 0 deletions src/runtime/Native/TypeOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static partial class TypeOffset
internal static int tp_descr_set { get; private set; }
internal static int tp_dict { get; private set; }
internal static int tp_dictoffset { get; private set; }
internal static int tp_finalize { get; private set; }
internal static int tp_flags { get; private set; }
internal static int tp_free { get; private set; }
internal static int tp_getattro { get; private set; }
Expand Down
Loading
Loading