| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -293,6 +293,8 @@ internal static bool ToManaged(IntPtr value, Type type, | |||
| 293 | 293 | } | |
| 294 | 294 | ||
| 295 | 295 | ||
| 296 | + internal static bool ToManagedValue(BorrowedReference value, Type obType, out object result, bool setError) | ||
| 297 | + => ToManagedValue(value.DangerousGetAddress(), obType, out result, setError); | ||
| 296 | 298 | internal static bool ToManagedValue(IntPtr value, Type obType, | |
| 297 | 299 | out object result, bool setError) | |
| 298 | 300 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -262,6 +262,7 @@ public static void SetError(IntPtr ob, IntPtr value) | |||
| 262 | 262 | Runtime.PyErr_SetObject(ob, value); | |
| 263 | 263 | } | |
| 264 | 264 | ||
| 265 | + internal const string DispatchInfoAttribute = "__dispatch_info__"; | ||
| 265 | 266 | /// <summary> | |
| 266 | 267 | /// SetError Method | |
| 267 | 268 | /// </summary> | |
@@ -288,8 +289,9 @@ public static void SetError(Exception e) | |||
| 288 | 289 | IntPtr etype = Runtime.PyObject_GetAttrString(op, "__class__"); | |
| 289 | 290 | #if NETSTANDARD | |
| 290 | 291 | var exceptionInfo = ExceptionDispatchInfo.Capture(e); | |
| 291 | - Runtime.XDecref(op); | ||
| 292 | - op = Converter.ToPython(exceptionInfo); | ||
| 292 | + IntPtr pyInfo = Converter.ToPython(exceptionInfo); | ||
| 293 | + Runtime.PyObject_SetAttrString(op, DispatchInfoAttribute, pyInfo); | ||
| 294 | + Runtime.XDecref(pyInfo); | ||
| 293 | 295 | #endif | |
| 294 | 296 | ||
| 295 | 297 | Runtime.PyErr_SetObject(etype, op); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -69,14 +69,19 @@ internal static Exception ThrowLastAsClrException() | |||
| 69 | 69 | Runtime.PyErr_Fetch(out var type, out var value, out var traceback); | |
| 70 | 70 | try | |
| 71 | 71 | { | |
| 72 | - var clrObject = ManagedType.GetManagedObject(value) as CLRObject; | ||
| 73 | 72 | #if NETSTANDARD | |
| 74 | - if (clrObject?.inst is ExceptionDispatchInfo storedException) | ||
| 73 | + if (!value.IsNull()) | ||
| 75 | 74 | { | |
| 76 | - storedException.Throw(); | ||
| 77 | - throw storedException.SourceException; // unreachable | ||
| 75 | + var exceptionInfo = TryGetDispatchInfo(value); | ||
| 76 | + if (exceptionInfo != null) | ||
| 77 | + { | ||
| 78 | + exceptionInfo.Throw(); | ||
| 79 | + throw exceptionInfo.SourceException; // unreachable | ||
| 80 | + } | ||
| 78 | 81 | } | |
| 79 | 82 | #endif | |
| 83 | + | ||
| 84 | + var clrObject = ManagedType.GetManagedObject(value) as CLRObject; | ||
| 80 | 85 | if (clrObject?.inst is Exception e) | |
| 81 | 86 | { | |
| 82 | 87 | throw e; | |
@@ -98,6 +103,37 @@ internal static Exception ThrowLastAsClrException() | |||
| 98 | 103 | } | |
| 99 | 104 | } | |
| 100 | 105 | ||
| 106 | + #if NETSTANDARD | ||
| 107 | + static ExceptionDispatchInfo TryGetDispatchInfo(BorrowedReference exception) | ||
| 108 | + { | ||
| 109 | + if (exception.IsNull) return null; | ||
| 110 | + | ||
| 111 | + var pyInfo = Runtime.PyObject_GetAttrString(exception, Exceptions.DispatchInfoAttribute); | ||
| 112 | + if (pyInfo.IsNull()) | ||
| 113 | + { | ||
| 114 | + if (Exceptions.ExceptionMatches(Exceptions.AttributeError)) | ||
| 115 | + { | ||
| 116 | + Exceptions.Clear(); | ||
| 117 | + } | ||
| 118 | + return null; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + try | ||
| 122 | + { | ||
| 123 | + if (Converter.ToManagedValue(pyInfo, typeof(ExceptionDispatchInfo), out object result, setError: false)) | ||
| 124 | + { | ||
| 125 | + return (ExceptionDispatchInfo)result; | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + return null; | ||
| 129 | + } | ||
| 130 | + finally | ||
| 131 | + { | ||
| 132 | + pyInfo.Dispose(); | ||
| 133 | + } | ||
| 134 | + } | ||
| 135 | + #endif | ||
| 136 | + | ||
| 101 | 137 | /// <summary> | |
| 102 | 138 | /// Requires lock to be acquired elsewhere | |
| 103 | 139 | /// </summary> | |
@@ -106,19 +142,20 @@ static Exception FromPyErr(BorrowedReference typeHandle, BorrowedReference value | |||
| 106 | 142 | Exception inner = null; | |
| 107 | 143 | string pythonTypeName = null, msg = "", tracebackText = null; | |
| 108 | 144 | ||
| 109 | - var clrObject = ManagedType.GetManagedObject(valueHandle) as CLRObject; | ||
| 110 | - if (clrObject?.inst is Exception e) | ||
| 111 | - { | ||
| 112 | - return e; | ||
| 113 | - } | ||
| 114 | - | ||
| 115 | 145 | #if NETSTANDARD | |
| 116 | - if (clrObject?.inst is ExceptionDispatchInfo exceptionDispatchInfo) | ||
| 146 | + var exceptionDispatchInfo = TryGetDispatchInfo(valueHandle); | ||
| 147 | + if (exceptionDispatchInfo != null) | ||
| 117 | 148 | { | |
| 118 | 149 | return exceptionDispatchInfo.SourceException; | |
| 119 | 150 | } | |
| 120 | 151 | #endif | |
| 121 | 152 | ||
| 153 | + var clrObject = ManagedType.GetManagedObject(valueHandle) as CLRObject; | ||
| 154 | + if (clrObject?.inst is Exception e) | ||
| 155 | + { | ||
| 156 | + return e; | ||
| 157 | + } | ||
| 158 | + | ||
| 122 | 159 | var type = PyObject.FromNullableReference(typeHandle); | |
| 123 | 160 | var value = PyObject.FromNullableReference(valueHandle); | |
| 124 | 161 | var traceback = PyObject.FromNullableReference(tracebackHandle); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -937,7 +937,10 @@ internal static bool PyObject_IsIterable(IntPtr pointer) | |||
| 937 | 937 | internal static extern int PyObject_HasAttrString(IntPtr pointer, string name); | |
| 938 | 938 | ||
| 939 | 939 | [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] | |
| 940 | + [Obsolete("Use overload accepting BorrowedReference")] | ||
| 940 | 941 | internal static extern IntPtr PyObject_GetAttrString(IntPtr pointer, string name); | |
| 942 | + [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] | ||
| 943 | + internal static extern NewReference PyObject_GetAttrString(BorrowedReference pointer, string name); | ||
| 941 | 944 | ||
| 942 | 945 | [DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)] | |
| 943 | 946 | internal static extern int PyObject_SetAttrString(IntPtr pointer, string name, IntPtr value); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments