Commit 5aabdb34 authored by Marius Wachtler's avatar Marius Wachtler

imp.load_module has to always set __file__ even if the module already exists

parent eb3b2738
......@@ -4171,21 +4171,24 @@ BoxedModule* createModule(BoxedString* name, const char* fn, const char* doc) {
BoxedDict* d = getSysModulesDict();
BoxedModule* module = NULL;
// Surprisingly, there are times that we need to return the existing module if
// one exists:
Box* existing = d->getOrNull(name);
if (existing && PyModule_Check(existing)) {
return static_cast<BoxedModule*>(existing);
module = static_cast<BoxedModule*>(existing);
} else {
module = new BoxedModule();
moduleInit(module, name, boxString(doc ? doc : ""));
d->d[name] = module;
}
BoxedModule* module = new BoxedModule();
moduleInit(module, name, boxString(doc ? doc : ""));
if (fn)
module->giveAttr("__file__", boxString(fn));
module->setattr(internStringMortal("__file__"), boxString(fn), NULL);
d->d[name] = module;
if (name->s() == "__main__")
module->giveAttr("__builtins__", builtins_module);
module->setattr(internStringMortal("__builtins__"), builtins_module, NULL);
return module;
}
......
......@@ -35,3 +35,11 @@ def n(s):
return str(s).replace(".pyc", ".py")
print n(m), n(m.__name__), n(m.__file__), hasattr(m, "__path__")
import sys, types
name = "json"
m = sys.modules[name] = types.ModuleType(name)
print sorted(dir(m))
s = imp.find_module(name)
m = imp.load_module(name, *s)
print name in m.__file__, sorted(dir(m))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment