Commit cc3a1cc3 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Apparently you can override what import returns

By replacing the entry in sys.modules.  pytest does this.
parent ae7851d6
......@@ -612,7 +612,7 @@ class BoxedClass;
// TODO these shouldn't be here
void setupRuntime();
void teardownRuntime();
BoxedModule* createAndRunModule(const std::string& name, const std::string& fn);
Box* createAndRunModule(const std::string& name, const std::string& fn);
BoxedModule* createModule(const std::string& name, const std::string& fn, const char* doc = NULL);
// TODO where to put this
......
......@@ -39,7 +39,7 @@ static void removeModule(const std::string& name) {
d->d.erase(b_name);
}
BoxedModule* createAndRunModule(const std::string& name, const std::string& fn) {
Box* createAndRunModule(const std::string& name, const std::string& fn) {
BoxedModule* module = createModule(name, fn);
AST_Module* ast = caching_parse_file(fn.c_str());
......@@ -49,10 +49,14 @@ BoxedModule* createAndRunModule(const std::string& name, const std::string& fn)
removeModule(name);
raiseRaw(e);
}
return module;
Box* r = getSysModulesDict()->getOrNull(boxString(name));
if (!r)
raiseExcHelper(ImportError, "Loaded module %.200s not found in sys.modules", name.c_str());
return r;
}
static BoxedModule* createAndRunModule(const std::string& name, const std::string& fn, const std::string& module_path) {
static Box* createAndRunModule(const std::string& name, const std::string& fn, const std::string& module_path) {
BoxedModule* module = createModule(name, fn);
Box* b_path = boxStringPtr(&module_path);
......@@ -69,7 +73,11 @@ static BoxedModule* createAndRunModule(const std::string& name, const std::strin
removeModule(name);
raiseRaw(e);
}
return module;
Box* r = getSysModulesDict()->getOrNull(boxString(name));
if (!r)
raiseExcHelper(ImportError, "Loaded module %.200s not found in sys.modules", name.c_str());
return r;
}
#if LLVMREV < 210072
......
......@@ -2589,18 +2589,16 @@ BoxedModule* createModule(const std::string& name, const std::string& fn, const
// Surprisingly, there are times that we need to return the existing module if
// one exists:
Box*& ptr = d->d[b_name];
if (ptr && isSubclass(ptr->cls, module_cls)) {
return static_cast<BoxedModule*>(ptr);
} else {
ptr = NULL;
Box* existing = d->getOrNull(b_name);
if (existing && isSubclass(existing->cls, module_cls)) {
return static_cast<BoxedModule*>(existing);
}
BoxedModule* module = new BoxedModule();
moduleInit(module, boxString(name), boxString(doc ? doc : ""));
module->giveAttr("__file__", boxString(fn));
ptr = module;
d->d[b_name] = module;
return module;
}
......
# skip-if: True
import os
import sys
sys.modules[__name__] = os
import sys_modules_replacement_target
print hasattr(sys_modules_replacement_target, "path")
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