mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
Fix for SF bug #1029475 : reload() doesn't work with PEP 302 loaders.
This commit is contained in:
parent
f4aca755bc
commit
7ec642a4d2
2 changed files with 26 additions and 6 deletions
|
@ -12,7 +12,13 @@ def get_file():
|
||||||
return __file__
|
return __file__
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
reload_src = test_src+"""\
|
||||||
|
reloaded = True
|
||||||
|
"""
|
||||||
|
|
||||||
test_co = compile(test_src, "<???>", "exec")
|
test_co = compile(test_src, "<???>", "exec")
|
||||||
|
reload_co = compile(reload_src, "<???>", "exec")
|
||||||
|
|
||||||
test_path = "!!!_test_!!!"
|
test_path = "!!!_test_!!!"
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,6 +38,7 @@ class TestImporter:
|
||||||
"hooktestpackage": (True, test_co),
|
"hooktestpackage": (True, test_co),
|
||||||
"hooktestpackage.sub": (True, test_co),
|
"hooktestpackage.sub": (True, test_co),
|
||||||
"hooktestpackage.sub.subber": (False, test_co),
|
"hooktestpackage.sub.subber": (False, test_co),
|
||||||
|
"reloadmodule": (False, test_co),
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, path=test_path):
|
def __init__(self, path=test_path):
|
||||||
|
@ -52,8 +59,7 @@ class TestImporter:
|
||||||
|
|
||||||
def load_module(self, fullname):
|
def load_module(self, fullname):
|
||||||
ispkg, code = self.modules[fullname]
|
ispkg, code = self.modules[fullname]
|
||||||
mod = imp.new_module(fullname)
|
mod = sys.modules.setdefault(fullname,imp.new_module(fullname))
|
||||||
sys.modules[fullname] = mod
|
|
||||||
mod.__file__ = "<%s>" % self.__class__.__name__
|
mod.__file__ = "<%s>" % self.__class__.__name__
|
||||||
mod.__loader__ = self
|
mod.__loader__ = self
|
||||||
if ispkg:
|
if ispkg:
|
||||||
|
@ -163,6 +169,14 @@ class ImportHooksTestCase(ImportHooksBaseTestCase):
|
||||||
self.assertEqual(hooktestpackage.sub.__loader__, importer)
|
self.assertEqual(hooktestpackage.sub.__loader__, importer)
|
||||||
self.assertEqual(hooktestpackage.sub.subber.__loader__, importer)
|
self.assertEqual(hooktestpackage.sub.subber.__loader__, importer)
|
||||||
|
|
||||||
|
TestImporter.modules['reloadmodule'] = (False, test_co)
|
||||||
|
import reloadmodule
|
||||||
|
self.failIf(hasattr(reloadmodule,'reloaded'))
|
||||||
|
|
||||||
|
TestImporter.modules['reloadmodule'] = (False, reload_co)
|
||||||
|
reload(reloadmodule)
|
||||||
|
self.failUnless(hasattr(reloadmodule,'reloaded'))
|
||||||
|
|
||||||
def testMetaPath(self):
|
def testMetaPath(self):
|
||||||
i = MetaImporter()
|
i = MetaImporter()
|
||||||
sys.meta_path.append(i)
|
sys.meta_path.append(i)
|
||||||
|
|
|
@ -2252,7 +2252,7 @@ PyObject *
|
||||||
PyImport_ReloadModule(PyObject *m)
|
PyImport_ReloadModule(PyObject *m)
|
||||||
{
|
{
|
||||||
PyObject *modules = PyImport_GetModuleDict();
|
PyObject *modules = PyImport_GetModuleDict();
|
||||||
PyObject *path = NULL;
|
PyObject *path = NULL, *loader = NULL;
|
||||||
char *name, *subname;
|
char *name, *subname;
|
||||||
char buf[MAXPATHLEN+1];
|
char buf[MAXPATHLEN+1];
|
||||||
struct filedescr *fdp;
|
struct filedescr *fdp;
|
||||||
|
@ -2295,11 +2295,17 @@ PyImport_ReloadModule(PyObject *m)
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
}
|
}
|
||||||
buf[0] = '\0';
|
buf[0] = '\0';
|
||||||
fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, NULL);
|
fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
|
||||||
Py_XDECREF(path);
|
Py_XDECREF(path);
|
||||||
if (fdp == NULL)
|
|
||||||
|
if (fdp == NULL) {
|
||||||
|
Py_XDECREF(loader);
|
||||||
return NULL;
|
return NULL;
|
||||||
newm = load_module(name, fp, buf, fdp->type, NULL);
|
}
|
||||||
|
|
||||||
|
newm = load_module(name, fp, buf, fdp->type, loader);
|
||||||
|
Py_XDECREF(loader);
|
||||||
|
|
||||||
if (fp)
|
if (fp)
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
if (newm == NULL) {
|
if (newm == NULL) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue