|
| 1 | +"""The installed JPype survives an exception that CPython holds without an instance. |
| 2 | +
|
| 3 | +jpype1 1.8.0 on CPython 3.11 ends the process with a segmentation fault when such an exception |
| 4 | +goes through JPype's own code: the ``KeyboardInterrupt`` of a Ctrl-C, or the ``StopIteration`` of |
| 5 | +``next()``. CPython 3.12 and later always hold an instance. ``pyproject.toml`` keeps jpype1 below |
| 6 | +1.8 on CPython 3.11, and this test fails when the installed JPype has the fault. |
| 7 | +
|
| 8 | +Each case runs in a child process, because the fault ends the process it happens in. Nothing |
| 9 | +depends on timing: ``signal.raise_signal`` and ``next()`` raise at a fixed point. |
| 10 | +""" |
| 11 | + |
| 12 | +import subprocess # nosec B404 - test-controlled child process |
| 13 | +import sys |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +_CHILD = r""" |
| 18 | +import signal |
| 19 | +import sys |
| 20 | +
|
| 21 | +import jpype |
| 22 | +
|
| 23 | +from arcadedb.jvm import start_jvm |
| 24 | +
|
| 25 | +start_jvm(heap_size="256m") |
| 26 | +# a parent that ignores SIGINT hands that to its child, and Python then installs no handler |
| 27 | +signal.signal(signal.SIGINT, signal.default_int_handler) |
| 28 | +
|
| 29 | +
|
| 30 | +def exhausted_iterator(): |
| 31 | + next(iter([])) |
| 32 | +
|
| 33 | +
|
| 34 | +def ctrl_c(): |
| 35 | + signal.raise_signal(signal.SIGINT) |
| 36 | +
|
| 37 | +
|
| 38 | +class Number: |
| 39 | + def __index__(self): |
| 40 | + signal.raise_signal(signal.SIGINT) |
| 41 | + return 1 |
| 42 | +
|
| 43 | + __int__ = __index__ |
| 44 | +
|
| 45 | +
|
| 46 | +case = sys.argv[1] |
| 47 | +print("jpype", jpype.__version__, flush=True) |
| 48 | +try: |
| 49 | + if case == "conversion": |
| 50 | + jpype.JInt(Number()) |
| 51 | + else: |
| 52 | + function = {"stop_iteration": exhausted_iterator, "ctrl_c": ctrl_c}[case] |
| 53 | + supplier = jpype.JProxy("java.util.function.Supplier", dict={"get": function}) |
| 54 | + jpype.JClass("java.util.Optional").empty().orElseGet(supplier) |
| 55 | +except BaseException as exc: |
| 56 | + print("raised", type(exc).__name__, flush=True) |
| 57 | +""" |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + "case, expected", |
| 62 | + [ |
| 63 | + # a Java method calls a Python function |
| 64 | + ("stop_iteration", "StopIteration"), |
| 65 | + ("ctrl_c", "KeyboardInterrupt"), |
| 66 | + # no Java-to-Python call: JPype converts a Python object for Java |
| 67 | + ("conversion", "KeyboardInterrupt"), |
| 68 | + ], |
| 69 | +) |
| 70 | +def test_jpype_raises_instead_of_ending_the_process(case, expected): |
| 71 | + result = ( |
| 72 | + subprocess.run( # nosec B603 - fixed argv, no shell, test-owned code string |
| 73 | + [sys.executable, "-c", _CHILD, case], |
| 74 | + capture_output=True, |
| 75 | + text=True, |
| 76 | + timeout=300, |
| 77 | + check=False, |
| 78 | + ) |
| 79 | + ) |
| 80 | + said = f"exit {result.returncode}\n{result.stdout}\n{result.stderr[-2000:]}" |
| 81 | + # the crash ends the child process, so its exit code is not 0 |
| 82 | + assert result.returncode == 0, said |
| 83 | + assert result.stdout.split()[-2:] == ["raised", expected], said |
0 commit comments