10000 Fix exception refcnt by BadSingleton · Pull Request #1402 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Fix exception refcnt #1402

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Code review fixes
  • Loading branch information
BadSingleton committed Mar 4, 2021
commit 5ea9c3ca63ecbaf8a6c39142a4d0fb73b006918c
2 changes: 1 addition & 1 deletion src/runtime/clrobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected override void OnLoad(InterDomainContext context)

public override string ToString()
{
retu 8000 rn $"<CLRObject wrapping {inst}>";
return $"<CLRObject: {inst}>";
}
}
}
19 changes: 5 additions & 14 deletions src/runtime/exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ internal static Exception ToException(IntPtr ob)
static void ClearOffsetHelper(IntPtr ob, int offset)
{
var field = Marshal.ReadIntPtr(ob, offset);
Runtime.Py_CLEAR(ref field);
Runtime.XDecref(field);
Marshal.WriteIntPtr(ob, offset, IntPtr.Zero);
}

// As seen in exceptions.c, every derived type must also clean the base.
Expand Down Expand Up @@ -200,19 +201,9 @@ internal static void SetArgsAndCause(IntPtr ob)

if (e.InnerException != null)
{
if(e.InnerException is PythonException)
{
// If the error is a Python exception, write the real one
var pyerr = (e.InnerException as PythonException);
Runtime.XIncref(pyerr.PyValue);
Runtime.PyException_SetCause(ob, pyerr.PyValue);
}
else
{
// Note: For an AggregateException, InnerException is only the first of the InnerExceptions.
IntPtr cause = CLRObject.GetInstHandle(e.InnerException);
Runtime.PyException_SetCause(ob, cause);
}
// Note: For an AggregateException, InnerException is only the first of the InnerExceptions.
IntPtr cause = CLRObject.GetInstHandle(e.InnerException);
Runtime.PyException_SetCause(ob, cause);
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ def do_test_leak():
orig_exc = {x for x in get_all_objects() if isinstance(x, Exception)}
func()
exc = {x for x in get_all_objects() if isinstance(x, Exception)}
assert not exc - orig_exc
possibly_leaked = exc - orig_exc
assert not possibly_leaked

return do_test_leak

Expand Down
0