diff Python/import.c @ 76333:4dce3afc392c

Issue #13959: Simplify imp.reload() by relying on a module's __loader__. Since import now sets __loader__ on all modules it creates and imp.reload() already relied on the attribute for modules that import didn't create, the only potential compatibility issue is if people were deleting the attribute on modules and expecting imp.reload() to continue to work.
author Brett Cannon <brett@python.org>
date Sun, 15 Apr 2012 17:56:09 -0400
parents d777f854a66e
children 2df37938b8e1
line wrap: on
line diff
--- a/Python/import.c	Sun Apr 15 17:47:19 2012 -0400
+++ b/Python/import.c	Sun Apr 15 17:56:09 2012 -0400
@@ -153,7 +153,6 @@
 };
 
 static PyObject *initstr = NULL;
-_Py_IDENTIFIER(__path__);
 
 /* Initialize things */
 
@@ -3106,12 +3105,12 @@
     PyInterpreterState *interp = PyThreadState_Get()->interp;
     PyObject *modules_reloading = interp->modules_reloading;
     PyObject *modules = PyImport_GetModuleDict();
-    PyObject *path_list = NULL, *loader = NULL, *existing_m = NULL;
-    PyObject *name, *bufobj, *subname;
+    PyObject *loader = NULL, *existing_m = NULL;
+    PyObject *name;
     Py_ssize_t subname_start;
-    struct filedescr *fdp;
-    FILE *fp = NULL;
     PyObject *newm = NULL;
+    _Py_IDENTIFIER(__loader__);
+    _Py_IDENTIFIER(load_module);
 
     if (modules_reloading == NULL) {
         Py_FatalError("PyImport_ReloadModule: "
@@ -3149,51 +3148,28 @@
 
     subname_start = PyUnicode_FindChar(name, '.', 0,
                                        PyUnicode_GET_LENGTH(name), -1);
-    if (subname_start == -1) {
-        Py_INCREF(name);
-        subname = name;
-    }
-    else {
+    if (subname_start != -1) {
         PyObject *parentname, *parent;
-        Py_ssize_t len;
         parentname = PyUnicode_Substring(name, 0, subname_start);
         if (parentname == NULL) {
             goto error;
         }
         parent = PyDict_GetItem(modules, parentname);
+        Py_XDECREF(parent);
         if (parent == NULL) {
             PyErr_Format(PyExc_ImportError,
                 "reload(): parent %R not in sys.modules",
                  parentname);
-            Py_DECREF(parentname);
             goto error;
         }
-        Py_DECREF(parentname);
-        path_list = _PyObject_GetAttrId(parent, &PyId___path__);
-        if (path_list == NULL)
-            PyErr_Clear();
-        subname_start++;
-        len = PyUnicode_GET_LENGTH(name) - (subname_start + 1);
-        subname = PyUnicode_Substring(name, subname_start, len);
     }
-    if (subname == NULL)
-        goto error;
-    fdp = find_module(name, subname, path_list,
-                      &bufobj, &fp, &loader);
-    Py_DECREF(subname);
-    Py_XDECREF(path_list);
-
-    if (fdp == NULL) {
-        Py_XDECREF(loader);
+
+    loader = _PyObject_GetAttrId(m, &PyId___loader__);
+    if (loader == NULL) {
         goto error;
     }
-
-    newm = load_module(name, fp, bufobj, fdp->type, loader);
-    Py_XDECREF(bufobj);
-    Py_XDECREF(loader);
-
-    if (fp)
-        fclose(fp);
+    newm = _PyObject_CallMethodId(loader, &PyId_load_module, "O", name);
+    Py_DECREF(loader);
     if (newm == NULL) {
         /* load_module probably removed name from modules because of
          * the error.  Put back the original module object.  We're