Skip to content

Commit

Permalink
Implement unicode/bytes utility routines in Cython code
Browse files Browse the repository at this point in the history
  • Loading branch information
dalcinl committed Jun 15, 2016
1 parent 0e8efee commit f8277ae
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/MPI/MPI.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include "atimport.pxi"
bootstrap()
initialize()

include "asmpistr.pxi"
include "asstring.pxi"
include "asbuffer.pxi"
include "asmemory.pxi"
include "asarray.pxi"
Expand Down
6 changes: 3 additions & 3 deletions src/MPI/asbuffer.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <object>view.obj
cdef str format = None
Expand All @@ -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

#------------------------------------------------------------------------------
Expand Down
22 changes: 0 additions & 22 deletions src/MPI/asmpistr.pxi

This file was deleted.

35 changes: 35 additions & 0 deletions src/MPI/asstring.pxi
Original file line number Diff line number Diff line change
@@ -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)

#------------------------------------------------------------------------------
2 changes: 1 addition & 1 deletion src/MPI/typestr.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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

# -----------------------------------------------------------------------------

Expand Down
35 changes: 0 additions & 35 deletions src/atimport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f8277ae

Please sign in to comment.