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

Skip to content

Commit 8a1d04c

Browse files
committed
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.
1 parent 7ceedb8 commit 8a1d04c

2 files changed

Lines changed: 13 additions & 34 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Core and Builtins
3232
Library
3333
-------
3434

35+
- Issue #13959: Make imp.reload() always use a module's __loader__ to perform
36+
the reload.
37+
3538
- Issue #13959: Add imp.py and rename the built-in module to _imp, allowing for
3639
re-implementing parts of the module in pure Python.
3740

Python/import.c

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
153153
};
154154

155155
static PyObject *initstr = NULL;
156-
_Py_IDENTIFIER(__path__);
157156

158157
/* Initialize things */
159158

@@ -3106,12 +3105,12 @@ PyImport_ReloadModule(PyObject *m)
31063105
PyInterpreterState *interp = PyThreadState_Get()->interp;
31073106
PyObject *modules_reloading = interp->modules_reloading;
31083107
PyObject *modules = PyImport_GetModuleDict();
3109-
PyObject *path_list = NULL, *loader = NULL, *existing_m = NULL;
3110-
PyObject *name, *bufobj, *subname;
3108+
PyObject *loader = NULL, *existing_m = NULL;
3109+
PyObject *name;
31113110
Py_ssize_t subname_start;
3112-
struct filedescr *fdp;
3113-
FILE *fp = NULL;
31143111
PyObject *newm = NULL;
3112+
_Py_IDENTIFIER(__loader__);
3113+
_Py_IDENTIFIER(load_module);
31153114

31163115
if (modules_reloading == NULL) {
31173116
Py_FatalError("PyImport_ReloadModule: "
@@ -3149,51 +3148,28 @@ PyImport_ReloadModule(PyObject *m)
31493148

31503149
subname_start = PyUnicode_FindChar(name, '.', 0,
31513150
PyUnicode_GET_LENGTH(name), -1);
3152-
if (subname_start == -1) {
3153-
Py_INCREF(name);
3154-
subname = name;
3155-
}
3156-
else {
3151+
if (subname_start != -1) {
31573152
PyObject *parentname, *parent;
3158-
Py_ssize_t len;
31593153
parentname = PyUnicode_Substring(name, 0, subname_start);
31603154
if (parentname == NULL) {
31613155
goto error;
31623156
}
31633157
parent = PyDict_GetItem(modules, parentname);
3158+
Py_XDECREF(parent);
31643159
if (parent == NULL) {
31653160
PyErr_Format(PyExc_ImportError,
31663161
"reload(): parent %R not in sys.modules",
31673162
parentname);
3168-
Py_DECREF(parentname);
31693163
goto error;
31703164
}
3171-
Py_DECREF(parentname);
3172-
path_list = _PyObject_GetAttrId(parent, &PyId___path__);
3173-
if (path_list == NULL)
3174-
PyErr_Clear();
3175-
subname_start++;
3176-
len = PyUnicode_GET_LENGTH(name) - (subname_start + 1);
3177-
subname = PyUnicode_Substring(name, subname_start, len);
31783165
}
3179-
if (subname == NULL)
3180-
goto error;
3181-
fdp = find_module(name, subname, path_list,
3182-
&bufobj, &fp, &loader);
3183-
Py_DECREF(subname);
3184-
Py_XDECREF(path_list);
31853166

3186-
if (fdp == NULL) {
3187-
Py_XDECREF(loader);
3167+
loader = _PyObject_GetAttrId(m, &PyId___loader__);
3168+
if (loader == NULL) {
31883169
goto error;
31893170
}
3190-
3191-
newm = load_module(name, fp, bufobj, fdp->type, loader);
3192-
Py_XDECREF(bufobj);
3193-
Py_XDECREF(loader);
3194-
3195-
if (fp)
3196-
fclose(fp);
3171+
newm = _PyObject_CallMethodId(loader, &PyId_load_module, "O", name);
3172+
Py_DECREF(loader);
31973173
if (newm == NULL) {
31983174
/* load_module probably removed name from modules because of
31993175
* the error. Put back the original module object. We're

0 commit comments

Comments
 (0)