1 2cimport cython 3 4cdef extern from *: 5 cdef Py_ssize_t PY_SSIZE_T_MIN 6 cdef Py_ssize_t PY_SSIZE_T_MAX 7 8 9############################################################ 10# tests for char* slicing 11 12cdef const char* cstring = "abcABCqtp" 13 14@cython.test_assert_path_exists("//PythonCapiCallNode") 15@cython.test_fail_if_path_exists("//AttributeNode") 16def slice_charptr_decode(): 17 """ 18 >>> print(str(slice_charptr_decode()).replace("u'", "'")) 19 ('a', 'abc', 'abcABCqtp') 20 """ 21 return (cstring[:1].decode('UTF-8'), 22 cstring[:3].decode('UTF-8'), 23 cstring[:9].decode('UTF-8')) 24 25@cython.test_assert_path_exists("//PythonCapiCallNode") 26@cython.test_fail_if_path_exists("//AttributeNode") 27def slice_charptr_decode_platform_encoding(): 28 """ 29 >>> print(str(slice_charptr_decode()).replace("u'", "'")) 30 ('a', 'abc', 'abcABCqtp') 31 """ 32 cdef bytes s = u'abcABCqtp'.encode() 33 cdef char* cstr = s 34 return (cstr[:1].decode(), 35 cstr[:3].decode(), 36 cstr[:9].decode()) 37 38@cython.test_assert_path_exists("//PythonCapiCallNode") 39@cython.test_fail_if_path_exists("//AttributeNode") 40def slice_charptr_decode_unknown_encoding(): 41 """ 42 >>> print(str(slice_charptr_decode_unknown_encoding()).replace("u'", "'")) 43 ('abcABCqtp', 'abcABCqtp', 'abc', 'abcABCqt') 44 """ 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)) 51 52@cython.test_assert_path_exists("//PythonCapiCallNode") 53@cython.test_fail_if_path_exists("//AttributeNode") 54def slice_charptr_decode_slice2(): 55 """ 56 >>> print(str(slice_charptr_decode_slice2()).replace("u'", "'")) 57 ('a', 'bc', 'tp') 58 """ 59 return (cstring[0:1].decode('UTF-8'), 60 cstring[1:3].decode('UTF-8'), 61 cstring[7:9].decode('UTF-8')) 62 63@cython.test_assert_path_exists("//PythonCapiCallNode") 64@cython.test_fail_if_path_exists("//AttributeNode") 65def slice_charptr_decode_strlen(): 66 """ 67 >>> print(str(slice_charptr_decode_strlen()).replace("u'", "'")) 68 ('abcABCqtp', 'bcABCqtp', '', 'BCq', 'abcA', '') 69 """ 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')) 76 77@cython.test_assert_path_exists("//PythonCapiCallNode") 78@cython.test_fail_if_path_exists("//AttributeNode") 79def slice_charptr_decode_unbound(): 80 """ 81 >>> print(str(slice_charptr_decode_unbound()).replace("u'", "'")) 82 ('a', 'abc', 'abcABCqtp') 83 """ 84 return (bytes.decode(cstring[:1], 'UTF-8'), 85 bytes.decode(cstring[:3], 'UTF-8', 'replace'), 86 bytes.decode(cstring[:9], 'UTF-8')) 87 88@cython.test_assert_path_exists("//PythonCapiCallNode") 89@cython.test_fail_if_path_exists("//AttributeNode") 90def slice_charptr_decode_errormode(): 91 """ 92 >>> print(str(slice_charptr_decode_errormode()).replace("u'", "'")) 93 ('a', 'abc', 'abcABCqtp') 94 """ 95 return (cstring[:1].decode('UTF-8', 'strict'), 96 cstring[:3].decode('UTF-8', 'replace'), 97 cstring[:9].decode('UTF-8', 'unicode_escape')) 98 99@cython.test_assert_path_exists("//PythonCapiCallNode") 100@cython.test_fail_if_path_exists("//AttributeNode") 101def slice_charptr_dynamic_bounds(): 102 """ 103 >>> print(str(slice_charptr_dynamic_bounds()).replace("u'", "'")) 104 ('abc', 'abc', 'bcAB', 'BCqtp') 105 """ 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')) 110 111@cython.test_assert_path_exists("//PythonCapiCallNode") 112@cython.test_fail_if_path_exists("//AttributeNode") 113def slice_charptr_dynamic_bounds_non_name(): 114 """ 115 >>> print(str(slice_charptr_dynamic_bounds_non_name()).replace("u'", "'")) 116 ('bcA', 'bcA', 'BCqtp', 'ABCqtp', 'bcABCqtp', 'bcABCqtp', 'cABC') 117 """ 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')) 125 126@cython.test_assert_path_exists("//PythonCapiCallNode") 127@cython.test_fail_if_path_exists("//AttributeNode") 128def slice_charptr_decode_large_bounds(): 129 """ 130 >>> print(str(slice_charptr_decode_large_bounds()).replace("u'", "'")) 131 ('abcABCqtp', '', '', '') 132 """ 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')) 137 138 139cdef return1(): return 1 140cdef return3(): return 3 141cdef return4(): return 4 142cdef return5(): return 5 143cdef return9(): return 9 144