Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitd55a914

Browse files
committed
fixed crash in SetError when PythonException does not have type or value
also: introduced StealingReference type for C API parameters
1 parent8e70b5b commitd55a914

File tree

5 files changed

+79
-11
lines changed

5 files changed

+79
-11
lines changed

‎src/runtime/NewReference.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ ref struct NewReference
1515
publicstaticimplicitoperatorBorrowedReference(inNewReferencereference)
1616
=>newBorrowedReference(reference.pointer);
1717

18+
/// <summary>
19+
/// Creates <see cref="NewReference"/> from a nullable <see cref="BorrowedReference"/>.
20+
/// Increments the reference count accordingly.
21+
/// </summary>
22+
[Pure]
23+
publicstaticNewReferenceFromNullable(BorrowedReferencereference)
24+
{
25+
if(reference.IsNull)returndefault;
26+
27+
IntPtraddress=reference.DangerousGetAddress();
28+
Runtime.XIncref(address);
29+
returnDangerousFromPointer(address);
30+
}
31+
1832
/// <summary>
1933
/// Returns <see cref="PyObject"/> wrapper around this reference, which now owns
2034
/// the pointer. Sets the original reference to <c>null</c>, as it no longer owns it.
@@ -44,6 +58,18 @@ public void Dispose()
4458
publicstaticNewReferenceDangerousFromPointer(IntPtrpointer)
4559
=>newNewReference{pointer=pointer};
4660

61+
/// <summary>
62+
/// Creates <see cref="NewReference"/> from a raw pointer
63+
/// and writes <c>null</c> to the original location.
64+
/// </summary>
65+
[Pure]
66+
publicstaticNewReferenceDangerousMoveFromPointer(refIntPtrpointer)
67+
{
68+
varpointerValue=pointer;
69+
pointer=IntPtr.Zero;
70+
returnDangerousFromPointer(pointerValue);
71+
}
72+
4773
publicIntPtrDangerousMoveToPointer()
4874
{
4975
if(this.IsNull())thrownewNullReferenceException();
@@ -61,11 +87,11 @@ internal static bool IsNull(in NewReference reference)
6187
=>reference.pointer==IntPtr.Zero;
6288

6389
// TODO: return some static type
64-
internalIntPtrSteal()
90+
internalStealingReferenceSteal()
6591
{
6692
varresult=this.pointer;
6793
this.pointer=IntPtr.Zero;
68-
returnresult;
94+
returnStealingReference.DangerousFromPointer(result);
6995
}
7096
}
7197

‎src/runtime/StealingReference.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespacePython.Runtime
2+
{
3+
usingSystem;
4+
usingSystem.Diagnostics.Contracts;
5+
6+
/// <summary>
7+
/// Represents a reference to a Python object, that is being stolen by a C API.
8+
/// </summary>
9+
[NonCopyable]
10+
refstructStealingReference
11+
{
12+
IntPtrpointer;
13+
14+
/// <summary>
15+
/// Creates <see cref="StealingReference"/> from a raw pointer
16+
/// </summary>
17+
[Pure]
18+
publicstaticStealingReferenceDangerousFromPointer(IntPtrpointer)
19+
=>newStealingReference{pointer=pointer};
20+
21+
publicIntPtrDangerousGetAddressOrNull()=>this.pointer;
22+
}
23+
}

‎src/runtime/exceptions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,10 @@ public static void SetError(Exception e)
283283
varpe=easPythonException;
284284
if(pe!=null)
285285
{
286-
Runtime.XIncref(pe.PyType);
287-
Runtime.XIncref(pe.PyValue);
288-
Runtime.XIncref(pe.PyTB);
289-
Runtime.PyErr_Restore(pe.PyType,pe.PyValue,pe.PyTB);
286+
Runtime.PyErr_Restore(
287+
NewReference.FromNullable(pe.PythonType).Steal(),
288+
NewReference.FromNullable(pe.Value).Steal(),
289+
NewReference.FromNullable(pe.Traceback).Steal());
290290
return;
291291
}
292292

‎src/runtime/pythonexception.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ static string TracebackHandleToString(BorrowedReference tracebackHandle) {
236236
publicvoidRestore()
237237
{
238238
IntPtrgs=PythonEngine.AcquireLock();
239-
Runtime.PyErr_Restore(_pyType,_pyValue,_pyTB);
240-
_pyType=IntPtr.Zero;
241-
_pyValue=IntPtr.Zero;
242-
_pyTB=IntPtr.Zero;
239+
Runtime.PyErr_Restore(
240+
NewReference.DangerousMoveFromPointer(ref_pyType).Steal(),
241+
NewReference.DangerousMoveFromPointer(ref_pyValue).Steal(),
242+
NewReference.DangerousMoveFromPointer(ref_pyTB).Steal());
243243
PythonEngine.ReleaseLock(gs);
244244
}
245245

@@ -254,6 +254,11 @@ public IntPtr PyType
254254
get{return_pyType;}
255255
}
256256

257+
/// <summary>
258+
/// Type of the exception (nullable).
259+
/// </summary>
260+
internalBorrowedReferencePythonType=>newBorrowedReference(_pyType);
261+
257262
/// <summary>
258263
/// PyValue Property
259264
/// </summary>
@@ -265,6 +270,11 @@ public IntPtr PyValue
265270
get{return_pyValue;}
266271
}
267272

273+
/// <summary>
274+
/// Exception object value (nullable).
275+
/// </summary>
276+
internalBorrowedReferenceValue=>newBorrowedReference(_pyValue);
277+
268278
/// <summary>
269279
/// PyTB Property
270280
/// </summary>
@@ -276,6 +286,11 @@ public IntPtr PyTB
276286
get{return_pyTB;}
277287
}
278288

289+
/// <summary>
290+
/// Traceback (nullable).
291+
/// </summary>
292+
internalBorrowedReferenceTraceback=>newBorrowedReference(_pyTB);
293+
279294
/// <summary>
280295
/// Message Property
281296
/// </summary>

‎src/runtime/runtime.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,11 @@ internal static IntPtr PyMem_Realloc(IntPtr ptr, long size)
19001900
internalstaticvoidPyErr_Fetch(outNewReferencetype,outNewReferencevalue,outNewReferencetraceback)
19011901
=>Delegates.PyErr_Fetch(outtype,outvalue,outtraceback);
19021902

1903-
internalstaticvoidPyErr_Restore(IntPtrob,IntPtrval,IntPtrtb)=>Delegates.PyErr_Restore(ob,val,tb);
1903+
internalstaticvoidPyErr_Restore(StealingReferenceob,StealingReferenceval,StealingReferencetb)
1904+
=>Delegates.PyErr_Restore(
1905+
ob.DangerousGetAddressOrNull(),
1906+
val.DangerousGetAddressOrNull(),
1907+
tb.DangerousGetAddressOrNull());
19041908

19051909
internalstaticvoidPyErr_Clear()=>Delegates.PyErr_Clear();
19061910

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp