File tree Expand file tree Collapse file tree 4 files changed +20
-6
lines changed Expand file tree Collapse file tree 4 files changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ namespace Python.Runtime
2
2
{
3
3
using System ;
4
4
using System . Diagnostics . Contracts ;
5
+ using System . Runtime . CompilerServices ;
5
6
6
7
/// <summary>
7
8
/// Represents a reference to a Python object, that is tracked by Python's reference counting.
@@ -65,12 +66,25 @@ public IntPtr DangerousMoveToPointerOrNull()
65
66
/// Call this method to move ownership of this reference to a Python C API function,
66
67
/// that steals reference passed to it.
67
68
/// </summary>
68
- public StolenReference Steal ( )
69
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
70
+ public StolenReference StealNullable ( )
69
71
{
70
72
IntPtr rawPointer = this . pointer ;
71
73
this . pointer = IntPtr . Zero ;
72
74
return new StolenReference ( rawPointer ) ;
73
75
}
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
+ }
74
88
/// <summary>
75
89
/// Removes this reference to a Python object, and sets it to <c>null</c>.
76
90
/// </summary>
Original file line number Diff line number Diff line change @@ -160,7 +160,7 @@ private void DisposeAll()
160
160
{
161
161
// Python requires finalizers to preserve exception:
162
162
// 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 ( ) ) ;
164
164
}
165
165
}
166
166
}
Original file line number Diff line number Diff line change @@ -218,7 +218,7 @@ protected virtual void Dispose(bool disposing)
218
218
{
219
219
// Python requires finalizers to preserve exception:
220
220
// 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 ( ) ) ;
222
222
}
223
223
}
224
224
else
Original file line number Diff line number Diff line change @@ -239,9 +239,9 @@ public void Restore()
239
239
{
240
240
IntPtr gs = PythonEngine . AcquireLock ( ) ;
241
241
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 ( ) ) ;
245
245
PythonEngine . ReleaseLock ( gs ) ;
246
246
}
247
247
You can’t perform that action at this time.
0 commit comments