kopia lustrzana https://github.com/micropython/micropython
tests/class_new: Add another testcase for __new__/__init__ interaction.
Similar to the existing testcase, but test that returning both value of native type and instance of another user class from __new__ lead to __init__ not being called, for better coverage.pull/3294/head
rodzic
ca21aed0a1
commit
b349479a49
|
@ -34,6 +34,9 @@ a.meth()
|
||||||
a = a.__new__(A)
|
a = a.__new__(A)
|
||||||
a.meth()
|
a.meth()
|
||||||
|
|
||||||
|
# __new__ returns not an instance of the class (None here), __init__
|
||||||
|
# should not be called
|
||||||
|
|
||||||
class B:
|
class B:
|
||||||
def __new__(self, v1, v2):
|
def __new__(self, v1, v2):
|
||||||
print("B.__new__", v1, v2)
|
print("B.__new__", v1, v2)
|
||||||
|
@ -43,3 +46,21 @@ class B:
|
||||||
print("B.__init__", v1, v2)
|
print("B.__init__", v1, v2)
|
||||||
|
|
||||||
print("B inst:", B(1, 2))
|
print("B inst:", B(1, 2))
|
||||||
|
|
||||||
|
|
||||||
|
# Variation of the above, __new__ returns an instance of another class,
|
||||||
|
# __init__ should not be called
|
||||||
|
|
||||||
|
class Dummy: pass
|
||||||
|
|
||||||
|
class C:
|
||||||
|
def __new__(cls):
|
||||||
|
print("C.__new__")
|
||||||
|
return Dummy()
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
# Should not be called in this test
|
||||||
|
print("C.__init__")
|
||||||
|
|
||||||
|
c = C()
|
||||||
|
print(isinstance(c, Dummy))
|
||||||
|
|
Ładowanie…
Reference in New Issue