Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a424998

Browse files
authored
ensure Py handles are inaccessible after PythonException is disposed (#1055)
1 parent 7fa0af2 commit a424998

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/runtime/pythonexception.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class PythonException : System.Exception, IPyDisposable
2121
public PythonException()
2222
{
2323
IntPtr gs = PythonEngine.AcquireLock();
24-
Runtime.PyErr_Fetch(ref _pyType, ref _pyValue, ref _pyTB);
24+
Runtime.PyErr_Fetch(out _pyType, out _pyValue, out _pyTB);
2525
if (_pyType != IntPtr.Zero && _pyValue != IntPtr.Zero)
2626
{
2727
string type;
@@ -161,12 +161,23 @@ public void Dispose()
161161
if (Runtime.Py_IsInitialized() > 0 && !Runtime.IsFinalizing)
162162
{
163163
IntPtr gs = PythonEngine.AcquireLock();
164-
Runtime.XDecref(_pyType);
165-
Runtime.XDecref(_pyValue);
164+
if (_pyType != IntPtr.Zero)
165+
{
166+
Runtime.XDecref(_pyType);
167+
_pyType= IntPtr.Zero;
168+
}
169+
170+
if (_pyValue != IntPtr.Zero)
171+
{
172+
Runtime.XDecref(_pyValue);
173+
_pyValue = IntPtr.Zero;
174+
}
175+
166176
// XXX Do we ever get TraceBack? //
167177
if (_pyTB != IntPtr.Zero)
168178
{
169179
Runtime.XDecref(_pyTB);
180+
_pyTB = IntPtr.Zero;
170181
}
171182
PythonEngine.ReleaseLock(gs);
172183
}

src/runtime/runtime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1933,7 +1933,7 @@ internal static IntPtr PyMem_Realloc(IntPtr ptr, long size)
19331933
internal static extern IntPtr PyErr_Occurred();
19341934

19351935
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
1936-
internal static extern void PyErr_Fetch(ref IntPtr ob, ref IntPtr val, ref IntPtr tb);
1936+
internal static extern void PyErr_Fetch(out IntPtr ob, out IntPtr val, out IntPtr tb);
19371937

19381938
[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
19391939
internal static extern void PyErr_Restore(IntPtr ob, IntPtr val, IntPtr tb);

0 commit comments

Comments
 (0)