Automatically coerce python 2.x unicode objects into UTF8 when passing to underlying code.

This commit is contained in:
Brian Cole 2014-06-06 13:58:45 -06:00 committed by William S Fulton
commit edd36b28d2

View file

@ -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;