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

Skip to content

Commit 222ef56

Browse files
committed
Fix reload() for package submodules.
1 parent 17fc85f commit 222ef56

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

Python/import.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,8 @@ PyImport_ReloadModule(m)
13061306
PyObject *m;
13071307
{
13081308
PyObject *modules = PyImport_GetModuleDict();
1309-
char *name;
1309+
PyObject *path = NULL;
1310+
char *name, *subname;
13101311
char buf[MAXPATHLEN+1];
13111312
struct filedescr *fdp;
13121313
FILE *fp = NULL;
@@ -1325,8 +1326,29 @@ PyImport_ReloadModule(m)
13251326
name);
13261327
return NULL;
13271328
}
1329+
subname = strrchr(name, '.');
1330+
if (subname == NULL)
1331+
subname = name;
1332+
else {
1333+
PyObject *parentname, *parent;
1334+
parentname = PyString_FromStringAndSize(name, (subname-name));
1335+
if (parentname == NULL)
1336+
return NULL;
1337+
parent = PyDict_GetItem(modules, parentname);
1338+
if (parent == NULL) {
1339+
PyErr_Format(PyExc_ImportError,
1340+
"reload(): parent %.200s not in sys.modules",
1341+
name);
1342+
return NULL;
1343+
}
1344+
subname++;
1345+
path = PyObject_GetAttrString(parent, "__path__");
1346+
if (path == NULL)
1347+
PyErr_Clear();
1348+
}
13281349
buf[0] = '\0';
1329-
fdp = find_module(name, (PyObject *)NULL, buf, MAXPATHLEN+1, &fp);
1350+
fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp);
1351+
Py_XDECREF(path);
13301352
if (fdp == NULL)
13311353
return NULL;
13321354
m = load_module(name, fp, buf, fdp->type);

0 commit comments

Comments
 (0)