From 8516c6fbe3fb79aecdc61b7c1930300e57228945 Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Mon, 9 Dec 2019 00:35:38 +0000 Subject: [PATCH] Gui: fix Gui.addCommand() in Python3 --- src/Gui/ApplicationPy.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Gui/ApplicationPy.cpp b/src/Gui/ApplicationPy.cpp index a71716e11730..0deb8c2136e1 100644 --- a/src/Gui/ApplicationPy.cpp +++ b/src/Gui/ApplicationPy.cpp @@ -1144,12 +1144,20 @@ PyObject* Application::sAddCommand(PyObject * /*self*/, PyObject *args) return 0; } Py::Callable inspect(mod.getAttr("stack")); - Py::Tuple args; - Py::List list(inspect.apply(args)); - args = list.getItem(0); + Py::List list(inspect.apply()); + std::string file; // usually this is the file name of the calling script - std::string file = args.getItem(1).as_string(); +#if (PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION==3 && PY_MINOR_VERSION>=5)) + Py::Object info = list.getItem(0); + PyObject *pyfile = PyStructSequence_GET_ITEM(*info,1); + if(!pyfile) + throw Py::Exception(); + file = Py::Object(pyfile).as_string(); +#else + Py::Tuple info = list.getItem(0); + file = info.getItem(1).as_string(); +#endif Base::FileInfo fi(file); // convert backslashes to slashes file = fi.filePath();