cython

Форк
0
/
tp_new.pyx 
231 строка · 5.6 Кб
1
# mode: run
2
# tag: exttype, tpnew
3
# ticket: t808
4

5
cimport cython
6

7
cdef class MyType:
8
    cdef public args, kwargs
9
    def __cinit__(self, *args, **kwargs):
10
        self.args, self.kwargs = args, kwargs
11
        print "CINIT"
12
    def __init__(self, *args, **kwargs):
13
        print "INIT"
14

15
cdef class MySubType(MyType):
16
    def __cinit__(self, *args, **kwargs):
17
        self.args, self.kwargs = args, kwargs
18
        print "CINIT(SUB)"
19
    def __init__(self, *args, **kwargs):
20
        print "INIT"
21

22
class MyClass(object):
23
    def __cinit__(self, *args, **kwargs):
24
        self.args, self.kwargs = args, kwargs
25
        print "CINIT"
26
    def __init__(self, *args, **kwargs):
27
        print "INIT"
28

29
class MyTypeSubClass(MyType):
30
    def __cinit__(self, *args, **kwargs):
31
        # not called: Python class!
32
        print "CINIT(PYSUB)"
33
    def __init__(self, *args, **kwargs):
34
        print "INIT"
35

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

38
cdef class Base(object):
39
    pass
40

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

45
def test_derived_vtab():
46
    """
47
    >>> test_derived_vtab()
48
    42
49
    """
50
    cdef Derived d = Derived.__new__(Derived)
51
    return d.f()
52

53

54
# only these can be safely optimised:
55

56
@cython.test_assert_path_exists('//PythonCapiCallNode')
57
@cython.test_fail_if_path_exists(
58
    '//SimpleCallNode/AttributeNode',
59
    '//PyMethodCallNode',
60
)
61
def make_new():
62
    """
63
    >>> isinstance(make_new(), MyType)
64
    CINIT
65
    True
66
    """
67
    m = MyType.__new__(MyType)
68
    return m
69

70
@cython.test_assert_path_exists('//PythonCapiCallNode')
71
@cython.test_fail_if_path_exists(
72
    '//SimpleCallNode/AttributeNode',
73
    '//PyMethodCallNode',
74
)
75
def make_new_typed_target():
76
    """
77
    >>> isinstance(make_new_typed_target(), MyType)
78
    CINIT
79
    True
80
    """
81
    cdef MyType m
82
    m = MyType.__new__(MyType)
83
    return m
84

85
@cython.test_assert_path_exists('//PythonCapiCallNode')
86
@cython.test_fail_if_path_exists(
87
    '//SimpleCallNode/AttributeNode',
88
    '//PyMethodCallNode',
89
)
90
def make_new_with_args():
91
    """
92
    >>> isinstance(make_new_with_args(), MyType)
93
    CINIT
94
    (1, 2, 3)
95
    {}
96
    True
97
    """
98
    m = MyType.__new__(MyType, 1, 2 ,3)
99
    print m.args
100
    print m.kwargs
101
    return m
102

103
@cython.test_assert_path_exists('//PythonCapiCallNode')
104
@cython.test_fail_if_path_exists(
105
    '//SimpleCallNode/AttributeNode',
106
    '//PyMethodCallNode',
107
)
108
def make_new_with_args_kwargs():
109
    """
110
    >>> isinstance(make_new_with_args_kwargs(), MyType)
111
    CINIT
112
    (1, 2, 3)
113
    {'a': 4}
114
    True
115
    """
116
    m = MyType.__new__(MyType, 1, 2 ,3, a=4)
117
    print m.args
118
    print m.kwargs
119
    return m
120

121
@cython.test_assert_path_exists('//PythonCapiCallNode')
122
@cython.test_fail_if_path_exists(
123
    '//SimpleCallNode/AttributeNode',
124
    '//PyMethodCallNode',
125
)
126
def make_new_builtin():
127
    """
128
    >>> isinstance(make_new_builtin(), tuple)
129
    True
130
    """
131
    m = dict.__new__(dict)
132
    m = list.__new__(list)
133
    m = tuple.__new__(tuple)
134
    return m
135

136
@cython.test_assert_path_exists('//PythonCapiCallNode')
137
@cython.test_fail_if_path_exists(
138
    '//SimpleCallNode/AttributeNode',
139
    '//PyMethodCallNode',
140
)
141
def make_new_none(type t=None):
142
    """
143
    >>> make_new_none()  # doctest: +ELLIPSIS
144
    Traceback (most recent call last):
145
    TypeError: ... is not a type object (NoneType)
146
    """
147
    m = t.__new__(t)
148
    return m
149

150
@cython.test_assert_path_exists('//PythonCapiCallNode')
151
@cython.test_fail_if_path_exists(
152
    '//SimpleCallNode/AttributeNode',
153
    '//PyMethodCallNode',
154
)
155
def make_new_kwargs(type t=None):
156
    """
157
    >>> m = make_new_kwargs(MyType)
158
    CINIT
159
    >>> isinstance(m, MyType)
160
    True
161
    >>> m.args
162
    (1, 2, 3)
163
    >>> m.kwargs
164
    {'a': 5}
165
    """
166
    m = t.__new__(t, 1, 2, 3, a=5)
167
    return m
168

169
# these cannot:
170

171
@cython.test_assert_path_exists('//PyMethodCallNode/AttributeNode')
172
@cython.test_fail_if_path_exists('//PythonCapiCallNode')
173
def make_new_pyclass():
174
    """
175
    >>> isinstance(make_new_pyclass(), MyTypeSubClass)
176
    CINIT
177
    True
178
    """
179
    m = MyClass.__new__(MyClass)
180
    m = MyTypeSubClass.__new__(MyTypeSubClass)
181
    return m
182

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):
186
    """
187
    >>> isinstance(make_new_args(), MyType)
188
    CINIT
189
    True
190
    >>> isinstance(make_new_args(MyType), MyType)
191
    CINIT
192
    True
193
    >>> isinstance(make_new_args(MyType, MyType), MyType)
194
    CINIT
195
    True
196

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
203
    """
204
    if t1 is None:
205
        t1 = MyType
206
    if t2 is None:
207
        t2 = MyType
208
    m = t1.__new__(t2)
209
    return m
210

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):
214
    """
215
    >>> make_new_none_typed()  # doctest: +ELLIPSIS
216
    Traceback (most recent call last):
217
    TypeError: ... is not a type object (NoneType)
218
    """
219
    m = t.__new__(t)
220
    return m
221

222
@cython.test_assert_path_exists('//PyMethodCallNode/AttributeNode')
223
@cython.test_fail_if_path_exists('//PythonCapiCallNode')
224
def make_new_untyped(t):
225
    """
226
    >>> make_new_untyped(None)  # doctest: +ELLIPSIS
227
    Traceback (most recent call last):
228
    TypeError: ... is not a type object (NoneType)
229
    """
230
    m = t.__new__(t)
231
    return m
232

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.