Add support for Python Bytes/Unicode distinction

This commit is contained in:
Alec Cooper 2016-01-27 19:45:37 -05:00
commit 33edfa4a62
2 changed files with 24 additions and 6 deletions

View file

@ -6,13 +6,18 @@ SWIGINTERN int
SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc) SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc)
{ {
%#if PY_VERSION_HEX>=0x03000000 %#if PY_VERSION_HEX>=0x03000000
%#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
if (PyBytes_Check(obj))
%#else
if (PyUnicode_Check(obj)) if (PyUnicode_Check(obj))
%#endif
%#else %#else
if (PyString_Check(obj)) if (PyString_Check(obj))
%#endif %#endif
{ {
char *cstr; Py_ssize_t len; char *cstr; Py_ssize_t len;
%#if PY_VERSION_HEX>=0x03000000 %#if PY_VERSION_HEX>=0x03000000
%#if !defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
if (!alloc && cptr) { if (!alloc && cptr) {
/* We can't allow converting without allocation, since the internal /* We can't allow converting without allocation, since the internal
representation of string in Python 3 is UCS-2/UCS-4 but we require representation of string in Python 3 is UCS-2/UCS-4 but we require
@ -21,8 +26,9 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc)
return SWIG_RuntimeError; return SWIG_RuntimeError;
} }
obj = PyUnicode_AsUTF8String(obj); obj = PyUnicode_AsUTF8String(obj);
PyBytes_AsStringAndSize(obj, &cstr, &len);
if(alloc) *alloc = SWIG_NEWOBJ; if(alloc) *alloc = SWIG_NEWOBJ;
%#endif
PyBytes_AsStringAndSize(obj, &cstr, &len);
%#else %#else
PyString_AsStringAndSize(obj, &cstr, &len); PyString_AsStringAndSize(obj, &cstr, &len);
%#endif %#endif
@ -50,19 +56,27 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc)
*alloc = SWIG_OLDOBJ; *alloc = SWIG_OLDOBJ;
} }
} else { } else {
%#if PY_VERSION_HEX>=0x03000000 %#if PY_VERSION_HEX>=0x03000000
assert(0); /* Should never reach here in Python 3 */ %#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
%#endif *cptr = PyBytes_AsString(obj);
%#else
assert(0); /* Should never reach here with Unicode strings in Python 3 */
%#endif
%#else
*cptr = SWIG_Python_str_AsChar(obj); *cptr = SWIG_Python_str_AsChar(obj);
%#endif
} }
} }
if (psize) *psize = len + 1; if (psize) *psize = len + 1;
%#if PY_VERSION_HEX>=0x03000000 %#if PY_VERSION_HEX>=0x03000000 && !defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
Py_XDECREF(obj); Py_XDECREF(obj);
%#endif %#endif
return SWIG_OK; return SWIG_OK;
} else { } else {
%#if defined(SWIG_PYTHON_2_UNICODE) %#if defined(SWIG_PYTHON_2_UNICODE)
%#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
%#error "Cannot use both SWIG_PYTHON_2_UNICODE and SWIG_PYTHON_STRICT_BYTE_CHAR at once"
%#endif
%#if PY_VERSION_HEX<0x03000000 %#if PY_VERSION_HEX<0x03000000
if (PyUnicode_Check(obj)) { if (PyUnicode_Check(obj)) {
char *cstr; Py_ssize_t len; char *cstr; Py_ssize_t len;
@ -112,11 +126,15 @@ SWIG_FromCharPtrAndSize(const char* carray, size_t size)
SWIG_InternalNewPointerObj(%const_cast(carray,char *), pchar_descriptor, 0) : SWIG_Py_Void(); SWIG_InternalNewPointerObj(%const_cast(carray,char *), pchar_descriptor, 0) : SWIG_Py_Void();
} else { } else {
%#if PY_VERSION_HEX >= 0x03000000 %#if PY_VERSION_HEX >= 0x03000000
%#if defined(SWIG_PYTHON_STRICT_BYTE_CHAR)
return PyBytes_FromStringAndSize(carray, %numeric_cast(size, Py_ssize_t));
%#else
%#if PY_VERSION_HEX >= 0x03010000 %#if PY_VERSION_HEX >= 0x03010000
return PyUnicode_DecodeUTF8(carray, %numeric_cast(size, Py_ssize_t), "surrogateescape"); return PyUnicode_DecodeUTF8(carray, %numeric_cast(size, Py_ssize_t), "surrogateescape");
%#else %#else
return PyUnicode_FromStringAndSize(carray, %numeric_cast(size, Py_ssize_t)); return PyUnicode_FromStringAndSize(carray, %numeric_cast(size, Py_ssize_t));
%#endif %#endif
%#endif
%#else %#else
return PyString_FromStringAndSize(carray, %numeric_cast(size, Py_ssize_t)); return PyString_FromStringAndSize(carray, %numeric_cast(size, Py_ssize_t));
%#endif %#endif

View file

@ -16,7 +16,7 @@ SWIG_AsWCharPtrAndSize(PyObject *obj, wchar_t **cptr, size_t *psize, int *alloc)
{ {
PyObject *tmp = 0; PyObject *tmp = 0;
int isunicode = PyUnicode_Check(obj); int isunicode = PyUnicode_Check(obj);
%#if PY_VERSION_HEX < 0x03000000 %#if PY_VERSION_HEX < 0x03000000 && !defined(SWIG_PYTHON_STRICT_UNICODE_WCHAR)
if (!isunicode && PyString_Check(obj)) { if (!isunicode && PyString_Check(obj)) {
obj = tmp = PyUnicode_FromObject(obj); obj = tmp = PyUnicode_FromObject(obj);
isunicode = 1; isunicode = 1;