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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
# mode: run
# tag: warnings
cimport cython
__doc__ = u"""
>>> s()
b'spam'
"""
_unicode = unicode
import sys
IS_PY3 = sys.version_info[0] >= 3
if not IS_PY3:
__doc__ = __doc__.replace(u" b'", u" '")
def print_large_number(n):
print(str(n).rstrip('L'))
DEF TUPLE = (1, 2, u"buckle my shoe")
DEF TRUE_FALSE = (True, False)
DEF NONE = None
DEF CHAR = c'x'
DEF INT0 = -1
DEF INT1 = 42
DEF INT2 = 0x42
DEF INT3 = -0x42
DEF LONG = 666L
DEF LARGE_NUM32 = (1 << 32) - 1
DEF LARGE_NUM64 = (1 << 64) - 1
DEF FLOAT = 12.5
DEF BYTES = b"spam"
DEF UNICODE = u"spam-u"
DEF TWO = TUPLE[1]
DEF FIVE = TWO + 3
DEF TRUE = TRUE_FALSE[0]
DEF FALSE = TRUE_FALSE[1]
DEF INT_TUPLE1 = TUPLE[:2]
DEF INT_TUPLE2 = TUPLE[1:4:2]
DEF ELLIPSIS = ...
DEF EXPRESSION = int(float(2*2)) + int(str(2)) + int(max(1,2,3)) + sum([TWO, FIVE])
DEF UNICODE_EXPRESSION = unicode(BYTES.decode('utf8')).encode('ascii').decode('latin1')
def c():
"""
>>> c()
120
"""
cdef char c = CHAR
return c
def i0():
"""
>>> i0() == -1
True
"""
cdef int i = INT0
return i
def i1():
"""
>>> i1() == 42
True
"""
cdef int i = INT1
return i
def i2():
"""
>>> i2() == 0x42
True
"""
cdef int i = INT2
return i
def i3():
"""
>>> i3() == -0x42
True
"""
cdef int i = INT3
return i
def l():
"""
>>> l()
666
"""
cdef long l = LONG
return l
def large_nums():
"""
>>> ul32, ul64, l64, n64 = large_nums()
>>> print_large_number(ul32)
4294967295
>>> print_large_number(ul64)
18446744073709551615
>>> print_large_number(l64)
4294967295
>>> print_large_number(n64)
-4294967295
"""
cdef unsigned long ul32 = LARGE_NUM32
cdef unsigned long long ul64 = LARGE_NUM64
cdef long long l64 = LARGE_NUM32
cdef long long n64 = -LARGE_NUM32
return ul32, ul64, l64, n64
def f():
"""
>>> f()
12.5
"""
cdef float f = FLOAT
return f
def s():
"""
see module docstring above
"""
cdef char* s = BYTES
return s
def type_of_bytes():
"""
>>> t, s = type_of_bytes()
>>> assert t is bytes, t
>>> assert type(s) is bytes, type(s)
"""
t = type(BYTES)
s = BYTES
return t, s
def type_of_unicode():
"""
>>> t, s = type_of_unicode()
>>> assert t is _unicode, t
>>> assert type(s) is _unicode, type(s)
"""
t = type(UNICODE)
s = UNICODE
return t, s
@cython.test_assert_path_exists('//TupleNode')
def constant_tuple():
"""
>>> constant_tuple()[:-1]
(1, 2)
>>> print(constant_tuple()[-1])
buckle my shoe
"""
cdef object t = TUPLE
return t
@cython.test_assert_path_exists('//IntNode')
def tuple_indexing():
"""
>>> tuple_indexing()
2
"""
cdef int two = INT_TUPLE1[-1]
return two
def two():
"""
>>> two()
2
"""
cdef int two = TWO
return two
def five():
"""
>>> five()
5
"""
cdef int five = FIVE
return five
@cython.test_assert_path_exists('//BoolNode')
def true():
"""
>>> true()
True
"""
cdef bint true = TRUE
return true
@cython.test_assert_path_exists('//BoolNode')
def false():
"""
>>> false()
False
"""
cdef bint false = FALSE
return false
def ellipsis():
"""
>>> ellipsis()
Ellipsis
"""
return ELLIPSIS
@cython.test_assert_path_exists('//IntNode')
@cython.test_fail_if_path_exists('//AddNode')
def expression():
"""
>>> expression()
16
"""
cdef int i = EXPRESSION
return i
def unicode_expression():
"""
>>> print(unicode_expression())
spam
"""
s = UNICODE_EXPRESSION
return s
def none():
"""
>>> none()
"""
return NONE
_WARNINGS = """
24:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
25:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
26:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
28:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
29:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
30:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
31:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
32:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
33:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
34:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
35:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
36:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
37:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
38:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
39:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
40:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
41:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
42:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
43:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
44:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
45:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
46:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
47:0: The 'DEF' statement is deprecated and will be removed in a future Cython version. Consider using global variables, constants, and in-place literals instead. See https://github.com/cython/cython/issues/4310
"""
|