py3: Gui: files A-P ported to python3
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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<Command*>::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;
|
||||
|
||||
@@ -234,7 +234,11 @@ QMap<QString, CallTip> 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<PyCodeObject*>(code), dict.ptr(), dict.ptr());
|
||||
#endif
|
||||
}
|
||||
Py_DECREF(code);
|
||||
if (!eval) {
|
||||
@@ -270,12 +274,14 @@ QMap<QString, CallTip> 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<PyInstanceObject*>(obj.ptr());
|
||||
PyObject* classobj = reinterpret_cast<PyObject*>(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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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__
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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++;
|
||||
|
||||
@@ -62,15 +62,37 @@ PyObject* PythonWorkbenchPy::appendMenu(PyObject *args)
|
||||
int nDepth = PyList_Size(pPath);
|
||||
for (int j=0; j<nDepth;++j) {
|
||||
PyObject* item = PyList_GetItem(pPath, j);
|
||||
if (!PyString_Check(item))
|
||||
if (PyUnicode_Check(item)) {
|
||||
#if PY_MAJOR_VERSION >= 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<nItems;++i) {
|
||||
PyObject* item = PyList_GetItem(pItems, i);
|
||||
if (!PyString_Check(item))
|
||||
if (PyUnicode_Check(item)) {
|
||||
#if PY_MAJOR_VERSION >= 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<std::string>::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<nDepth;++j) {
|
||||
PyObject* item = PyList_GetItem(pPath, j);
|
||||
if (!PyString_Check(item))
|
||||
if (PyUnicode_Check(item)) {
|
||||
#if PY_MAJOR_VERSION >= 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<nItems;++i) {
|
||||
PyObject* item = PyList_GetItem(pItems, i);
|
||||
if (!PyString_Check(item))
|
||||
if (PyUnicode_Check(item)) {
|
||||
#if PY_MAJOR_VERSION >= 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<nSize;++i) {
|
||||
PyObject* item = PyList_GetItem(pObject, i);
|
||||
if (!PyString_Check(item))
|
||||
if (PyUnicode_Check(item)) {
|
||||
#if PY_MAJOR_VERSION >= 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<std::string>::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<nSize;++i) {
|
||||
PyObject* item = PyList_GetItem(pObject, i);
|
||||
if (!PyString_Check(item))
|
||||
if (PyUnicode_Check(item)) {
|
||||
#if PY_MAJOR_VERSION >= 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<std::string>::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;
|
||||
|
||||
Reference in New Issue
Block a user