Skip to content

Commit 3433201

Browse files
committed
separate .Steal() and .StealNullable()
1 parent 00653dc commit 3433201

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

src/runtime/NewReference.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Python.Runtime
22
{
33
using System;
44
using System.Diagnostics.Contracts;
5+
using System.Runtime.CompilerServices;
56

67
/// <summary>
78
/// Represents a reference to a Python object, that is tracked by Python's reference counting.
@@ -65,12 +66,25 @@ public IntPtr DangerousMoveToPointerOrNull()
6566
/// Call this method to move ownership of this reference to a Python C API function,
6667
/// that steals reference passed to it.
6768
/// </summary>
68-
public StolenReference Steal()
69+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
70+
public StolenReference StealNullable()
6971
{
7072
IntPtr rawPointer = this.pointer;
7173
this.pointer = IntPtr.Zero;
7274
return new StolenReference(rawPointer);
7375
}
76+
77+
/// <summary>
78+
/// Call this method to move ownership of this reference to a Python C API function,
79+
/// that steals reference passed to it.
80+
/// </summary>
81+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
82+
public StolenReference Steal()
83+
{
84+
if (this.IsNull()) throw new NullReferenceException();
85+
86+
return this.StealNullable();
87+
}
7488
/// <summary>
7589
/// Removes this reference to a Python object, and sets it to <c>null</c>.
7690
/// </summary>

src/runtime/finalizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private void DisposeAll()
160160
{
161161
// Python requires finalizers to preserve exception:
162162
// https://docs.python.org/3/extending/newtypes.html#finalization-and-de-allocation
163-
Runtime.PyErr_Restore(errType.Steal(), errVal.Steal(), traceback.Steal());
163+
Runtime.PyErr_Restore(errType.StealNullable(), errVal.StealNullable(), traceback.StealNullable());
164164
}
165165
}
166166
}

src/runtime/pyobject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ protected virtual void Dispose(bool disposing)
218218
{
219219
// Python requires finalizers to preserve exception:
220220
// https://docs.python.org/3/extending/newtypes.html#finalization-and-de-allocation
221-
Runtime.PyErr_Restore(errType.Steal(), errVal.Steal(), traceback.Steal());
221+
Runtime.PyErr_Restore(errType.StealNullable(), errVal.StealNullable(), traceback.StealNullable());
222222
}
223223
}
224224
else

src/runtime/pythonexception.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ public void Restore()
239239
{
240240
IntPtr gs = PythonEngine.AcquireLock();
241241
Runtime.PyErr_Restore(
242-
Type.NewReferenceOrNull().Steal(),
243-
Value.NewReferenceOrNull().Steal(),
244-
Traceback.NewReferenceOrNull().Steal());
242+
Type.NewReferenceOrNull().StealNullable(),
243+
Value.NewReferenceOrNull().StealNullable(),
244+
Traceback.NewReferenceOrNull().StealNullable());
245245
PythonEngine.ReleaseLock(gs);
246246
}
247247

0 commit comments

Comments
 (0)