diff Python/import.c @ 76312:9e8cbf07068a

Handle importing pkg.mod by executing __import__('mod', {'__packaging__': 'pkg', level=1) w/o properly (and thus not segfaulting).
author Brett Cannon <brett@python.org>
date Sat, 14 Apr 2012 21:50:00 -0400
parents c5da5a2a9262
children c9fb9f5e16e7
line wrap: on
line diff
--- a/Python/import.c	Sat Apr 14 21:18:48 2012 -0400
+++ b/Python/import.c	Sat Apr 14 21:50:00 2012 -0400
@@ -3016,20 +3016,33 @@
             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 {