argument count error is wrong when subclass calls super().__init__()
A CircuitPython user noticed this error (https://github.com/adafruit/circuitpython/issues/3112). It is also present in the latest build of MicroPython. An incorrect number of arguments is reported in the exception when a subclass constructor is given the wrong number of arguments, and that constructor calls the superclass constructor.
class A:
def __init__(self):
pass
class B(A):
def __init__(self):
super().__init__() # if this is replaced with pass, the problem goes away
try:
print("A(1)")
A(1)
except Exception as e:
print(e)
try:
print("B(1)")
B(1)
except Exception as e:
print(e)
MicroPython v1.12-614-gc2317a3a8 on 2020-07-03; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> import args
A(1)
function takes 1 positional arguments but 2 were given
B(1)
function takes 2 positional arguments but 3 were given # should be 1 and 2
TypeError when calling super().init()
Using the code from the class_super_object test:
class Test(object):
def __init__(self):
super().__init__()
I get a TypeError: function takes 1 positional arguments but 0 were given but only on the INtime patform; this might have to do with uninitialized stack memory: mp_call_method_n_kw() in runtime.c gets called with n_args = 0 and the line int adjust = (args[1] == NULL) ? 0 : 1; is used to make n_args 1 when called on an object. When testing with msvc, args[1] is 0xFDFDFDFD which are the guard bytes before and after heap alloc'ed memory (so I guess it's actually undefined behaviour to access it since it's not allocated), hence the test happens to pass and adjust becomes 1. On INtime however args[1] happens to be 0, hence the error raised.