Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Jun 19, 2023
1 parent 7a56a41 commit 7fdfe5e
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,19 @@ PyImport_AddModuleObject(PyObject *name)
{
PyThreadState *tstate = _PyThreadState_GET();
PyObject *mod = import_add_module(tstate, name);
if (mod) {
PyObject *ref = PyWeakref_NewRef(mod, NULL);
Py_DECREF(mod);
if (ref == NULL) {
return NULL;
}
mod = PyWeakref_GetObject(ref);
Py_DECREF(ref);
if (!mod) {
return NULL;
}

// https://bugs.python.org/issue41994
PyObject *ref = PyWeakref_NewRef(mod, NULL);
Py_DECREF(mod);
if (ref == NULL) {
return NULL;
}

mod = PyWeakref_GetObject(ref);
Py_DECREF(ref);
return mod; /* borrowed reference */
}

Expand Down Expand Up @@ -1364,8 +1368,8 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
if (_PyUnicode_EqualToASCIIString(name, p->name)) {
if (p->initfunc == NULL) {
/* Cannot re-init internal module ("sys" or "builtins") */
mod = PyImport_AddModuleObject(name);
return Py_XNewRef(mod);
PyThreadState *tstate = _PyThreadState_GET();
return import_add_module(tstate, name);
}
mod = _PyImport_InitFunc_TrampolineCall(*p->initfunc);
if (mod == NULL) {
Expand Down Expand Up @@ -2240,11 +2244,17 @@ init_importlib(PyThreadState *tstate, PyObject *sysmod)
if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
return -1;
}
PyObject *importlib = PyImport_AddModule("_frozen_importlib"); // borrowed

PyObject *name = PyUnicode_FromString("_frozen_importlib");
if (name == NULL) {
return -1;
}
PyObject *importlib = import_add_module(tstate, name);
Py_DECREF(name);
if (importlib == NULL) {
return -1;
}
IMPORTLIB(interp) = Py_NewRef(importlib);
IMPORTLIB(interp) = importlib;

// Import the _imp module
if (verbose) {
Expand Down

0 comments on commit 7fdfe5e

Please sign in to comment.