Commit a75d888f authored by Kevin Modzelewski's avatar Kevin Modzelewski

3-arg execfile

parent 20ae1f2b
......@@ -601,6 +601,48 @@ Box* eval(Box* boxedCode, Box* globals, Box* locals) {
return evalMain(boxedCode, globals, locals, &pcf);
}
Box* execfile(Box* _fn, Box* globals, Box* locals) {
if (!PyString_Check(_fn)) {
raiseExcHelper(TypeError, "must be string, not %s", getTypeName(_fn));
}
BoxedString* fn = static_cast<BoxedString*>(_fn);
pickGlobalsAndLocals(globals, locals);
#if LLVMREV < 217625
bool exists;
llvm_error_code code = llvm::sys::fs::exists(fn->s, exists);
#if LLVMREV < 210072
ASSERT(code == 0, "%s: %s", code.message().c_str(), fn->s.c_str());
#else
assert(!code);
#endif
#else
bool exists = llvm::sys::fs::exists(std::string(fn->s()));
#endif
if (!exists)
raiseExcHelper(IOError, "No such file or directory: '%s'", fn->s().data());
AST_Module* parsed = caching_parse_file(fn->s().data());
assert(parsed);
CLFunction* caller_cl = getTopPythonFunction();
assert(caller_cl != NULL);
assert(caller_cl->source != NULL);
FutureFlags caller_future_flags = caller_cl->source->future_flags;
PyCompilerFlags pcf;
pcf.cf_flags = caller_future_flags;
CLFunction* cl = compileForEvalOrExec(parsed, parsed->body, fn, &pcf);
assert(cl);
return evalOrExec(cl, globals, locals);
}
Box* execMain(Box* boxedCode, Box* globals, Box* locals, PyCompilerFlags* flags) {
if (PyTuple_Check(boxedCode)) {
RELEASE_ASSERT(!globals, "");
......
......@@ -40,6 +40,7 @@ void compileAndRunModule(AST_Module* m, BoxedModule* bm);
CompiledFunction* cfForMachineFunctionName(const std::string&);
extern "C" Box* exec(Box* boxedCode, Box* globals, Box* locals, FutureFlags caller_future_flags);
extern "C" Box* execfile(Box* fn, Box* globals, Box* locals);
extern "C" Box* eval(Box* boxedCode, Box* globals, Box* locals);
extern "C" Box* compile(Box* source, Box* filename, Box* mode, Box** _args /* flags, dont_inherit */);
}
......
......@@ -1161,40 +1161,6 @@ Box* powFunc(Box* x, Box* y, Box* z) {
return rtn;
}
Box* execfile(Box* _fn) {
// The "globals" and "locals" arguments aren't implemented for now
if (!PyString_Check(_fn)) {
raiseExcHelper(TypeError, "must be string, not %s", getTypeName(_fn));
}
BoxedString* fn = static_cast<BoxedString*>(_fn);
#if LLVMREV < 217625
bool exists;
llvm_error_code code = llvm::sys::fs::exists(fn->s, exists);
#if LLVMREV < 210072
ASSERT(code == 0, "%s: %s", code.message().c_str(), fn->s.c_str());
#else
assert(!code);
#endif
#else
bool exists = llvm::sys::fs::exists(std::string(fn->s()));
#endif
if (!exists)
raiseExcHelper(IOError, "No such file or directory: '%s'", fn->data());
// Run directly inside the current module:
AST_Module* ast = caching_parse_file(fn->data());
ASSERT(getTopPythonFunction()->source->scoping->areGlobalsFromModule(), "need to pass custom globals in");
compileAndRunModule(ast, getCurrentModule());
return None;
}
Box* print(BoxedTuple* args, BoxedDict* kwargs) {
assert(args->cls == tuple_cls);
assert(!kwargs || kwargs->cls == dict_cls);
......@@ -1622,8 +1588,10 @@ void setupBuiltins() {
boxRTFunction((void*)coerceFunc, UNKNOWN, 2, 0, false, false), "coerce"));
builtins_module->giveAttr("divmod",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)divmod, UNKNOWN, 2), "divmod"));
builtins_module->giveAttr("execfile",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)execfile, UNKNOWN, 1), "execfile"));
builtins_module->giveAttr(
"execfile", new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)execfile, UNKNOWN, 3, 2, false, false),
"execfile", { NULL, NULL }));
CLFunction* compile_func = createRTFunction(
5, 2, false, false, ParamNames({ "source", "filename", "mode", "flags", "dont_inherit" }, "", ""));
......
# expected: fail
# - we throw some very weird error here
try:
execfile("doesnt_exist.py")
except IOError, e:
print e
print type(e)
import os
fn = os.path.join(os.path.dirname(__file__), 'execfile_target.py')
......@@ -14,3 +11,9 @@ execfile(fn)
print test_name
print type(execfile_target)
g = {}
l = {}
execfile(fn, g, l)
print sorted(g.keys())
print sorted(l.keys())
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