Commit 7062e5b2 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Fix execfile() behavior

It doesn't create a new module at all -- it directly uses the existing one.

Fixing that let us clean up some of the module handling code.
parent 97d6ebf9
......@@ -459,8 +459,8 @@ static_assert(sizeof(pyston::BoxedClass) == sizeof(struct _typeobject), "");
// TODO these shouldn't be here
void setupRuntime();
void teardownRuntime();
BoxedModule* compileAndRunModule(const std::string& name, const std::string& fn, bool add_to_sys_modules);
BoxedModule* createModule(const std::string& name, const std::string& fn, bool add_to_sys_modules = true);
BoxedModule* createAndRunModule(const std::string& name, const std::string& fn);
BoxedModule* createModule(const std::string& name, const std::string& fn);
std::string getPythonFuncAt(void* ip, void* sp);
......
......@@ -146,7 +146,7 @@ int main(int argc, char** argv) {
for (int i = 0; i < num_iterations; i++) {
try {
main_module = compileAndRunModule("__main__", fn, true);
main_module = createAndRunModule("__main__", fn);
} catch (Box* b) {
std::string msg = formatException(b);
printLastTraceback();
......@@ -159,7 +159,7 @@ int main(int argc, char** argv) {
if (repl && BENCH) {
if (!main_module) {
main_module = createModule("__main__", "<bench>", true);
main_module = createModule("__main__", "<bench>");
} else {
main_module->fn = "<bench>";
}
......@@ -197,7 +197,7 @@ int main(int argc, char** argv) {
printf(", targeting Python %d.%d.%d\n", PYTHON_VERSION_MAJOR, PYTHON_VERSION_MINOR, PYTHON_VERSION_MICRO);
if (!main_module) {
main_module = createModule("__main__", "<stdin>", true);
main_module = createModule("__main__", "<stdin>");
} else {
main_module->fn = "<stdin>";
}
......
......@@ -19,6 +19,8 @@
#include "llvm/Support/FileSystem.h"
#include "codegen/compvars.h"
#include "codegen/irgen/hooks.h"
#include "codegen/parser.h"
#include "core/ast.h"
#include "core/types.h"
#include "gc/collector.h"
......@@ -551,7 +553,9 @@ Box* execfile(Box* _fn) {
if (!exists)
raiseExcHelper(IOError, "No such file or directory: '%s'", fn->s.c_str());
compileAndRunModule("aoeu", fn->s, false);
// Run directly inside the current module:
AST_Module* ast = caching_parse(fn->s.c_str());
compileAndRunModule(ast, getCurrentModule());
return None;
}
......
......@@ -3633,8 +3633,8 @@ extern "C" Box* getGlobal(BoxedModule* m, std::string* name) {
raiseExcHelper(NameError, "global name '%s' is not defined", name->c_str());
}
BoxedModule* compileAndRunModule(const std::string& name, const std::string& fn, bool add_to_sys_modules) {
BoxedModule* module = createModule(name, fn, add_to_sys_modules);
BoxedModule* createAndRunModule(const std::string& name, const std::string& fn) {
BoxedModule* module = createModule(name, fn);
AST_Module* ast = caching_parse(fn.c_str());
compileAndRunModule(ast, module);
......@@ -3686,8 +3686,7 @@ extern "C" Box* import(const std::string* name) {
if (VERBOSITY() >= 1)
printf("Importing %s from %s\n", name->c_str(), fn.c_str());
// TODO duplication with jit.cpp:
BoxedModule* module = compileAndRunModule(*name, fn, true);
BoxedModule* module = createAndRunModule(*name, fn);
return module;
}
......
......@@ -829,16 +829,14 @@ void setupRuntime() {
TRACK_ALLOCATIONS = true;
}
BoxedModule* createModule(const std::string& name, const std::string& fn, bool add_to_sys_modules) {
BoxedModule* createModule(const std::string& name, const std::string& fn) {
assert(fn.size() && "probably wanted to set the fn to <stdin>?");
BoxedModule* module = new BoxedModule(name, fn);
if (add_to_sys_modules) {
BoxedDict* d = getSysModulesDict();
Box* b_name = boxStringPtr(&name);
assert(d->d.count(b_name) == 0);
d->d[b_name] = module;
}
BoxedDict* d = getSysModulesDict();
Box* b_name = boxStringPtr(&name);
assert(d->d.count(b_name) == 0);
d->d[b_name] = module;
module->giveAttr("__doc__", None);
return module;
......
print "execfile_target"
test_name = 1
import execfile_target
......@@ -11,3 +11,6 @@ fn = os.path.join(os.path.dirname(__file__), 'execfile_target.py')
execfile(fn)
print "done with first execfile"
execfile(fn)
print test_name
print type(execfile_target)
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