2017-02-14 21:57:56 +00:00
|
|
|
try:
|
|
|
|
# If we don't expose object.__new__ (small ports), there's
|
|
|
|
# nothing to test.
|
|
|
|
object.__new__
|
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
2017-06-10 18:03:01 +01:00
|
|
|
raise SystemExit
|
2017-08-30 19:33:42 +01:00
|
|
|
|
2014-05-21 22:32:00 +01:00
|
|
|
class A:
|
|
|
|
def __new__(cls):
|
|
|
|
print("A.__new__")
|
|
|
|
return super(cls, A).__new__(cls)
|
|
|
|
|
|
|
|
def __init__(self):
|
2017-08-30 19:33:42 +01:00
|
|
|
print("A.__init__")
|
2014-05-21 22:32:00 +01:00
|
|
|
|
|
|
|
def meth(self):
|
2014-07-05 05:55:00 +01:00
|
|
|
print('A.meth')
|
2014-05-21 22:32:00 +01:00
|
|
|
|
|
|
|
#print(A.__new__)
|
|
|
|
#print(A.__init__)
|
|
|
|
|
|
|
|
a = A()
|
2014-07-05 05:55:00 +01:00
|
|
|
a.meth()
|
|
|
|
|
|
|
|
a = A.__new__(A)
|
|
|
|
a.meth()
|
2014-05-21 22:32:00 +01:00
|
|
|
|
|
|
|
#print(a.meth)
|
|
|
|
#print(a.__init__)
|
|
|
|
#print(a.__new__)
|
2014-07-05 05:55:00 +01:00
|
|
|
|
|
|
|
# __new__ should automatically be a staticmethod, so this should work
|
|
|
|
a = a.__new__(A)
|
|
|
|
a.meth()
|
2015-08-21 11:56:14 +01:00
|
|
|
|
2017-08-31 22:39:06 +01:00
|
|
|
# __new__ returns not an instance of the class (None here), __init__
|
|
|
|
# should not be called
|
|
|
|
|
2015-08-21 11:56:14 +01:00
|
|
|
class B:
|
|
|
|
def __new__(self, v1, v2):
|
2017-08-30 19:33:42 +01:00
|
|
|
print("B.__new__", v1, v2)
|
|
|
|
|
|
|
|
def __init__(self, v1, v2):
|
|
|
|
# Should not be called in this test
|
|
|
|
print("B.__init__", v1, v2)
|
|
|
|
|
|
|
|
print("B inst:", B(1, 2))
|
2017-08-31 22:39:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
# 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))
|