Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/runtime/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,10 @@ internal static bool ToManagedValue(BorrowedReference value, Type obType,

if (obType.IsInstanceOfType(tmp) || IsSubclassOfRawGeneric(obType, type))
{
if (tmp is IPythonDerivedType)
{
ClassDerivedObject.PassedToManaged(value);
}
result = tmp;
return true;
}
Expand Down
21 changes: 17 additions & 4 deletions src/runtime/Finalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,24 @@ internal void AddFinalizedObject(ref IntPtr obj, int run
obj = IntPtr.Zero;
}

internal void AddDerivedFinalizedObject(ref IntPtr derived, int run)
/// <returns>Whether the object was queued, which it isn't while <see cref="Enable"/> is false</returns>
internal bool AddDerivedFinalizedObject(ref IntPtr derived, int run, object? deferred = null, bool keptReference = false)
{
if (derived == IntPtr.Zero)
throw new ArgumentNullException(nameof(derived));

if (!Enable)
{
return;
return false;
}

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);
return true;
}

internal void AddFinalizedBuffer(ref Py_buffer buffer)
Expand Down Expand Up @@ -254,7 +259,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 +372,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
3 changes: 3 additions & 0 deletions src/runtime/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,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