1 2cimport cython 3from cython cimport typeof 4 5from libc.string cimport const_char, const_uchar 6 7@cython.test_assert_path_exists( 8 "//NameNode[@name = 'st' and @type.is_string = True]", 9 "//NameNode[@name = 'ust' and @type.is_string = True]", 10 "//NameNode[@name = 'my_st' and @type.is_string = True]", 11 "//NameNode[@name = 'my_ust' and @type.is_string = True]", 12 ) 13def const_charptrs(): 14 """ 15 >>> const_charptrs() 16 """ 17 cdef object obj 18 cdef const_char* st = b'XYZ' 19 cdef const_uchar* ust = <unsigned char*>b'XYZ' # needs cast to unsigned 20 21 assert typeof(st) == "const_char *", typeof(st) 22 my_st = st 23 assert typeof(my_st) == "const_char *", typeof(my_st) 24 obj = my_st 25 assert obj == b'XYZ', obj 26 27 assert typeof(ust) == "const_uchar *", typeof(ust) 28 my_ust = ust 29 assert typeof(my_ust) == "const_uchar *", typeof(my_ust) 30 obj = my_ust 31 assert obj == b'XYZ', obj 32 33ctypedef char mychar 34ctypedef unsigned char myuchar 35 36def const_char_arrays(): 37 """ 38 >>> const_char_arrays() 39 """ 40 cdef int i 41 cdef object obj 42 cdef mychar[4] st 43 cdef myuchar[4] ust 44 cdef char ch 45 46 i = 0 47 for ch in b'XYZ\0': 48 st[i] = ch 49 ust[i] = <unsigned char>ch 50 i += 1 51 52 assert typeof(st) == "mychar [4]", typeof(st) 53 obj = st 54 assert obj == b'XYZ', obj 55 56 assert typeof(ust) == "myuchar [4]", typeof(ust) 57 obj = ust 58 assert obj == b'XYZ', obj 59