diff options
author | Brian Cole <coleb@eyesopen.com> | 2014-06-06 13:58:45 -0600 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2015-12-19 03:44:27 +0000 |
commit | edd36b28d2d70031642e780e6e05962430a60378 (patch) | |
tree | 83b0aa572f546e6bd725867622b67612af537a10 | |
parent | 0ae5bfa6e21add4a06068e48cc2f74462c34e1a7 (diff) | |
download | swig-edd36b28d2d70031642e780e6e05962430a60378.tar.gz |
Automatically coerce python 2.x unicode objects into UTF8 when passing to underlying code.
-rw-r--r-- | Lib/python/pystrings.swg | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/python/pystrings.swg b/Lib/python/pystrings.swg index 2eefaefea..813895e4c 100644 --- a/Lib/python/pystrings.swg +++ b/Lib/python/pystrings.swg @@ -63,6 +63,24 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc) %#endif return SWIG_OK; } else { +%#if PY_VERSION_HEX<0x03000000 + if (PyUnicode_Check(obj)) { + char *cstr; Py_ssize_t len; + obj = PyUnicode_AsUTF8String(obj); + if (PyString_AsStringAndSize(obj, &cstr, &len) == -1) { + Py_XDECREF(obj); + return SWIG_TypeError; + } + + if (alloc) *alloc = SWIG_NEWOBJ; + if (psize) *psize = len + 1; + *cptr = %new_copy_array(cstr, len + 1, char); + + Py_XDECREF(obj); + return SWIG_OK; + } +%#endif + swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); if (pchar_descriptor) { void* vptr = 0; |