blueloveTH 1 year ago
parent
commit
fa61a28740
3 changed files with 19 additions and 2 deletions
  1. 2 2
      include/pocketpy/pocketpy.h
  2. 4 0
      src/interpreter/vm.c
  3. 13 0
      src/public/py_exception.c

+ 2 - 2
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,

+ 4 - 0
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;

+ 13 - 0
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;
 }