Skip to content

Commit

Permalink
py3: Gui: files P-Z ported to python3
Browse files Browse the repository at this point in the history
  • Loading branch information
yorikvanhavre authored and looooo committed May 6, 2017
1 parent a3539c5 commit 4f044dc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Gui/Selection.cpp
Expand Up @@ -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*/)
Expand Down
4 changes: 4 additions & 0 deletions src/Gui/ViewProviderPyImp.cpp
Expand Up @@ -181,7 +181,11 @@ PyObject* ViewProviderPy::listDisplayModes(PyObject *args)
int i=0;

for ( std::vector<std::string>::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);
}

Expand Down
44 changes: 41 additions & 3 deletions src/Gui/WidgetFactory.cpp
Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -1347,11 +1378,18 @@ Py::Object PyResource::setValue(const Py::Tuple& args)
int nSize = PyList_Size(psValue);
for (int i=0; i<nSize;++i) {
PyObject* item = PyList_GetItem(psValue, i);
#if PY_MAJOR_VERSION >= 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;
Expand Down
4 changes: 4 additions & 0 deletions src/Gui/WorkbenchPyImp.cpp
Expand Up @@ -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;
}
Expand Down

0 comments on commit 4f044dc

Please sign in to comment.