8
cdef public args, kwargs
9
def __cinit__(self, *args, **kwargs):
10
self.args, self.kwargs = args, kwargs
12
def __init__(self, *args, **kwargs):
15
cdef class MySubType(MyType):
16
def __cinit__(self, *args, **kwargs):
17
self.args, self.kwargs = args, kwargs
19
def __init__(self, *args, **kwargs):
23
def __cinit__(self, *args, **kwargs):
24
self.args, self.kwargs = args, kwargs
26
def __init__(self, *args, **kwargs):
29
class MyTypeSubClass(MyType):
30
def __cinit__(self, *args, **kwargs):
31
# not called: Python class!
33
def __init__(self, *args, **kwargs):
36
# See ticket T808, vtab must be set even if there is no __cinit__.
38
cdef class Base(object):
41
cdef class Derived(Base):
45
def test_derived_vtab():
47
>>> test_derived_vtab()
50
cdef Derived d = Derived.__new__(Derived)
54
# only these can be safely optimised:
56
@cython.test_assert_path_exists('//PythonCapiCallNode')
57
@cython.test_fail_if_path_exists(
58
'//SimpleCallNode/AttributeNode',
63
>>> isinstance(make_new(), MyType)
67
m = MyType.__new__(MyType)
70
@cython.test_assert_path_exists('//PythonCapiCallNode')
71
@cython.test_fail_if_path_exists(
72
'//SimpleCallNode/AttributeNode',
75
def make_new_typed_target():
77
>>> isinstance(make_new_typed_target(), MyType)
82
m = MyType.__new__(MyType)
85
@cython.test_assert_path_exists('//PythonCapiCallNode')
86
@cython.test_fail_if_path_exists(
87
'//SimpleCallNode/AttributeNode',
90
def make_new_with_args():
92
>>> isinstance(make_new_with_args(), MyType)
98
m = MyType.__new__(MyType, 1, 2 ,3)
103
@cython.test_assert_path_exists('//PythonCapiCallNode')
104
@cython.test_fail_if_path_exists(
105
'//SimpleCallNode/AttributeNode',
106
'//PyMethodCallNode',
108
def make_new_with_args_kwargs():
110
>>> isinstance(make_new_with_args_kwargs(), MyType)
116
m = MyType.__new__(MyType, 1, 2 ,3, a=4)
121
@cython.test_assert_path_exists('//PythonCapiCallNode')
122
@cython.test_fail_if_path_exists(
123
'//SimpleCallNode/AttributeNode',
124
'//PyMethodCallNode',
126
def make_new_builtin():
128
>>> isinstance(make_new_builtin(), tuple)
131
m = dict.__new__(dict)
132
m = list.__new__(list)
133
m = tuple.__new__(tuple)
136
@cython.test_assert_path_exists('//PythonCapiCallNode')
137
@cython.test_fail_if_path_exists(
138
'//SimpleCallNode/AttributeNode',
139
'//PyMethodCallNode',
141
def make_new_none(type t=None):
143
>>> make_new_none() # doctest: +ELLIPSIS
144
Traceback (most recent call last):
145
TypeError: ... is not a type object (NoneType)
150
@cython.test_assert_path_exists('//PythonCapiCallNode')
151
@cython.test_fail_if_path_exists(
152
'//SimpleCallNode/AttributeNode',
153
'//PyMethodCallNode',
155
def make_new_kwargs(type t=None):
157
>>> m = make_new_kwargs(MyType)
159
>>> isinstance(m, MyType)
166
m = t.__new__(t, 1, 2, 3, a=5)
171
@cython.test_assert_path_exists('//PyMethodCallNode/AttributeNode')
172
@cython.test_fail_if_path_exists('//PythonCapiCallNode')
173
def make_new_pyclass():
175
>>> isinstance(make_new_pyclass(), MyTypeSubClass)
179
m = MyClass.__new__(MyClass)
180
m = MyTypeSubClass.__new__(MyTypeSubClass)
183
@cython.test_assert_path_exists('//PyMethodCallNode/AttributeNode')
184
@cython.test_fail_if_path_exists('//PythonCapiCallNode')
185
def make_new_args(type t1=None, type t2=None):
187
>>> isinstance(make_new_args(), MyType)
190
>>> isinstance(make_new_args(MyType), MyType)
193
>>> isinstance(make_new_args(MyType, MyType), MyType)
197
>>> isinstance(make_new_args(MyType, MySubType), MySubType)
198
Traceback (most recent call last):
199
TypeError: tp_new.MyType.__new__(tp_new.MySubType) is not safe, use tp_new.MySubType.__new__()
200
>>> isinstance(make_new_args(MySubType, MyType), MyType)
201
Traceback (most recent call last):
202
TypeError: tp_new.MySubType.__new__(tp_new.MyType): tp_new.MyType is not a subtype of tp_new.MySubType
211
@cython.test_assert_path_exists('//PyMethodCallNode/AttributeNode')
212
@cython.test_fail_if_path_exists('//PythonCapiCallNode')
213
def make_new_none_typed(tuple t=None):
215
>>> make_new_none_typed() # doctest: +ELLIPSIS
216
Traceback (most recent call last):
217
TypeError: ... is not a type object (NoneType)
222
@cython.test_assert_path_exists('//PyMethodCallNode/AttributeNode')
223
@cython.test_fail_if_path_exists('//PythonCapiCallNode')
224
def make_new_untyped(t):
226
>>> make_new_untyped(None) # doctest: +ELLIPSIS
227
Traceback (most recent call last):
228
TypeError: ... is not a type object (NoneType)