Repository navigation
Refresh args and __cause__ of a __namespace__ exception created from .NET #174
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
base: master
Are you sure you want to change the base?
Changes from all commits
d98d46f
d0b9c19
30aa2d0
e109105
e734894
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,14 +77,24 @@ internal static void Shutdown() | |
| /// __getattr__ implementation, and thus dereferencing a NULL | ||
| /// pointer. | ||
| /// </summary> | ||
| internal static bool SetArgsAndCause(BorrowedReference ob, Exception e) | ||
| /// <param name="message">the message read by the refresh of an exception created from .NET, | ||
| /// which reports a message Python can't hold by returning false</param> | ||
| internal static bool SetArgsAndCause(BorrowedReference ob, Exception e, string? message = null) | ||
| { | ||
| NewReference args; | ||
| if (!string.IsNullOrEmpty(e.Message)) | ||
| if (!string.IsNullOrEmpty(message ?? e.Message)) | ||
| { | ||
| using var msg = Runtime.PyString_FromString(message ?? e.Message); | ||
| if (msg.IsNull()) | ||
| { | ||
| if (message != null) | ||
| { | ||
| return false; | ||
| } | ||
| throw PythonException.ThrowLastAsClrException(); | ||
| } | ||
| args = Runtime.PyTuple_New(1); | ||
| using var msg = Runtime.PyString_FromString(e.Message); | ||
| Runtime.PyTuple_SetItem(args.Borrow(), 0, msg.StealOrThrow()); | ||
| Runtime.PyTuple_SetItem(args.Borrow(), 0, msg.Steal()); | ||
| } | ||
| else | ||
| { | ||
|
|
@@ -102,7 +112,16 @@ internal static bool SetArgsAndCause(BorrowedReference ob, Exception e) | |
| if (e.InnerException != null) | ||
| { | ||
| // Note: For an AggregateException, InnerException is only the first of the InnerExceptions. | ||
| using var cause = CLRObject.GetReference(e.InnerException); | ||
| // a generated inner exception keeps its Python object, when it has one in this runtime | ||
| // whose CLR wrapper is still alive: .NET may have collected the weak wrapper meanwhile | ||
| var peer = e.InnerException is IPythonDerivedType derived ? PythonDerivedType.GetPyObj(derived) : default; | ||
| var wrapper = peer.Ref != null && peer.Run == Runtime.GetRun() | ||
| ? ManagedType.GetManagedObject(peer.CheckRun()) | ||
| : null; | ||
| using var cause = wrapper != null | ||
| ? ClassDerivedObject.ToPython((IPythonDerivedType)e.InnerException) | ||
| : CLRObject.GetReference(e.InnerException); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Known limit, not blocking: ordinary .NET GC can collect a generated inner exception's weak CLR wrapper before
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recorded under Known limits, per the maintainer decision on exotic cases, with no code change. |
||
| GC.KeepAlive(wrapper); | ||
| Runtime.PyException_SetCause(ob, cause.Steal()); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.