From fc8724f0e6d122419c735546ea2588c6a65d9640 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 22 Feb 2014 11:35:51 +0100 Subject: [PATCH] + Avoid to unnecessarily throw Py::Exception --- src/Gui/Application.cpp | 68 +++++++++++++++++++------------------ src/Gui/PythonConsolePy.cpp | 36 ++++++++++++++++++++ src/Gui/PythonConsolePy.h | 4 +++ 3 files changed, 75 insertions(+), 33 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index 1963886e2..e9e4fd6f8 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -1091,45 +1091,47 @@ QPixmap Application::workbenchIcon(const QString& wb) const // get its Icon member if possible try { Py::Object handler(pcWorkbench); - Py::Object member = handler.getAttr(std::string("Icon")); - Py::String data(member); - std::string content = data.as_std_string(); + 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(); - // test if in XPM format - QByteArray ary; - int strlen = (int)content.size(); - ary.resize(strlen); - for (int j=0; j 0) { - // Make sure to remove crap around the XPM data - QList lines = ary.split('\n'); - QByteArray buffer; - buffer.reserve(ary.size()+lines.size()); - for (QList::iterator it = lines.begin(); it != lines.end(); ++it) { - QByteArray trim = it->trimmed(); - if (!trim.isEmpty()) { - buffer.append(trim); - buffer.append('\n'); + // test if in XPM format + QByteArray ary; + int strlen = (int)content.size(); + ary.resize(strlen); + for (int j=0; j 0) { + // Make sure to remove crap around the XPM data + QList lines = ary.split('\n'); + QByteArray buffer; + buffer.reserve(ary.size()+lines.size()); + for (QList::iterator it = lines.begin(); it != lines.end(); ++it) { + QByteArray trim = it->trimmed(); + if (!trim.isEmpty()) { + buffer.append(trim); + buffer.append('\n'); + } + } + icon.loadFromData(buffer, "XPM"); + } + else { + // is it a file name... + QString file = QString::fromUtf8(content.c_str()); + icon.load(file); + if (icon.isNull()) { + // ... or the name of another icon? + icon = BitmapFactory().pixmap(file.toUtf8()); } } - icon.loadFromData(buffer, "XPM"); - } - else { - // is it a file name... - QString file = QString::fromUtf8(content.c_str()); - icon.load(file); - if (icon.isNull()) { - // ... or the name of another icon? - icon = BitmapFactory().pixmap(file.toUtf8()); + + if (!icon.isNull()) { + BitmapFactory().addPixmapToCache(iconName.c_str(), icon); } - } - if (!icon.isNull()) { - BitmapFactory().addPixmapToCache(iconName.c_str(), icon); + return icon; } - - return icon; } catch (Py::Exception& e) { e.clear(); diff --git a/src/Gui/PythonConsolePy.cpp b/src/Gui/PythonConsolePy.cpp index fe28a0c6f..918e3d0ee 100644 --- a/src/Gui/PythonConsolePy.cpp +++ b/src/Gui/PythonConsolePy.cpp @@ -57,6 +57,15 @@ PythonStdout::~PythonStdout() { } +Py::Object PythonStdout::getattr(const char *name) +{ + if (strcmp(name, "softspace") == 0) { + int i=0; + return Py::Int(i); + } + return getattr_methods(name); +} + Py::Object PythonStdout::repr() { std::string s; @@ -119,6 +128,15 @@ PythonStderr::~PythonStderr() { } +Py::Object PythonStderr::getattr(const char *name) +{ + if (strcmp(name, "softspace") == 0) { + int i=0; + return Py::Int(i); + } + return getattr_methods(name); +} + Py::Object PythonStderr::repr() { std::string s; @@ -180,6 +198,15 @@ OutputStdout::~OutputStdout() { } +Py::Object OutputStdout::getattr(const char *name) +{ + if (strcmp(name, "softspace") == 0) { + int i=0; + return Py::Int(i); + } + return getattr_methods(name); +} + Py::Object OutputStdout::repr() { std::string s; @@ -239,6 +266,15 @@ OutputStderr::~OutputStderr() { } +Py::Object OutputStderr::getattr(const char *name) +{ + if (strcmp(name, "softspace") == 0) { + int i=0; + return Py::Int(i); + } + return getattr_methods(name); +} + Py::Object OutputStderr::repr() { std::string s; diff --git a/src/Gui/PythonConsolePy.h b/src/Gui/PythonConsolePy.h index b1ad4fcfa..64b46e8e5 100644 --- a/src/Gui/PythonConsolePy.h +++ b/src/Gui/PythonConsolePy.h @@ -50,6 +50,7 @@ public: PythonStdout(PythonConsole *pc); ~PythonStdout(); + Py::Object getattr(const char *name); Py::Object repr(); Py::Object write(const Py::Tuple&); Py::Object flush(const Py::Tuple&); @@ -74,6 +75,7 @@ public: PythonStderr(PythonConsole *pc); ~PythonStderr(); + Py::Object getattr(const char *name); Py::Object repr(); Py::Object write(const Py::Tuple&); Py::Object flush(const Py::Tuple&); @@ -95,6 +97,7 @@ public: OutputStdout(); ~OutputStdout(); + Py::Object getattr(const char *name); Py::Object repr(); Py::Object write(const Py::Tuple&); Py::Object flush(const Py::Tuple&); @@ -116,6 +119,7 @@ public: OutputStderr(); ~OutputStderr(); + Py::Object getattr(const char *name); Py::Object repr(); Py::Object write(const Py::Tuple&); Py::Object flush(const Py::Tuple&);