[Pyrex] Pyrex & Unicode

Alexander Belchenko bialix at ukr.net
Sat Nov 10 09:00:23 CET 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Farzin Shakib пишет:
> I am really struggling with with Pyrex and unicode.
> 
> How do I pass a unicode string to a pyrex function and within pyrex
> convert it to a char* so I can pass it to a C function, and vice versa?

It's incorrect asumption that you could convert unicode string to char*.
Instead of PyString_FromStringAndSize you should use unicode Python API:
http://docs.python.org/api/unicodeObjects.html

> 
> Consider the following simple Pyrex code
> 
> cdef extern from "string.h":
> int strlen( char* s )
> cdef extern from "Python.h":
> object PyString_FromStringAndSize( char* s, int len )
> cdef extern from "util.h":
> void utilPutString( char* file, char* text )
> char* utilGetString( char* file )
> 
> def putStr( char* file, char* text ):
> utilPutString( file, text )
> 
> def getStr( char* file ):
> cdef char* text
> cdef int len
> text = utilGetString( file )
> len = strlen( text )
> retText = PyString_FromStringAndSize( text, len )
> return( retText )
> 
> Now the following works
> 
> text = u'apple'
> putStr( 'file.txt', text )
> text2 = getStr( 'file.txt' )
> if text == text2: print "PASSED"
> else: print "FAILED"
> 
> as long as text is an ASCII. But if text is a real unicode, it does not;
> such as:
> 
> text = u'??'
> putStr( 'file.txt', text )
> text2 = getStr( 'file.txt' )
> if text == text2: print "PASSED"
> else: print "FAILED"
> 
> I get
> 
> Traceback (most recent call last):
> File "test.py", line 5, in ?
> putStr( 'file.txt', text )
> File "test.pyx", line 9, in test.putStr
> utilPutString( file, text )
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 
> 0-1: ordinal not in range(128)
> 
> 
> How do I modify the above pyrex to make this work?
> 
> Thanks
> Farzin

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHNWUXzYr338mxwCURAukSAJoCCd8Xw77C2LPfwBW9QIl2d84PwQCeJCEG
zKLoC5UqQZ6UQoSqbkWQiTc=
=Zu5r
-----END PGP SIGNATURE-----




More information about the Pyrex mailing list