File: int_literals.pyx

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,092 kB
  • sloc: python: 83,539; ansic: 18,831; cpp: 1,402; xml: 1,031; javascript: 511; makefile: 403; sh: 204; sed: 11
file content (205 lines) | stat: -rw-r--r-- 4,648 bytes parent folder | download
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
# mode: run
# tag: syntax

from __future__ import absolute_import

cimport cython
from cython cimport typeof

import sys


def valid_underscore_literals():
    """
    >>> valid_underscore_literals()
    """
    # Copied from CPython's test_grammar.py
    assert 0_0_0 == 0
    assert 4_2 == 42
    assert 1_0000_0000 == 100000000
    assert 0b1001_0100 == 0b10010100
    assert 0xffff_ffff == 0xffffffff
    assert 0o5_7_7 == 0o577
    assert 1_00_00.5 == 10000.5
    assert 1e1_0 == 1e10
    assert .1_4 == .14
    assert 1_0 == 1_0L == 1_0LL == 1_0UL == 1_0ULL
    assert typeof(1_0ULL) == "unsigned long long"


@cython.test_assert_path_exists(
    '//IntNode[@longness = "LL"]',
    '//IntNode[@longness = "L"]',
    )
@cython.test_fail_if_path_exists('//IntNode[@longness = ""]')
def c_longs():
    """
    >>> c_longs() == (1, 1, -1, 18446744073709551615)  or  c_longs()
    True
    """
    cdef long a = 1L
    cdef unsigned long ua = 1UL
    cdef long long aa = 0xFFFFFFFFFFFFFFFFLL
    cdef unsigned long long uaa = 0xFFFFFFFFFFFFFFFFULL
    return a, ua, int(aa), uaa

@cython.test_assert_path_exists(
    '//IntNode[@longness = "LL"]',
    '//IntNode[@longness = "L"]',
    )
@cython.test_fail_if_path_exists('//IntNode[@longness = ""]')
def negative_c_longs():
    """
    >>> negative_c_longs() == (-1, -9223285636854775809)  or  negative_c_longs()
    True
    """
    cdef long a = -1L
    cdef long long aa = -9223285636854775809LL
    return a, aa

def py_longs():
    """
    >>> py_longs() == (
    ...     1, 1, 100000000000000000000000000000000, -100000000000000000000000000000000
    ...     )  or  py_longs()
    True
    """
    return 1, 1L, 100000000000000000000000000000000, -100000000000000000000000000000000

@cython.test_fail_if_path_exists("//NumBinopNode", "//IntBinopNode")
@cython.test_assert_path_exists("//ReturnStatNode/IntNode")
def py_huge_calculated_long():
    """
    >>> py_huge_calculated_long() == (
    ...     1606938044258990275541962092341162602522202993782792835301376
    ...     )  or  py_huge_calculated_long()
    True
    """
    return 1 << 200

@cython.test_fail_if_path_exists("//NumBinopNode", "//IntBinopNode")
@cython.test_assert_path_exists("//ReturnStatNode/IntNode")
def py_huge_computation_small_result():
    """
    >>> py_huge_computation_small_result()
    2
    """
    return (1 << 200) >> 199

@cython.test_fail_if_path_exists("//NumBinopNode", "//IntBinopNode")
#@cython.test_assert_path_exists("//ReturnStatNode/IntNode")
def py_huge_computation_small_result_neg():
    """
    >>> py_huge_computation_small_result_neg() == (
    ...    -2535301200456458802993406410752, -2535301200456458802993406410752
    ...    )  or  py_huge_computation_small_result_neg()
    True
    """
    return -(2 ** 101), (-2) ** 101

def large_literal():
    """
    >>> type(large_literal()) is int
    True
    """
    if sys.version_info[0] >= 3 or sys.maxint > 0xFFFFFFFFFFFF:
        return 0xFFFFFFFFFFFF
    else:
        return 0xFFFFFFF

def c_long_types():
    """
    >>> c_long_types()
    long
    long
    long long
    unsigned long
    unsigned long
    unsigned long long
    """
    print typeof(1)
    print typeof(1L)
    print typeof(1LL)
    print typeof(1U)
    print typeof(1UL)
    print typeof(1ULL)

# different ways to write an integer in Python

def c_oct():
    """
    >>> c_oct()
    (1, -17, 63)
    """
    cdef int a = 0o01
    cdef int b = -0o21
    cdef int c = 0o77
    return a,b,c

def c_oct_py2_legacy():
    """
    >>> c_oct_py2_legacy()
    (1, -17, 63)
    """
    cdef int a = 001
    cdef int b = -021
    cdef int c = 077
    return a,b,c

def py_oct():
    """
    >>> py_oct()
    (1, -17, 63)
    """
    return 0o01, -0o21, 0o77

def py_oct_py2_legacy():
    """
    >>> py_oct_py2_legacy()
    (1, -17, 63)
    """
    return 001, -021, 077

def c_hex():
    """
    >>> c_hex()
    (1, -33, 255)
    """
    cdef int a = 0x01
    cdef int b = -0x21
    cdef int c = 0xFF
    return a,b,c

def py_hex():
    """
    >>> py_hex()
    (1, -33, 255)
    """
    return 0x01, -0x21, 0xFF

def c_bin():
    """
    >>> c_bin()
    (1, -2, 15)
    """
    cdef int a = 0b01
    cdef int b = -0b10
    cdef int c = 0b1111
    return a,b,c

def py_bin():
    """
    >>> py_bin()
    (1, -2, 15)
    """
    return 0b01, -0b10, 0b1111

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