|
14 | 14 | # ============================================================================== |
15 | 15 |
|
16 | 16 | import atexit |
| 17 | +import importlib |
| 18 | +import re |
17 | 19 | import sys |
18 | 20 |
|
19 | 21 | import pytest |
|
26 | 28 | ) |
27 | 29 |
|
28 | 30 |
|
29 | | -if PYPY or WASM or sys.version_info < (3, 14): |
| 31 | +if ( |
| 32 | + PYPY |
| 33 | + or WASM |
| 34 | + or sys.version_info < (3, 14) |
| 35 | + or not getattr(sys.implementation, 'supports_isolated_interpreters', False) |
| 36 | +): |
30 | 37 | pytest.skip('Test for CPython 3.14+ only', allow_module_level=True) |
31 | 38 |
|
32 | 39 |
|
| 40 | +from concurrent import interpreters |
33 | 41 | from concurrent.futures import InterpreterPoolExecutor, as_completed |
34 | 42 |
|
35 | 43 |
|
@@ -64,3 +72,29 @@ def concurrent_run(func, /, *args, **kwargs): |
64 | 72 |
|
65 | 73 |
|
66 | 74 | run(object) # warm-up |
| 75 | + |
| 76 | + |
| 77 | +def test_import_failure(): |
| 78 | + sub_interpreter = interpreters.create() |
| 79 | + |
| 80 | + with pytest.raises( |
| 81 | + ImportError, |
| 82 | + match=re.escape('module optree._C does not support loading in subinterpreters'), |
| 83 | + ) as excinfo: |
| 84 | + run(importlib.import_module, 'optree') |
| 85 | + |
| 86 | + with pytest.raises( |
| 87 | + interpreters.ExecutionFailed, |
| 88 | + match=re.escape('module optree._C does not support loading in subinterpreters'), |
| 89 | + ) as excinfo: |
| 90 | + sub_interpreter.call(importlib.import_module, 'optree') |
| 91 | + assert excinfo.value.excinfo.type.__name__ == 'ImportError' |
| 92 | + |
| 93 | + with pytest.raises( |
| 94 | + interpreters.ExecutionFailed, |
| 95 | + match=re.escape('module optree._C does not support loading in subinterpreters'), |
| 96 | + ) as excinfo: |
| 97 | + sub_interpreter.exec('import optree') |
| 98 | + assert excinfo.value.excinfo.type.__name__ == 'ImportError' |
| 99 | + |
| 100 | + sub_interpreter.close() |
0 commit comments