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