Skip to content

Commit 83d0486

Browse files
committed
Add tests for exception chaining in Python 3.
1 parent e9b22d9 commit 83d0486

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/testing/exceptiontest.cs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22

3-
43
namespace Python.Test
54
{
65
//========================================================================
@@ -55,6 +54,25 @@ public static bool ThrowException()
5554
{
5655
throw new OverflowException("error");
5756
}
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+
}
5876
}
5977

6078

src/tests/test_exceptions.py

+21
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,27 @@ def testPicklingExceptions(self):
345345

346346
self.assertEqual(exc.args, loaded.args)
347347

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+
348369

349370
def test_suite():
350371
return unittest.makeSuite(ExceptionTests)

0 commit comments

Comments
 (0)