10000 gh-117953: Small Cleanup of Extensions-Related Machinery Code by ericsnowcurrently · Pull Request #118167 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-117953: Small Cleanup of Extensions-Related Machinery Code #118167

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
Prev Previous commit
Next Next commit
Clean up create_builtin().
  • Loading branch information
ericsnowcurrently committed Apr 22, 2024
commit d841ae8f618570e7b4dc79497c720e382500e61b
60 changes: 33 additions & 27 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1392,39 +1392,45 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
}

PyObject *modules = MODULES(tstate->interp);
struct _inittab *found = NULL;
for (struct _inittab *p = INITTAB; p->name != NULL; p++) {
if (_PyUnicode_EqualToASCIIString(name, p->name)) {
if (p->initfunc == NULL) {
/* Cannot re-init internal module ("sys" or "builtins") */
return import_add_module(tstate, name);
}
mod = (*p->initfunc)();
if (mod == NULL) {
return NULL;
}
found = p;
}
}
if (found == NULL) {
// not found
Py_RETURN_NONE;
}

if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) {
return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec);
}
else {
/* Remember pointer to module init function. */
PyModuleDef *def = PyModule_GetDef(mod);
if (def == NULL) {
return NULL;
}
PyModInitFunction p0 = (PyModInitFunction)found->initfunc;
if (p0 == NULL) {
/* Cannot re-init internal module ("sys" or "builtins") */
assert(is_core_module(tstate->interp, name, name));
return import_add_module(tstate, name);
}

def->m_base.m_init = p->initfunc;
if (_PyImport_FixupExtensionObject(mod, name, name,
modules) < 0) {
return NULL;
}
return mod;
}
}
mod = p0();
if (mod == NULL) {
return NULL;
}

// not found
Py_RETURN_NONE;
if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) {
return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec);
}
else {
/* Remember pointer to module init function. */
PyModuleDef *def = PyModule_GetDef(mod);
if (def == NULL) {
return NULL;
}

def->m_base.m_init = p0;
if (_PyImport_FixupExtensionObject(mod, name, name, modules) < 0) {
return NULL;
}
return mod;
}
}


Expand Down
0