Skip to content

Commit

Permalink
Limit string representation of argument list in length
Browse files Browse the repository at this point in the history
otherwise when passing image data to the QImage constructor this can get rather long
  • Loading branch information
usiems committed May 12, 2023
1 parent 5fc0dfc commit ea27670
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/PythonQtSlot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,20 @@ PyObject *PythonQtMemberFunction_Call(PythonQtSlotInfo* info, PyObject* m_self,
return nullptr;
}

namespace {
QString limitString(const QString& aString, int maxLength = 2000, const QString& ellipsis = "...")
{
if (aString.length() <= maxLength)
return aString;

const int beforeSize = (maxLength - ellipsis.length()) / 2;
auto beforeEllipsis = aString.left(beforeSize);
auto afterEllipsis = aString.right(maxLength - ellipsis.length() - beforeSize);

return beforeEllipsis + ellipsis + afterEllipsis;
}
};

PyObject *PythonQtSlotFunction_CallImpl(PythonQtClassInfo* classInfo, QObject* objectToCall, PythonQtSlotInfo* info, PyObject *args, PyObject * kw, void* firstArg, void** directReturnValuePointer, PythonQtPassThisOwnershipType* passThisOwnershipToCPP)
{
int argc = args?PyTuple_Size(args):0;
Expand Down Expand Up @@ -381,7 +395,7 @@ PyObject *PythonQtSlotFunction_CallImpl(PythonQtClassInfo* classInfo, QObject* o

ok = PythonQtCallSlot(classInfo, objectToCall, combinedArgs, false, slotInfo, firstArg, &r, directReturnValuePointer, passThisOwnershipToCPP);
if (!ok && !PyErr_Occurred()) {
QString e = QString("Called ") + info->fullSignature() + " with wrong arguments: " + PythonQtConv::PyObjGetString(args);
QString e = QString("Called ") + info->fullSignature() + " with wrong arguments: " + limitString(PythonQtConv::PyObjGetString(args));
PyErr_SetString(PyExc_ValueError, QStringToPythonConstCharPointer(e));
}
} else {
Expand Down Expand Up @@ -413,7 +427,7 @@ PyObject *PythonQtSlotFunction_CallImpl(PythonQtClassInfo* classInfo, QObject* o
}
}
if (!ok && !PyErr_Occurred()) {
QString e = QString("Could not find matching overload for given arguments:\n" + PythonQtConv::PyObjGetString(args) + "\n The following slots are available:\n");
QString e = QString("Could not find matching overload for given arguments:\n" + limitString(PythonQtConv::PyObjGetString(args)) + "\n The following slots are available:\n");
PythonQtSlotInfo* i = info;
while (i) {
e += QString(i->fullSignature()) + "\n";
Expand All @@ -431,11 +445,11 @@ PyObject *PythonQtSlotFunction_CallImpl(PythonQtClassInfo* classInfo, QObject* o
#endif
ok = PythonQtCallSlot(classInfo, objectToCall, args, false, info, firstArg, &r, directReturnValuePointer, passThisOwnershipToCPP);
if (!ok && !PyErr_Occurred()) {
QString e = QString("Called ") + info->fullSignature() + " with wrong arguments: " + PythonQtConv::PyObjGetString(args);
QString e = QString("Called ") + info->fullSignature() + " with wrong arguments: " + limitString(PythonQtConv::PyObjGetString(args));
PyErr_SetString(PyExc_ValueError, QStringToPythonConstCharPointer(e));
}
} else {
QString e = QString("Called ") + info->fullSignature() + " with wrong number of arguments: " + PythonQtConv::PyObjGetString(args);
QString e = QString("Called ") + info->fullSignature() + " with wrong number of arguments: " + limitString(PythonQtConv::PyObjGetString(args));
PyErr_SetString(PyExc_ValueError, QStringToPythonConstCharPointer(e));
}
}
Expand Down

0 comments on commit ea27670

Please sign in to comment.