+ Avoid to unnecessarily throw Py::Exception

This commit is contained in:
wmayer 2014-02-22 11:35:51 +01:00
parent 3bebf0dcea
commit fc8724f0e6
3 changed files with 75 additions and 33 deletions

View File

@ -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<strlen; j++)
ary[j]=content[j];
if (ary.indexOf("/* XPM */") > 0) {
// Make sure to remove crap around the XPM data
QList<QByteArray> lines = ary.split('\n');
QByteArray buffer;
buffer.reserve(ary.size()+lines.size());
for (QList<QByteArray>::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<strlen; j++)
ary[j]=content[j];
if (ary.indexOf("/* XPM */") > 0) {
// Make sure to remove crap around the XPM data
QList<QByteArray> lines = ary.split('\n');
QByteArray buffer;
buffer.reserve(ary.size()+lines.size());
for (QList<QByteArray>::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();

View File

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

View File

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