forked from cython/cython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fused_cpdef.pyx
220 lines (185 loc) · 5.03 KB
/
fused_cpdef.pyx
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
# cython: language_level=3str
# mode: run
cimport cython
import sys, io
cy = __import__("cython")
cpdef func1(self, cython.integral x):
print(f"{self},", end=' ')
if cython.integral is int:
print('x is int', x, cython.typeof(x))
else:
print('x is long', x, cython.typeof(x))
class A(object):
meth = func1
def __str__(self):
return "A"
cdef class B:
cpdef int meth(self, cython.integral x):
print(f"{self},", end=' ')
if cython.integral is int:
print('x is int', x, cython.typeof(x))
else:
print('x is long', x, cython.typeof(x))
return 0
def __str__(self):
return "B"
pyfunc = func1
def test_fused_cpdef():
"""
>>> test_fused_cpdef()
None, x is int 2 int
None, x is long 2 long
None, x is long 2 long
<BLANKLINE>
None, x is int 2 int
None, x is long 2 long
<BLANKLINE>
A, x is int 2 int
A, x is long 2 long
A, x is long 2 long
A, x is long 2 long
<BLANKLINE>
B, x is long 2 long
"""
func1[int](None, 2)
func1[long](None, 2)
func1(None, 2)
print()
pyfunc[cy.int](None, 2)
pyfunc(None, 2)
print()
A.meth[cy.int](A(), 2)
A.meth(A(), 2)
A().meth[cy.long](2)
A().meth(2)
print()
B().meth(2)
midimport_run = io.StringIO()
realstdout = sys.stdout
sys.stdout = midimport_run
try:
# Run `test_fused_cpdef()` during import and save the result for
# `test_midimport_run()`.
test_fused_cpdef()
except Exception as e:
midimport_run.write(f"{e!r}\n")
finally:
sys.stdout = realstdout
def test_midimport_run():
# At one point, dynamically calling fused cpdef functions during import
# would fail because the type signature-matching indices weren't
# yet initialized.
# (See Compiler.FusedNode.FusedCFuncDefNode._fused_signature_index,
# GH-3366.)
"""
>>> test_midimport_run()
None, x is int 2 int
None, x is long 2 long
None, x is long 2 long
<BLANKLINE>
None, x is int 2 int
None, x is long 2 long
<BLANKLINE>
A, x is int 2 int
A, x is long 2 long
A, x is long 2 long
A, x is long 2 long
<BLANKLINE>
B, x is long 2 long
"""
print(midimport_run.getvalue(), end='')
def assert_raise(func, *args):
try:
func(*args)
except TypeError:
pass
else:
assert False, "Function call did not raise TypeError"
def test_badcall():
"""
>>> test_badcall()
"""
assert_raise(pyfunc)
assert_raise(pyfunc, 1, 2, 3)
assert_raise(pyfunc[cy.int], 10, 11, 12)
assert_raise(pyfunc, None, object())
assert_raise(A().meth)
assert_raise(A.meth)
assert_raise(A().meth[cy.int])
assert_raise(A.meth[cy.int])
assert_raise(B().meth, 1, 2, 3)
def test_nomatch():
"""
>>> func1(None, ())
Traceback (most recent call last):
TypeError: No matching signature found
"""
ctypedef long double long_double
cpdef multiarg(cython.integral x, cython.floating y):
if cython.integral is int:
print("x is an int,", end=' ')
else:
print("x is a long,", end=' ')
if cython.floating is long_double:
print("y is a long double:", end=' ')
elif float is cython.floating:
print("y is a float:", end=' ')
else:
print("y is a double:", end=' ')
print(x, y)
def test_multiarg():
"""
>>> test_multiarg()
x is an int, y is a float: 1 2.0
x is an int, y is a float: 1 2.0
x is a long, y is a double: 4 5.0
>>> multiarg()
Traceback (most recent call last):
TypeError: Expected at least 2 arguments, got 0
>>> multiarg(1, 2.0, 3) # doctest: +ELLIPSIS
Traceback (most recent call last):
TypeError: ...2...arg...3...
"""
multiarg[int, float](1, 2.0)
multiarg[cy.int, cy.float](1, 2.0)
multiarg(4, 5.0)
def test_ambiguousmatch():
"""
>>> multiarg(5, ())
Traceback (most recent call last):
TypeError: Function call with ambiguous argument types
>>> multiarg((), 2.0)
Traceback (most recent call last):
TypeError: Function call with ambiguous argument types
"""
# https://github.com/cython/cython/issues/4409
# default arguments + fused cpdef were crashing
cpdef literal_default(cython.integral x, some_string="value"):
return x, some_string
cpdef mutable_default(cython.integral x, some_value=[]):
some_value.append(x)
return some_value
def test_defaults():
"""
>>> literal_default(1)
(1, 'value')
>>> literal_default(1, "hello")
(1, 'hello')
>>> mutable_default(1)
[1]
>>> mutable_default(2)
[1, 2]
>>> mutable_default(3,[])
[3]
"""
cdef class C:
cpdef object has_default_struct(self, cython.floating x, a=None):
return x, a
# https://github.com/cython/cython/issues/5588
# On some Python versions this was causing a compiler crash
def test_call_has_default_struct(C c, double x):
"""
>>> test_call_has_default_struct(C(), 5.)
(5.0, None)
"""
return c.has_default_struct(x)