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; ...@@ -612,7 +612,7 @@ class BoxedClass;
// TODO these shouldn't be here // TODO these shouldn't be here
void setupRuntime(); void setupRuntime();
void teardownRuntime(); 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); BoxedModule* createModule(const std::string& name, const std::string& fn, const char* doc = NULL);
// TODO where to put this // TODO where to put this
......
...@@ -39,7 +39,7 @@ static void removeModule(const std::string& name) { ...@@ -39,7 +39,7 @@ static void removeModule(const std::string& name) {
d->d.erase(b_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); BoxedModule* module = createModule(name, fn);
AST_Module* ast = caching_parse_file(fn.c_str()); AST_Module* ast = caching_parse_file(fn.c_str());
...@@ -49,10 +49,14 @@ BoxedModule* createAndRunModule(const std::string& name, const std::string& fn) ...@@ -49,10 +49,14 @@ BoxedModule* createAndRunModule(const std::string& name, const std::string& fn)
removeModule(name); removeModule(name);
raiseRaw(e); 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); BoxedModule* module = createModule(name, fn);
Box* b_path = boxStringPtr(&module_path); Box* b_path = boxStringPtr(&module_path);
...@@ -69,7 +73,11 @@ static BoxedModule* createAndRunModule(const std::string& name, const std::strin ...@@ -69,7 +73,11 @@ static BoxedModule* createAndRunModule(const std::string& name, const std::strin
removeModule(name); removeModule(name);
raiseRaw(e); 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 #if LLVMREV < 210072
......
...@@ -2589,18 +2589,16 @@ BoxedModule* createModule(const std::string& name, const std::string& fn, const ...@@ -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 // Surprisingly, there are times that we need to return the existing module if
// one exists: // one exists:
Box*& ptr = d->d[b_name]; Box* existing = d->getOrNull(b_name);
if (ptr && isSubclass(ptr->cls, module_cls)) { if (existing && isSubclass(existing->cls, module_cls)) {
return static_cast<BoxedModule*>(ptr); return static_cast<BoxedModule*>(existing);
} else {
ptr = NULL;
} }
BoxedModule* module = new BoxedModule(); BoxedModule* module = new BoxedModule();
moduleInit(module, boxString(name), boxString(doc ? doc : "")); moduleInit(module, boxString(name), boxString(doc ? doc : ""));
module->giveAttr("__file__", boxString(fn)); module->giveAttr("__file__", boxString(fn));
ptr = module; d->d[b_name] = module;
return 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