5
const Py_ssize_t PY_SSIZE_T_MIN
6
const Py_ssize_t PY_SSIZE_T_MAX
9
############################################################
10
# tests for char* slicing
12
cdef const char* cstring = "abcABCqtp"
14
@cython.test_assert_path_exists("//PythonCapiCallNode")
15
@cython.test_fail_if_path_exists("//AttributeNode")
16
def slice_charptr_decode():
18
>>> print(str(slice_charptr_decode()).replace("u'", "'"))
19
('a', 'abc', 'abcABCqtp')
21
return (cstring[:1].decode('UTF-8'),
22
cstring[:3].decode('UTF-8'),
23
cstring[:9].decode('UTF-8'))
25
@cython.test_assert_path_exists("//PythonCapiCallNode")
26
@cython.test_fail_if_path_exists("//AttributeNode")
27
def slice_charptr_decode_platform_encoding():
29
>>> print(str(slice_charptr_decode()).replace("u'", "'"))
30
('a', 'abc', 'abcABCqtp')
32
cdef bytes s = u'abcABCqtp'.encode()
34
return (cstr[:1].decode(),
38
@cython.test_assert_path_exists("//PythonCapiCallNode")
39
@cython.test_fail_if_path_exists("//AttributeNode")
40
def slice_charptr_decode_unknown_encoding():
42
>>> print(str(slice_charptr_decode_unknown_encoding()).replace("u'", "'"))
43
('abcABCqtp', 'abcABCqtp', 'abc', 'abcABCqt')
45
cdef const char* enc = 'UTF-8'
46
cdef const char* error_handling = 'strict'
47
return (cstring.decode(enc),
48
cstring.decode(enc, error_handling),
49
cstring[:3].decode(enc),
50
cstring[:8].decode(enc, error_handling))
52
@cython.test_assert_path_exists("//PythonCapiCallNode")
53
@cython.test_fail_if_path_exists("//AttributeNode")
54
def slice_charptr_decode_slice2():
56
>>> print(str(slice_charptr_decode_slice2()).replace("u'", "'"))
59
return (cstring[0:1].decode('UTF-8'),
60
cstring[1:3].decode('UTF-8'),
61
cstring[7:9].decode('UTF-8'))
63
@cython.test_assert_path_exists("//PythonCapiCallNode")
64
@cython.test_fail_if_path_exists("//AttributeNode")
65
def slice_charptr_decode_strlen():
67
>>> print(str(slice_charptr_decode_strlen()).replace("u'", "'"))
68
('abcABCqtp', 'bcABCqtp', '', 'BCq', 'abcA', '')
70
return (cstring.decode('UTF-8'),
71
cstring[1:].decode('UTF-8'),
72
cstring[9:].decode('UTF-8'),
73
cstring[-5:-2].decode('UTF-8'),
74
cstring[:-5].decode('UTF-8'),
75
cstring[:-9].decode('UTF-8'))
77
@cython.test_assert_path_exists("//PythonCapiCallNode")
78
@cython.test_fail_if_path_exists("//AttributeNode")
79
def slice_charptr_decode_unbound():
81
>>> print(str(slice_charptr_decode_unbound()).replace("u'", "'"))
82
('a', 'abc', 'abcABCqtp')
84
return (bytes.decode(cstring[:1], 'UTF-8'),
85
bytes.decode(cstring[:3], 'UTF-8', 'replace'),
86
bytes.decode(cstring[:9], 'UTF-8'))
88
@cython.test_assert_path_exists("//PythonCapiCallNode")
89
@cython.test_fail_if_path_exists("//AttributeNode")
90
def slice_charptr_decode_errormode():
92
>>> print(str(slice_charptr_decode_errormode()).replace("u'", "'"))
93
('a', 'abc', 'abcABCqtp')
95
return (cstring[:1].decode('UTF-8', 'strict'),
96
cstring[:3].decode('UTF-8', 'replace'),
97
cstring[:9].decode('UTF-8', 'unicode_escape'))
99
@cython.test_assert_path_exists("//PythonCapiCallNode")
100
@cython.test_fail_if_path_exists("//AttributeNode")
101
def slice_charptr_dynamic_bounds():
103
>>> print(str(slice_charptr_dynamic_bounds()).replace("u'", "'"))
104
('abc', 'abc', 'bcAB', 'BCqtp')
106
return (cstring[:return3()].decode('UTF-8'),
107
cstring[0:return3()].decode('UTF-8'),
108
cstring[return1():return5()].decode('UTF-8'),
109
cstring[return4():return9()].decode('UTF-8'))
111
@cython.test_assert_path_exists("//PythonCapiCallNode")
112
@cython.test_fail_if_path_exists("//AttributeNode")
113
def slice_charptr_dynamic_bounds_non_name():
115
>>> print(str(slice_charptr_dynamic_bounds_non_name()).replace("u'", "'"))
116
('bcA', 'bcA', 'BCqtp', 'ABCqtp', 'bcABCqtp', 'bcABCqtp', 'cABC')
118
return ((cstring+1)[:return3()].decode('UTF-8'),
119
(cstring+1)[0:return3()].decode('UTF-8'),
120
(cstring+1)[return3():].decode('UTF-8'),
121
(cstring+1)[2:].decode('UTF-8'),
122
(cstring+1)[0:].decode('UTF-8'),
123
(cstring+1)[:].decode('UTF-8'),
124
(cstring+1)[return1():return5()].decode('UTF-8'))
126
@cython.test_assert_path_exists("//PythonCapiCallNode")
127
@cython.test_fail_if_path_exists("//AttributeNode")
128
def slice_charptr_decode_large_bounds():
130
>>> print(str(slice_charptr_decode_large_bounds()).replace("u'", "'"))
131
('abcABCqtp', '', '', '')
133
return (cstring[PY_SSIZE_T_MIN:9].decode('UTF-8'),
134
cstring[PY_SSIZE_T_MAX:PY_SSIZE_T_MIN].decode('UTF-8'),
135
cstring[PY_SSIZE_T_MIN:PY_SSIZE_T_MIN].decode('UTF-8'),
136
cstring[PY_SSIZE_T_MAX:PY_SSIZE_T_MAX].decode('UTF-8'))
139
cdef return1(): return 1
140
cdef return3(): return 3
141
cdef return4(): return 4
142
cdef return5(): return 5
143
cdef return9(): return 9