From 4f044dcbe400f1a5cade25550980b9c002bb37c3 Mon Sep 17 00:00:00 2001 From: Yorik van Havre Date: Sat, 6 May 2017 20:10:53 +0200 Subject: [PATCH] py3: Gui: files P-Z ported to python3 --- src/Gui/Selection.cpp | 4 ++++ src/Gui/ViewProviderPyImp.cpp | 4 ++++ src/Gui/WidgetFactory.cpp | 44 ++++++++++++++++++++++++++++++++--- src/Gui/WorkbenchPyImp.cpp | 4 ++++ 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/Gui/Selection.cpp b/src/Gui/Selection.cpp index 3e9bac496959..a631be65249a 100644 --- a/src/Gui/Selection.cpp +++ b/src/Gui/Selection.cpp @@ -1186,7 +1186,11 @@ PyObject *SelectionSingleton::sCountObjectsOfType(PyObject * /*self*/, PyObject return NULL; unsigned int count = Selection().countObjectsOfType(objecttype, document); +#if PY_MAJOR_VERSION < 3 return PyInt_FromLong(count); +#else + return PyLong_FromLong(count); +#endif } PyObject *SelectionSingleton::sGetSelection(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/) diff --git a/src/Gui/ViewProviderPyImp.cpp b/src/Gui/ViewProviderPyImp.cpp index 478f765c7327..9a7cfd78e1d7 100644 --- a/src/Gui/ViewProviderPyImp.cpp +++ b/src/Gui/ViewProviderPyImp.cpp @@ -181,7 +181,11 @@ PyObject* ViewProviderPy::listDisplayModes(PyObject *args) int i=0; for ( std::vector::iterator it = modes.begin(); it != modes.end(); ++it ) { +#if PY_MAJOR_VERSION >= 3 + PyObject* str = PyUnicode_FromString(it->c_str()); +#else PyObject* str = PyString_FromString(it->c_str()); +#endif PyList_SetItem(pyList, i++, str); } diff --git a/src/Gui/WidgetFactory.cpp b/src/Gui/WidgetFactory.cpp index 08c061320d54..5aee4d0ed2aa 100644 --- a/src/Gui/WidgetFactory.cpp +++ b/src/Gui/WidgetFactory.cpp @@ -219,14 +219,25 @@ bool PythonWrapper::toCString(const Py::Object& pyobject, std::string& str) { if (PyUnicode_Check(pyobject.ptr())) { PyObject* unicode = PyUnicode_AsUTF8String(pyobject.ptr()); +#if PY_MAJOR_VERSION >= 3 + str = PyBytes_AsString(unicode); +#else str = PyString_AsString(unicode); +#endif Py_DECREF(unicode); return true; } +#if PY_MAJOR_VERSION >= 3 + else if (PyBytes_Check(pyobject.ptr())) { + str = PyBytes_AsString(pyobject.ptr()); + return true; + } +#else else if (PyString_Check(pyobject.ptr())) { str = PyString_AsString(pyobject.ptr()); return true; } +#endif #if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE) if (Shiboken::String::check(pyobject.ptr())) { const char* s = Shiboken::String::toCString(pyobject.ptr()); @@ -853,12 +864,16 @@ Py::Object UiLoaderPy::createWidget(const Py::Tuple& args) // 1st argument Py::String str(args[0]); std::string className; +#if PY_MAJOR_VERSION >= 3 + className = str.as_std_string("utf-8"); +#else if (str.isUnicode()) { className = str.as_std_string("utf-8"); } else { className = (std::string)str; } +#endif // 2nd argument QWidget* parent = 0; if (wrap.loadCoreModule() && args.size() > 1) { @@ -871,12 +886,16 @@ Py::Object UiLoaderPy::createWidget(const Py::Tuple& args) std::string objectName; if (args.size() > 2) { Py::String str(args[2]); +#if PY_MAJOR_VERSION >= 3 + objectName = str.as_std_string("utf-8"); +#else if (str.isUnicode()) { objectName = str.as_std_string("utf-8"); } else { objectName = (std::string)str; } +#endif } QWidget* widget = loader.createWidget(QString::fromLatin1(className.c_str()), parent, @@ -1328,13 +1347,25 @@ Py::Object PyResource::setValue(const Py::Tuple& args) throw Py::Exception(); QVariant v; - if (PyString_Check(psValue)) { + if (PyUnicode_Check(psValue)) { +#if PY_MAJOR_VERSION >= 3 + v = QString::fromUtf8(PyUnicode_AsUTF8(psValue)); +#else + PyObject* unicode = PyUnicode_AsUTF8String(psValue); + v = QString::fromUtf8(PyString_AsString(unicode)); + Py_DECREF(unicode); + } + else if (PyString_Check(psValue)) { v = QString::fromLatin1(PyString_AsString(psValue)); +#endif + } +#if PY_MAJOR_VERSION < 3 else if (PyInt_Check(psValue)) { int val = PyInt_AsLong(psValue); v = val; } +#endif else if (PyLong_Check(psValue)) { unsigned int val = PyLong_AsLong(psValue); v = val; @@ -1347,11 +1378,18 @@ Py::Object PyResource::setValue(const Py::Tuple& args) int nSize = PyList_Size(psValue); for (int i=0; i= 3 + if (!PyUnicode_Check(item)) +#else if (!PyString_Check(item)) +#endif continue; - +#if PY_MAJOR_VERSION >= 3 + char* pItem = PyUnicode_AsUTF8(item); +#else char* pItem = PyString_AsString(item); - str.append(QString::fromLatin1(pItem)); +#endif + str.append(QString::fromUtf8(pItem)); } v = str; diff --git a/src/Gui/WorkbenchPyImp.cpp b/src/Gui/WorkbenchPyImp.cpp index 78f0acf2da5d..1fa913e8c3a2 100644 --- a/src/Gui/WorkbenchPyImp.cpp +++ b/src/Gui/WorkbenchPyImp.cpp @@ -56,7 +56,11 @@ PyObject* WorkbenchPy::name(PyObject *args) PY_TRY { std::string name = getWorkbenchPtr()->name(); +#if PY_MAJOR_VERSION >= 3 + PyObject* pyName = PyUnicode_FromString(name.c_str()); +#else PyObject* pyName = PyString_FromString(name.c_str()); +#endif return pyName; }PY_CATCH; }