Skip to content

Commit 48920c6

Browse files
committed
Add a test case.
1 parent 6f63e57 commit 48920c6

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

Lib/test/test_import/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
Py_GIL_DISABLED,
4646
no_rerun,
4747
force_not_colorized_test_class,
48+
catch_unraisable_exception
4849
)
4950
from test.support.import_helper import (
5051
forget, make_legacy_pyc, unlink, unload, ready_to_import,
@@ -2517,6 +2518,30 @@ def test_disallowed_reimport(self):
25172518
excsnap = _interpreters.run_string(interpid, script)
25182519
self.assertIsNot(excsnap, None)
25192520

2521+
@requires_subinterpreters
2522+
def test_pyinit_function_raises_exception(self):
2523+
import _testsinglephase
2524+
filename = _testsinglephase.__file__
2525+
script = f"""if True:
2526+
from test.test_import import import_extension_from_file
2527+
2528+
import_extension_from_file('_testsinglephase_raise_exception', {filename!r})"""
2529+
2530+
interp = _interpreters.create()
2531+
try:
2532+
with catch_unraisable_exception() as cm:
2533+
exception = _interpreters.run_string(interp, script)
2534+
unraisable = cm.unraisable
2535+
finally:
2536+
_interpreters.destroy(interp)
2537+
2538+
self.assertIsNotNone(exception)
2539+
self.assertIsNotNone(exception.type.__name__, "ImportError")
2540+
self.assertIsNotNone(exception.msg, "failed to import from subinterpreter due to exception")
2541+
self.assertIsNotNone(unraisable)
2542+
self.assertIs(unraisable.exc_type, RuntimeError)
2543+
self.assertEqual(str(unraisable.exc_value), "evil")
2544+
25202545

25212546
class TestSinglePhaseSnapshot(ModuleSnapshot):
25222547
"""A representation of a single-phase init module for testing.

Modules/_testsinglephase.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,3 +801,11 @@ PyInit__testsinglephase_circular(void)
801801
}
802802
return Py_NewRef(static_module_circular);
803803
}
804+
805+
806+
PyMODINIT_FUNC
807+
PyInit__testsinglephase_raise_exception(void)
808+
{
809+
PyErr_SetString(PyExc_RuntimeError, "evil");
810+
return NULL;
811+
}

0 commit comments

Comments
 (0)