1# object.__new__(cls) is the only way in Python to allocate empty
2# (non-initialized) instance of class.
3# See e.g. http://infohost.nmt.edu/tcc/help/pubs/python/web/new-new-method.html
4# TODO: Find reference in CPython docs
5try:
6    # If we don't expose object.__new__ (small ports), there's
7    # nothing to test.
8    object.__new__
9except AttributeError:
10    print("SKIP")
11    raise SystemExit
12
13class Foo:
14
15    def __new__(cls):
16        # Should not be called in this test
17        print("in __new__")
18        raise RuntimeError
19
20    def __init__(self):
21        print("in __init__")
22        self.attr = "something"
23
24
25o = object.__new__(Foo)
26#print(o)
27print("Result of __new__ has .attr:", hasattr(o, "attr"))
28print("Result of __new__ is already a Foo:", isinstance(o, Foo))
29
30o.__init__()
31#print(dir(o))
32print("After __init__ has .attr:", hasattr(o, "attr"))
33print(".attr:", o.attr)
34
35# should only be able to call __new__ on user types
36try:
37    object.__new__(1)
38except TypeError:
39    print("TypeError")
40try:
41    object.__new__(int)
42except TypeError:
43    print("TypeError")
44