lv_micropython/tests/basics/subclass_native_init.py
stijn 338df1ae35 py/objtype: Allow passing keyword arguments to native base __init__.
Allowing passing keyword arguments to a native base's __init__, i.e.
`make_new` in the C code.  Previously only positional arguments were
allowed.

The main trade-off in this commit is that every call to the native base's
`make_new` is now going to be preceded by a call to
`mp_map_init_fixed_table` even though most of what that does is unused and
instead it merely serves as a way to pass the number of keyword arguments.

Fixes issue #15465.

Signed-off-by: stijn <stijn@ignitron.net>
2024-10-07 11:37:52 +11:00

74 lines
1.5 KiB
Python

# test subclassing a native type and overriding __init__
# overriding list.__init__()
class L(list):
def __init__(self, a, b):
super().__init__([a, b])
print(L(2, 3))
# with keyword arguments, with star arguments and without because those use different C calls
class D(dict):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
print(D())
print(D([('a', 1)]))
print(D([('a', 1)], a=2, b=3))
print(D(a=2, b=3))
class D(dict):
def __init__(self):
super().__init__()
print(D())
class D(dict):
def __init__(self):
super().__init__([])
print(D())
class D(dict):
def __init__(self):
super().__init__(a=1)
print(D())
class D(dict):
def __init__(self):
super().__init__([], a=1)
print(D())
# inherits implicitly from object
class A:
def __init__(self):
print("A.__init__")
super().__init__()
A()
# inherits explicitly from object
class B(object):
def __init__(self):
print("B.__init__")
super().__init__()
B()
# multiple inheritance with object explicitly mentioned
class C:
pass
class D(C, object):
def __init__(self):
print('D.__init__')
super().__init__()
def reinit(self):
print('D.foo')
super().__init__()
a = D()
a.__init__()
a.reinit()
# call __init__() after object is already init'd
class L(list):
def reinit(self):
super().__init__(range(2))
a = L(range(5))
print(a)
a.reinit()
print(a)