From faac8c36baf14ac8781c2d2850ebbf2b5e596265 Mon Sep 17 00:00:00 2001 From: Sebastian Hoogen Date: Sat, 31 Jan 2015 22:36:39 +0100 Subject: [PATCH] add encoding parameter to .as_std_string() to handle PyUnicode Objects. Usually ASCII for object names and Utf-8 for file names and console output. issue #995 --- src/App/PropertyPythonObject.cpp | 2 +- src/Gui/Application.cpp | 12 ++++++------ src/Gui/ApplicationPy.cpp | 2 +- src/Gui/View3DPy.cpp | 16 ++++++++-------- src/Gui/ViewProviderPythonFeature.cpp | 16 ++++++++-------- src/Mod/Complete/Gui/AppCompleteGui.cpp | 6 +++--- src/Mod/Robot/App/WaypointPyImp.cpp | 4 ++-- src/Mod/Start/Gui/AppStartGui.cpp | 4 ++-- 8 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/App/PropertyPythonObject.cpp b/src/App/PropertyPythonObject.cpp index facc8fe10..9ca6b4df5 100644 --- a/src/App/PropertyPythonObject.cpp +++ b/src/App/PropertyPythonObject.cpp @@ -104,7 +104,7 @@ std::string PropertyPythonObject::toString() const args.setItem(0, dump); Py::Object res = method.apply(args); Py::String str(res); - repr = str.as_std_string(); + repr = str.as_std_string("ascii"); } catch (Py::Exception&) { Base::PyException e; // extract the Python error text diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index 39c2000a8..4cdf421ad 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -998,7 +998,7 @@ bool Application::activateWorkbench(const char* name) Py::Callable method(handler.getAttr(std::string("GetClassName"))); Py::Tuple args; Py::String result(method.apply(args)); - type = result.as_std_string(); + type = result.as_std_string("ascii"); if (Base::Type::fromName(type.c_str()).isDerivedFrom(Gui::PythonBaseWorkbench::getClassTypeId())) { Workbench* wb = WorkbenchManager::instance()->createWorkbench(name, type); handler.setAttr(std::string("__Workbench__"), Py::Object(wb->getPyObject(), true)); @@ -1012,7 +1012,7 @@ bool Application::activateWorkbench(const char* name) // can be defined after the call of Initialize() if (type.empty()) { Py::String result(method.apply(args)); - type = result.as_std_string(); + type = result.as_std_string("ascii"); } } @@ -1111,7 +1111,7 @@ QPixmap Application::workbenchIcon(const QString& wb) const if (handler.hasAttr(std::string("Icon"))) { Py::Object member = handler.getAttr(std::string("Icon")); Py::String data(member); - std::string content = data.as_std_string(); + std::string content = data.as_std_string("utf-8"); // test if in XPM format QByteArray ary; @@ -1178,7 +1178,7 @@ QString Application::workbenchToolTip(const QString& wb) const Py::Object member = handler.getAttr(std::string("ToolTip")); if (member.isString()) { Py::String tip(member); - return QString::fromUtf8(tip.as_std_string().c_str()); + return QString::fromUtf8(tip.as_std_string("utf-8").c_str()); } } catch (Py::Exception& e) { @@ -1203,7 +1203,7 @@ QString Application::workbenchMenuText(const QString& wb) const Py::Object member = handler.getAttr(std::string("MenuText")); if (member.isString()) { Py::String tip(member); - return QString::fromUtf8(tip.as_std_string().c_str()); + return QString::fromUtf8(tip.as_std_string("utf-8").c_str()); } } catch (Py::Exception& e) { @@ -1288,7 +1288,7 @@ void Application::setupContextMenu(const char* recipient, MenuItem* items) const e.clear(); if (o.isString()) { Py::String s(o); - std::clog << "Application::setupContextMenu: " << s.as_std_string() << std::endl; + std::clog << "Application::setupContextMenu: " << s.as_std_string("utf-8") << std::endl; } } } diff --git a/src/Gui/ApplicationPy.cpp b/src/Gui/ApplicationPy.cpp index ad2d5945c..5c30b20f2 100644 --- a/src/Gui/ApplicationPy.cpp +++ b/src/Gui/ApplicationPy.cpp @@ -613,7 +613,7 @@ PyObject* Application::sAddWorkbenchHandler(PyObject * /*self*/, PyObject *args, // Search for some methods and members without invoking them Py::Callable(object.getAttr(std::string("Initialize"))); Py::Callable(object.getAttr(std::string("GetClassName"))); - item = name.as_std_string(); + item = name.as_std_string("ascii"); PyObject* wb = PyDict_GetItemString(Instance->_pcWorkbenchDictionary,item.c_str()); if (wb) { diff --git a/src/Gui/View3DPy.cpp b/src/Gui/View3DPy.cpp index d6bd42eea..35c935051 100644 --- a/src/Gui/View3DPy.cpp +++ b/src/Gui/View3DPy.cpp @@ -1711,11 +1711,11 @@ void View3DInventorPy::eventCallback(void * ud, SoEventCallback * n) Py::Object o = Py::type(e); if (o.isString()) { Py::String s(o); - Base::Console().Warning("%s\n", s.as_std_string().c_str()); + Base::Console().Warning("%s\n", s.as_std_string("utf-8").c_str()); } else { Py::String s(o.repr()); - Base::Console().Warning("%s\n", s.as_std_string().c_str()); + Base::Console().Warning("%s\n", s.as_std_string("utf-8").c_str()); } // Prints message to console window if we are in interactive mode PyErr_Print(); @@ -1866,11 +1866,11 @@ void View3DInventorPy::eventCallbackPivy(void * ud, SoEventCallback * n) Py::Object o = Py::type(e); if (o.isString()) { Py::String s(o); - Base::Console().Warning("%s\n", s.as_std_string().c_str()); + Base::Console().Warning("%s\n", s.as_std_string("utf-8").c_str()); } else { Py::String s(o.repr()); - Base::Console().Warning("%s\n", s.as_std_string().c_str()); + Base::Console().Warning("%s\n", s.as_std_string("utf-8").c_str()); } // Prints message to console window if we are in interactive mode PyErr_Print(); @@ -1899,11 +1899,11 @@ void View3DInventorPy::eventCallbackPivyEx(void * ud, SoEventCallback * n) Py::Object o = Py::type(e); if (o.isString()) { Py::String s(o); - Base::Console().Warning("%s\n", s.as_std_string().c_str()); + Base::Console().Warning("%s\n", s.as_std_string("utf-8").c_str()); } else { Py::String s(o.repr()); - Base::Console().Warning("%s\n", s.as_std_string().c_str()); + Base::Console().Warning("%s\n", s.as_std_string("utf-8").c_str()); } // Prints message to console window if we are in interactive mode PyErr_Print(); @@ -2031,11 +2031,11 @@ void View3DInventorPy::draggerCallback(void * ud, SoDragger* n) Py::Object o = Py::type(e); if (o.isString()) { Py::String s(o); - Base::Console().Warning("%s\n", s.as_std_string().c_str()); + Base::Console().Warning("%s\n", s.as_std_string("utf-8").c_str()); } else { Py::String s(o.repr()); - Base::Console().Warning("%s\n", s.as_std_string().c_str()); + Base::Console().Warning("%s\n", s.as_std_string("utf-8").c_str()); } // Prints message to console window if we are in interactive mode PyErr_Print(); diff --git a/src/Gui/ViewProviderPythonFeature.cpp b/src/Gui/ViewProviderPythonFeature.cpp index f5f869e8b..9e9c54174 100644 --- a/src/Gui/ViewProviderPythonFeature.cpp +++ b/src/Gui/ViewProviderPythonFeature.cpp @@ -247,10 +247,10 @@ QIcon ViewProviderPythonFeatureImp::getIcon() const Py::Callable method(vp.getAttr(std::string("getIcon"))); Py::Tuple args; Py::String str(method.apply(args)); - std::string content = str.as_std_string(); + std::string content = str.as_std_string("utf-8"); QPixmap icon; // Check if the passed string is a filename, otherwise treat as xpm data - QFileInfo fi(QString::fromAscii(content.c_str())); + QFileInfo fi(QString::fromUtf8(content.c_str())); if (fi.isFile() && fi.exists()) { icon.load(fi.absoluteFilePath()); } else { @@ -649,9 +649,9 @@ const char* ViewProviderPythonFeatureImp::getDefaultDisplayMode() const Py::Callable method(vp.getAttr(std::string("getDefaultDisplayMode"))); Py::Tuple args; Py::String str(method.apply(args)); - if (str.isUnicode()) - str = str.encode("ascii"); // json converts strings into unicode - mode = str.as_std_string(); + //if (str.isUnicode()) + // str = str.encode("ascii"); // json converts strings into unicode + mode = str.as_std_string("ascii"); return mode.c_str(); } } @@ -680,7 +680,7 @@ std::vector ViewProviderPythonFeatureImp::getDisplayModes(void) con Py::Sequence list(method.apply(args)); for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { Py::String str(*it); - modes.push_back(str.as_std_string()); + modes.push_back(str.as_std_string("ascii")); } } else { @@ -690,7 +690,7 @@ std::vector ViewProviderPythonFeatureImp::getDisplayModes(void) con Py::Sequence list(method.apply(args)); for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { Py::String str(*it); - modes.push_back(str.as_std_string()); + modes.push_back(str.as_std_string("ascii")); } } } @@ -717,7 +717,7 @@ std::string ViewProviderPythonFeatureImp::setDisplayMode(const char* ModeName) Py::Tuple args(1); args.setItem(0, Py::String(ModeName)); Py::String str(method.apply(args)); - return str.as_std_string(); + return str.as_std_string("ascii"); } } } diff --git a/src/Mod/Complete/Gui/AppCompleteGui.cpp b/src/Mod/Complete/Gui/AppCompleteGui.cpp index 438f6dbc7..c844fd223 100644 --- a/src/Mod/Complete/Gui/AppCompleteGui.cpp +++ b/src/Mod/Complete/Gui/AppCompleteGui.cpp @@ -97,7 +97,7 @@ void CompleteGuiExport initCompleteGui() Py::Callable method(handler.getAttr(std::string("GetClassName"))); Py::Tuple args; Py::String result(method.apply(args)); - type = result.as_std_string(); + type = result.as_std_string("ascii"); if (type == "Gui::PythonWorkbench") { Gui::Workbench* wb = Gui::WorkbenchManager::instance()->createWorkbench("DraftWorkbench", type); handler.setAttr(std::string("__Workbench__"), Py::Object(wb->getPyObject(), true)); @@ -120,11 +120,11 @@ void CompleteGuiExport initCompleteGui() Py::Object o = Py::type(e); if (o.isString()) { Py::String s(o); - Base::Console().Error("%s\n", s.as_std_string().c_str()); + Base::Console().Error("%s\n", s.as_std_string("utf-8").c_str()); } else { Py::String s(o.repr()); - Base::Console().Error("%s\n", s.as_std_string().c_str()); + Base::Console().Error("%s\n", s.as_std_string("utf-8").c_str()); } // Prints message to console window if we are in interactive mode PyErr_Print(); diff --git a/src/Mod/Robot/App/WaypointPyImp.cpp b/src/Mod/Robot/App/WaypointPyImp.cpp index 4115b0895..3965b5183 100644 --- a/src/Mod/Robot/App/WaypointPyImp.cpp +++ b/src/Mod/Robot/App/WaypointPyImp.cpp @@ -160,7 +160,7 @@ Py::String WaypointPy::getName(void) const void WaypointPy::setName(Py::String arg) { - getWaypointPtr()->Name = arg.as_std_string(); + getWaypointPtr()->Name = arg.as_std_string("ascii"); } Py::String WaypointPy::getType(void) const @@ -181,7 +181,7 @@ Py::String WaypointPy::getType(void) const void WaypointPy::setType(Py::String arg) { - std::string typeStr(arg.as_std_string()); + std::string typeStr(arg.as_std_string("ascii")); if(typeStr=="PTP") getWaypointPtr()->Type = Waypoint::PTP; else if(typeStr=="LIN") diff --git a/src/Mod/Start/Gui/AppStartGui.cpp b/src/Mod/Start/Gui/AppStartGui.cpp index fa210c0b2..54c3e308e 100644 --- a/src/Mod/Start/Gui/AppStartGui.cpp +++ b/src/Mod/Start/Gui/AppStartGui.cpp @@ -71,11 +71,11 @@ void StartGuiExport initStartGui() Py::Object o = Py::type(e); if (o.isString()) { Py::String s(o); - Base::Console().Error("%s\n", s.as_std_string().c_str()); + Base::Console().Error("%s\n", s.as_std_string("utf-8").c_str()); } else { Py::String s(o.repr()); - Base::Console().Error("%s\n", s.as_std_string().c_str()); + Base::Console().Error("%s\n", s.as_std_string("utf-8").c_str()); } // Prints message to console window if we are in interactive mode PyErr_Print();