From 6cad72453b8001a5aaac86a566308c95fe1db02a Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 9 Dec 2025 14:45:08 +0800 Subject: [PATCH] fix error of "A() takes no arguments" --- src/interpreter/vm.c | 8 ++++++++ tests/400_class.py | 22 +++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index ed662c0f..e650a9a4 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -580,6 +580,7 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall // [cls, NULL, args..., kwargs...] py_Ref new_f = py_tpfindmagic(py_totype(p0), __new__); assert(new_f && py_isnil(p0 + 1)); + bool is_default_new = new_f->type == tp_nativefunc && new_f->_cfunc == pk__object_new; // prepare a copy of args and kwargs int span = self->stack.sp - argv; @@ -603,6 +604,13 @@ FrameResult VM__vectorcall(VM* self, uint16_t argc, uint16_t kwargc, bool opcall // [__init__, self, args..., kwargs...] if(VM__vectorcall(self, argc, kwargc, false) == RES_ERROR) return RES_ERROR; *py_retval() = p0[1]; // restore the new instance + } else { + if(is_default_new) { + if(argc != 0 || kwargc != 0) { + TypeError("%t() takes no arguments", p0->type); + return RES_ERROR; + } + } } // reset the stack self->stack.sp = p0; diff --git a/tests/400_class.py b/tests/400_class.py index f18a4261..82186cef 100644 --- a/tests/400_class.py +++ b/tests/400_class.py @@ -135,4 +135,24 @@ assert MyClass.b == 1 assert MyClass.c == 2 assert MyClass.d == 3 -assert MyClass(1, 2).m == 1 \ No newline at end of file +assert MyClass(1, 2).m == 1 + +class E: pass +try: + E(1,2,3) + exit(1) +except TypeError: + pass + +class E1: + def __new__(cls, a, b): + o = object.__new__(cls) + o.a = a + o.b = b + return o + + def sum(self): + return self.a + self.b + +e1 = E1(3,4) +assert e1.sum() == 7