summaryrefslogtreecommitdiffstats
path: root/Python/import.c
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-04-15 01:50:00 (GMT)
committerBrett Cannon <brett@python.org>2012-04-15 01:50:00 (GMT)
commit49f8d8b016b030b7501ce8dc2babe5dfc14985e9 (patch)
tree8ae9126a13aa37da0d7d53e914e5e20864cb5c47 /Python/import.c
parent59f9c3affc68611ef871212ab17d086c33107d82 (diff)
downloadcpython-49f8d8b016b030b7501ce8dc2babe5dfc14985e9.zip
cpython-49f8d8b016b030b7501ce8dc2babe5dfc14985e9.tar.gz
cpython-49f8d8b016b030b7501ce8dc2babe5dfc14985e9.tar.bz2
Handle importing pkg.mod by executing
__import__('mod', {'__packaging__': 'pkg', level=1) w/o properly (and thus not segfaulting).
Diffstat (limited to 'Python/import.c')
-rw-r--r--Python/import.c21
1 files changed, 17 insertions, 4 deletions
diff --git a/Python/import.c b/Python/import.c
index be83ab4..5136d98 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -3016,20 +3016,33 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
Py_DECREF(partition);
if (level == 0) {
- final_mod = PyDict_GetItemWithError(interp->modules, front);
+ final_mod = PyDict_GetItem(interp->modules, front);
Py_DECREF(front);
- Py_XINCREF(final_mod);
+ if (final_mod == NULL) {
+ PyErr_Format(PyExc_KeyError,
+ "%R not in sys.modules as expected", front);
+ }
+ else {
+ Py_INCREF(final_mod);
+ }
}
else {
Py_ssize_t cut_off = PyUnicode_GetLength(name) -
PyUnicode_GetLength(front);
Py_ssize_t abs_name_len = PyUnicode_GetLength(abs_name);
- PyObject *to_return = PyUnicode_Substring(name, 0,
+ PyObject *to_return = PyUnicode_Substring(abs_name, 0,
abs_name_len - cut_off);
final_mod = PyDict_GetItem(interp->modules, to_return);
- Py_INCREF(final_mod);
Py_DECREF(to_return);
+ if (final_mod == NULL) {
+ PyErr_Format(PyExc_KeyError,
+ "%R not in sys.modules as expected",
+ to_return);
+ }
+ else {
+ Py_INCREF(final_mod);
+ }
}
}
else {