summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorBrian Cole <coleb@eyesopen.com>2014-06-06 13:58:45 -0600
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2015-12-19 03:44:27 +0000
commitedd36b28d2d70031642e780e6e05962430a60378 (patch)
tree83b0aa572f546e6bd725867622b67612af537a10 /Lib
parent0ae5bfa6e21add4a06068e48cc2f74462c34e1a7 (diff)
downloadswig-edd36b28d2d70031642e780e6e05962430a60378.tar.gz
Automatically coerce python 2.x unicode objects into UTF8 when passing to underlying code.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/python/pystrings.swg18
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;