cython

Форк
0
/
int_literals.pyx 
202 строки · 4.4 Кб
1
# mode: run
2
# tag: syntax
3

4
from __future__ import absolute_import
5

6
cimport cython
7
from cython cimport typeof
8

9

10
def valid_underscore_literals():
11
    """
12
    >>> valid_underscore_literals()
13
    """
14
    # Copied from CPython's test_grammar.py
15
    assert 0_0_0 == 0
16
    assert 4_2 == 42
17
    assert 1_0000_0000 == 100000000
18
    assert 0b1001_0100 == 0b10010100
19
    assert 0xffff_ffff == 0xffffffff
20
    assert 0o5_7_7 == 0o577
21
    assert 1_00_00.5 == 10000.5
22
    assert 1e1_0 == 1e10
23
    assert .1_4 == .14
24
    assert 1_0 == 1_0L == 1_0LL == 1_0UL == 1_0ULL
25
    assert typeof(1_0ULL) == "unsigned long long"
26

27

28
@cython.test_assert_path_exists(
29
    '//IntNode[@longness = "LL"]',
30
    '//IntNode[@longness = "L"]',
31
    )
32
@cython.test_fail_if_path_exists('//IntNode[@longness = ""]')
33
def c_longs():
34
    """
35
    >>> c_longs() == (1, 1, -1, 18446744073709551615)  or  c_longs()
36
    True
37
    """
38
    cdef long a = 1L
39
    cdef unsigned long ua = 1UL
40
    cdef long long aa = 0xFFFFFFFFFFFFFFFFLL
41
    cdef unsigned long long uaa = 0xFFFFFFFFFFFFFFFFULL
42
    return a, ua, int(aa), uaa
43

44
@cython.test_assert_path_exists(
45
    '//IntNode[@longness = "LL"]',
46
    '//IntNode[@longness = "L"]',
47
    )
48
@cython.test_fail_if_path_exists('//IntNode[@longness = ""]')
49
def negative_c_longs():
50
    """
51
    >>> negative_c_longs() == (-1, -9223285636854775809)  or  negative_c_longs()
52
    True
53
    """
54
    cdef long a = -1L
55
    cdef long long aa = -9223285636854775809LL
56
    return a, aa
57

58
def py_longs():
59
    """
60
    >>> py_longs() == (
61
    ...     1, 1, 100000000000000000000000000000000, -100000000000000000000000000000000
62
    ...     )  or  py_longs()
63
    True
64
    """
65
    return 1, 1L, 100000000000000000000000000000000, -100000000000000000000000000000000
66

67
@cython.test_fail_if_path_exists("//NumBinopNode", "//IntBinopNode")
68
@cython.test_assert_path_exists("//ReturnStatNode/IntNode")
69
def py_huge_calculated_long():
70
    """
71
    >>> py_huge_calculated_long() == (
72
    ...     1606938044258990275541962092341162602522202993782792835301376
73
    ...     )  or  py_huge_calculated_long()
74
    True
75
    """
76
    return 1 << 200
77

78
@cython.test_fail_if_path_exists("//NumBinopNode", "//IntBinopNode")
79
@cython.test_assert_path_exists("//ReturnStatNode/IntNode")
80
def py_huge_computation_small_result():
81
    """
82
    >>> py_huge_computation_small_result()
83
    2
84
    """
85
    return (1 << 200) >> 199
86

87
@cython.test_fail_if_path_exists("//NumBinopNode", "//IntBinopNode")
88
#@cython.test_assert_path_exists("//ReturnStatNode/IntNode")
89
def py_huge_computation_small_result_neg():
90
    """
91
    >>> py_huge_computation_small_result_neg() == (
92
    ...    -2535301200456458802993406410752, -2535301200456458802993406410752
93
    ...    )  or  py_huge_computation_small_result_neg()
94
    True
95
    """
96
    return -(2 ** 101), (-2) ** 101
97

98

99
def large_literal():
100
    """
101
    >>> type(large_literal()) is int
102
    True
103
    """
104
    return 0xFFFFFFFFFFFF
105

106

107
def c_long_types():
108
    """
109
    >>> c_long_types()
110
    long
111
    long
112
    long long
113
    unsigned long
114
    unsigned long
115
    unsigned long long
116
    """
117
    print typeof(1)
118
    print typeof(1L)
119
    print typeof(1LL)
120
    print typeof(1U)
121
    print typeof(1UL)
122
    print typeof(1ULL)
123

124
# different ways to write an integer in Python
125

126
def c_oct():
127
    """
128
    >>> c_oct()
129
    (1, -17, 63)
130
    """
131
    cdef int a = 0o01
132
    cdef int b = -0o21
133
    cdef int c = 0o77
134
    return a,b,c
135

136
def c_oct_py2_legacy():
137
    """
138
    >>> c_oct_py2_legacy()
139
    (1, -17, 63)
140
    """
141
    cdef int a = 001
142
    cdef int b = -021
143
    cdef int c = 077
144
    return a,b,c
145

146
def py_oct():
147
    """
148
    >>> py_oct()
149
    (1, -17, 63)
150
    """
151
    return 0o01, -0o21, 0o77
152

153
def py_oct_py2_legacy():
154
    """
155
    >>> py_oct_py2_legacy()
156
    (1, -17, 63)
157
    """
158
    return 001, -021, 077
159

160
def c_hex():
161
    """
162
    >>> c_hex()
163
    (1, -33, 255)
164
    """
165
    cdef int a = 0x01
166
    cdef int b = -0x21
167
    cdef int c = 0xFF
168
    return a,b,c
169

170
def py_hex():
171
    """
172
    >>> py_hex()
173
    (1, -33, 255)
174
    """
175
    return 0x01, -0x21, 0xFF
176

177
def c_bin():
178
    """
179
    >>> c_bin()
180
    (1, -2, 15)
181
    """
182
    cdef int a = 0b01
183
    cdef int b = -0b10
184
    cdef int c = 0b1111
185
    return a,b,c
186

187
def py_bin():
188
    """
189
    >>> py_bin()
190
    (1, -2, 15)
191
    """
192
    return 0b01, -0b10, 0b1111
193

194
def big_value():
195
    """
196
    >>> big_value() == (10**10000)
197
    True
198
    """
199
    # Not quite a literal, but Cython expands the binop and inserts the literal
200
    # into the C source. Which means it must be converted to a hex string to avoid
201
    # hitting Python's integer conversion limits
202
    return 10**10000
203

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

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

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

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