From edd36b28d2d70031642e780e6e05962430a60378 Mon Sep 17 00:00:00 2001 From: Brian Cole Date: Fri, 6 Jun 2014 13:58:45 -0600 Subject: Automatically coerce python 2.x unicode objects into UTF8 when passing to underlying code. --- Lib/python/pystrings.swg | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'Lib') 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; -- cgit v1.2.1