cython-devel

changeset 2126:d273a3dc784b

support conversion between Python byte strings and unsigned char*
author Stefan Behnel <scoder@users.berlios.de>
date Fri Sep 11 22:59:36 2009 +0200 (3 years ago)
parents ed5b5bbb959e
children ab5bc89815406fb452ff1c142c74ba4db672
files Cython/Compiler/ExprNodes.py Cython/Compiler/PyrexTypes.py tests/run/unsigned_char_ptr_bytes_conversion_T359.pyx
line diff
1.1 --- a/Cython/Compiler/ExprNodes.py Fri Sep 11 21:04:59 2009 +0200 1.2 +++ b/Cython/Compiler/ExprNodes.py Fri Sep 11 22:59:36 2009 +0200 1.3 @@ -928,7 +928,10 @@ 1.4 if dst_type == PyrexTypes.c_char_ptr_type: 1.5 self.type = PyrexTypes.c_char_ptr_type 1.6 return self 1.7 - 1.8 + elif dst_type == PyrexTypes.c_uchar_ptr_type: 1.9 + self.type = PyrexTypes.c_char_ptr_type 1.10 + return CastNode(self, PyrexTypes.c_uchar_ptr_type) 1.11 + 1.12 if dst_type.is_int: 1.13 if not self.type.is_pyobject and len(self.entry.init) == 1: 1.14 return CharNode(self.pos, value=self.value)
2.1 --- a/Cython/Compiler/PyrexTypes.py Fri Sep 11 21:04:59 2009 +0200 2.2 +++ b/Cython/Compiler/PyrexTypes.py Fri Sep 11 22:59:36 2009 +0200 2.3 @@ -1537,6 +1537,18 @@ 2.4 CPtrType.__init__(self, c_char_type) 2.5 2.6 2.7 +class CUCharPtrType(CStringType, CPtrType): 2.8 + # C 'unsigned char *' type. 2.9 + 2.10 + pymemberdef_typecode = "T_STRING" 2.11 + 2.12 + to_py_function = "__Pyx_PyBytes_FromUString" 2.13 + from_py_function = "__Pyx_PyBytes_AsUString" 2.14 + 2.15 + def __init__(self): 2.16 + CPtrType.__init__(self, c_uchar_type) 2.17 + 2.18 + 2.19 class UnspecifiedType(PyrexType): 2.20 # Used as a placeholder until the type can be determined. 2.21 2.22 @@ -1624,6 +1636,7 @@ 2.23 c_null_ptr_type = CNullPtrType(c_void_type) 2.24 c_char_array_type = CCharArrayType(None) 2.25 c_char_ptr_type = CCharPtrType() 2.26 +c_uchar_ptr_type = CUCharPtrType() 2.27 c_utf8_char_array_type = CUTF8CharArrayType(None) 2.28 c_char_ptr_ptr_type = CPtrType(c_char_ptr_type) 2.29 c_int_ptr_type = CPtrType(c_int_type) 2.30 @@ -1768,6 +1781,8 @@ 2.31 # Construct a C pointer type. 2.32 if base_type is c_char_type: 2.33 return c_char_ptr_type 2.34 + elif base_type is c_uchar_type: 2.35 + return c_uchar_ptr_type 2.36 elif base_type is error_type: 2.37 return error_type 2.38 else: 2.39 @@ -1817,6 +1832,9 @@ 2.40 #define __Pyx_PyBytes_AsString PyBytes_AsString 2.41 #endif 2.42 2.43 +#define __Pyx_PyBytes_FromUString(s) __Pyx_PyBytes_FromString((char*)s) 2.44 +#define __Pyx_PyBytes_AsUString(s) ((unsigned char*) __Pyx_PyBytes_AsString(s)) 2.45 + 2.46 #define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False)) 2.47 static INLINE int __Pyx_PyObject_IsTrue(PyObject*); 2.48 static INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x);
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/tests/run/unsigned_char_ptr_bytes_conversion_T359.pyx Fri Sep 11 22:59:36 2009 +0200 3.3 @@ -0,0 +1,18 @@ 3.4 +__doc__ = u""" 3.5 +>>> py_string1.decode('ASCII') == 'test toast taste' 3.6 +True 3.7 +>>> py_string1 == py_string2 == py_string3 3.8 +True 3.9 +""" 3.10 + 3.11 +cdef unsigned char* some_c_unstring = 'test toast taste' 3.12 + 3.13 +py_string1 = some_c_unstring 3.14 + 3.15 +cdef unsigned char* c_unstring_from_py = py_string1 3.16 + 3.17 +py_string2 = c_unstring_from_py 3.18 + 3.19 +cdef char* c_string_from_py = py_string2 3.20 + 3.21 +py_string3 = c_string_from_py