This commit is contained in:
blueloveTH 2023-01-29 06:02:11 +08:00
parent 57c8f9ede4
commit 2008ecbd0f
3 changed files with 10 additions and 9 deletions

View File

@ -62,14 +62,12 @@ void __initializeBuiltinFunctions(VM* _vm) {
}); });
_vm->bindBuiltinFunc<1>("eval", [](VM* vm, const pkpy::ArgList& args) { _vm->bindBuiltinFunc<1>("eval", [](VM* vm, const pkpy::ArgList& args) {
const _Str& expr = vm->PyStr_AS_C(args[0]); _Code code = vm->compile(vm->PyStr_AS_C(args[0]), "<eval>", EVAL_MODE);
_Code code = vm->compile(expr, "<eval>", EVAL_MODE);
return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals); return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals);
}); });
_vm->bindBuiltinFunc<1>("exec", [](VM* vm, const pkpy::ArgList& args) { _vm->bindBuiltinFunc<1>("exec", [](VM* vm, const pkpy::ArgList& args) {
const _Str& expr = vm->PyStr_AS_C(args[0]); _Code code = vm->compile(vm->PyStr_AS_C(args[0]), "<exec>", EXEC_MODE);
_Code code = vm->compile(expr, "<exec>", EXEC_MODE);
vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals); vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals);
return vm->None; return vm->None;
}); });

View File

@ -44,16 +44,13 @@ __NOT_ENOUGH_LINES:
} }
try{ try{
vm->compile(line, "<stdin>", mode); vm->exec(line, "<stdin>", mode);
}catch(NeedMoreLines& ne){ }catch(NeedMoreLines& ne){
buffer += line; buffer += line;
buffer += '\n'; buffer += '\n';
need_more_lines = ne.isClassDef ? 3 : 2; need_more_lines = ne.isClassDef ? 3 : 2;
if (need_more_lines) return NEED_MORE_LINES; if (need_more_lines) return NEED_MORE_LINES;
}catch(...){
// do nothing
} }
vm->exec(line, "<stdin>", mode);
return EXEC_STARTED; return EXEC_STARTED;
} }
}; };

View File

@ -17,4 +17,10 @@ def f(x):
exec('a = x') exec('a = x')
return a return a
assert f(2) == 2 assert f(2) == 2
exec(
"exec('a = eval(\"3 + 5\")')"
)
assert a == 8