This commit is contained in:
blueloveTH 2024-08-10 16:13:44 +08:00
parent b51bf33572
commit 34d620c82f
3 changed files with 10 additions and 1 deletions

View File

@ -75,6 +75,8 @@ bool py_exec(const char* source,
enum py_CompileMode mode,
py_Ref module) PY_RAISE;
/// Compile a source string into a code object.
/// Use python's `exec()` or `eval()` to execute it.
bool py_compile(const char* source,
const char* filename,
enum py_CompileMode mode,

View File

@ -463,7 +463,11 @@ static bool _builtins_execdyn(const char* title, int argc, py_Ref argv, enum py_
// [globals, locals, code]
CodeObject* co = py_touserdata(code);
if(!co->src->is_dynamic) py_shrink(3);
if(!co->src->is_dynamic) {
if(argc != 1)
return ValueError("code object is not dynamic, so globals and locals must be None");
py_shrink(3);
}
Frame* frame = pk_current_vm->top_frame;
return pk_exec(co, frame ? frame->module : NULL);
}

View File

@ -59,6 +59,9 @@ globals = {'a': 2}
locals = {'b': 3}
assert eval('a*b', globals, locals) == 6
code = compile('a*b', '<string>', 'eval')
assert eval(code, globals, locals) == 6
try:
exec('a*b*c', globals, locals)
exit(1)