Skip to content

Commit b932173

Browse files
Factor out import_run_extension().
1 parent 5756201 commit b932173

File tree

1 file changed

+86
-109
lines changed

1 file changed

+86
-109
lines changed

Python/import.c

Lines changed: 86 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,86 @@ import_find_extension(PyThreadState *tstate,
13901390
return mod;
13911391
}
13921392

1393+
static PyObject *
1394+
import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
1395+
struct _Py_ext_module_loader_info *info,
1396+
PyObject *spec, PyObject *modules)
1397+
{
1398+
PyObject *mod = NULL;
1399+
PyModuleDef *def = NULL;
1400+
1401+
struct _Py_ext_module_loader_result res;
1402+
if (_PyImport_RunModInitFunc(p0, info, &res) < 0) {
1403+
/* We discard res.def. */
1404+
assert(res.module == NULL);
1405+
assert(PyErr_Occurred());
1406+
goto finally;
1407+
}
1408+
assert(!PyErr_Occurred());
1409+
1410+
mod = res.module;
1411+
res.module = NULL;
1412+
def = res.def;
1413+
assert(def != NULL);
1414+
1415+
if (mod == NULL) {
1416+
//assert(!is_singlephase(def));
1417+
assert(mod == NULL);
1418+
mod = PyModule_FromDefAndSpec(def, spec);
1419+
if (mod == NULL) {
1420+
goto finally;
1421+
}
1422+
}
1423+
else {
1424+
assert(is_singlephase(def));
1425+
assert(PyModule_Check(mod));
1426+
1427+
const char *name_buf = PyBytes_AS_STRING(info->name_encoded);
1428+
if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
1429+
Py_CLEAR(mod);
1430+
goto finally;
1431+
}
1432+
1433+
/* Remember pointer to module init function. */
1434+
def->m_base.m_init = p0;
1435+
1436+
if (info->filename != NULL) {
1437+
/* Remember the filename as the __file__ attribute */
1438+
if (PyModule_AddObjectRef(mod, "__file__", info->filename) < 0) {
1439+
PyErr_Clear(); /* Not important enough to report */
1440+
}
1441+
}
1442+
1443+
struct singlephase_global_update singlephase = {0};
1444+
// gh-88216: Extensions and def->m_base.m_copy can be updated
1445+
// when the extension module doesn't support sub-interpreters.
1446+
if (def->m_size == -1
1447+
&& !is_core_module(tstate->interp, info->name, info->path))
1448+
{
1449+
singlephase.m_dict = PyModule_GetDict(mod);
1450+
assert(singlephase.m_dict != NULL);
1451+
}
1452+
if (update_global_state_for_extension(
1453+
tstate, info->path, info->name, def, &singlephase) < 0)
1454+
{
1455+
Py_CLEAR(mod);
1456+
goto finally;
1457+
}
1458+
1459+
PyObject *modules = get_modules_dict(tstate, true);
1460+
if (finish_singlephase_extension(
1461+
tstate, mod, def, info->name, modules) < 0)
1462+
{
1463+
Py_CLEAR(mod);
1464+
goto finally;
1465+
}
1466+
}
1467+
1468+
finally:
1469+
return mod;
1470+
}
1471+
1472+
13931473
static int
13941474
clear_singlephase_extension(PyInterpreterState *interp,
13951475
PyObject *name, PyObject *path)
@@ -1495,8 +1575,6 @@ is_builtin(PyObject *name)
14951575
static PyObject*
14961576
create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
14971577
{
1498-
PyModuleDef *def = NULL;
1499-
15001578
struct _Py_ext_module_loader_info info;
15011579
if (_Py_ext_module_loader_info_init_for_builtin(&info, name) < 0) {
15021580
return NULL;
@@ -1532,54 +1610,9 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
15321610
goto finally;
15331611
}
15341612

1535-
struct _Py_ext_module_loader_result res;
1536-
if (_PyImport_RunModInitFunc(p0, &info, &res) < 0) {
1537-
goto finally;
1538-
}
1539-
1540-
mod = res.module;
1541-
res.module = NULL;
1542-
def = res.def;
1543-
assert(def != NULL);
1544-
1545-
if (mod == NULL) {
1546-
assert(!is_singlephase(def));
1547-
mod = PyModule_FromDefAndSpec(def, spec);
1548-
if (mod == NULL) {
1549-
goto finally;
1550-
}
1551-
}
1552-
else {
1553-
assert(is_singlephase(def));
1554-
1555-
/* Remember pointer to module init function. */
1556-
def->m_base.m_init = p0;
1557-
1558-
struct singlephase_global_update singlephase = {0};
1559-
// gh-88216: Extensions and def->m_base.m_copy can be updated
1560-
// when the extension module doesn't support sub-interpreters.
1561-
if (def->m_size == -1
1562-
&& !is_core_module(tstate->interp, info.name, info.path))
1563-
{
1564-
singlephase.m_dict = PyModule_GetDict(mod);
1565-
assert(singlephase.m_dict != NULL);
1566-
}
1567-
1568-
if (update_global_state_for_extension(
1569-
tstate, info.name, info.path, def, &singlephase) < 0)
1570-
{
1571-
Py_CLEAR(mod);
1572-
goto finally;
1573-
}
1574-
1575-
PyObject *modules = get_modules_dict(tstate, true);
1576-
if (finish_singlephase_extension(
1577-
tstate, mod, def, info.name, modules) < 0)
1578-
{
1579-
Py_CLEAR(mod);
1580-
goto finally;
1581-
}
1582-
}
1613+
/* Now load it. */
1614+
mod = import_run_extension(
1615+
tstate, p0, &info, spec, get_modules_dict(tstate, true));
15831616

15841617
finally:
15851618
_Py_ext_module_loader_info_clear(&info);
@@ -3894,7 +3927,6 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
38943927
/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
38953928
{
38963929
PyObject *mod = NULL;
3897-
PyModuleDef *def = NULL;
38983930
PyThreadState *tstate = _PyThreadState_GET();
38993931

39003932
struct _Py_ext_module_loader_info info;
@@ -3938,67 +3970,12 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
39383970
goto finally;
39393971
}
39403972

3941-
struct _Py_ext_module_loader_result res;
3942-
if (_PyImport_RunModInitFunc(p0, &info, &res) < 0) {
3943-
assert(PyErr_Occurred());
3944-
goto finally;
3945-
}
3946-
3947-
mod = res.module;
3948-
res.module = NULL;
3949-
def = res.def;
3950-
assert(def != NULL);
3951-
3973+
mod = import_run_extension(
3974+
tstate, p0, &info, spec, get_modules_dict(tstate, true));
39523975
if (mod == NULL) {
3953-
//assert(!is_singlephase(def));
3954-
mod = PyModule_FromDefAndSpec(def, spec);
3955-
if (mod == NULL) {
3956-
goto finally;
3957-
}
3958-
}
3959-
else {
3960-
assert(is_singlephase(def));
3961-
assert(!is_core_module(tstate->interp, info.name, info.filename));
3962-
assert(!is_core_module(tstate->interp, info.name, info.name));
3963-
3964-
const char *name_buf = PyBytes_AS_STRING(info.name_encoded);
3965-
if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
3966-
Py_CLEAR(mod);
3967-
goto finally;
3968-
}
3969-
3970-
/* Remember pointer to module init function. */
3971-
def->m_base.m_init = p0;
3972-
3973-
/* Remember the filename as the __file__ attribute */
3974-
if (PyModule_AddObjectRef(mod, "__file__", info.filename) < 0) {
3975-
PyErr_Clear(); /* Not important enough to report */
3976-
}
3977-
3978-
struct singlephase_global_update singlephase = {0};
3979-
// gh-88216: Extensions and def->m_base.m_copy can be updated
3980-
// when the extension module doesn't support sub-interpreters.
3981-
if (def->m_size == -1) {
3982-
singlephase.m_dict = PyModule_GetDict(mod);
3983-
assert(singlephase.m_dict != NULL);
3984-
}
3985-
if (update_global_state_for_extension(
3986-
tstate, info.filename, info.name, def, &singlephase) < 0)
3987-
{
3988-
Py_CLEAR(mod);
3989-
goto finally;
3990-
}
3991-
3992-
PyObject *modules = get_modules_dict(tstate, true);
3993-
if (finish_singlephase_extension(
3994-
tstate, mod, def, info.name, modules) < 0)
3995-
{
3996-
Py_CLEAR(mod);
3997-
goto finally;
3998-
}
39993976
}
40003977

4001-
// XXX Shouldn't this happen in the error cases too.
3978+
// XXX Shouldn't this happen in the error cases too (i.e. in "finally")?
40023979
if (fp) {
40033980
fclose(fp);
40043981
}

0 commit comments

Comments
 (0)