Skip to content

Fixed reference counting for exception objects in Py.With #1062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2020
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
11 changes: 9 additions & 2 deletions src/runtime/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Python.Runtime
{
internal class Util
internal static class Util
{
internal static Int64 ReadCLong(IntPtr tp, int offset)
{
Expand All @@ -29,5 +29,12 @@ internal static void WriteCLong(IntPtr type, int offset, Int64 flags)
Marshal.WriteInt64(type, offset, flags);
}
}

/// <summary>
/// Null-coalesce: if <paramref name="primary"/> parameter is not
/// <see cref="IntPtr.Zero"/>, return it. Otherwise return <paramref name="fallback"/>.
/// </summary>
internal static IntPtr Coalesce(this IntPtr primary, IntPtr fallback)
=> primary == IntPtr.Zero ? fallback : primary;
}
}
}
9 changes: 6 additions & 3 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -758,11 +758,14 @@ public static void With(PyObject obj, Action<dynamic> Body)
catch (PythonException e)
{
ex = e;
type = ex.PyType;
val = ex.PyValue;
traceBack = ex.PyTB;
type = ex.PyType.Coalesce(type);
val = ex.PyValue.Coalesce(val);
traceBack = ex.PyTB.Coalesce(traceBack);
}

Runtime.XIncref(type);
Runtime.XIncref(val);
Runtime.XIncref(traceBack);
var exitResult = obj.InvokeMethod("__exit__", new PyObject(type), new PyObject(val), new PyObject(traceBack));

if (ex != null && !exitResult.IsTrue()) throw ex;
Expand Down