4
cdef bytes b12345 = b'12345'
6
def index_literal(int i):
8
Python 3 returns integer values on indexing, Py2 returns byte
11
>>> index_literal(0) in (ord('1'), '1')
13
>>> index_literal(-5) in (ord('1'), '1')
15
>>> index_literal(2) in (ord('3'), '3')
17
>>> index_literal(4) in (ord('5'), '5')
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):
28
>>> index_literal_char_cast(0) == ord('1')
30
>>> index_literal_char_cast(-5) == ord('1')
32
>>> index_literal_char_cast(2) == ord('3')
34
>>> index_literal_char_cast(4) == ord('5')
36
>>> index_literal_char_cast(6)
37
Traceback (most recent call last):
38
IndexError: string index out of range
40
return <char>(b"12345"[i])
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):
48
>>> index_nonliteral_char_cast(0) == ord('1')
50
>>> index_nonliteral_char_cast(-5) == ord('1')
52
>>> index_nonliteral_char_cast(2) == ord('3')
54
>>> index_nonliteral_char_cast(4) == ord('5')
56
>>> index_nonliteral_char_cast(6)
57
Traceback (most recent call last):
58
IndexError: string index out of range
60
return <char>(b12345[i])
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):
68
>>> index_literal_uchar_cast(0) == ord('1')
70
>>> index_literal_uchar_cast(-5) == ord('1')
72
>>> index_literal_uchar_cast(2) == ord('3')
74
>>> index_literal_uchar_cast(4) == ord('5')
76
>>> index_literal_uchar_cast(6)
77
Traceback (most recent call last):
78
IndexError: string index out of range
80
return <unsigned char>(b"12345"[i])
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):
88
>>> index_nonliteral_uchar_cast(0) == ord('1')
90
>>> index_nonliteral_uchar_cast(-5) == ord('1')
92
>>> index_nonliteral_uchar_cast(2) == ord('3')
94
>>> index_nonliteral_uchar_cast(4) == ord('5')
96
>>> index_nonliteral_uchar_cast(6)
97
Traceback (most recent call last):
98
IndexError: string index out of range
100
return <unsigned char>(b12345[i])
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):
108
>>> index_literal_char_coerce(0) == ord('1')
110
>>> index_literal_char_coerce(-5) == ord('1')
112
>>> index_literal_char_coerce(2) == ord('3')
114
>>> index_literal_char_coerce(4) == ord('5')
116
>>> index_literal_char_coerce(6)
117
Traceback (most recent call last):
118
IndexError: string index out of range
120
cdef char result = b"12345"[i]
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):
129
>>> index_nonliteral_char_coerce(0) == ord('1')
131
>>> index_nonliteral_char_coerce(-5) == ord('1')
133
>>> index_nonliteral_char_coerce(2) == ord('3')
135
>>> index_nonliteral_char_coerce(4) == ord('5')
137
>>> index_nonliteral_char_coerce(6)
138
Traceback (most recent call last):
139
IndexError: string index out of range
141
cdef char result = b12345[i]
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):
151
>>> index_literal_char_coerce_no_check(0) == ord('1')
153
>>> index_literal_char_coerce_no_check(-5) == ord('1')
155
>>> index_literal_char_coerce_no_check(2) == ord('3')
157
>>> index_literal_char_coerce_no_check(4) == ord('5')
160
cdef char result = b"12345"[i]
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):
170
>>> index_nonliteral_char_coerce_no_check(0) == ord('1')
172
>>> index_nonliteral_char_coerce_no_check(-5) == ord('1')
174
>>> index_nonliteral_char_coerce_no_check(2) == ord('3')
176
>>> index_nonliteral_char_coerce_no_check(4) == ord('5')
179
cdef char result = b12345[i]