diff options
author | Michael Stahl <mstahl@redhat.com> | 2012-11-25 15:59:18 +0100 |
---|---|---|
committer | Michael Stahl <mstahl@redhat.com> | 2012-11-25 16:06:40 +0100 |
commit | af2b7fac27aa812229c6611fd35a77aa51b290f2 (patch) | |
tree | 517f99b3235c50a1ff046438b7344d7ad70861eb /pyuno/source/module/pyuno_util.cxx | |
parent | a38b59265c08276fce6d73ce541cadb41aa6d347 (diff) |
pyuno: fix handling of "str", "unicode", "bytes" types:
Replace currrent wrappers of Python 2 only PyString_* functions with
better abstractions that handle default "str" (PyStr_*) or byte strings
("str"/"bytes" depending on version, PyStrBytes_*) and adjust all
invocations to work on appropriate string types.
Fixes obvious "attributes typeName and/or value of uno.Enum are not
strings" exceptions with Python 3.
Change-Id: I255dcb1bc198fd7f6a62b83b957901521071a480
Diffstat (limited to 'pyuno/source/module/pyuno_util.cxx')
-rw-r--r-- | pyuno/source/module/pyuno_util.cxx | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/pyuno/source/module/pyuno_util.cxx b/pyuno/source/module/pyuno_util.cxx index 88136b312adb..9f058ef3f35e 100644 --- a/pyuno/source/module/pyuno_util.cxx +++ b/pyuno/source/module/pyuno_util.cxx @@ -69,7 +69,7 @@ PyRef ustring2PyUnicode( const OUString & str ) PyRef ustring2PyString( const OUString &str ) { OString o = OUStringToOString( str, osl_getThreadTextEncoding() ); - return PyRef( PyString_FromString( o.getStr() ), SAL_NO_ACQUIRE ); + return PyRef( PyStr_FromString( o.getStr() ), SAL_NO_ACQUIRE ); } OUString pyString2ustring( PyObject *pystr ) @@ -80,14 +80,24 @@ OUString pyString2ustring( PyObject *pystr ) #if Py_UNICODE_SIZE == 2 ret = OUString( (sal_Unicode * ) PyUnicode_AS_UNICODE( pystr ) ); #else +#if PY_MAJOR_VERSION >= 3 + Py_ssize_t size(0); + char *pUtf8(PyUnicode_AsUTF8AndSize(pystr, &size)); + ret = OUString(pUtf8, size, RTL_TEXTENCODING_UTF8); +#else PyObject* pUtf8 = PyUnicode_AsUTF8String(pystr); - ret = OUString(PyString_AsString(pUtf8), PyString_Size(pUtf8), RTL_TEXTENCODING_UTF8); + ret = OUString(PyStr_AsString(pUtf8), PyStr_Size(pUtf8), RTL_TEXTENCODING_UTF8); Py_DECREF(pUtf8); #endif +#endif } else { - char *name = PyString_AsString(pystr ); +#if PY_MAJOR_VERSION >= 3 + char *name = PyBytes_AsString(pystr); // hmmm... is this a good idea? +#else + char *name = PyString_AsString(pystr); +#endif ret = OUString( name, strlen(name), osl_getThreadTextEncoding() ); } return ret; |