1
2cimport cython
3
4__doc__ = u"""
5    >>> s()
6    b'spam'
7"""
8
9_unicode = unicode
10
11import sys
12IS_PY3 = sys.version_info[0] >= 3
13
14if not IS_PY3:
15    __doc__ = __doc__.replace(u" b'", u" '")
16
17
18def print_large_number(n):
19    print(str(n).rstrip('L'))
20
21
22DEF TUPLE = (1, 2, u"buckle my shoe")
23DEF TRUE_FALSE = (True, False)
24DEF NONE = None
25
26DEF CHAR = c'x'
27DEF INT0 = -1
28DEF INT1 = 42
29DEF INT2 = 0x42
30DEF INT3 = -0x42
31DEF LONG = 666L
32DEF LARGE_NUM32 = (1 << 32) - 1
33DEF LARGE_NUM64 = (1 << 64) - 1
34DEF FLOAT = 12.5
35DEF BYTES = b"spam"
36DEF UNICODE = u"spam-u"
37DEF TWO = TUPLE[1]
38DEF FIVE = TWO + 3
39DEF TRUE  = TRUE_FALSE[0]
40DEF FALSE = TRUE_FALSE[1]
41DEF INT_TUPLE1 = TUPLE[:2]
42DEF INT_TUPLE2 = TUPLE[1:4:2]
43DEF ELLIPSIS = ...
44DEF EXPRESSION = int(float(2*2)) + int(str(2)) + int(max(1,2,3)) + sum([TWO, FIVE])
45DEF UNICODE_EXPRESSION = unicode(BYTES.decode('utf8')).encode('ascii').decode('latin1')
46
47
48def c():
49    """
50    >>> c()
51    120
52    """
53    cdef char c = CHAR
54    return c
55
56def i0():
57    """
58    >>> i0() == -1
59    True
60    """
61    cdef int i = INT0
62    return i
63
64def i1():
65    """
66    >>> i1() == 42
67    True
68    """
69    cdef int i = INT1
70    return i
71
72def i2():
73    """
74    >>> i2() == 0x42
75    True
76    """
77    cdef int i = INT2
78    return i
79
80def i3():
81    """
82    >>> i3() == -0x42
83    True
84    """
85    cdef int i = INT3
86    return i
87
88def l():
89    """
90    >>> l()
91    666
92    """
93    cdef long l = LONG
94    return l
95
96def large_nums():
97    """
98    >>> ul32, ul64, l64, n64 = large_nums()
99    >>> print_large_number(ul32)
100    4294967295
101    >>> print_large_number(ul64)
102    18446744073709551615
103    >>> print_large_number(l64)
104    4294967295
105    >>> print_large_number(n64)
106    -4294967295
107    """
108    cdef unsigned long ul32 = LARGE_NUM32
109    cdef unsigned long long ul64 = LARGE_NUM64
110    cdef long long l64 = LARGE_NUM32
111    cdef long long n64 = -LARGE_NUM32
112    return ul32, ul64, l64, n64
113
114def f():
115    """
116    >>> f()
117    12.5
118    """
119    cdef float f = FLOAT
120    return f
121
122def s():
123    """
124    see module docstring above
125    """
126    cdef char* s = BYTES
127    return s
128
129def type_of_bytes():
130    """
131    >>> t, s = type_of_bytes()
132    >>> assert t is bytes, t
133    >>> assert type(s) is bytes, type(s)
134    """
135    t = type(BYTES)
136    s = BYTES
137    return t, s
138
139def type_of_unicode():
140    """
141    >>> t, s = type_of_unicode()
142    >>> assert t is _unicode, t
143    >>> assert type(s) is _unicode, type(s)
144    """
145    t = type(UNICODE)
146    s = UNICODE
147    return t, s
148
149@cython.test_assert_path_exists('//TupleNode')
150def constant_tuple():
151    """
152    >>> constant_tuple()[:-1]
153    (1, 2)
154    >>> print(constant_tuple()[-1])
155    buckle my shoe
156    """
157    cdef object t = TUPLE
158    return t
159
160@cython.test_assert_path_exists('//IntNode')
161def tuple_indexing():
162    """
163    >>> tuple_indexing()
164    2
165    """
166    cdef int two = INT_TUPLE1[-1]
167    return two
168
169def two():
170    """
171    >>> two()
172    2
173    """
174    cdef int two = TWO
175    return two
176
177def five():
178    """
179    >>> five()
180    5
181    """
182    cdef int five = FIVE
183    return five
184
185@cython.test_assert_path_exists('//BoolNode')
186def true():
187    """
188    >>> true()
189    True
190    """
191    cdef bint true = TRUE
192    return true
193
194@cython.test_assert_path_exists('//BoolNode')
195def false():
196    """
197    >>> false()
198    False
199    """
200    cdef bint false = FALSE
201    return false
202
203def ellipsis():
204    """
205    >>> ellipsis()
206    Ellipsis
207    """
208    return ELLIPSIS
209
210@cython.test_assert_path_exists('//IntNode')
211@cython.test_fail_if_path_exists('//AddNode')
212def expression():
213    """
214    >>> expression()
215    16
216    """
217    cdef int i = EXPRESSION
218    return i
219
220
221def unicode_expression():
222    """
223    >>> print(unicode_expression())
224    spam
225    """
226    s = UNICODE_EXPRESSION
227    return s
228
229
230def none():
231    """
232    >>> none()
233    """
234    return NONE
235