summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-09-01 00:39:06 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-09-01 00:43:52 +0300
commitb349479a49aab440b2f38fc6e45b3df4bf7a1aa7 (patch)
treec0576af4cb3e2a9e5969afecf122da0182e14bd9
parentca21aed0a11903db04fc96056f8a08e452aaf504 (diff)
downloadmicropython-b349479a49aab440b2f38fc6e45b3df4bf7a1aa7.tar.gz
micropython-b349479a49aab440b2f38fc6e45b3df4bf7a1aa7.zip
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.
-rw-r--r--tests/basics/class_new.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/tests/basics/class_new.py b/tests/basics/class_new.py
index 4912f71f0b..1f6a86c648 100644
--- a/tests/basics/class_new.py
+++ b/tests/basics/class_new.py
@@ -34,6 +34,9 @@ a.meth()
a = a.__new__(A)
a.meth()
+# __new__ returns not an instance of the class (None here), __init__
+# should not be called
+
class B:
def __new__(self, v1, v2):
print("B.__new__", v1, v2)
@@ -43,3 +46,21 @@ class B:
print("B.__init__", v1, v2)
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))