diff options
author | Andreas Becker <atayoohoo@googlemail.com> | 2011-05-07 20:35:03 +0100 |
---|---|---|
committer | Michael Meeks <michael.meeks@novell.com> | 2011-05-07 20:35:03 +0100 |
commit | a09ce46818fd4d5e08b3af9a478501cd8ef5b4fe (patch) | |
tree | 187c9164d436201442794dee227627e2b9173124 /pyuno/source/module/pyuno_impl.hxx | |
parent | 7cf799064f5d64bca62626dc73723c2c5e95ca00 (diff) |
Port PyUno to support Python 3
Diffstat (limited to 'pyuno/source/module/pyuno_impl.hxx')
-rw-r--r-- | pyuno/source/module/pyuno_impl.hxx | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/pyuno/source/module/pyuno_impl.hxx b/pyuno/source/module/pyuno_impl.hxx index c1a38b5e06aa..51bc10a3df4d 100644 --- a/pyuno/source/module/pyuno_impl.hxx +++ b/pyuno/source/module/pyuno_impl.hxx @@ -28,6 +28,8 @@ #ifndef _PYUNO_IMPL_ #define _PYUNO_IMPL_ +#include <Python.h> + #include <pyuno/pyuno.hxx> #include <boost/unordered_map.hpp> @@ -48,6 +50,49 @@ #include <cppuhelper/implbase2.hxx> #include <cppuhelper/weakref.hxx> +// 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) +{ + // 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); +} + +inline PyObject* PyString_FromString(const char *string) +{ + return PyUnicode_FromString(string); +} + +inline int PyString_Check(PyObject *object) +{ + return PyBytes_Check(object); +} + +inline Py_ssize_t PyString_Size(PyObject *object) +{ + return PyBytes_Size(object); +} + +inline PyObject* PyString_FromStringAndSize(const char *string, Py_ssize_t len) +{ + return PyBytes_FromStringAndSize(string, len); +} +#endif /* PY_MAJOR_VERSION >= 3 */ + namespace pyuno { @@ -142,9 +187,6 @@ com::sun::star::uno::Any PyObjectToAny (PyObject* o) void raiseInvocationTargetExceptionWhenNeeded( const Runtime &runtime ) throw ( com::sun::star::reflection::InvocationTargetException ); -// bool CheckPyObjectTypes (PyObject* o, Sequence<Type> types); -// bool CheckPyObjectType (PyObject* o, Type type); //Only check 1 object. - com::sun::star::uno::TypeClass StringToTypeClass (char* string); PyRef PyUNO_callable_new ( |