Skip to content

Commit

Permalink
Gui: remove Py2 code from several src/Gui .cpp files
Browse files Browse the repository at this point in the history
  • Loading branch information
luzpaz authored and wwmayer committed Apr 14, 2021
1 parent b7a1576 commit 6a71841
Show file tree
Hide file tree
Showing 10 changed files with 1 addition and 191 deletions.
8 changes: 0 additions & 8 deletions src/Gui/ApplicationPy.cpp
Expand Up @@ -737,11 +737,7 @@ PyObject* Application::sGetLocale(PyObject * /*self*/, PyObject *args)
return NULL;

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::sSetLocale(PyObject * /*self*/, PyObject *args)
Expand Down Expand Up @@ -819,11 +815,7 @@ PyObject* Application::sAddPreferencePage(PyObject * /*self*/, PyObject *args)

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);

Expand Down
21 changes: 0 additions & 21 deletions src/Gui/CommandPyImp.cpp
Expand Up @@ -77,11 +77,7 @@ PyObject* CommandPy::listAll(PyObject *args)
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;
Expand Down Expand Up @@ -123,11 +119,7 @@ PyObject* CommandPy::listByShortcut(PyObject *args)
PyObject* pyList = PyList_New(matches.size());
int i=0;
for (std::string match : matches) {
#if PY_MAJOR_VERSION >= 3
PyObject* str = PyUnicode_FromString(match.c_str());
#else
PyObject* str = PyString_FromString(match.c_str());
#endif
PyList_SetItem(pyList, i++, str);
}
return pyList;
Expand Down Expand Up @@ -178,11 +170,7 @@ PyObject* CommandPy::getShortcut(PyObject *args)

Command* cmd = this->getCommandPtr();
if (cmd) {
#if PY_MAJOR_VERSION >= 3
PyObject* str = PyUnicode_FromString(cmd->getAction() ? cmd->getAction()->shortcut().toString().toStdString().c_str() : "");
#else
PyObject* str = PyString_FromString(cmd->getAction() ? cmd->getAction()->shortcut().toString().toStdString().c_str() : "");
#endif
return str;
}
else {
Expand Down Expand Up @@ -283,21 +271,12 @@ PyObject* CommandPy::getInfo(PyObject *args)
if (action)
shortcutTxt = action->shortcut().toString().toStdString();

#if PY_MAJOR_VERSION >= 3
PyObject* strMenuTxt = PyUnicode_FromString(menuTxt ? menuTxt : "");
PyObject* strTooltipTxt = PyUnicode_FromString(tooltipTxt ? tooltipTxt : "");
PyObject* strWhatsThisTxt = PyUnicode_FromString(whatsThisTxt ? whatsThisTxt : "");
PyObject* strStatustipTxt = PyUnicode_FromString(statustipTxt ? statustipTxt : "");
PyObject* strPixMapTxt = PyUnicode_FromString(pixMapTxt ? pixMapTxt : "");
PyObject* strShortcutTxt = PyUnicode_FromString(!shortcutTxt.empty() ? shortcutTxt.c_str() : "");
#else
PyObject* strMenuTxt = PyString_FromString(menuTxt ? menuTxt : "");
PyObject* strTooltipTxt = PyString_FromString(tooltipTxt ? tooltipTxt : "");
PyObject* strWhatsThisTxt = PyString_FromString(whatsThisTxt ? whatsThisTxt : "");
PyObject* strStatustipTxt = PyString_FromString(statustipTxt ? statustipTxt : "");
PyObject* strPixMapTxt = PyString_FromString(pixMapTxt ? pixMapTxt : "");
PyObject* strShortcutTxt = PyString_FromString(!shortcutTxt.empty() ? shortcutTxt.c_str() : "");
#endif
PyList_SetItem(pyList, 0, strMenuTxt);
PyList_SetItem(pyList, 1, strTooltipTxt);
PyList_SetItem(pyList, 2, strWhatsThisTxt);
Expand Down
8 changes: 0 additions & 8 deletions src/Gui/OnlineDocumentation.cpp
Expand Up @@ -169,11 +169,7 @@ 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);
Expand Down Expand Up @@ -204,11 +200,7 @@ 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);
Expand Down
24 changes: 1 addition & 23 deletions src/Gui/PythonConsole.cpp
Expand Up @@ -171,17 +171,9 @@ 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
}

/**
Expand Down Expand Up @@ -323,11 +315,7 @@ 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)) {
Expand Down Expand Up @@ -359,11 +347,7 @@ void InteractiveInterpreter::runCode(PyCodeObject* code) const
}

std::string err = str.str();
#if PY_MAJOR_VERSION >= 3
errdata = PyUnicode_FromString(err.c_str());
#else
errdata = PyString_FromString(err.c_str());
#endif
}
}
PyErr_Restore(errobj, errdata, errtraceback);
Expand Down Expand Up @@ -474,13 +458,8 @@ 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), QString::fromLatin1(platform));
Expand Down Expand Up @@ -837,12 +816,11 @@ void PythonConsole::runSource(const QString& line)
setFocus(); // if focus was lost
}
catch (const Base::SystemExitException&) {
#if PY_MAJOR_VERSION >= 3
// In Python the exception must be cleared because when the message box below appears
// callable Python objects can be invoked and due to a failing assert the application
// will be aborted.
PyErr_Clear();
#endif

ParameterGrp::handle hPrefGrp = getWindowParameter();
bool check = hPrefGrp->GetBool("CheckSystemExit",true);
int ret = QMessageBox::Yes;
Expand Down
24 changes: 0 additions & 24 deletions src/Gui/PythonConsolePy.cpp
Expand Up @@ -80,15 +80,9 @@ Py::Object PythonStdout::write(const Py::Tuple& args)
try {
Py::Object output(args[0]);
if (PyUnicode_Check(output.ptr())) {
#if PY_MAJOR_VERSION >= 3
PyObject* unicode = PyUnicode_AsEncodedString(output.ptr(), "utf-8", 0);
if (unicode) {
const char* string = PyBytes_AsString(unicode);
#else
PyObject* unicode = PyUnicode_AsEncodedObject(output.ptr(), "utf-8", "strict");
if (unicode) {
const char* string = PyString_AsString(unicode);
#endif
int maxlen = qstrlen(string) > 10000 ? 10000 : -1;
pyConsole->insertPythonOutput(QString::fromUtf8(string, maxlen));
Py_DECREF(unicode);
Expand Down Expand Up @@ -163,15 +157,9 @@ Py::Object PythonStderr::write(const Py::Tuple& args)
try {
Py::Object output(args[0]);
if (PyUnicode_Check(output.ptr())) {
#if PY_MAJOR_VERSION >= 3
PyObject* unicode = PyUnicode_AsEncodedString(output.ptr(), "utf-8", 0);
if (unicode) {
const char* string = PyBytes_AsString(unicode);
#else
PyObject* unicode = PyUnicode_AsEncodedObject(output.ptr(), "utf-8", "strict");
if (unicode) {
const char* string = PyString_AsString(unicode);
#endif
int maxlen = qstrlen(string) > 10000 ? 10000 : -1;
pyConsole->insertPythonError(QString::fromUtf8(string, maxlen));
Py_DECREF(unicode);
Expand Down Expand Up @@ -245,15 +233,9 @@ Py::Object OutputStdout::write(const Py::Tuple& args)
try {
Py::Object output(args[0]);
if (PyUnicode_Check(output.ptr())) {
#if PY_MAJOR_VERSION >= 3
PyObject* unicode = PyUnicode_AsEncodedString(output.ptr(), "utf-8", 0);
if (unicode) {
const char* string = PyBytes_AsString(unicode);
#else
PyObject* unicode = PyUnicode_AsEncodedObject(output.ptr(), "utf-8", "strict");
if (unicode) {
const char* string = PyString_AsString(unicode);
#endif
Base::Console().Message("%s",string);
Py_DECREF(unicode);
}
Expand Down Expand Up @@ -326,15 +308,9 @@ Py::Object OutputStderr::write(const Py::Tuple& args)
try {
Py::Object output(args[0]);
if (PyUnicode_Check(output.ptr())) {
#if PY_MAJOR_VERSION >= 3
PyObject* unicode = PyUnicode_AsEncodedString(output.ptr(), "utf-8", 0);
if (unicode) {
const char* string = PyBytes_AsString(unicode);
#else
PyObject* unicode = PyUnicode_AsEncodedObject(output.ptr(), "utf-8", "strict");
if (unicode) {
const char* string = PyString_AsString(unicode);
#endif
Base::Console().Error("%s",string);
Py_DECREF(unicode);
}
Expand Down
8 changes: 0 additions & 8 deletions src/Gui/PythonDebugger.cpp
Expand Up @@ -437,11 +437,7 @@ 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;
Expand Down Expand Up @@ -580,11 +576,7 @@ 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++;
Expand Down
10 changes: 0 additions & 10 deletions src/Gui/Selection.cpp
Expand Up @@ -2067,11 +2067,7 @@ PyObject *SelectionSingleton::sCountObjectsOfType(PyObject * /*self*/, PyObject
return NULL;

unsigned int count = Selection().countObjectsOfType(objecttype, document, resolve);
#if PY_MAJOR_VERSION < 3
return PyInt_FromLong(count);
#else
return PyLong_FromLong(count);
#endif
}

PyObject *SelectionSingleton::sGetSelection(PyObject * /*self*/, PyObject *args)
Expand Down Expand Up @@ -2340,15 +2336,9 @@ PyObject *SelectionSingleton::sSetVisible(PyObject * /*self*/, PyObject *args)
if(visible == Py_None) {
vis = -1;
}
#if PY_MAJOR_VERSION < 3
else if(PyInt_Check(visible)) {
vis = PyInt_AsLong(visible);
}
#else
else if(PyLong_Check(visible)) {
vis = PyLong_AsLong(visible);
}
#endif
else {
vis = PyObject_IsTrue(visible)?1:0;
}
Expand Down
52 changes: 0 additions & 52 deletions src/Gui/WidgetFactory.cpp
Expand Up @@ -324,25 +324,14 @@ 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 @@ -1002,16 +991,7 @@ 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 @@ -1024,16 +1004,7 @@ 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 @@ -1484,24 +1455,9 @@ Py::Object PyResource::setValue(const Py::Tuple& args)

QVariant v;
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 @@ -1514,17 +1470,9 @@ 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
const char* pItem = PyUnicode_AsUTF8(item);
#else
char* pItem = PyString_AsString(item);
#endif
str.append(QString::fromUtf8(pItem));
}

Expand Down

0 comments on commit 6a71841

Please sign in to comment.