From fa61a28740e6ee3a9acb7af6a774865a129d14fc Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 15 Aug 2024 11:14:38 +0800 Subject: [PATCH] ... --- include/pocketpy/pocketpy.h | 4 ++-- src/interpreter/vm.c | 4 ++++ src/public/py_exception.c | 13 +++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 22c632de..04eebcde 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -468,7 +468,7 @@ PK_EXPORT bool KeyError(py_Ref key) PY_RAISE; /// Python equivalent to `bool(val)`. /// 1: true, 0: false, -1: error -PK_EXPORT int py_bool(py_Ref val) PY_RAISE PY_RETURN; +PK_EXPORT int py_bool(py_Ref val) PY_RAISE; /// Compare two objects. /// 1: lhs == rhs, 0: lhs != rhs, -1: error @@ -598,7 +598,7 @@ enum py_PredefinedTypes { tp_nativefunc, tp_boundmethod, tp_super, // 1 slot + py_Type - tp_BaseException, // 2 slots (arg + inner exc) + tp_BaseException, // 2 slots (arg + inner_exc) tp_Exception, tp_bytes, tp_namedict, diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 626e939a..4eb376d4 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -486,6 +486,10 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall } if(p0->type == tp_nativefunc) { + if(kwargc) { + TypeError("nativefunc does not accept keyword arguments"); + return RES_ERROR; + } bool ok = py_callcfunc(p0->_cfunc, p1 - argv, argv); self->stack.sp = p0; return ok ? RES_RETURN : RES_ERROR; diff --git a/src/public/py_exception.c b/src/public/py_exception.c index 3a3de884..0d04cc13 100644 --- a/src/public/py_exception.c +++ b/src/public/py_exception.c @@ -99,6 +99,18 @@ static bool _py_BaseException__str__(int argc, py_Ref argv) { return true; } +static bool BaseException_args(int argc, py_Ref argv){ + PY_CHECK_ARGC(1); + py_Ref arg = py_getslot(argv, 0); + if(!py_isnil(arg)) { + py_newtuple(py_retval(), 1); + py_setslot(py_retval(), 0, arg); + }else{ + py_newtuple(py_retval(), 0); + } + return true; +} + py_Type pk_BaseException__register() { py_Type type = pk_newtype("BaseException", tp_object, NULL, BaseException__dtor, false, false); @@ -106,6 +118,7 @@ py_Type pk_BaseException__register() { py_bindmagic(type, __init__, _py_BaseException__init__); py_bindmagic(type, __repr__, _py_BaseException__repr__); py_bindmagic(type, __str__, _py_BaseException__str__); + py_bindproperty(type, "args", BaseException_args, NULL); return type; }