Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 32a319a

Browse filesBrowse files
Factor out import_run_extension().
1 parent f651b52 commit 32a319a
Copy full SHA for 32a319a

File tree

Expand file treeCollapse file tree

1 file changed

+85
-102
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+85
-102
lines changed

‎Python/import.c

Copy file name to clipboardExpand all lines: Python/import.c
+85-102Lines changed: 85 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,84 @@ import_find_extension(PyThreadState *tstate,
13861386
return mod;
13871387
}
13881388

1389+
static PyObject *
1390+
import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
1391+
struct _Py_ext_module_loader_info *info,
1392+
PyObject *spec, PyObject *modules)
1393+
{
1394+
PyObject *mod = NULL;
1395+
PyModuleDef *def = NULL;
1396+
1397+
struct _Py_ext_module_loader_result res;
1398+
if (_PyImport_RunModInitFunc(p0, info, &res) < 0) {
1399+
/* We discard res.def. */
1400+
assert(res.module == NULL);
1401+
assert(PyErr_Occurred());
1402+
goto finally;
1403+
}
1404+
assert(!PyErr_Occurred());
1405+
1406+
mod = res.module;
1407+
res.module = NULL;
1408+
def = res.def;
1409+
assert(def != NULL);
1410+
1411+
if (mod == NULL) {
1412+
//assert(!is_singlephase(def));
1413+
assert(mod == NULL);
1414+
mod = PyModule_FromDefAndSpec(def, spec);
1415+
if (mod == NULL) {
1416+
goto finally;
1417+
}
1418+
}
1419+
else {
1420+
assert(is_singlephase(def));
1421+
assert(PyModule_Check(mod));
1422+
mod = Py_NewRef(mod);
1423+
1424+
const char *name_buf = PyBytes_AS_STRING(info->name_encoded);
1425+
if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
1426+
Py_CLEAR(mod);
1427+
goto finally;
1428+
}
1429+
1430+
if (info->filename != NULL) {
1431+
/* Remember the filename as the __file__ attribute */
1432+
if (PyModule_AddObjectRef(mod, "__file__", info->filename) < 0) {
1433+
PyErr_Clear(); /* Not important enough to report */
1434+
}
1435+
}
1436+
1437+
struct singlephase_global_update singlephase = {0};
1438+
// gh-88216: Extensions and def->m_base.m_copy can be updated
1439+
// when the extension module doesn't support sub-interpreters.
1440+
if (def->m_size == -1
1441+
&& !is_core_module(tstate->interp, info->name, info->path))
1442+
{
1443+
singlephase.m_dict = PyModule_GetDict(mod);
1444+
assert(singlephase.m_dict != NULL);
1445+
}
1446+
if (update_global_state_for_extension(
1447+
tstate, info->path, info->name, def, &singlephase) < 0)
1448+
{
1449+
Py_CLEAR(mod);
1450+
goto finally;
1451+
}
1452+
1453+
PyObject *modules = get_modules_dict(tstate, true);
1454+
if (finish_singlephase_extension(
1455+
tstate, mod, def, info->name, modules) < 0)
1456+
{
1457+
Py_CLEAR(mod);
1458+
goto finally;
1459+
}
1460+
}
1461+
1462+
finally:
1463+
return mod;
1464+
}
1465+
1466+
13891467
static int
13901468
clear_singlephase_extension(PyInterpreterState *interp,
13911469
PyObject *name, PyObject *path)
@@ -1491,8 +1569,6 @@ is_builtin(PyObject *name)
14911569
static PyObject*
14921570
create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
14931571
{
1494-
PyModuleDef *def = NULL;
1495-
14961572
struct _Py_ext_module_loader_info info;
14971573
if (_Py_ext_module_loader_info_init_for_builtin(&info, name) < 0) {
14981574
return NULL;
@@ -1528,49 +1604,9 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
15281604
goto finally;
15291605
}
15301606

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

15751611
finally:
15761612
_Py_ext_module_loader_info_clear(&info);
@@ -3885,7 +3921,6 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
38853921
/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
38863922
{
38873923
PyObject *mod = NULL;
3888-
PyModuleDef *def = NULL;
38893924
PyThreadState *tstate = _PyThreadState_GET();
38903925

38913926
struct _Py_ext_module_loader_info info;
@@ -3929,65 +3964,13 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
39293964
goto finally;
39303965
}
39313966

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

3990-
// XXX Shouldn't this happen in the error cases too.
3973+
// XXX Shouldn't this happen in the error cases too (i.e. in "finally")?
39913974
if (fp) {
39923975
fclose(fp);
39933976
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.