From aaeb49c2382648653705911a6ed8d86ec6354cff 2011-10-18 15:24:00 From: florianlink Date: 2011-10-18 15:24:00 Subject: [PATCH] added support for std::exception handling git-svn-id: svn://svn.code.sf.net/p/pythonqt/code/trunk@198 ea8d5007-eb21-0410-b261-ccb3ea6e24a9 --- diff --git a/src/PythonQtSlot.cpp b/src/PythonQtSlot.cpp index 74bc69c..b019b7e 100644 --- a/src/PythonQtSlot.cpp +++ b/src/PythonQtSlot.cpp @@ -47,6 +47,9 @@ #include "PythonQtConversion.h" #include +#include +#include + #define PYTHONQT_MAX_ARGS 32 @@ -156,25 +159,52 @@ bool PythonQtCallSlot(PythonQtClassInfo* classInfo, QObject* objectToCall, PyObj } // invoke the slot via metacall - (info->decorator()?info->decorator():objectToCall)->qt_metacall(QMetaObject::InvokeMetaMethod, info->slotIndex(), argList); - + bool hadException = false; + try { + (info->decorator()?info->decorator():objectToCall)->qt_metacall(QMetaObject::InvokeMetaMethod, info->slotIndex(), argList); + } catch (std::bad_alloc & e) { + hadException = true; + QByteArray what("std::bad_alloc: "); + what += e.what(); + PyErr_SetString(PyExc_MemoryError, what.constData()); + } catch (std::runtime_error & e) { + hadException = true; + QByteArray what("std::runtime_error: "); + what += e.what(); + PyErr_SetString(PyExc_RuntimeError, what.constData()); + } catch (std::logic_error & e) { + hadException = true; + QByteArray what("std::logic_error: "); + what += e.what(); + PyErr_SetString(PyExc_RuntimeError, what.constData()); + } catch (std::exception& e) { + hadException = true; + QByteArray what("std::exception: "); + what += e.what(); + PyErr_SetString(PyExc_StandardError, what.constData()); + } + if (profilingCB) { profilingCB(PythonQt::Leave, NULL, NULL); } // handle the return value (which in most cases still needs to be converted to a Python object) - if (argList[0] || returnValueParam.typeId == QMetaType::Void) { - if (directReturnValuePointer) { - result = NULL; - } else { - // the resulting object maybe present already, because we created it above at 1)... - if (!result) { - result = PythonQtConv::ConvertQtValueToPython(returnValueParam, argList[0]); + if (!hadException) { + if (argList[0] || returnValueParam.typeId == QMetaType::Void) { + if (directReturnValuePointer) { + result = NULL; + } else { + // the resulting object maybe present already, because we created it above at 1)... + if (!result) { + result = PythonQtConv::ConvertQtValueToPython(returnValueParam, argList[0]); + } } + } else { + QString e = QString("Called ") + info->fullSignature() + ", return type '" + returnValueParam.name + "' is ignored because it is unknown to PythonQt. Probably you should register it using qRegisterMetaType() or add a default constructor decorator to the class."; + PyErr_SetString(PyExc_ValueError, e.toLatin1().data()); + result = NULL; } } else { - QString e = QString("Called ") + info->fullSignature() + ", return type '" + returnValueParam.name + "' is ignored because it is unknown to PythonQt. Probably you should register it using qRegisterMetaType() or add a default constructor decorator to the class."; - PyErr_SetString(PyExc_ValueError, e.toLatin1().data()); result = NULL; } }