summaryrefslogtreecommitdiff
path: root/pyuno/source/module/pyuno_impl.hxx
diff options
context:
space:
mode:
authorMichael Stahl <mstahl@redhat.com>2012-11-25 15:59:18 +0100
committerMichael Stahl <mstahl@redhat.com>2012-11-25 16:06:40 +0100
commitaf2b7fac27aa812229c6611fd35a77aa51b290f2 (patch)
tree517f99b3235c50a1ff046438b7344d7ad70861eb /pyuno/source/module/pyuno_impl.hxx
parenta38b59265c08276fce6d73ce541cadb41aa6d347 (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_impl.hxx')
-rw-r--r--pyuno/source/module/pyuno_impl.hxx76
1 files changed, 55 insertions, 21 deletions
diff --git a/pyuno/source/module/pyuno_impl.hxx b/pyuno/source/module/pyuno_impl.hxx
index 601673d06b11..e794c9d40738 100644
--- a/pyuno/source/module/pyuno_impl.hxx
+++ b/pyuno/source/module/pyuno_impl.hxx
@@ -49,44 +49,78 @@
// In Python 3, the PyString_* functions have been replaced by PyBytes_*
// and PyUnicode_* functions.
#if PY_MAJOR_VERSION >= 3
-inline char* PyString_AsString(PyObject *object)
+
+// compatibility wrappers for Python "str" type (PyUnicode in 3, PyString in 2)
+inline PyObject* PyStr_FromString(const char *string)
{
- // check whether object is already of type "PyBytes"
- if(PyBytes_Check(object))
- {
- return PyBytes_AsString(object);
- }
-
- // object is not encoded yet, so encode it to utf-8
- PyObject *pystring;
- pystring = PyUnicode_AsUTF8String(object);
- if(!pystring)
- {
- PyErr_SetString(PyExc_ValueError, "cannot utf-8 decode string");
- return 0;
- }
- return PyBytes_AsString(pystring);
+ return PyUnicode_FromString(string);
}
-inline PyObject* PyString_FromString(const char *string)
+inline char * PyStr_AsString(PyObject *object)
{
- return PyUnicode_FromString(string);
+ return PyUnicode_AsUTF8(object);
}
-inline int PyString_Check(PyObject *object)
+inline int PyStr_Check(PyObject *object)
+{
+ return PyUnicode_Check(object);
+}
+
+// compatibility wrappers for Python non-Unicode string/buffer type
+// (PyBytes in 3, PyString in 2)
+inline int PyStrBytes_Check(PyObject *object)
{
return PyBytes_Check(object);
}
-inline Py_ssize_t PyString_Size(PyObject *object)
+inline char* PyStrBytes_AsString(PyObject *object)
+{
+ return PyBytes_AsString(object);
+}
+
+inline Py_ssize_t PyStrBytes_Size(PyObject *object)
{
return PyBytes_Size(object);
}
-inline PyObject* PyString_FromStringAndSize(const char *string, Py_ssize_t len)
+inline PyObject* PyStrBytes_FromStringAndSize(const char *string, Py_ssize_t len)
{
return PyBytes_FromStringAndSize(string, len);
}
+#else
+inline char * PyStr_AsString(PyObject *object)
+{
+ return PyString_AsString(object);
+}
+
+inline PyObject* PyStr_FromString(const char *string)
+{
+ return PyString_FromString(string);
+}
+
+inline int PyStr_Check(PyObject *object)
+{
+ return PyString_Check(object);
+}
+inline int PyStrBytes_Check(PyObject *object)
+{
+ return PyString_Check(object);
+}
+
+inline char* PyStrBytes_AsString(PyObject *object)
+{
+ return PyString_AsString(object);
+}
+
+inline Py_ssize_t PyStrBytes_Size(PyObject *object)
+{
+ return PyString_Size(object);
+}
+
+inline PyObject* PyStrBytes_FromStringAndSize(const char *string, Py_ssize_t len)
+{
+ return PyString_FromStringAndSize(string, len);
+}
#endif /* PY_MAJOR_VERSION >= 3 */
namespace pyuno