8000 gh-101758: Clean Up Uses of Import State by ericsnowcurrently · Pull Request #101919 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-101758: Clean Up Uses of Import State #101919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4433124
Group code in import.c.
ericsnowcurrently Feb 14, 2023
7626bab
Move PyState_*() to import.c.
ericsnowcurrently Feb 14, 2023
5836e93
Add _PyImport_GetNextModuleIndex().
ericsnowcurrently Feb 14, 2023
bc031c1
Add _PyImport_SwapPackageContext() and _PyImport_ResolveNameWithPacka…
ericsnowcurrently Feb 14, 2023
f67c299
Add _PyImport_GetBuiltinModuleNames().
ericsnowcurrently Feb 14, 2023
9b312e1
Add an "extension modules" section.
ericsnowcurrently Feb 14, 2023
24ec4de
Move the "extension modules" section down.
ericsnowcurrently Feb 14, 2023
b2043e4
Add _PyImport_GetDLOpenFlags() and _PyImport_SetDLOpenFlags().
ericsnowcurrently Feb 14, 2023
43d8de7
Hide sys.modules.
ericsnowcurrently Feb 14, 2023
b7cabeb
_PyInterpreterState_ClearModules() -> _PyImport_ClearModulesByIndex().
ericsnowcurrently Feb 14, 2023
d4693ea
Hide interp->import_func.
ericsnowcurrently Feb 14, 2023
7120be2
Hide interp->importlib.
ericsnowcurrently Feb 14, 2023
1935ee5
Add _PyImport_InitCore() and _PyImport_InitExternal().
8000 ericsnowcurrently Feb 14, 2023
7e29b7c
Re-order import.c sections.
ericsnowcurrently Feb 14, 2023
66de41d
Add _PyImport_FiniExternal() and _PyImport_FiniCore().
ericsnowcurrently Feb 14, 2023
384bd69
Factor out init_builtin_modules_table() and fini_builtin_builtins_tab…
ericsnowcurrently Feb 14, 2023
e4777b7
Add PyInterpreterState.imports.
ericsnowcurrently Feb 14, 2023
48fb47c
Move the lifecycle sections to the bottom.
ericsnowcurrently Feb 14, 2023
e8930c8
Add macros for runtime-global import state.
ericsnowcurrently Feb 14, 2023
79a72c4
minor fixes
ericsnowcurrently Feb 14, 2023
0527913
Drop _PyState_AddModule().
ericsnowcurrently Feb 15, 2023
a4deb8a
Drop a wrong assert.
ericsnowcurrently Feb 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add _PyImport_SwapPackageContext() and _PyImport_ResolveNameWithPacka…
…geContext().
  • Loading branch information
ericsnowcurrently committed Feb 14, 2023
commit bc031c17f78245a482fc344daf1be79698ea7c5e
2 changes: 2 additions & 0 deletions Include/internal/pycore_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct _import_runtime_state {
};

extern Py_ssize_t _PyImport_GetNextModuleIndex(void);
extern const char * _PyImport_ResolveNameWithPackageContext(const char *name);
extern const char * _PyImport_SwapPackageContext(const char *newcontext);


#ifdef HAVE_FORK
Expand Down
19 changes: 1 addition & 18 deletions Objects/moduleobject.c
8000
Original file line number Diff line number Diff line change
Expand Up @@ -208,24 +208,7 @@ _PyModule_CreateInitialized(PyModuleDef* module, int module_api_version)
"module %s: PyModule_Create is incompatible with m_slots", name);
return NULL;
}
/* Make sure name is fully qualified.

This is a bit of a hack: when the shared library is loaded,
the module name is "package.module", but the module calls
PyModule_Create*() with just "module" for the name. The shared
library loader squirrels away the true name of the module in
_Py_PackageContext, and PyModule_Create*() will substitute this
(if the name actually matches).
*/
#define _Py_PackageContext (_PyRuntime.imports.pkgcontext)
if (_Py_PackageContext != NULL) {
const char *p = strrchr(_Py_PackageContext, '.');
if (p != NULL && strcmp(module->m_name, p+1) == 0) {
name = _Py_PackageContext;
_Py_PackageContext = NULL;
}
}
#undef _Py_PackageContext
name = _PyImport_ResolveNameWithPackageContext(name);
if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
return NULL;

Expand Down
33 changes: 33 additions & 0 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ static struct _inittab *inittab_copy = NULL;
#define import_lock_thread _PyRuntime.imports.lock.thread
#define import_lock_level _PyRuntime.imports.lock.level

#define _Py_PackageContext (_PyRuntime.imports.pkgcontext)


/*********************/
/* runtime lifecycle */
Expand Down Expand Up @@ -719,6 +721,37 @@ Generally, when multiple interpreters are involved, some of the above
gets even messier.
*/

/* Make sure name is fully qualified.

This is a bit of a hack: when the shared library is loaded,
the module name is "package.module", but the module calls
PyModule_Create*() with just "module" for the name. The shared
library loader squirrels away the true name of the module in
_Py_PackageContext, and PyModule_Create*() will substitute this
(if the name actually matches).
*/
const char *
_PyImport_ResolveNameWithPackageContext(const char *name)
{
if (_Py_PackageContext != NULL) {
const char *p = strrchr(_Py_PackageContext, '.');
if (p != NULL && strcmp(name, p+1) == 0) {
name = _Py_PackageContext;
_Py_PackageContext = NULL;
}
}
return name;
}

const char *
_PyImport_SwapPackageContext(const char *newcontext)
{
const char *oldcontext = _Py_PackageContext;
_Py_PackageContext = newcontext;
return oldcontext;
}


/* Magic for extension modules (built-in as well as dynamically
loaded). To prevent initializing an extension module more than
once, we keep a static dictionary 'extensions' keyed by the tuple
Expand Down
17 changes: 7 additions & 10 deletions Python/importdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
#endif
PyObject *name_unicode = NULL, *name = NULL, *path = NULL, *m = NULL;
const char *name_buf, *hook_prefix;
const char *oldcontext;
const char *oldcontext, *newcontext;
dl_funcptr exportfunc;
PyModuleDef *def;
PyModInitFunction p0;
Expand All @@ -113,6 +113,10 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
"spec.name must be a string");
goto error;
}
newcontext = PyUnicode_AsUTF8(name_unicode);
if (newcontext == NULL) {
goto error;
}

name = get_encoded_name(name_unicode, &hook_prefix);
if (name == NULL) {
Expand Down Expand Up @@ -160,16 +164,9 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
p0 = (PyModInitFunction)exportfunc;

/* Package context is needed for single-phase init */
#define _Py_PackageContext (_PyRuntime.imports.pkgcontext)
oldcontext = _Py_PackageContext;
_Py_PackageContext = PyUnicode_AsUTF8(name_unicode);
if (_Py_PackageContext == NULL) {
_Py_PackageContext = oldcontext;
goto error;
}
oldcontext = _PyImport_SwapPackageContext(newcontext);
m = _PyImport_InitFunc_TrampolineCall(p0);
_Py_PackageContext = oldcontext;
#undef _Py_PackageContext
_PyImport_SwapPackageContext(oldcontext);

if (m == NULL) {
if (!PyErr_Occurred()) {
Expand Down
0