From 7e39fed86260a2807e2e7a41b9afc47bee53f234 Mon Sep 17 00:00:00 2001 From: Yorik van Havre Date: Fri, 5 May 2017 19:33:03 +0200 Subject: [PATCH] py3: Gui: files A-P ported to python3 --- src/Gui/Application.cpp | 39 +++++++- src/Gui/ApplicationPy.cpp | 12 +++ src/Gui/CallTips.cpp | 6 ++ src/Gui/Command.cpp | 26 ++++- src/Gui/FreeCADGuiInit.py | 6 ++ src/Gui/OnlineDocumentation.cpp | 8 ++ src/Gui/PythonConsole.cpp | 17 ++++ src/Gui/PythonConsolePy.cpp | 16 +++ src/Gui/PythonDebugger.cpp | 8 ++ src/Gui/PythonWorkbenchPyImp.cpp | 161 +++++++++++++++++++++++++++---- 10 files changed, 274 insertions(+), 25 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index 245d2f72f5..2de7dce685 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -212,7 +212,11 @@ FreeCADGui_getSoDBVersion(PyObject * /*self*/, PyObject *args) { if (!PyArg_ParseTuple(args, "")) return NULL; +#if PY_MAJOR_VERSION >= 3 + return PyUnicode_FromString(SoDB::getVersion()); +#else return PyString_FromString(SoDB::getVersion()); +#endif } struct PyMethodDef FreeCADGui_methods[] = { @@ -281,7 +285,8 @@ Application::Application(bool GUIenabled) // setting up Python binding Base::PyGILStateLocker lock; - PyObject* module = Py_InitModule3("FreeCADGui", Application::Methods, + + PyDoc_STRVAR(FreeCADGui_doc, "The functions in the FreeCADGui module allow working with GUI documents,\n" "view providers, views, workbenches and much more.\n\n" "The FreeCADGui instance provides a list of references of GUI documents which\n" @@ -289,7 +294,25 @@ Application::Application(bool GUIenabled) "objects in the associated App document. An App and GUI document can be\n" "accessed with the same name.\n\n" "The FreeCADGui module also provides a set of functions to work with so called\n" - "workbenches."); + "workbenches." + ); + +#if PY_MAJOR_VERSION >= 3 + // if this returns a valid pointer then the 'FreeCADGui' Python module was loaded, + // otherwise the executable was launched + PyObject *module = PyImport_AddModule("FreeCADGui"); + if (!module) { + static struct PyModuleDef FreeCADGuiModuleDef = {PyModuleDef_HEAD_INIT,"FreeCADGui", FreeCADGui_doc, -1, Application::Methods}; + module = PyModule_Create(&FreeCADGuiModuleDef); + _PyImport_FixupBuiltin(module, "FreeCADGui"); + } + else { + // extend the method list + PyModule_AddFunctions(module, Application::Methods); + } +#else + PyObject* module = Py_InitModule3("FreeCADGui", Application::Methods, FreeCADGui_doc); +#endif Py::Module(module).setAttr(std::string("ActiveDocument"),Py::None()); UiLoaderPy::init_type(); @@ -303,8 +326,12 @@ Application::Application(bool GUIenabled) PyModule_AddObject(module, "PySideUic", pySide->module().ptr()); //insert Selection module - PyObject* pSelectionModule = Py_InitModule3("Selection", SelectionSingleton::Methods, - "Selection module"); +#if PY_MAJOR_VERSION >= 3 + static struct PyModuleDef SelectionModuleDef = {PyModuleDef_HEAD_INIT,"Selection", "Selection module", -1, SelectionSingleton::Methods}; + PyObject* pSelectionModule = PyModule_Create(&SelectionModuleDef); +#else + PyObject* pSelectionModule = Py_InitModule3("Selection", SelectionSingleton::Methods,"Selection module"); +#endif Py_INCREF(pSelectionModule); PyModule_AddObject(module, "Selection", pSelectionModule); @@ -1204,7 +1231,11 @@ QStringList Application::workbenches(void) const // insert all items while (PyDict_Next(_pcWorkbenchDictionary, &pos, &key, &value)) { /* do something interesting with the values... */ +#if PY_MAJOR_VERSION >= 3 + const char* wbName = PyUnicode_AsUTF8(key); +#else const char* wbName = PyString_AsString(key); +#endif // add only allowed workbenches bool ok = true; if (!extra.isEmpty()&&ok) { diff --git a/src/Gui/ApplicationPy.cpp b/src/Gui/ApplicationPy.cpp index e5a284da66..fcaa2378d0 100644 --- a/src/Gui/ApplicationPy.cpp +++ b/src/Gui/ApplicationPy.cpp @@ -616,7 +616,11 @@ PyObject* Application::sGetLocale(PyObject * /*self*/, PyObject *args,PyObject * return NULL; // NULL triggers exception std::string locale = Translator::instance()->activeLanguage(); +#if PY_MAJOR_VERSION >= 3 + return PyUnicode_FromString(locale.c_str()); +#else return PyString_FromString(locale.c_str()); +#endif } PyObject* Application::sCreateDialog(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/) @@ -658,7 +662,11 @@ PyObject* Application::sAddPreferencePage(PyObject * /*self*/, PyObject *args,Py PyObject* dlg; // old style classes +#if PY_MAJOR_VERSION >= 3 + if (PyArg_ParseTuple(args, "O!s", &PyType_Type, &dlg, &grp)) { +#else if (PyArg_ParseTuple(args, "O!s", &PyClass_Type, &dlg, &grp)) { +#endif // add to the preferences dialog new PrefPagePyProducer(Py::Object(dlg), grp); @@ -1046,7 +1054,11 @@ PyObject* Application::sListCommands(PyObject * /*self*/, PyObject *args,PyObjec PyObject* pyList = PyList_New(cmds.size()); int i=0; for ( std::vector::iterator it = cmds.begin(); it != cmds.end(); ++it ) { +#if PY_MAJOR_VERSION >= 3 + PyObject* str = PyUnicode_FromString((*it)->getName()); +#else PyObject* str = PyString_FromString((*it)->getName()); +#endif PyList_SetItem(pyList, i++, str); } return pyList; diff --git a/src/Gui/CallTips.cpp b/src/Gui/CallTips.cpp index f76dc9cc5b..b95fae82b4 100644 --- a/src/Gui/CallTips.cpp +++ b/src/Gui/CallTips.cpp @@ -234,7 +234,11 @@ QMap CallTipsList::extractTips(const QString& context) const PyObject* eval = 0; if (PyCode_Check(code)) { +#if PY_MAJOR_VERSION >= 3 + eval = PyEval_EvalCode(code, dict.ptr(), dict.ptr()); +#else eval = PyEval_EvalCode(reinterpret_cast(code), dict.ptr(), dict.ptr()); +#endif } Py_DECREF(code); if (!eval) { @@ -270,12 +274,14 @@ QMap CallTipsList::extractTips(const QString& context) const else if (PyObject_IsSubclass(type.ptr(), typeobj.o) == 1) { obj = type; } +#if PY_MAJOR_VERSION < 3 else if (PyInstance_Check(obj.ptr())) { // instances of old style classes PyInstanceObject* inst = reinterpret_cast(obj.ptr()); PyObject* classobj = reinterpret_cast(inst->in_class); obj = Py::Object(classobj); } +#endif else if (PyObject_IsInstance(obj.ptr(), basetype.o) == 1) { // New style class which can be a module, type, list, tuple, int, float, ... // Make sure it's not a type objec diff --git a/src/Gui/Command.cpp b/src/Gui/Command.cpp index 732834da60..d5f0332e69 100644 --- a/src/Gui/Command.cpp +++ b/src/Gui/Command.cpp @@ -920,12 +920,19 @@ const char* PythonCommand::getResource(const char* sName) const pcTemp = PyDict_GetItemString(_pcPyResourceDict,sName); if (!pcTemp) return ""; +#if PY_MAJOR_VERSION >= 3 + if (!PyUnicode_Check(pcTemp)) { +#else if (!PyString_Check(pcTemp)) { +#endif throw Base::TypeError("PythonCommand::getResource(): Method GetResources() of the Python " "command object returns a dictionary which holds not only strings"); } - +#if PY_MAJOR_VERSION >= 3 + return PyUnicode_AsUTF8(pcTemp); +#else return PyString_AsString(pcTemp); +#endif } void PythonCommand::activated(int iMsg) @@ -988,9 +995,17 @@ const char* PythonCommand::getHelpUrl(void) const pcTemp = Interpreter().runMethodObject(_pcPyCommand, "CmdHelpURL"); if (! pcTemp ) return ""; +#if PY_MAJOR_VERSION >= 3 + if (! PyUnicode_Check(pcTemp) ) +#else if (! PyString_Check(pcTemp) ) +#endif throw Base::TypeError("PythonCommand::CmdHelpURL(): Method CmdHelpURL() of the Python command object returns no string"); +#if PY_MAJOR_VERSION >= 3 + return PyUnicode_AsUTF8(pcTemp); +#else return PyString_AsString(pcTemp); +#endif } Action * PythonCommand::createAction(void) @@ -1298,12 +1313,19 @@ const char* PythonGroupCommand::getResource(const char* sName) const pcTemp = PyDict_GetItemString(_pcPyResource, sName); if (!pcTemp) return ""; +#if PY_MAJOR_VERSION >= 3 + if (!PyUnicode_Check(pcTemp)) { +#else if (!PyString_Check(pcTemp)) { +#endif throw Base::ValueError("PythonGroupCommand::getResource(): Method GetResources() of the Python " "group command object returns a dictionary which holds not only strings"); } - +#if PY_MAJOR_VERSION >= 3 + return PyUnicode_AsUTF8(pcTemp); +#else return PyString_AsString(pcTemp); +#endif } const char* PythonGroupCommand::getWhatsThis() const diff --git a/src/Gui/FreeCADGuiInit.py b/src/Gui/FreeCADGuiInit.py index b5e9409618..55ab05c48b 100644 --- a/src/Gui/FreeCADGuiInit.py +++ b/src/Gui/FreeCADGuiInit.py @@ -99,6 +99,12 @@ class NoneWorkbench ( Workbench ): def InitApplications(): import sys,os,traceback + try: + # Python3 + import io as cStringIO + except ImportError: + # Python2 + import cStringIO # Searching modules dirs +++++++++++++++++++++++++++++++++++++++++++++++++++ # (additional module paths are already cached) ModDirs = FreeCAD.__ModDirs__ diff --git a/src/Gui/OnlineDocumentation.cpp b/src/Gui/OnlineDocumentation.cpp index c918188e12..e87642b229 100644 --- a/src/Gui/OnlineDocumentation.cpp +++ b/src/Gui/OnlineDocumentation.cpp @@ -151,7 +151,11 @@ QByteArray PythonOnlineHelp::loadResource(const QString& filename) const if (result) { Py_DECREF(result); result = PyDict_GetItemString(dict, "htmldocument"); +#if PY_MAJOR_VERSION >= 3 + const char* contents = PyUnicode_AsUTF8(result); +#else const char* contents = PyString_AsString(result); +#endif res.append("HTTP/1.0 200 OK\n"); res.append("Content-type: text/html\n"); res.append(contents); @@ -182,7 +186,11 @@ QByteArray PythonOnlineHelp::loadResource(const QString& filename) const if (result) { Py_DECREF(result); result = PyDict_GetItemString(dict, "page"); +#if PY_MAJOR_VERSION >= 3 + const char* page = PyUnicode_AsUTF8(result); +#else const char* page = PyString_AsString(result); +#endif res.append("HTTP/1.0 200 OK\n"); res.append("Content-type: text/html\n"); res.append(page); diff --git a/src/Gui/PythonConsole.cpp b/src/Gui/PythonConsole.cpp index 31991b9c06..6403f8fe23 100644 --- a/src/Gui/PythonConsole.cpp +++ b/src/Gui/PythonConsole.cpp @@ -165,9 +165,17 @@ void InteractiveInterpreter::setPrompt() Base::PyGILStateLocker lock; d->sysmodule = PyImport_ImportModule("sys"); if (!PyObject_HasAttrString(d->sysmodule, "ps1")) +#if PY_MAJOR_VERSION >= 3 + PyObject_SetAttrString(d->sysmodule, "ps1", PyUnicode_FromString(">>> ")); +#else PyObject_SetAttrString(d->sysmodule, "ps1", PyString_FromString(">>> ")); +#endif if (!PyObject_HasAttrString(d->sysmodule, "ps2")) +#if PY_MAJOR_VERSION >= 3 + PyObject_SetAttrString(d->sysmodule, "ps2", PyUnicode_FromString("... ")); +#else PyObject_SetAttrString(d->sysmodule, "ps2", PyString_FromString("... ")); +#endif } /** @@ -301,7 +309,11 @@ void InteractiveInterpreter::runCode(PyCodeObject* code) const throw Base::PyException(); /* not incref'd */ // It seems that the return value is always 'None' or Null +#if PY_MAJOR_VERSION >= 3 + presult = PyEval_EvalCode((PyObject*)code, dict, dict); /* run compiled bytecode */ +#else presult = PyEval_EvalCode(code, dict, dict); /* run compiled bytecode */ +#endif Py_XDECREF(code); /* decref the code object */ if (!presult) { if (PyErr_ExceptionMatches(PyExc_SystemExit)) { @@ -414,8 +426,13 @@ PythonConsole::PythonConsole(QWidget *parent) d->_stdin = PySys_GetObject("stdin"); PySys_SetObject("stdin", d->_stdinPy); +#if PY_MAJOR_VERSION >= 3 + const char* version = PyUnicode_AsUTF8(PySys_GetObject("version")); + const char* platform = PyUnicode_AsUTF8(PySys_GetObject("platform")); +#else const char* version = PyString_AsString(PySys_GetObject("version")); const char* platform = PyString_AsString(PySys_GetObject("platform")); +#endif d->info = QString::fromLatin1("Python %1 on %2\n" "Type 'help', 'copyright', 'credits' or 'license' for more information.") .arg(QString::fromLatin1(version)).arg(QString::fromLatin1(platform)); diff --git a/src/Gui/PythonConsolePy.cpp b/src/Gui/PythonConsolePy.cpp index 8e08a37961..216aedbc8a 100644 --- a/src/Gui/PythonConsolePy.cpp +++ b/src/Gui/PythonConsolePy.cpp @@ -81,7 +81,11 @@ Py::Object PythonStdout::write(const Py::Tuple& args) if (PyUnicode_Check(output.ptr())) { PyObject* unicode = PyUnicode_AsEncodedObject(output.ptr(), "utf-8", "strict"); if (unicode) { +#if PY_MAJOR_VERSION >= 3 + const char* string = PyBytes_AsString(unicode); +#else const char* string = PyString_AsString(unicode); +#endif int maxlen = qstrlen(string) > 10000 ? 10000 : -1; pyConsole->insertPythonOutput(QString::fromUtf8(string, maxlen)); Py_DECREF(unicode); @@ -152,7 +156,11 @@ Py::Object PythonStderr::write(const Py::Tuple& args) if (PyUnicode_Check(output.ptr())) { PyObject* unicode = PyUnicode_AsEncodedObject(output.ptr(), "utf-8", "strict"); if (unicode) { +#if PY_MAJOR_VERSION >= 3 + const char* string = PyBytes_AsString(unicode); +#else const char* string = PyString_AsString(unicode); +#endif int maxlen = qstrlen(string) > 10000 ? 10000 : -1; pyConsole->insertPythonError(QString::fromUtf8(string, maxlen)); Py_DECREF(unicode); @@ -222,7 +230,11 @@ Py::Object OutputStdout::write(const Py::Tuple& args) if (PyUnicode_Check(output.ptr())) { PyObject* unicode = PyUnicode_AsEncodedObject(output.ptr(), "utf-8", "strict"); if (unicode) { +#if PY_MAJOR_VERSION >= 3 + const char* string = PyBytes_AsString(unicode); +#else const char* string = PyString_AsString(unicode); +#endif Base::Console().Message("%s",string); Py_DECREF(unicode); } @@ -290,7 +302,11 @@ Py::Object OutputStderr::write(const Py::Tuple& args) if (PyUnicode_Check(output.ptr())) { PyObject* unicode = PyUnicode_AsEncodedObject(output.ptr(), "utf-8", "strict"); if (unicode) { +#if PY_MAJOR_VERSION >= 3 + const char* string = PyBytes_AsString(unicode); +#else const char* string = PyString_AsString(unicode); +#endif Base::Console().Error("%s",string); Py_DECREF(unicode); } diff --git a/src/Gui/PythonDebugger.cpp b/src/Gui/PythonDebugger.cpp index 703d0a6cee..796251741f 100644 --- a/src/Gui/PythonDebugger.cpp +++ b/src/Gui/PythonDebugger.cpp @@ -437,7 +437,11 @@ void PythonDebugger::runFile(const QString& fn) dict = PyModule_GetDict(module); dict = PyDict_Copy(dict); if (PyDict_GetItemString(dict, "__file__") == NULL) { +#if PY_MAJOR_VERSION >= 3 + PyObject *f = PyUnicode_FromString((const char*)pxFileName); +#else PyObject *f = PyString_FromString((const char*)pxFileName); +#endif if (f == NULL) { fclose(fp); return; @@ -576,7 +580,11 @@ int PythonDebugger::tracer_callback(PyObject *obj, PyFrameObject *frame, int wha //no = frame->f_tstate->recursion_depth; //std::string funcname = PyString_AsString(frame->f_code->co_name); +#if PY_MAJOR_VERSION >= 3 + QString file = QString::fromUtf8(PyUnicode_AsUTF8(frame->f_code->co_filename)); +#else QString file = QString::fromUtf8(PyString_AsString(frame->f_code->co_filename)); +#endif switch (what) { case PyTrace_CALL: self->depth++; diff --git a/src/Gui/PythonWorkbenchPyImp.cpp b/src/Gui/PythonWorkbenchPyImp.cpp index bb1b954f9e..9317dddb41 100644 --- a/src/Gui/PythonWorkbenchPyImp.cpp +++ b/src/Gui/PythonWorkbenchPyImp.cpp @@ -62,15 +62,37 @@ PyObject* PythonWorkbenchPy::appendMenu(PyObject *args) int nDepth = PyList_Size(pPath); for (int j=0; j= 3 + char* pItem = PyUnicode_AsUTF8(item); + path.push_back(pItem); +#else + PyObject* unicode = PyUnicode_AsEncodedString(item, "utf-8", 0); + char* pItem = PyString_AsString(unicode); + Py_DECREF(unicode); + path.push_back(pItem); + } else if (PyString_Check(item)) { + char* pItem = PyString_AsString(item); + path.push_back(pItem); +#endif + } else { continue; - char* pItem = PyString_AsString(item); - path.push_back(pItem); + } } + } else if (PyUnicode_Check(pPath)) { +#if PY_MAJOR_VERSION >= 3 + char* pItem = PyUnicode_AsUTF8(pPath); + path.push_back(pItem); +#else + PyObject* unicode = PyUnicode_AsEncodedString(pPath, "utf-8", 0); + char* pItem = PyString_AsString(unicode); + Py_DECREF(unicode); + path.push_back(pItem); } else if (PyString_Check(pPath)) { // one single item char* pItem = PyString_AsString(pPath); path.push_back(pItem); +#endif } else { PyErr_SetString(PyExc_AssertionError, "Expected either a string or a stringlist as first argument"); return NULL; // NULL triggers exception @@ -82,15 +104,37 @@ PyObject* PythonWorkbenchPy::appendMenu(PyObject *args) int nItems = PyList_Size(pItems); for (int i=0; i= 3 + char* pItem = PyUnicode_AsUTF8(item); + items.push_back(pItem); +#else + PyObject* unicode = PyUnicode_AsEncodedString(item, "utf-8", 0); + char* pItem = PyString_AsString(unicode); + Py_DECREF(unicode); + items.push_back(pItem); + } else if (PyString_Check(item)) { + char* pItem = PyString_AsString(item); + items.push_back(pItem); +#endif + } else { continue; - char* pItem = PyString_AsString(item); - items.push_back(pItem); + } } + } else if (PyUnicode_Check(pItems)) { +#if PY_MAJOR_VERSION >= 3 + char* pItem = PyUnicode_AsUTF8(pItems); + items.push_back(pItem); +#else + PyObject* unicode = PyUnicode_AsEncodedString(pItems, "utf-8", 0); + char* pItem = PyString_AsString(unicode); + Py_DECREF(unicode); + items.push_back(pItem); } else if (PyString_Check(pItems)) { // one single item char* pItem = PyString_AsString(pItems); items.push_back(pItem); +#endif } else { PyErr_SetString(PyExc_AssertionError, "Expected either a string or a stringlist as first argument"); return NULL; // NULL triggers exception @@ -127,7 +171,11 @@ PyObject* PythonWorkbenchPy::listMenus(PyObject *args) PyObject* pyList = PyList_New(menus.size()); int i=0; for (std::list::iterator it = menus.begin(); it != menus.end(); ++it, ++i ) { +#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); } return pyList; @@ -149,15 +197,37 @@ PyObject* PythonWorkbenchPy::appendContextMenu(PyObject *args) int nDepth = PyList_Size(pPath); for (int j=0; j= 3 + char* pItem = PyUnicode_AsUTF8(item); + path.push_back(pItem); +#else + PyObject* unicode = PyUnicode_AsEncodedString(item, "utf-8", 0); + char* pItem = PyString_AsString(unicode); + Py_DECREF(unicode); + path.push_back(pItem); + } else if (PyString_Check(item)) { + char* pItem = PyString_AsString(item); + path.push_back(pItem); +#endif + } else { continue; - char* pItem = PyString_AsString(item); - path.push_back(pItem); + } } + } else if (PyUnicode_Check(pPath)) { +#if PY_MAJOR_VERSION >= 3 + char* pItem = PyUnicode_AsUTF8(pPath); + path.push_back(pItem); +#else + PyObject* unicode = PyUnicode_AsEncodedString(pPath, "utf-8", 0); + char* pItem = PyString_AsString(unicode); + Py_DECREF(unicode); + path.push_back(pItem); } else if (PyString_Check(pPath)) { // one single item char* pItem = PyString_AsString(pPath); path.push_back(pItem); +#endif } else { PyErr_SetString(PyExc_AssertionError, "Expected either a string or a stringlist as first argument"); return NULL; // NULL triggers exception @@ -169,15 +239,37 @@ PyObject* PythonWorkbenchPy::appendContextMenu(PyObject *args) int nItems = PyList_Size(pItems); for (int i=0; i= 3 + char* pItem = PyUnicode_AsUTF8(item); + items.push_back(pItem); +#else + PyObject* unicode = PyUnicode_AsEncodedString(item, "utf-8", 0); + char* pItem = PyString_AsString(unicode); + Py_DECREF(unicode); + items.push_back(pItem); + } else if (PyString_Check(item)) { + char* pItem = PyString_AsString(item); + items.push_back(pItem); +#endif + } else { continue; - char* pItem = PyString_AsString(item); - items.push_back(pItem); + } } + } else if (PyUnicode_Check(pItems)) { +#if PY_MAJOR_VERSION >= 3 + char* pItem = PyUnicode_AsUTF8(pItems); + items.push_back(pItem); +#else + PyObject* unicode = PyUnicode_AsEncodedString(pItems, "utf-8", 0); + char* pItem = PyString_AsString(unicode); + Py_DECREF(unicode); + items.push_back(pItem); } else if (PyString_Check(pItems)) { // one single item char* pItem = PyString_AsString(pItems); items.push_back(pItem); +#endif } else { PyErr_SetString(PyExc_AssertionError, "Expected either a string or a stringlist as first argument"); return NULL; // NULL triggers exception @@ -219,12 +311,23 @@ PyObject* PythonWorkbenchPy::appendToolbar(PyObject *args) int nSize = PyList_Size(pObject); for (int i=0; i= 3 + char* pItem = PyUnicode_AsUTF8(item); + items.push_back(pItem); +#else + PyObject* unicode = PyUnicode_AsEncodedString(item, "utf-8", 0); + char* pItem = PyString_AsString(unicode); + Py_DECREF(unicode); + items.push_back(pItem); + } else if (PyString_Check(item)) { + char* pItem = PyString_AsString(item); + items.push_back(pItem); +#endif + } else { continue; - char* pItem = PyString_AsString(item); - items.push_back(pItem); + } } - getPythonBaseWorkbenchPtr()->appendToolbar( psToolBar, items ); Py_Return; @@ -256,7 +359,11 @@ PyObject* PythonWorkbenchPy::listToolbars(PyObject *args) PyObject* pyList = PyList_New(bars.size()); int i=0; for (std::list::iterator it = bars.begin(); it != bars.end(); ++it, ++i ) { +#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); } return pyList; @@ -280,10 +387,22 @@ PyObject* PythonWorkbenchPy::appendCommandbar(PyObject *args) int nSize = PyList_Size(pObject); for (int i=0; i= 3 + char* pItem = PyUnicode_AsUTF8(item); + items.push_back(pItem); +#else + PyObject* unicode = PyUnicode_AsEncodedString(item, "utf-8", 0); + char* pItem = PyString_AsString(unicode); + Py_DECREF(unicode); + items.push_back(pItem); + } else if (PyString_Check(item)) { + char* pItem = PyString_AsString(item); + items.push_back(pItem); +#endif + } else { continue; - char* pItem = PyString_AsString(item); - items.push_back(pItem); + } } getPythonBaseWorkbenchPtr()->appendCommandbar( psToolBar, items ); @@ -317,7 +436,11 @@ PyObject* PythonWorkbenchPy::listCommandbars(PyObject *args) PyObject* pyList = PyList_New(bars.size()); int i=0; for (std::list::iterator it = bars.begin(); it != bars.end(); ++it, ++i) { +#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); } return pyList;