From 63abe9c5c350b80da556e8b946fa440b784d0a91 Mon Sep 17 00:00:00 2001 From: Lisandro Dalcin Date: Fri, 16 Apr 2010 10:31:25 -0300 Subject: [PATCH] define PyBytes_XXX for Py<2.6 and use these through the codebase --- Cython/Compiler/ExprNodes.py | 6 +++--- Cython/Compiler/ModuleNode.py | 22 ++++++++++++++++++++-- Cython/Compiler/Optimize.py | 2 +- Cython/Compiler/PyrexTypes.py | 20 ++++---------------- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 4f104a9c..dda284c7 100755 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -2262,14 +2262,14 @@ class SliceIndexNode(ExprNode): if self.base.type.is_string: if self.stop is None: code.putln( - "%s = __Pyx_PyBytes_FromString(%s + %s); %s" % ( + "%s = PyBytes_FromString(%s + %s); %s" % ( self.result(), self.base.result(), self.start_code(), code.error_goto_if_null(self.result(), self.pos))) else: code.putln( - "%s = __Pyx_PyBytes_FromStringAndSize(%s + %s, %s - %s); %s" % ( + "%s = PyBytes_FromStringAndSize(%s + %s, %s - %s); %s" % ( self.result(), self.base.result(), self.start_code(), @@ -6107,7 +6107,7 @@ class CoerceToBooleanNode(CoercionNode): _special_builtins = { Builtin.list_type : 'PyList_GET_SIZE', Builtin.tuple_type : 'PyTuple_GET_SIZE', - Builtin.bytes_type : '__Pyx_PyBytes_GET_SIZE', + Builtin.bytes_type : 'PyBytes_GET_SIZE', Builtin.unicode_type : 'PyUnicode_GET_SIZE', } diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index 9ef17f20..641c0805 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -522,11 +522,29 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): #if PY_MAJOR_VERSION >= 3 #define PyBaseString_Type PyUnicode_Type + #define PyStringObject PyUnicodeObject #define PyString_Type PyUnicode_Type + #define PyString_Check PyUnicode_Check #define PyString_CheckExact PyUnicode_CheckExact -#else +#endif + +#if PY_VERSION_HEX < 0x02060000 + #define PyBytesObject PyStringObject #define PyBytes_Type PyString_Type + #define PyBytes_Check PyString_Check #define PyBytes_CheckExact PyString_CheckExact + #define PyBytes_FromString PyString_FromString + #define PyBytes_FromStringAndSize PyString_FromStringAndSize + #define PyBytes_FromFormat PyString_FromFormat + #define PyBytes_DecodeEscape PyString_DecodeEscape + #define PyBytes_AsString PyString_AsString + #define PyBytes_AsStringAndSize PyString_AsStringAndSize + #define PyBytes_Size PyString_Size + #define PyBytes_AS_STRING PyString_AS_STRING + #define PyBytes_GET_SIZE PyString_GET_SIZE + #define PyBytes_Repr PyString_Repr + #define PyBytes_Concat PyString_Concat + #define PyBytes_ConcatAndDel PyString_ConcatAndDel #endif #if PY_MAJOR_VERSION >= 3 @@ -1663,7 +1681,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): self.generate_filename_init_call(code) code.putln("%s = PyTuple_New(0); %s" % (Naming.empty_tuple, code.error_goto_if_null(Naming.empty_tuple, self.pos))); - code.putln("%s = __Pyx_PyBytes_FromStringAndSize(\"\", 0); %s" % (Naming.empty_bytes, code.error_goto_if_null(Naming.empty_bytes, self.pos))); + code.putln("%s = PyBytes_FromStringAndSize(\"\", 0); %s" % (Naming.empty_bytes, code.error_goto_if_null(Naming.empty_bytes, self.pos))); code.putln("/*--- Library function declarations ---*/") env.generate_library_function_declarations(code) diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index 625f10e1..69a8ebf9 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -1359,7 +1359,7 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): _map_to_capi_len_function = { Builtin.unicode_type : "PyUnicode_GET_SIZE", Builtin.str_type : "Py_SIZE", # works in Py2 and Py3 - Builtin.bytes_type : "__Pyx_PyBytes_GET_SIZE", + Builtin.bytes_type : "PyBytes_GET_SIZE", Builtin.list_type : "PyList_GET_SIZE", Builtin.tuple_type : "PyTuple_GET_SIZE", Builtin.dict_type : "PyDict_Size", diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index 26e54343..6f29bbf9 100755 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -1989,8 +1989,8 @@ class CStringType(object): is_string = 1 is_unicode = 0 - to_py_function = "__Pyx_PyBytes_FromString" - from_py_function = "__Pyx_PyBytes_AsString" + to_py_function = "PyBytes_FromString" + from_py_function = "PyBytes_AsString" exception_value = "NULL" def literal_code(self, value): @@ -2455,20 +2455,8 @@ def typecast(to_type, from_type, expr_code): type_conversion_predeclarations = """ /* Type Conversion Predeclarations */ -#if PY_MAJOR_VERSION < 3 -#define __Pyx_PyBytes_FromString PyString_FromString -#define __Pyx_PyBytes_FromStringAndSize PyString_FromStringAndSize -#define __Pyx_PyBytes_AsString PyString_AsString -#define __Pyx_PyBytes_GET_SIZE PyString_GET_SIZE -#else -#define __Pyx_PyBytes_FromString PyBytes_FromString -#define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize -#define __Pyx_PyBytes_AsString PyBytes_AsString -#define __Pyx_PyBytes_GET_SIZE PyBytes_GET_SIZE -#endif - -#define __Pyx_PyBytes_FromUString(s) __Pyx_PyBytes_FromString((char*)s) -#define __Pyx_PyBytes_AsUString(s) ((unsigned char*) __Pyx_PyBytes_AsString(s)) +#define __Pyx_PyBytes_FromUString(s) PyBytes_FromString((char*)s) +#define __Pyx_PyBytes_AsUString(s) ((unsigned char*) PyBytes_AsString(s)) #define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False)) static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*); -- 2.26.2