Python ctypes program works on 3.2 but incompatiable on 3.4 -
i have program tokenizes thai text calling libtahi c library. programs works ok python 3.2 fails python3.4. idea why fails on 3.4?
please find below program code , outputs both version of python:
#!/usr/bin/python3 # apt-get install libthai0 libthai-doc libthai-dev libthai-data ctypes import * ctypes.util import find_library thai_library = find_library('thai') if not thai_library: raise oserror('cannot find libthai in system') thai = cdll.loadlibrary(thai_library) thai.th_wbrk_line.restype = c_int thai.th_wbrk_line.argtypes = [c_wchar_p,c_wchar_p,c_size_t,c_wchar_p] def whitespace(ain): # expects bytes ain=ain.decode('utf8') aout=whitespace_string(ain) return aout.encode('utf8') def whitespace_string(ain): # expects string # ain='แล้วพบกันใหม่' aout=' '*(2*len(ain)+1) # assume maximum length set of 1 character + white space adelim=' ' asize=len(aout) res=thai.th_wbrk_line(ain,aout,asize,adelim) result=aout[:res] return result """ แล้วพบกันใหม่ means 'see later.' , compound 3 words: แล้ว พบกัน ใหม่ """ if __name__ == "__main__": ain='แล้วพบกันใหม่' aout=whitespace_string(ain) print(ain,'=>',aout) aout=whitespace(ain.encode('utf8')) print(aout.decode('utf8'))
the outputs are: python3.2 tokenization happens:
python3.2 thai_libthai.py แล้วพบกันใหม่ => แล้ว พบ กัน ใหม่ แล้ว พบ กัน ใหม่
with python3.4 result string blank:
python3.4 thai_libthai.py แล้วพบกันใหม่ =>
using aout = (c_wchar * (2 * len(ain) + 1))()
makes code work on both python 3.2 , 3.4. thank eryksun!
Comments
Post a Comment