Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
Add _PyRuntimeState.imports.inittab.
  • Loading branch information
ericsnowcurrently committed Nov 11, 2022
commit 3fdf7eca1234035062e3e3594bede533d8a391a8
2 changes: 2 additions & 0 deletions Include/internal/pycore_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ extern "C" {


struct _import_runtime_state {
/* The builtin modules (defined in config.c). */
struct _inittab *inittab;
/* The most recent value assigned to a PyModuleDef.m_base.m_index.
This is incremented each time PyModuleDef_Init() is called,
which is just about every time an extension module is imported.
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ PyAPI_FUNC(int) _Py_IsLocaleCoercionTarget(const char *ctype_loc);

/* Various one-time initializers */

extern PyStatus _PyImport_Init(void);
extern PyStatus _PyFaulthandler_Init(int enable);
extern int _PyTraceMalloc_Init(int enable);
extern PyObject * _PyBuiltin_Init(PyInterpreterState *interp);
Expand Down
20 changes: 16 additions & 4 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ _imp_release_lock_impl(PyObject *module)
Py_RETURN_NONE;
}

PyStatus
_PyImport_Init(void)
{
if (_PyRuntime.imports.inittab != NULL) {
return _PyStatus_ERR("global import state already initialized");
}
_PyRuntime.imports.inittab = PyImport_Inittab;
return _PyStatus_OK();
}

static inline void _extensions_cache_clear(void);

void
Expand All @@ -228,6 +238,7 @@ _PyImport_Fini(void)
PyThread_free_lock(import_lock);
import_lock = NULL;
}
_PyRuntime.imports.inittab = NULL;
}

void
Expand Down Expand Up @@ -889,9 +900,10 @@ static int
is_builtin(PyObject *name)
{
int i;
for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
if (_PyUnicode_EqualToASCIIString(name, PyImport_Inittab[i].name)) {
if (PyImport_Inittab[i].initfunc == NULL)
struct _inittab *inittab = _PyRuntime.imports.inittab;
for (i = 0; inittab[i].name != NULL; i++) {
if (_PyUnicode_EqualToASCIIString(name, inittab[i].name)) {
if (inittab[i].initfunc == NULL)
return -1;
else
return 1;
Expand Down Expand Up @@ -984,7 +996,7 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
}

PyObject *modules = tstate->interp->modules;
for (struct _inittab *p = PyImport_Inittab; p->name != NULL; p++) {
for (struct _inittab *p = _PyRuntime.imports.inittab; p->name != NULL; p++) {
if (_PyUnicode_EqualToASCIIString(name, p->name)) {
if (p->initfunc == NULL) {
/* Cannot re-init internal module ("sys" or "builtins") */
Expand Down
5 changes: 5 additions & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,11 @@ pycore_init_runtime(_PyRuntimeState *runtime,
return status;
}

status = _PyImport_Init();
if (_PyStatus_EXCEPTION(status)) {
return status;
}

status = _PyInterpreterState_Enable(runtime);
if (_PyStatus_EXCEPTION(status)) {
return status;
Expand Down
5 changes: 3 additions & 2 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2252,8 +2252,9 @@ list_builtin_module_names(void)
if (list == NULL) {
return NULL;
}
for (Py_ssize_t i = 0; PyImport_Inittab[i].name != NULL; i++) {
PyObject *name = PyUnicode_FromString(PyImport_Inittab[i].name);
struct _inittab *inittab = _PyRuntime.imports.inittab;
for (Py_ssize_t i = 0; inittab[i].name != NULL; i++) {
PyObject *name = PyUnicode_FromString(inittab[i].name);
if (name == NULL) {
goto error;
}
Expand Down