From 51d814d222bef3ed4795a5e61b3e72b20989e5ff Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Thu, 26 Dec 2019 18:34:05 +0800 Subject: [PATCH] Gui: handle exception in property item display --- src/Gui/propertyeditor/PropertyItem.cpp | 92 +++++++++++++++---------- 1 file changed, 54 insertions(+), 38 deletions(-) diff --git a/src/Gui/propertyeditor/PropertyItem.cpp b/src/Gui/propertyeditor/PropertyItem.cpp index e94075eb73..1fd77d65b8 100644 --- a/src/Gui/propertyeditor/PropertyItem.cpp +++ b/src/Gui/propertyeditor/PropertyItem.cpp @@ -347,49 +347,65 @@ QVariant PropertyItem::toString(const QVariant& prop) const if (prop != QVariant() || propertyItems.size()!=1) return prop; - Base::PyGILStateLocker lock; - Py::Object pyobj(propertyItems[0]->getPyObject(), true); std::ostringstream ss; - if (pyobj.isNone()) { - ss << ""; - } - else if(pyobj.isSequence()) { - ss << '['; - Py::Sequence seq(pyobj); - bool first = true; - Py_ssize_t i=0; - for (i=0; i<2 && i < seq.size(); ++i) { - if (first) - first = false; - else - ss << ", "; - ss << Py::Object(seq[i]).as_string(); + Base::PyGILStateLocker lock; + try { + Base::PyGILStateLocker lock; + Py::Object pyobj(propertyItems[0]->getPyObject(), true); + std::ostringstream ss; + if (pyobj.isNone()) { + ss << ""; } + else if(pyobj.isSequence()) { + ss << '['; + Py::Sequence seq(pyobj); + bool first = true; + Py_ssize_t i=0; + for (i=0; i<2 && i < seq.size(); ++i) { + if (first) + first = false; + else + ss << ", "; + ss << Py::Object(seq[i]).as_string(); + } - if (i < seq.size()) - ss << "..."; - ss << ']'; - } - else if (pyobj.isMapping()) { - ss << '{'; - Py::Mapping map(pyobj); - bool first = true; - auto it = map.begin(); - for(int i=0; i<2 && it != map.end(); ++it, ++i) { - if (first) - first = false; - else - ss << ", "; - const auto &v = *it; - ss << Py::Object(v.first).as_string() << ':' << Py::Object(v.second).as_string(); + if (i < seq.size()) + ss << "..."; + ss << ']'; } + else if (pyobj.isMapping()) { + ss << '{'; + Py::Mapping map(pyobj); + bool first = true; + auto it = map.begin(); + for(int i=0; i<2 && it != map.end(); ++it, ++i) { + if (first) + first = false; + else + ss << ", "; + const auto &v = *it; + ss << Py::Object(v.first).as_string() << ':' << Py::Object(v.second).as_string(); + } - if (it != map.end()) - ss << "..."; - ss << '}'; - } - else { - ss << pyobj.as_string(); + if (it != map.end()) + ss << "..."; + ss << '}'; + } + else + ss << pyobj.as_string(); + } catch (Py::Exception &) { + Base::PyException e; + ss.str(""); + ss << "ERR: " << e.what(); + } catch (Base::Exception &e) { + ss.str(""); + ss << "ERR: " << e.what(); + } catch (std::exception &e) { + ss.str(""); + ss << "ERR: " << e.what(); + } catch (...) { + ss.str(""); + ss << "ERR!"; } return QVariant(QString::fromUtf8(ss.str().c_str()));