Skip to content
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
Next Next commit
reworked PythonException to simplify memory management, and enable .N…
…ET interop

New method ThrowLastAsClrException should be used everywhere instead of obsolete PythonException constructor. It automatically restores .NET exceptions, and applies codecs for Python exceptions.

Traceback, PyVal and PyType are now stored and returned as PyObjects.

PythonException now has InnerException set from its cause (e.g. __cause__ in Python, if any).

PythonException.Restore no longer clears the exception instance.

All helper methods were removed from public API surface.
  • Loading branch information
lostmsu committed May 13, 2020
commit 663df73b856b312460fc316ac121e0692dd87141
2 changes: 2 additions & 0 deletions src/runtime/BorrowedReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ readonly ref struct BorrowedReference
public IntPtr DangerousGetAddress()
=> this.IsNull ? throw new NullReferenceException() : this.pointer;

public static BorrowedReference Null => new BorrowedReference();

/// <summary>
/// Creates new instance of <see cref="BorrowedReference"/> from raw pointer. Unsafe.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/NewReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public PyObject MoveToPyObject()
return result;
}
/// <summary>
/// Returns <see cref="PyObject"/> wrapper around this reference, which now owns
/// the pointer. Sets the original reference to <c>null</c>, as it no longer owns it.
/// </summary>
public PyObject MoveToPyObjectOrNull() => this.IsNull() ? null : this.MoveToPyObject();
/// <summary>
/// Removes this reference to a Python object, and sets it to <c>null</c>.
/// </summary>
public void Dispose()
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/converterextensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ static IPyObjectEncoder[] GetEncoders(Type type)
#region Decoding
static readonly ConcurrentDictionary<TypePair, Converter.TryConvertFromPythonDelegate>
pythonToClr = new ConcurrentDictionary<TypePair, Converter.TryConvertFromPythonDelegate>();
internal static bool TryDecode(BorrowedReference value, BorrowedReference type, Type targetType, out object result)
=> TryDecode(value.DangerousGetAddress(), type.DangerousGetAddress(), targetType, out result);
internal static bool TryDecode(IntPtr pyHandle, IntPtr pyType, Type targetType, out object result)
{
if (pyHandle == IntPtr.Zero) throw new ArgumentNullException(nameof(pyHandle));
Expand Down
5 changes: 1 addition & 4 deletions src/runtime/exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,7 @@ public static void SetError(Exception e)
var pe = e as PythonException;
if (pe != null)
{
Runtime.XIncref(pe.PyType);
Runtime.XIncref(pe.PyValue);
Runtime.XIncref(pe.PyTB);
Runtime.PyErr_Restore(pe.PyType, pe.PyValue, pe.PyTB);
pe.Restore();
return;
}

Expand Down
6 changes: 6 additions & 0 deletions src/runtime/managedtype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ internal abstract class ManagedType
internal IntPtr tpHandle; // PyType *


/// <summary>
/// Given a Python object, return the associated managed object or null.
/// </summary>
internal static ManagedType GetManagedObject(BorrowedReference ob)
=> GetManagedObject(ob.DangerousGetAddress());

/// <summary>
/// Given a Python object, return the associated managed object or null.
/// </summary>
Expand Down
15 changes: 14 additions & 1 deletion src/runtime/pyobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public class PyObject : DynamicObject, IEnumerable, IPyDisposable

protected internal IntPtr obj = IntPtr.Zero;

internal BorrowedReference Reference => new BorrowedReference(obj);
public static PyObject None => new PyObject(new BorrowedReference(Runtime.PyNone));
internal BorrowedReference Reference => new BorrowedReference(this.obj);
internal NewReference MakeNewReference()
=> NewReference.DangerousFromPointer(Runtime.SelfIncRef(this.obj));

/// <summary>
/// PyObject Constructor
Expand Down Expand Up @@ -125,6 +128,13 @@ public static PyObject FromManagedObject(object ob)
return new PyObject(op);
}

/// <summary>
/// Creates new <see cref="PyObject"/> from a nullable reference.
/// When <paramref name="reference"/> is <c>null</c>, <c>null</c> is returned.
/// </summary>
internal static PyObject FromNullableReference(BorrowedReference reference)
=> reference.IsNull ? null : new PyObject(reference);


/// <summary>
/// AsManagedObject Method
Expand Down Expand Up @@ -226,6 +236,9 @@ public IntPtr[] GetTrackedHandles()
return new IntPtr[] { obj };
}

internal BorrowedReference GetPythonTypeReference()
=> new BorrowedReference(Runtime.PyObject_TYPE(obj));

/// <summary>
/// GetPythonType Method
/// </summary>
Expand Down
17 changes: 7 additions & 10 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,9 @@ public static void With(PyObject obj, Action<dynamic> Body)
// Behavior described here:
// https://docs.python.org/2/reference/datamodel.html#with-statement-context-managers

IntPtr type = Runtime.PyNone;
IntPtr val = Runtime.PyNone;
IntPtr traceBack = Runtime.PyNone;
PyObject type = PyObject.None;
PyObject val = PyObject.None;
PyObject traceBack = PyObject.None;
PythonException ex = null;

try
Expand All @@ -769,15 +769,12 @@ public static void With(PyObject obj, Action<dynamic> Body)
catch (PythonException e)
{
ex = e;
type = ex.PyType.Coalesce(type);
val = ex.PyValue.Coalesce(val);
traceBack = ex.PyTB.Coalesce(traceBack);
type = ex.PyType ?? type;
val = ex.PyValue ?? val;
traceBack = ex.PyTB ?? traceBack;
}

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

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