summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-02-15 22:32:31 (GMT)
committerGitHub <noreply@github.com>2023-02-15 22:32:31 (GMT)
commitb2fc5492789623d656953d458f3eeaac03c1ef56 (patch)
treeef2614bd53b935d48170ae2d936ca873ffc92cfc /Objects
parentc1ce0d178fe57b50f37578b285a343d77485ac02 (diff)
downloadcpython-b2fc5492789623d656953d458f3eeaac03c1ef56.zip
cpython-b2fc5492789623d656953d458f3eeaac03c1ef56.tar.gz
cpython-b2fc5492789623d656953d458f3eeaac03c1ef56.tar.bz2
gh-101758: Clean Up Uses of Import State (gh-101919)
This change is almost entirely moving code around and hiding import state behind internal API. We introduce no changes to behavior, nor to non-internal API. (Since there was already going to be a lot of churn, I took this as an opportunity to re-organize import.c into topically-grouped sections of code.) The motivation is to simplify a number of upcoming changes. Specific changes: * move existing import-related code to import.c, wherever possible * add internal API for interacting with import state (both global and per-interpreter) * use only API outside of import.c (to limit churn there when changing the location, etc.) * consolidate the import-related state of PyInterpreterState into a single struct field (this changes layout slightly) * add macros for import state in import.c (to simplify changing the location) * group code in import.c into sections *remove _PyState_AddModule() https://github.com/python/cpython/issues/101758
Diffstat (limited to 'Objects')
-rw-r--r--Objects/moduleobject.c25
1 files changed, 3 insertions, 22 deletions
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 24190e3..a0be19a 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -42,10 +42,9 @@ PyModuleDef_Init(PyModuleDef* def)
{
assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY);
if (def->m_base.m_index == 0) {
- _PyRuntime.imports.last_module_index++;
Py_SET_REFCNT(def, 1);
Py_SET_TYPE(def, &PyModuleDef_Type);
- def->m_base.m_index = _PyRuntime.imports.last_module_index;
+ def->m_base.m_index = _PyImport_GetNextModuleIndex();
}
return (PyObject*)def;
}
@@ -209,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;
@@ -710,8 +692,7 @@ static PyObject *
module_repr(PyModuleObject *m)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
-
- return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
+ return _PyImport_ImportlibModuleRepr(interp, (PyObject *)m);
}
/* Check if the "_initializing" attribute of the module spec is set to true.