| 1 |
import unittest |
| 2 |
|
| 3 |
import jpyutil |
| 4 |
|
| 5 |
re |
| 6 |
lf |
| 7 |
jpyutil.init_jvm(jvm_maxmem='512M', jvm_classpath=['target/classes', 'target/test-classes']) |
| 8 |
import jpy |
| 9 |
|
| 10 |
|
| 11 |
class TestPythonExceptionChain(unittest.TestCase): |
| 12 |
""" |
| 13 |
Verifies that PyLib_HandlePythonException preserves the full Python |
| 14 |
exception chain when a Python script is executed via Java (EvalTestFixture). |
| 15 |
|
| 16 |
The chain is formatted by Python's own traceback.format_exception(), so all |
| 17 |
three chaining cases defined by PEP 3134 are exercised: |
| 18 |
|
| 19 |
- Explicit cause (raise B from A) |
| 20 |
→ "The above exception was the direct cause of the following exception" |
| 21 |
- Implicit context (except A: raise B) |
| 22 |
→ "During handling of the above exception, another exception occurred" |
| 23 |
- Suppressed (raise B from None) |
| 24 |
→ only B should appear; A must be absent |
| 25 |
""" |
| 26 |
|
| 27 |
def setUp(self): |
| 28 |
self.fixture = jpy.get_type('org.jpy.fixtures.EvalTestFixture') |
| 29 |
self.assertIsNotNone(self.fixture) |
| 30 |
|
| 31 |
def _run_script(self, script: str) -> str: |
| 32 |
"""Execute a Python snippet through Java and return the RuntimeError message.""" |
| 33 |
with self.assertRaises(RuntimeError) as ctx: |
| 34 |
self.fixture.script(script) |
| 35 |
msg = str(ctx.exception) |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
self.assertIn("Error in Python interpreter:", msg, |
| 41 |
f"Expected prefix 'Error in Python interpreter:' not found in:\n{msg}") |
| 42 |
return msg |
| 43 |
|
| 44 |
def test_explicit_cause(self): |
| 45 |
"""raise B from A — both exceptions plus the 'direct cause' phrase must appear.""" |
| 46 |
msg = self._run_script( |
| 47 |
"try:\n" |
| 48 |
" raise TypeError('root cause')\n" |
| 49 |
"except TypeError as e:\n" |
| 50 |
" raise ValueError('outer error') from e\n" |
| 51 |
) |
| 52 |
self.assertIn("TypeError", msg) |
| 53 |
self.assertIn("root cause", msg) |
| 54 |
self.assertIn("ValueError", msg) |
| 55 |
self.assertIn("outer error", msg) |
| 56 |
|
| 57 |
self.assertIn("direct cause", msg) |
| 58 |
|
| 59 |
def test_implicit_context(self): |
| 60 |
"""Implicit context (no 'from') — both exceptions must appear with the right phrase.""" |
| 61 |
msg = self._run_script( |
| 62 |
"try:\n" |
| 63 |
" raise TypeError('original error')\n" |
| 64 |
"except TypeError:\n" |
| 65 |
" raise ValueError('replacement error')\n" |
| 66 |
) |
| 67 |
self.assertIn("TypeError", msg) |
| 68 |
self.assertIn("original error", msg) |
| 69 |
self.assertIn("ValueError", msg) |
| 70 |
self.assertIn("replacement error", msg) |
| 71 |
|
| 72 |
self.assertIn("During handling of the above exception", msg) |
| 73 |
|
| 74 |
def test_suppressed_context(self): |
| 75 |
"""raise B from None — context is suppressed; only B should appear.""" |
| 76 |
msg = self._run_script( |
| 77 |
"try:\n" |
| 78 |
" raise TypeError('hidden cause')\n" |
| 79 |
"except TypeError:\n" |
| 80 |
" raise ValueError('only this') from None\n" |
| 81 |
) |
| 82 |
self.assertIn("ValueError", msg) |
| 83 |
self.assertIn("only this", msg) |
| 84 |
|
| 85 |
self.assertNotIn("TypeError", msg) |
| 86 |
self.assertNotIn("hidden cause", msg) |
| 87 |
|
| 88 |
def test_three_level_chain(self): |
| 89 |
"""A → B → C explicit chain — all three levels must be present.""" |
| 90 |
msg = self._run_script( |
| 91 |
"try:\n" |
| 92 |
" try:\n" |
| 93 |
" raise RuntimeError('level 1')\n" |
| 94 |
" except RuntimeError as e:\n" |
| 95 |
" raise TypeError('level 2') from e\n" |
| 96 |
"except TypeError as e:\n" |
| 97 |
" raise ValueError('level 3') from e\n" |
| 98 |
) |
| 99 |
self.assertIn("RuntimeError", msg) |
| 100 |
self.assertIn("level 1", msg) |
| 101 |
self.assertIn("TypeError", msg) |
| 102 |
self.assertIn("level 2", msg) |
| 103 |
self.assertIn("ValueError", msg) |
| 104 |
self.assertIn("level 3", msg) |
| 105 |
|
| 106 |
|
| 107 |
if __name__ == '__main__': |
| 108 |
print('\nRunning ' + __file__) |
| 109 |
unittest.main() |
| 110 |
|