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 23950be

Browse filesBrowse files
gh-117953: Small Cleanup of Extensions-Related Machinery Code (gh-118167)
This is a collection of very basic cleanups I've pulled out of gh-118116. It is mostly renaming variables and moving a couple bits of code in functionally equivalent ways.
1 parent d0b664e commit 23950be
Copy full SHA for 23950be

File tree

Expand file treeCollapse file tree

3 files changed

+105
-75
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+105
-75
lines changed

‎Include/internal/pycore_import.h

Copy file name to clipboardExpand all lines: Include/internal/pycore_import.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extern int _PyImport_SetModuleString(const char *name, PyObject* module);
2222
extern void _PyImport_AcquireLock(PyInterpreterState *interp);
2323
extern int _PyImport_ReleaseLock(PyInterpreterState *interp);
2424

25+
// This is used exclusively for the sys and builtins modules:
2526
extern int _PyImport_FixupBuiltin(
2627
PyObject *mod,
2728
const char *name, /* UTF-8 encoded string */

‎Python/import.c

Copy file name to clipboardExpand all lines: Python/import.c
+85-49Lines changed: 85 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,10 +1125,10 @@ _PyImport_CheckSubinterpIncompatibleExtensionAllowed(const char *name)
11251125

11261126
static PyObject *
11271127
get_core_module_dict(PyInterpreterState *interp,
1128-
PyObject *name, PyObject *filename)
1128+
PyObject *name, PyObject *path)
11291129
{
11301130
/* Only builtin modules are core. */
1131-
if (filename == name) {
1131+
if (path == name) {
11321132
assert(!PyErr_Occurred());
11331133
if (PyUnicode_CompareWithASCIIString(name, "sys") == 0) {
11341134
return interp->sysdict_copy;
@@ -1143,11 +1143,11 @@ get_core_module_dict(PyInterpreterState *interp,
11431143
}
11441144

11451145
static inline int
1146-
is_core_module(PyInterpreterState *interp, PyObject *name, PyObject *filename)
1146+
is_core_module(PyInterpreterState *interp, PyObject *name, PyObject *path)
11471147
{
11481148
/* This might be called before the core dict copies are in place,
11491149
so we can't rely on get_core_module_dict() here. */
1150-
if (filename == name) {
1150+
if (path == name) {
11511151
if (PyUnicode_CompareWithASCIIString(name, "sys") == 0) {
11521152
return 1;
11531153
}
@@ -1159,7 +1159,7 @@ is_core_module(PyInterpreterState *interp, PyObject *name, PyObject *filename)
11591159
}
11601160

11611161
static int
1162-
fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
1162+
fix_up_extension(PyObject *mod, PyObject *name, PyObject *path)
11631163
{
11641164
if (mod == NULL || !PyModule_Check(mod)) {
11651165
PyErr_BadInternalCall();
@@ -1180,7 +1180,7 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
11801180
// bpo-44050: Extensions and def->m_base.m_copy can be updated
11811181
// when the extension module doesn't support sub-interpreters.
11821182
if (def->m_size == -1) {
1183-
if (!is_core_module(tstate->interp, name, filename)) {
1183+
if (!is_core_module(tstate->interp, name, path)) {
11841184
assert(PyUnicode_CompareWithASCIIString(name, "sys") != 0);
11851185
assert(PyUnicode_CompareWithASCIIString(name, "builtins") != 0);
11861186
if (def->m_base.m_copy) {
@@ -1202,7 +1202,7 @@ fix_up_extension(PyObject *mod, PyObject *name, PyObject *filename)
12021202

12031203
// XXX Why special-case the main interpreter?
12041204
if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
1205-
if (_extensions_cache_set(filename, name, def) < 0) {
1205+
if (_extensions_cache_set(path, name, def) < 0) {
12061206
return -1;
12071207
}
12081208
}
@@ -1227,10 +1227,10 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
12271227

12281228
static PyObject *
12291229
import_find_extension(PyThreadState *tstate, PyObject *name,
1230-
PyObject *filename)
1230+
PyObject *path)
12311231
{
12321232
/* Only single-phase init modules will be in the cache. */
1233-
PyModuleDef *def = _extensions_cache_get(filename, name);
1233+
PyModuleDef *def = _extensions_cache_get(path, name);
12341234
if (def == NULL) {
12351235
return NULL;
12361236
}
@@ -1253,7 +1253,7 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
12531253
if (m_copy == NULL) {
12541254
/* It might be a core module (e.g. sys & builtins),
12551255
for which we don't set m_copy. */
1256-
m_copy = get_core_module_dict(tstate->interp, name, filename);
1256+
m_copy = get_core_module_dict(tstate->interp, name, path);
12571257
if (m_copy == NULL) {
12581258
return NULL;
12591259
}
@@ -1292,16 +1292,16 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
12921292
int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
12931293
if (verbose) {
12941294
PySys_FormatStderr("import %U # previously loaded (%R)\n",
1295-
name, filename);
1295+
name, path);
12961296
}
12971297
return mod;
12981298
}
12991299

13001300
static int
13011301
clear_singlephase_extension(PyInterpreterState *interp,
1302-
PyObject *name, PyObject *filename)
1302+
PyObject *name, PyObject *path)
13031303
{
1304-
PyModuleDef *def = _extensions_cache_get(filename, name);
1304+
PyModuleDef *def = _extensions_cache_get(path, name);
13051305
if (def == NULL) {
13061306
if (PyErr_Occurred()) {
13071307
return -1;
@@ -1322,7 +1322,7 @@ clear_singlephase_extension(PyInterpreterState *interp,
13221322
}
13231323

13241324
/* Clear the cached module def. */
1325-
_extensions_cache_delete(filename, name);
1325+
_extensions_cache_delete(path, name);
13261326

13271327
return 0;
13281328
}
@@ -1336,18 +1336,28 @@ int
13361336
_PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules)
13371337
{
13381338
int res = -1;
1339+
assert(mod != NULL && PyModule_Check(mod));
1340+
13391341
PyObject *nameobj;
13401342
nameobj = PyUnicode_InternFromString(name);
13411343
if (nameobj == NULL) {
13421344
return -1;
13431345
}
1346+
1347+
PyModuleDef *def = PyModule_GetDef(mod);
1348+
if (def == NULL) {
1349+
PyErr_BadInternalCall();
1350+
goto finally;
1351+
}
1352+
13441353
if (PyObject_SetItem(modules, nameobj, mod) < 0) {
13451354
goto finally;
13461355
}
13471356
if (fix_up_extension(mod, nameobj, nameobj) < 0) {
13481357
PyMapping_DelItem(modules, nameobj);
13491358
goto finally;
13501359
}
1360+
13511361
res = 0;
13521362

13531363
finally:
@@ -1382,39 +1392,45 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
13821392
}
13831393

13841394
PyObject *modules = MODULES(tstate->interp);
1395+
struct _inittab *found = NULL;
13851396
for (struct _inittab *p = INITTAB; p->name != NULL; p++) {
13861397
if (_PyUnicode_EqualToASCIIString(name, p->name)) {
1387-
if (p->initfunc == NULL) {
1388-
/* Cannot re-init internal module ("sys" or "builtins") */
1389-
return import_add_module(tstate, name);
1390-
}
1391-
mod = (*p->initfunc)();
1392-
if (mod == NULL) {
1393-
return NULL;
1394-
}
1398+
found = p;
1399+
}
1400+
}
1401+
if (found == NULL) {
1402+
// not found
1403+
Py_RETURN_NONE;
1404+
}
13951405

1396-
if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) {
1397-
return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec);
1398-
}
1399-
else {
1400-
/* Remember pointer to module init function. */
1401-
PyModuleDef *def = PyModule_GetDef(mod);
1402-
if (def == NULL) {
1403-
return NULL;
1404-
}
1406+
PyModInitFunction p0 = (PyModInitFunction)found->initfunc;
1407+
if (p0 == NULL) {
1408+
/* Cannot re-init internal module ("sys" or "builtins") */
1409+
assert(is_core_module(tstate->interp, name, name));
1410+
return import_add_module(tstate, name);
1411+
}
14051412

1406-
def->m_base.m_init = p->initfunc;
1407-
if (_PyImport_FixupExtensionObject(mod, name, name,
1408-
modules) < 0) {
1409-
return NULL;
1410-
}
1411-
return mod;
1412-
}
1413-
}
1413+
mod = p0();
1414+
if (mod == NULL) {
1415+
return NULL;
14141416
}
14151417

1416-
// not found
1417-
Py_RETURN_NONE;
1418+
if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) {
1419+
return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec);
1420+
}
1421+
else {
1422+
/* Remember pointer to module init function. */
1423+
PyModuleDef *def = PyModule_GetDef(mod);
1424+
if (def == NULL) {
1425+
return NULL;
1426+
}
1427+
1428+
def->m_base.m_init = p0;
1429+
if (_PyImport_FixupExtensionObject(mod, name, name, modules) < 0) {
1430+
return NULL;
1431+
}
1432+
return mod;
1433+
}
14181434
}
14191435

14201436

@@ -3724,44 +3740,64 @@ static PyObject *
37243740
_imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
37253741
/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
37263742
{
3727-
PyObject *mod, *name, *path;
3743+
PyObject *mod, *name, *filename;
37283744
FILE *fp;
37293745

37303746
name = PyObject_GetAttrString(spec, "name");
37313747
if (name == NULL) {
37323748
return NULL;
37333749
}
37343750

3735-
path = PyObject_GetAttrString(spec, "origin");
3736-
if (path == NULL) {
3751+
filename = PyObject_GetAttrString(spec, "origin");
3752+
if (filename == NULL) {
37373753
Py_DECREF(name);
37383754
return NULL;
37393755
}
37403756

37413757
PyThreadState *tstate = _PyThreadState_GET();
3742-
mod = import_find_extension(tstate, name, path);
3758+
mod = import_find_extension(tstate, name, filename);
37433759
if (mod != NULL || _PyErr_Occurred(tstate)) {
37443760
assert(mod == NULL || !_PyErr_Occurred(tstate));
37453761
goto finally;
37463762
}
37473763

3764+
if (PySys_Audit("import", "OOOOO", name, filename,
3765+
Py_None, Py_None, Py_None) < 0)
3766+
{
3767+
goto finally;
3768+
}
3769+
3770+
/* Is multi-phase init or this is the first time being loaded. */
3771+
3772+
/* We would move this (and the fclose() below) into
3773+
* _PyImport_GetModInitFunc(), but it isn't clear if the intervening
3774+
* code relies on fp still being open. */
37483775
if (file != NULL) {
3749-
fp = _Py_fopen_obj(path, "r");
3776+
fp = _Py_fopen_obj(filename, "r");
37503777
if (fp == NULL) {
37513778
goto finally;
37523779
}
37533780
}
3754-
else
3781+
else {
37553782
fp = NULL;
3783+
}
37563784

37573785
mod = _PyImport_LoadDynamicModuleWithSpec(spec, fp);
3786+
if (mod != NULL) {
3787+
/* Remember the filename as the __file__ attribute */
3788+
if (PyModule_AddObjectRef(mod, "__file__", filename) < 0) {
3789+
PyErr_Clear(); /* Not important enough to report */
3790+
}
3791+
}
37583792

3759-
if (fp)
3793+
// XXX Shouldn't this happen in the error cases too.
3794+
if (fp) {
37603795
fclose(fp);
3796+
}
37613797

37623798
finally:
37633799
Py_DECREF(name);
3764-
Py_DECREF(path);
3800+
Py_DECREF(filename);
37653801
return mod;
37663802
}
37673803

‎Python/importdl.c

Copy file name to clipboardExpand all lines: Python/importdl.c
+19-26Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ PyObject *
9797
_PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
9898
{
9999
#ifndef MS_WINDOWS
100-
PyObject *pathbytes = NULL;
100+
PyObject *filename_bytes = NULL;
101+
const char *filename_buf;
101102
#endif
102-
PyObject *name_unicode = NULL, *name = NULL, *path = NULL, *m = NULL;
103+
PyObject *name_unicode = NULL, *name = NULL, *filename = NULL, *m = NULL;
103104
const char *name_buf, *hook_prefix;
104105
const char *oldcontext, *newcontext;
105106
dl_funcptr exportfunc;
@@ -126,26 +127,23 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
126127
}
127128
name_buf = PyBytes_AS_STRING(name);
128129

129-
path = PyObject_GetAttrString(spec, "origin");
130-
if (path == NULL)
131-
goto error;
132-
133-
if (PySys_Audit("import", "OOOOO", name_unicode, path,
134-
Py_None, Py_None, Py_None) < 0) {
130+
filename = PyObject_GetAttrString(spec, "origin");
131+
if (filename == NULL) {
135132
goto error;
136133
}
137134

138135
#ifdef MS_WINDOWS
139-
exportfunc = _PyImport_FindSharedFuncptrWindows(hook_prefix, name_buf,
140-
path, fp);
136+
exportfunc = _PyImport_FindSharedFuncptrWindows(
137+
hook_prefix, name_buf, filename, fp);
141138
#else
142-
pathbytes = PyUnicode_EncodeFSDefault(path);
143-
if (pathbytes == NULL)
139+
filename_bytes = PyUnicode_EncodeFSDefault(filename);
140+
if (filename_bytes == NULL) {
144141
goto error;
145-
exportfunc = _PyImport_FindSharedFuncptr(hook_prefix, name_buf,
146-
PyBytes_AS_STRING(pathbytes),
147-
fp);
148-
Py_DECREF(pathbytes);
142+
}
143+
filename_buf = PyBytes_AS_STRING(filename_bytes);
144+
exportfunc = _PyImport_FindSharedFuncptr(
145+
hook_prefix, name_buf, filename_buf, fp);
146+
Py_DECREF(filename_bytes);
149147
#endif
150148

151149
if (exportfunc == NULL) {
@@ -157,7 +155,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
157155
hook_prefix, name_buf);
158156
if (msg == NULL)
159157
goto error;
160-
PyErr_SetImportError(msg, name_unicode, path);
158+
PyErr_SetImportError(msg, name_unicode, filename);
161159
Py_DECREF(msg);
162160
}
163161
goto error;
@@ -199,7 +197,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
199197
if (PyObject_TypeCheck(m, &PyModuleDef_Type)) {
200198
Py_DECREF(name_unicode);
201199
Py_DECREF(name);
202-
Py_DECREF(path);
200+
Py_DECREF(filename);
203201
return PyModule_FromDefAndSpec((PyModuleDef*)m, spec);
204202
}
205203

@@ -228,25 +226,20 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
228226
}
229227
def->m_base.m_init = p0;
230228

231-
/* Remember the filename as the __file__ attribute */
232-
if (PyModule_AddObjectRef(m, "__file__", path) < 0) {
233-
PyErr_Clear(); /* Not important enough to report */
234-
}
235-
236229
PyObject *modules = PyImport_GetModuleDict();
237-
if (_PyImport_FixupExtensionObject(m, name_unicode, path, modules) < 0)
230+
if (_PyImport_FixupExtensionObject(m, name_unicode, filename, modules) < 0)
238231
goto error;
239232

240233
Py_DECREF(name_unicode);
241234
Py_DECREF(name);
242-
Py_DECREF(path);
235+
Py_DECREF(filename);
243236

244237
return m;
245238

246239
error:
247240
Py_DECREF(name_unicode);
248241
Py_XDECREF(name);
249-
Py_XDECREF(path);
242+
Py_XDECREF(filename);
250243
Py_XDECREF(m);
251244
return NULL;
252245
}

0 commit comments

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