cython

Форк
0
/
bytes_indexing.pyx 
179 строк · 5.1 Кб
1

2
cimport cython
3

4
cdef bytes b12345 = b'12345'
5

6
def index_literal(int i):
7
    """
8
    Python 3 returns integer values on indexing, Py2 returns byte
9
    string literals...
10

11
    >>> index_literal(0) in (ord('1'), '1')
12
    True
13
    >>> index_literal(-5) in (ord('1'), '1')
14
    True
15
    >>> index_literal(2) in (ord('3'), '3')
16
    True
17
    >>> index_literal(4) in (ord('5'), '5')
18
    True
19
    """
20
    return b"12345"[i]
21

22

23
@cython.test_assert_path_exists("//PythonCapiCallNode")
24
@cython.test_fail_if_path_exists("//IndexNode",
25
                                 "//CoerceFromPyTypeNode")
26
def index_literal_char_cast(int i):
27
    """
28
    >>> index_literal_char_cast(0) == ord('1')
29
    True
30
    >>> index_literal_char_cast(-5) == ord('1')
31
    True
32
    >>> index_literal_char_cast(2) == ord('3')
33
    True
34
    >>> index_literal_char_cast(4) == ord('5')
35
    True
36
    >>> index_literal_char_cast(6)
37
    Traceback (most recent call last):
38
    IndexError: string index out of range
39
    """
40
    return <char>(b"12345"[i])
41

42

43
@cython.test_assert_path_exists("//PythonCapiCallNode")
44
@cython.test_fail_if_path_exists("//IndexNode",
45
                                 "//CoerceFromPyTypeNode")
46
def index_nonliteral_char_cast(int i):
47
    """
48
    >>> index_nonliteral_char_cast(0) == ord('1')
49
    True
50
    >>> index_nonliteral_char_cast(-5) == ord('1')
51
    True
52
    >>> index_nonliteral_char_cast(2) == ord('3')
53
    True
54
    >>> index_nonliteral_char_cast(4) == ord('5')
55
    True
56
    >>> index_nonliteral_char_cast(6)
57
    Traceback (most recent call last):
58
    IndexError: string index out of range
59
    """
60
    return <char>(b12345[i])
61

62

63
@cython.test_assert_path_exists("//PythonCapiCallNode")
64
@cython.test_fail_if_path_exists("//IndexNode",
65
                                 "//CoerceFromPyTypeNode")
66
def index_literal_uchar_cast(int i):
67
    """
68
    >>> index_literal_uchar_cast(0) == ord('1')
69
    True
70
    >>> index_literal_uchar_cast(-5) == ord('1')
71
    True
72
    >>> index_literal_uchar_cast(2) == ord('3')
73
    True
74
    >>> index_literal_uchar_cast(4) == ord('5')
75
    True
76
    >>> index_literal_uchar_cast(6)
77
    Traceback (most recent call last):
78
    IndexError: string index out of range
79
    """
80
    return <unsigned char>(b"12345"[i])
81

82

83
@cython.test_assert_path_exists("//PythonCapiCallNode")
84
@cython.test_fail_if_path_exists("//IndexNode",
85
                                 "//CoerceFromPyTypeNode")
86
def index_nonliteral_uchar_cast(int i):
87
    """
88
    >>> index_nonliteral_uchar_cast(0) == ord('1')
89
    True
90
    >>> index_nonliteral_uchar_cast(-5) == ord('1')
91
    True
92
    >>> index_nonliteral_uchar_cast(2) == ord('3')
93
    True
94
    >>> index_nonliteral_uchar_cast(4) == ord('5')
95
    True
96
    >>> index_nonliteral_uchar_cast(6)
97
    Traceback (most recent call last):
98
    IndexError: string index out of range
99
    """
100
    return <unsigned char>(b12345[i])
101

102

103
@cython.test_assert_path_exists("//PythonCapiCallNode")
104
@cython.test_fail_if_path_exists("//IndexNode",
105
                                 "//CoerceFromPyTypeNode")
106
def index_literal_char_coerce(int i):
107
    """
108
    >>> index_literal_char_coerce(0) == ord('1')
109
    True
110
    >>> index_literal_char_coerce(-5) == ord('1')
111
    True
112
    >>> index_literal_char_coerce(2) == ord('3')
113
    True
114
    >>> index_literal_char_coerce(4) == ord('5')
115
    True
116
    >>> index_literal_char_coerce(6)
117
    Traceback (most recent call last):
118
    IndexError: string index out of range
119
    """
120
    cdef char result = b"12345"[i]
121
    return result
122

123

124
@cython.test_assert_path_exists("//PythonCapiCallNode")
125
@cython.test_fail_if_path_exists("//IndexNode",
126
                                 "//CoerceFromPyTypeNode")
127
def index_nonliteral_char_coerce(int i):
128
    """
129
    >>> index_nonliteral_char_coerce(0) == ord('1')
130
    True
131
    >>> index_nonliteral_char_coerce(-5) == ord('1')
132
    True
133
    >>> index_nonliteral_char_coerce(2) == ord('3')
134
    True
135
    >>> index_nonliteral_char_coerce(4) == ord('5')
136
    True
137
    >>> index_nonliteral_char_coerce(6)
138
    Traceback (most recent call last):
139
    IndexError: string index out of range
140
    """
141
    cdef char result = b12345[i]
142
    return result
143

144

145
@cython.test_assert_path_exists("//PythonCapiCallNode")
146
@cython.test_fail_if_path_exists("//IndexNode",
147
                                 "//CoerceFromPyTypeNode")
148
@cython.boundscheck(False)
149
def index_literal_char_coerce_no_check(int i):
150
    """
151
    >>> index_literal_char_coerce_no_check(0) == ord('1')
152
    True
153
    >>> index_literal_char_coerce_no_check(-5) == ord('1')
154
    True
155
    >>> index_literal_char_coerce_no_check(2) == ord('3')
156
    True
157
    >>> index_literal_char_coerce_no_check(4) == ord('5')
158
    True
159
    """
160
    cdef char result = b"12345"[i]
161
    return result
162

163

164
@cython.test_assert_path_exists("//PythonCapiCallNode")
165
@cython.test_fail_if_path_exists("//IndexNode",
166
                                 "//CoerceFromPyTypeNode")
167
@cython.boundscheck(False)
168
def index_nonliteral_char_coerce_no_check(int i):
169
    """
170
    >>> index_nonliteral_char_coerce_no_check(0) == ord('1')
171
    True
172
    >>> index_nonliteral_char_coerce_no_check(-5) == ord('1')
173
    True
174
    >>> index_nonliteral_char_coerce_no_check(2) == ord('3')
175
    True
176
    >>> index_nonliteral_char_coerce_no_check(4) == ord('5')
177
    True
178
    """
179
    cdef char result = b12345[i]
180
    return result
181

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

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

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

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