summaryrefslogtreecommitdiff
path: root/tests/run/tp_new.pyx
blob: 45b52259fca192a0cf632cecde680f58b1f939fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# mode: run
# tag: exttype, tpnew
# ticket: t808

cimport cython

cdef class MyType:
    cdef public args, kwargs
    def __cinit__(self, *args, **kwargs):
        self.args, self.kwargs = args, kwargs
        print "CINIT"
    def __init__(self, *args, **kwargs):
        print "INIT"

cdef class MySubType(MyType):
    def __cinit__(self, *args, **kwargs):
        self.args, self.kwargs = args, kwargs
        print "CINIT(SUB)"
    def __init__(self, *args, **kwargs):
        print "INIT"

class MyClass(object):
    def __cinit__(self, *args, **kwargs):
        self.args, self.kwargs = args, kwargs
        print "CINIT"
    def __init__(self, *args, **kwargs):
        print "INIT"

class MyTypeSubClass(MyType):
    def __cinit__(self, *args, **kwargs):
        # not called: Python class!
        print "CINIT(PYSUB)"
    def __init__(self, *args, **kwargs):
        print "INIT"

# See ticket T808, vtab must be set even if there is no __cinit__.

cdef class Base(object):
    pass

cdef class Derived(Base):
    cpdef int f(self):
        return 42

def test_derived_vtab():
    """
    >>> test_derived_vtab()
    42
    """
    cdef Derived d = Derived.__new__(Derived)
    return d.f()


# only these can be safely optimised:

@cython.test_assert_path_exists('//PythonCapiCallNode')
@cython.test_fail_if_path_exists(
    '//SimpleCallNode/AttributeNode',
    '//PyMethodCallNode',
)
def make_new():
    """
    >>> isinstance(make_new(), MyType)
    CINIT
    True
    """
    m = MyType.__new__(MyType)
    return m

@cython.test_assert_path_exists('//PythonCapiCallNode')
@cython.test_fail_if_path_exists(
    '//SimpleCallNode/AttributeNode',
    '//PyMethodCallNode',
)
def make_new_typed_target():
    """
    >>> isinstance(make_new_typed_target(), MyType)
    CINIT
    True
    """
    cdef MyType m
    m = MyType.__new__(MyType)
    return m

@cython.test_assert_path_exists('//PythonCapiCallNode')
@cython.test_fail_if_path_exists(
    '//SimpleCallNode/AttributeNode',
    '//PyMethodCallNode',
)
def make_new_with_args():
    """
    >>> isinstance(make_new_with_args(), MyType)
    CINIT
    (1, 2, 3)
    {}
    True
    """
    m = MyType.__new__(MyType, 1, 2 ,3)
    print m.args
    print m.kwargs
    return m

@cython.test_assert_path_exists('//PythonCapiCallNode')
@cython.test_fail_if_path_exists(
    '//SimpleCallNode/AttributeNode',
    '//PyMethodCallNode',
)
def make_new_with_args_kwargs():
    """
    >>> isinstance(make_new_with_args_kwargs(), MyType)
    CINIT
    (1, 2, 3)
    {'a': 4}
    True
    """
    m = MyType.__new__(MyType, 1, 2 ,3, a=4)
    print m.args
    print m.kwargs
    return m

@cython.test_assert_path_exists('//PythonCapiCallNode')
@cython.test_fail_if_path_exists(
    '//SimpleCallNode/AttributeNode',
    '//PyMethodCallNode',
)
def make_new_builtin():
    """
    >>> isinstance(make_new_builtin(), tuple)
    True
    """
    m = dict.__new__(dict)
    m = list.__new__(list)
    m = tuple.__new__(tuple)
    return m

@cython.test_assert_path_exists('//PythonCapiCallNode')
@cython.test_fail_if_path_exists(
    '//SimpleCallNode/AttributeNode',
    '//PyMethodCallNode',
)
def make_new_none(type t=None):
    """
    >>> make_new_none()  # doctest: +ELLIPSIS
    Traceback (most recent call last):
    TypeError: ... is not a type object (NoneType)
    """
    m = t.__new__(t)
    return m

@cython.test_assert_path_exists('//PythonCapiCallNode')
@cython.test_fail_if_path_exists(
    '//SimpleCallNode/AttributeNode',
    '//PyMethodCallNode',
)
def make_new_kwargs(type t=None):
    """
    >>> m = make_new_kwargs(MyType)
    CINIT
    >>> isinstance(m, MyType)
    True
    >>> m.args
    (1, 2, 3)
    >>> m.kwargs
    {'a': 5}
    """
    m = t.__new__(t, 1, 2, 3, a=5)
    return m

# these cannot:

@cython.test_assert_path_exists('//PyMethodCallNode/AttributeNode')
@cython.test_fail_if_path_exists('//PythonCapiCallNode')
def make_new_pyclass():
    """
    >>> isinstance(make_new_pyclass(), MyTypeSubClass)
    CINIT
    True
    """
    m = MyClass.__new__(MyClass)
    m = MyTypeSubClass.__new__(MyTypeSubClass)
    return m

@cython.test_assert_path_exists('//PyMethodCallNode/AttributeNode')
@cython.test_fail_if_path_exists('//PythonCapiCallNode')
def make_new_args(type t1=None, type t2=None):
    """
    >>> isinstance(make_new_args(), MyType)
    CINIT
    True
    >>> isinstance(make_new_args(MyType), MyType)
    CINIT
    True
    >>> isinstance(make_new_args(MyType, MyType), MyType)
    CINIT
    True

    >>> isinstance(make_new_args(MyType, MySubType), MySubType)
    Traceback (most recent call last):
    TypeError: tp_new.MyType.__new__(tp_new.MySubType) is not safe, use tp_new.MySubType.__new__()
    >>> isinstance(make_new_args(MySubType, MyType), MyType)
    Traceback (most recent call last):
    TypeError: tp_new.MySubType.__new__(tp_new.MyType): tp_new.MyType is not a subtype of tp_new.MySubType
    """
    if t1 is None:
        t1 = MyType
    if t2 is None:
        t2 = MyType
    m = t1.__new__(t2)
    return m

@cython.test_assert_path_exists('//PyMethodCallNode/AttributeNode')
@cython.test_fail_if_path_exists('//PythonCapiCallNode')
def make_new_none_typed(tuple t=None):
    """
    >>> make_new_none_typed()  # doctest: +ELLIPSIS
    Traceback (most recent call last):
    TypeError: ... is not a type object (NoneType)
    """
    m = t.__new__(t)
    return m

@cython.test_assert_path_exists('//PyMethodCallNode/AttributeNode')
@cython.test_fail_if_path_exists('//PythonCapiCallNode')
def make_new_untyped(t):
    """
    >>> make_new_untyped(None)  # doctest: +ELLIPSIS
    Traceback (most recent call last):
    TypeError: ... is not a type object (NoneType)
    """
    m = t.__new__(t)
    return m