From f1d9c4afc368756d01e4a84ef95e97c118db9603 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Fri, 30 May 2008 07:31:17 +0200 Subject: [PATCH] use PyBytes_*() functions instead of PyString_*() in Py3 --- Cython/Compiler/Nodes.py | 11 ++++++++--- Cython/Compiler/PyrexTypes.py | 12 ++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index db1370a0..b6460f94 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -4238,7 +4238,11 @@ static void __Pyx_AddTraceback(char *funcname) { if (!py_funcname) goto bad; py_globals = PyModule_GetDict(%(GLOBALS)s); if (!py_globals) goto bad; + #if PY_VERSION_MAJOR < 3 empty_string = PyString_FromStringAndSize("", 0); + #else + empty_string = PyBytes_FromStringAndSize("", 0); + #endif if (!empty_string) goto bad; py_code = PyCode_New( 0, /*int argcount,*/ @@ -4351,17 +4355,18 @@ static int __Pyx_InitStrings(__Pyx_StringTabEntry *t) { *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL); } else if (t->intern) { *t->p = PyString_InternFromString(t->s); + } else { + *t->p = PyString_FromStringAndSize(t->s, t->n - 1); } #else /* Python 3+ has unicode identifiers */ if (t->is_identifier || (t->is_unicode && t->intern)) { *t->p = PyUnicode_InternFromString(t->s); } else if (t->is_unicode) { *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1); + } else { + *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1); } #endif - else { - *t->p = PyString_FromStringAndSize(t->s, t->n - 1); - } if (!*t->p) return -1; ++t; diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index b7950da7..310251c7 100644 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -929,8 +929,8 @@ class CStringType: is_string = 1 is_unicode = 0 - to_py_function = "PyString_FromString" - from_py_function = "PyString_AsString" + to_py_function = "__Pyx_PyBytes_FromString" + from_py_function = "__Pyx_PyBytes_AsString" exception_value = "NULL" def literal_code(self, value): @@ -1172,6 +1172,14 @@ def typecast(to_type, from_type, expr_code): type_conversion_predeclarations = """ /* Type Conversion Predeclarations */ +#if PY_VERSION_MAJOR < 3 +#define __Pyx_PyBytes_FromString PyString_FromString +#define __Pyx_PyBytes_AsString PyString_AsString +#else +#define __Pyx_PyBytes_FromString PyBytes_FromString +#define __Pyx_PyBytes_AsString PyBytes_AsString +#endif + #define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False)) static INLINE int __Pyx_PyObject_IsTrue(PyObject* x); static INLINE PY_LONG_LONG __pyx_PyInt_AsLongLong(PyObject* x); -- 2.26.2