From f8277aec7f912449e2a508e5f8be4f4a3fa8a5dd Mon Sep 17 00:00:00 2001 From: Lisandro Dalcin Date: Wed, 15 Jun 2016 16:07:52 +0300 Subject: [PATCH] Implement unicode/bytes utility routines in Cython code --- src/MPI/MPI.pyx | 2 +- src/MPI/asbuffer.pxi | 6 +++--- src/MPI/asmpistr.pxi | 22 ---------------------- src/MPI/asstring.pxi | 35 +++++++++++++++++++++++++++++++++++ src/MPI/typestr.pxi | 2 +- src/atimport.h | 35 ----------------------------------- 6 files changed, 40 insertions(+), 62 deletions(-) delete mode 100644 src/MPI/asmpistr.pxi create mode 100644 src/MPI/asstring.pxi diff --git a/src/MPI/MPI.pyx b/src/MPI/MPI.pyx index 06480bdf3..60a7cd9a3 100644 --- a/src/MPI/MPI.pyx +++ b/src/MPI/MPI.pyx @@ -10,7 +10,7 @@ include "atimport.pxi" bootstrap() initialize() -include "asmpistr.pxi" +include "asstring.pxi" include "asbuffer.pxi" include "asmemory.pxi" include "asarray.pxi" diff --git a/src/MPI/asbuffer.pxi b/src/MPI/asbuffer.pxi index fe0480ce4..bc360613f 100644 --- a/src/MPI/asbuffer.pxi +++ b/src/MPI/asbuffer.pxi @@ -300,13 +300,13 @@ cdef inline object getformat(memory buf): # if view.obj == NULL: if view.format != NULL: - return mpistr(view.format) + return pystr(view.format) else: return "B" elif view.format != NULL: # XXX this is a hack if view.format != BYTE_FMT: - return mpistr(view.format) + return pystr(view.format) # cdef object ob = view.obj cdef str format = None @@ -317,7 +317,7 @@ cdef inline object getformat(memory buf): format = ob.typecode except (AttributeError, TypeError): if view.format != NULL: - format = mpistr(view.format) + format = pystr(view.format) return format #------------------------------------------------------------------------------ diff --git a/src/MPI/asmpistr.pxi b/src/MPI/asmpistr.pxi deleted file mode 100644 index 1b9bccd5d..000000000 --- a/src/MPI/asmpistr.pxi +++ /dev/null @@ -1,22 +0,0 @@ -#------------------------------------------------------------------------------ - -cdef extern from *: - object PyMPIString_AsStringAndSize(object,const char*[],Py_ssize_t*) - object PyMPIString_FromString(const char[]) - object PyMPIString_FromStringAndSize(const char[],Py_ssize_t) - -#------------------------------------------------------------------------------ - -cdef inline object asmpistr(object ob, char *s[]): - cdef const char *buf = NULL - ob = PyMPIString_AsStringAndSize(ob, &buf, NULL) - if s != NULL: s[0] = buf - return ob - -cdef inline object tompistr(const char s[], int n): - return PyMPIString_FromStringAndSize(s, n) - -cdef inline object mpistr(const char s[]): - return PyMPIString_FromString(s) - -#------------------------------------------------------------------------------ diff --git a/src/MPI/asstring.pxi b/src/MPI/asstring.pxi new file mode 100644 index 000000000..4a81762b2 --- /dev/null +++ b/src/MPI/asstring.pxi @@ -0,0 +1,35 @@ +#------------------------------------------------------------------------------ + +cdef extern from *: + enum: PY3 "(PY_MAJOR_VERSION>=3)" + int PyUnicode_Check(object) + object PyUnicode_AsUTF8String(object) + object PyUnicode_AsASCIIString(object) + object PyUnicode_FromString(const char[]) + object PyUnicode_FromStringAndSize(const char[],Py_ssize_t) + object PyBytes_FromString(const char[]) + object PyBytes_FromStringAndSize(const char[],Py_ssize_t) + int PyBytes_AsStringAndSize(object,char*[],Py_ssize_t*) except -1 + +#------------------------------------------------------------------------------ + +cdef inline object asmpistr(object ob, char *s[]): + if PyUnicode_Check(ob): + if PY3: ob = PyUnicode_AsUTF8String(ob); + else: ob = PyUnicode_AsASCIIString(ob); + PyBytes_AsStringAndSize(ob, s, NULL) + return ob + +cdef inline object tompistr(const char s[], int n): + if PY3: return PyUnicode_FromStringAndSize(s, n) + else: return PyBytes_FromStringAndSize(s, n) + +cdef inline object mpistr(const char s[]): + if PY3: return PyUnicode_FromString(s) + else: return PyBytes_FromString(s) + +cdef inline object pystr(const char s[]): + if PY3: return PyUnicode_FromString(s) + else: return PyBytes_FromString(s) + +#------------------------------------------------------------------------------ diff --git a/src/MPI/typestr.pxi b/src/MPI/typestr.pxi index c793a4086..321738759 100644 --- a/src/MPI/typestr.pxi +++ b/src/MPI/typestr.pxi @@ -5,7 +5,7 @@ def _typecode(Datatype datatype not None): Map MPI datatype to typecode string """ cdef char *tc = Datatype2String(datatype.ob_mpi) - return mpistr(tc) if tc != NULL else None + return pystr(tc) if tc != NULL else None # ----------------------------------------------------------------------------- diff --git a/src/atimport.h b/src/atimport.h index d310ee9d8..4a6bbbdf8 100644 --- a/src/atimport.h +++ b/src/atimport.h @@ -42,41 +42,6 @@ /* ------------------------------------------------------------------------- */ -static PyObject * -PyMPIString_AsStringAndSize(PyObject *ob, const char **s, Py_ssize_t *n) -{ - PyObject *b = NULL; - if (PyUnicode_Check(ob)) { -#if PY_MAJOR_VERSION >= 3 - b = PyUnicode_AsUTF8String(ob); -#else - b = PyUnicode_AsASCIIString(ob); -#endif - if (!b) return NULL; - } else { - b = ob; Py_INCREF(ob); - } -#if PY_MAJOR_VERSION >= 3 - if (PyBytes_AsStringAndSize(b, (char **)s, n) < 0) { -#else - if (PyString_AsStringAndSize(b, (char **)s, n) < 0) { -#endif - Py_DECREF(b); - return NULL; - } - return b; -} - -#if PY_MAJOR_VERSION >= 3 -#define PyMPIString_FromString PyUnicode_FromString -#define PyMPIString_FromStringAndSize PyUnicode_FromStringAndSize -#else -#define PyMPIString_FromString PyString_FromString -#define PyMPIString_FromStringAndSize PyString_FromStringAndSize -#endif - -/* ------------------------------------------------------------------------- */ - /* Local variables: c-basic-offset: 2