| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3b9e18a commit 071f25d
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,8 +31,9 @@ internal CLRObject(Object ob, IntPtr tp) : base() | |||
| 31 | 31 | this.gcHandle = gc; | |
| 32 | 32 | inst = ob; | |
| 33 | 33 | ||
| 34 | - // Fix the BaseException args slot if wrapping a CLR exception | ||
| 35 | - Exceptions.SetArgs(py); | ||
| 34 | + // Fix the BaseException args (and __cause__ in case of Python 3) | ||
| 35 | + // slot if wrapping a CLR exception | ||
| 36 | + Exceptions.SetArgsAndCause(py); | ||
| 36 | 37 | } | |
| 37 | 38 | ||
| 38 | 39 | ||
@@ -70,4 +71,4 @@ internal static IntPtr GetInstHandle(Object ob) | |||
| 70 | 71 | return co.pyHandle; | |
| 71 | 72 | } | |
| 72 | 73 | } | |
| 73 | - } | ||
| 74 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -144,7 +144,7 @@ internal static void Shutdown() | |||
| 144 | 144 | /// </summary> | |
| 145 | 145 | /// <param name="e">A CLR exception</param> | |
| 146 | 146 | /// <param name="ob">The python object wrapping </param> | |
| 147 | - internal static void SetArgs(IntPtr ob) | ||
| 147 | + internal static void SetArgsAndCause(IntPtr ob) | ||
| 148 | 148 | { | |
| 149 | 149 | var e = ExceptionClassObject.ToException(ob); | |
| 150 | 150 | if (e == null) | |
@@ -163,6 +163,14 @@ internal static void SetArgs(IntPtr ob) | |||
| 163 | 163 | } | |
| 164 | 164 | ||
| 165 | 165 | Marshal.WriteIntPtr(ob, ExceptionOffset.args, args); | |
| 166 | + | ||
| 167 | + #if !(PYTHON25 || PYTHON26 || PYTHON27) | ||
| 168 | + if (e.InnerException != null) | ||
| 169 | + { | ||
| 170 | + IntPtr cause = CLRObject.GetInstHandle(e.InnerException); | ||
| 171 | + Marshal.WriteIntPtr(ob, ExceptionOffset.cause, cause); | ||
| 172 | + } | ||
| 173 | + #endif | ||
| 166 | 174 | } | |
| 167 | 175 | ||
| 168 | 176 | /// <summary> | |
@@ -434,4 +442,4 @@ puplic static variables on the Exceptions class filled in from | |||
| 434 | 442 | //PyAPI_DATA(PyObject *) PyExc_BytesWarning; | |
| 435 | 443 | #endif | |
| 436 | 444 | } | |
| 437 | - } | ||
| 445 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,5 @@ | |||
| 1 | 1 | using System; | |
| 2 | 2 | ||
| 3 | - | ||
| 4 | 3 | namespace Python.Test | |
| 5 | 4 | { | |
| 6 | 5 | //======================================================================== | |
@@ -55,6 +54,25 @@ public static bool ThrowException() | |||
| 55 | 54 | { | |
| 56 | 55 | throw new OverflowException("error"); | |
| 57 | 56 | } | |
| 57 | + | ||
| 58 | + public static void ThrowChainedExceptions() | ||
| 59 | + { | ||
| 60 | + try | ||
| 61 | + { | ||
| 62 | + try | ||
| 63 | + { | ||
| 64 | + throw new Exception("Innermost exception"); | ||
| 65 | + } | ||
| 66 | + catch (Exception exc) | ||
| 67 | + { | ||
| 68 | + throw new Exception("Inner exception", exc); | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | + catch (Exception exc2) | ||
| 72 | + { | ||
| 73 | + throw new Exception("Outer exception", exc2); | ||
| 74 | + } | ||
| 75 | + } | ||
| 58 | 76 | } | |
| 59 | 77 | ||
| 60 | 78 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -345,6 +345,27 @@ def testPicklingExceptions(self): | |||
| 345 | 345 | ||
| 346 | 346 | self.assertEqual(exc.args, loaded.args) | |
| 347 | 347 | ||
| 348 | + def testChainedExceptions(self): | ||
| 349 | + if six.PY3: | ||
| 350 | + from Python.Test import ExceptionTest | ||
| 351 | + | ||
| 352 | + try: | ||
| 353 | + ExceptionTest.ThrowChainedExceptions() | ||
| 354 | + except Exception as exc: | ||
| 355 | + msgs = [ | ||
| 356 | + "Outer exception", | ||
| 357 | + "Inner exception", | ||
| 358 | + "Innermost exception" | ||
| 359 | + ] | ||
| 360 | + | ||
| 361 | + for msg in msgs: | ||
| 362 | + self.assertEqual(exc.Message, msg) | ||
| 363 | + self.assertEqual(exc.__cause__, exc.InnerException) | ||
| 364 | + exc = exc.__cause__ | ||
| 365 | + | ||
| 366 | + else: | ||
| 367 | + self.fail("Test should raise an exception") | ||
| 368 | + | ||
| 348 | 369 | ||
| 349 | 370 | def test_suite(): | |
| 350 | 371 | return unittest.makeSuite(ExceptionTests) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments