diff --git a/src/pocketpy.h b/src/pocketpy.h index 6777d844..f9826cfc 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -56,7 +56,8 @@ void __initializeBuiltinFunctions(VM* _vm) { if (!args[0]->isType(vm->_tp_str)) vm->typeError("eval() argument must be a string"); const _Str& expr = vm->PyStr_AS_C(args[0]); _Code code = compile(vm, expr, "", EVAL_MODE); - return vm->exec(code); // not working in function + if(code == nullptr) return vm->None; + return vm->_exec(code); // not working in function }); _vm->bindBuiltinFunc("repr", [](VM* vm, PyVarList args) { @@ -413,7 +414,7 @@ void __initializeBuiltinFunctions(VM* _vm) { void __runCodeBuiltins(VM* vm, const char* src){ _Code code = compile(vm, src, "builtins.py"); - vm->exec(code, {}, vm->builtins); + if(code != nullptr) vm->_exec(code, {}, vm->builtins); } #include "builtins.h" diff --git a/src/vm.h b/src/vm.h index 539a0afb..1eb1a929 100644 --- a/src/vm.h +++ b/src/vm.h @@ -390,7 +390,7 @@ public: } if(i < args.size()) typeError("too many arguments"); - return exec(fn.code, locals); + return _exec(fn.code, locals); } typeError("'" + callable->getTypeName() + "' object is not callable"); return None; @@ -399,18 +399,10 @@ public: inline PyVar call(const PyVar& obj, const _Str& func, PyVarList args){ return call(getAttr(obj, func), args); } - - PyVar exec(const _Code& code, const PyVarDict& locals={}, PyVar _module=nullptr){ - if(code == nullptr) UNREACHABLE(); - if(_module == nullptr) _module = _main; - auto frame = std::make_shared( - code.get(), - locals, - &_module->attribs - ); + PyVar exec(const _Code& code, const PyVarDict& locals={}, PyVar _module=nullptr){ try { - return runFrame(frame); + return _exec(code, locals, _module); } catch (const std::exception& e) { if(const _Error* _ = dynamic_cast(&e)){ _stderr(e.what()); @@ -422,6 +414,17 @@ public: return None; } } + + PyVar _exec(const _Code& code, const PyVarDict& locals={}, PyVar _module=nullptr){ + if(code == nullptr) UNREACHABLE(); + if(_module == nullptr) _module = _main; + auto frame = std::make_shared( + code.get(), + locals, + &_module->attribs + ); + return runFrame(frame); + } PyVar newUserClassType(_Str name, PyVar base){ PyVar obj = newClassType(name, base);