Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b65e85c

Browse files
committed
Fix problem discovered by Greg McFarlane: when an imported module
replaces its own entry in sys.module, reference count errors ensue; even if there is no reference count problem, it would be preferable for the import to yield the new thing in sys.modules anyway (if only because that's what later imports will yield). This opens the road to an official hack to implement a __getattr__ like feature for modules: stick an instance in sys.modules[__name__].
1 parent e664896 commit b65e85c

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

Python/import.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ PyImport_ExecCodeModule(name, co)
182182
if (v == NULL)
183183
return NULL;
184184
Py_DECREF(v);
185+
186+
if ((m = PyDict_GetItemString(_PyImport_Modules, name)) == NULL) {
187+
PyErr_SetString(PyExc_SystemError,
188+
"loaded module not found in sys.modules");
189+
return NULL;
190+
}
191+
185192
Py_INCREF(m);
186193

187194
return m;

0 commit comments

Comments
 (0)