Skip to content

Commit 1a23716

Browse files
gh-117953: Imply Single-phase Init if the Init Function Fails (gh-118684)
This ensures the kind is always either _Py_ext_module_kind_SINGLEPHASE or _Py_ext_module_kind_MULTIPHASE.
1 parent 853163d commit 1a23716

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Python/importdl.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@ _PyImport_RunModInitFunc(PyModInitFunction p0,
426426
/* Validate the result (and populate "res". */
427427

428428
if (m == NULL) {
429+
/* The init func for multi-phase init modules is expected
430+
* to return a PyModuleDef after calling PyModuleDef_Init().
431+
* That function never raises an exception nor returns NULL,
432+
* so at this point it must be a single-phase init modules. */
433+
res.kind = _Py_ext_module_kind_SINGLEPHASE;
429434
if (PyErr_Occurred()) {
430435
_Py_ext_module_loader_result_set_error(
431436
&res, _Py_ext_module_loader_result_EXCEPTION);
@@ -436,6 +441,8 @@ _PyImport_RunModInitFunc(PyModInitFunction p0,
436441
}
437442
goto error;
438443
} else if (PyErr_Occurred()) {
444+
/* Likewise, we infer that this is a single-phase init module. */
445+
res.kind = _Py_ext_module_kind_SINGLEPHASE;
439446
_Py_ext_module_loader_result_set_error(
440447
&res, _Py_ext_module_loader_result_ERR_UNREPORTED_EXC);
441448
/* We would probably be correct to decref m here,

0 commit comments

Comments
 (0)