8000 Improve Python <-> .NET exception integration by lostmsu · Pull Request #1134 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Improve Python <-> .NET exception integration #1134

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 42 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
663df73
reworked PythonException to simplify memory management, and enable .N…
lostmsu May 5, 2020
d45c39a
allows codecs to encode and decode thrown exceptions
lostmsu Feb 25, 2020
0d941c5
turned on CLR exception marshaling
lostmsu May 5, 2020
02cd234
revert to C# 7.3
lostmsu May 5, 2020
defb200
fixed Restore when not all exception fields are set
lostmsu May 5, 2020
945dc85
cleaned PythonException code up a bit
lostmsu May 5, 2020
3d95e60
remove unused overloads of FromPyErr
lostmsu May 5, 2020
d667998
capture exception on Exceptions.SetError, restore in ThrowLastAsClrEx…
lostmsu May 5, 2020
bec8b7d
fixed failure in ExceptionEncoded test case caused by ExceptionDispat…
lostmsu May 5, 2020
0961c94
added tests for __cause__/InnerException #893 #1098
lostmsu May 6, 2020
2cd8627
introduced StolenReference type
lostmsu May 14, 2020
e4c1b9b
prevent undebuggable StackOverflowException during exception interop
lostmsu May 29, 2020
81cd197
Merge branch 'master' into PR/ExceptionsImprovement
lostmsu Jun 22, 2020
3c0b737
README: added summary of new exception handling features
lostmsu Jun 22, 2020
257a765
merge latest master
lostmsu Apr 9, 2021
c0fe430
reworked `PythonException`:
lostmsu Apr 9, 2021
e58411d
rum embedding tests before Python tests
lostmsu Apr 9, 2021
00653dc
PythonException.StackTrace is GIL-safe
lostmsu Apr 9, 2021
3433201
separate .Steal() and .StealNullable()
lostmsu Apr 11, 2021
95cc52f
can't test exception type when runtime is down
lostmsu Apr 11, 2021
63ad42c
PythonException: dispose intermediate values used in stack trace gene…
lostmsu Apr 11, 2021
faec7fc
Point users to Message property of PythonException
lostmsu Apr 11, 2021
dfc70f6
minor change in PythonEngine.With
lostmsu Apr 11, 2021
d976acf
simplified code of PythonException and added a lot more checks
lostmsu Apr 11, 2021
146ebf3
fixed type of reference in PyException_SetCause
lostmsu Apr 11, 2021
272687b
minor fixes to Converter.ToArray:
lostmsu Apr 11, 2021
2cd3f61
added a few debug checks to Exceptions.SetError
lostmsu Apr 11, 2021
e79f041
method binding failure now supports non-Python exception causes
lostmsu Apr 11, 2021
d068f36
XDecref now checks, that refcount is positive in debug builds
lostmsu Apr 11, 2021
4877fe7
fixed __cause__ on overload bind failure and array conversion
lostmsu Apr 11, 2021
ed594c1
cache PythonException message
lostmsu Apr 11, 2021
e5bce06
minor code cleanup
lostmsu Apr 11, 2021
6819e7b
improved handling of dict offset in object instances
lostmsu Apr 11, 2021
6679d1c
added a few debug checks
lostmsu Apr 11, 2021
67bd1c2
merge latest master
lostmsu May 15, 2021
2e57b04
+ class diagram for ManagedType and subclasses
lostmsu May 23, 2021
539ce81
added OverloadMapper to ManagedTypes class diagram
lostmsu May 23, 2021
25e3864
refactored tp_dealloc in ExtensionType and descendants
lostmsu May 23, 2021
5bca333
refactored tp_clear in ExtensionType and descendants into a virtual C…
lostmsu May 23, 2021
7271d88
all Dealloc overrides simply duplicate Clear, so just call both from …
lostmsu May 23, 2021
4f3f648
fixup! merge latest master
SDEII May 29, 2021
c500a39
fixup! reworked `PythonException`:
lostmsu May 29, 2021
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
minor code cleanup
  • Loading branch information
lostmsu committed Apr 11, 2021
commit e5bce06b4819abfd77542215362a5c26cdb9b26e
5 changes: 4 additions & 1 deletion src/runtime/clrobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal CLRObject(object ob, IntPtr tp)

// Fix the BaseException args (and __cause__ in case of Python 3)
// slot if wrapping a CLR exception
Exceptions.SetArgsAndCause(py);
Exceptions.SetArgsAndCause(ObjectReference);
}

protected CLRObject()
Expand Down Expand Up @@ -78,6 +78,9 @@ internal static IntPtr GetInstHandle(object ob)
return co.pyHandle;
}

internal static NewReference GetReference(object ob)
=> NewReference.DangerousFromPointer(GetInstHandle(ob));

internal static CLRObject Restore(object ob, IntPtr pyHandle, InterDomainContext context)
{
CLRObject co = new CLRObject()
Expand Down
25 changes: 8 additions & 17 deletions src/runtime/exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,18 @@ internal ExceptionClassObject(Type tp) : base(tp)
{
}

internal static Exception ToException(IntPtr ob)
internal static Exception ToException(BorrowedReference ob)
{
var co = GetManagedObject(ob) as CLRObject;
if (co == null)
{
return null;
}
var e = co.inst as Exception;
if (e == null)
{
return null;
}
return e;
return co?.inst as Exception;
}

/// <summary>
/// Exception __repr__ implementation
/// </summary>
public new static IntPtr tp_repr(IntPtr ob)
{
Exception e = ToException(ob);
Exception e = ToException(new BorrowedReference(ob));
if (e == null)
{
return Exceptions.RaiseTypeError("invalid object");
Expand All @@ -67,7 +58,7 @@ internal static Exception ToException(IntPtr ob)
/// </summary>
public new static IntPtr tp_str(IntPtr ob)
{
Exception e = ToException(ob);
Exception e = ToException(new BorrowedReference(ob));
if (e == null)
{
return Exceptions.RaiseTypeError("invalid object");
Expand Down Expand Up @@ -157,7 +148,7 @@ internal static void Shutdown()
/// pointer.
/// </summary>
/// <param name="ob">The python object wrapping </param>
internal static void SetArgsAndCause(IntPtr ob)
internal static void SetArgsAndCause(BorrowedReference ob)
{
// e: A CLR Exception
Exception e = ExceptionClassObject.ToException(ob);
Expand All @@ -178,13 +169,13 @@ internal static void SetArgsAndCause(IntPtr ob)
args = Runtime.PyTuple_New(0);
}

Marshal.WriteIntPtr(ob, ExceptionOffset.args, args);
Marshal.WriteIntPtr(ob.DangerousGetAddress(), ExceptionOffset.args, args);

if (e.InnerException != null)
{
// Note: For an AggregateException, InnerException is only the first of the InnerExceptions.
IntPtr cause = CLRObject.GetInstHandle(e.InnerException);
Marshal.WriteIntPtr(ob, ExceptionOffset.cause, cause);
using var cause = CLRObject.GetReference(e.InnerException);
Runtime.PyException_SetCause(ob, cause.Steal());
}
}

Expand Down
0