diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-10-27 14:02:43 +0200 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-11-01 17:52:29 +0200 |
| commit | 8b7f7fa1eaa44fcaa3e6d494793b2d5a27590a06 (patch) | |
| tree | 4278f50c98d5cf495d480a58e4b9de34bbb3a7ea /sources/pyside6 | |
| parent | 7004b33373def9b8b7e31ee93074fe309e8025fe (diff) | |
Improve the error handling of the QML singleton creation
Make the error messages more verbose and add additional checks.
Move the INCREF out of the check function.
Task-number: PYSIDE-2432
Pick-to: 6.6
Change-Id: I60e4ad90685f9a1a013aafb5b68503fcc59cec7d
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/pyside6')
| -rw-r--r-- | sources/pyside6/libpysideqml/pysideqmlregistertype.cpp | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/sources/pyside6/libpysideqml/pysideqmlregistertype.cpp b/sources/pyside6/libpysideqml/pysideqmlregistertype.cpp index 247d2b1b8..b24c6b800 100644 --- a/sources/pyside6/libpysideqml/pysideqmlregistertype.cpp +++ b/sources/pyside6/libpysideqml/pysideqmlregistertype.cpp @@ -284,16 +284,28 @@ int qmlRegisterType(PyObject *pyObj, const char *uri, int versionMajor, int vers // Singleton helpers -bool checkSingletonCallback(PyObject *callback) +// Check the arguments of a singleton callback (C++: "QJSValue cb(QQmlEngine *, QJSEngine *)", +// but we drop the QJSEngine since it will be the same as QQmlEngine when the latter exists. +static bool checkSingletonCallback(PyObject *callback) { + if (callback == nullptr) { + PyErr_SetString(PyExc_TypeError, "No callback specified."); + return false; + } if (PyCallable_Check(callback) == 0) { - PyErr_Format(PyExc_TypeError, "Invalid callback specified."); + PyErr_Format(PyExc_TypeError, "Invalid callback specified (%S).", callback); return false; } Shiboken::AutoDecRef funcCode(PyObject_GetAttrString(callback, "__code__")); - Shiboken::AutoDecRef argCount(PyObject_GetAttrString(funcCode, "co_argcount")); - if (PyLong_AsLong(argCount) != 1) { - PyErr_Format(PyExc_TypeError, "Callback has a bad parameter count."); + if (funcCode.isNull()) { + PyErr_Format(PyExc_TypeError, "Cannot retrieve code of callback (%S).", callback); + return false; + } + Shiboken::AutoDecRef argCountAttr(PyObject_GetAttrString(funcCode, "co_argcount")); + const int argCount = PyLong_AsLong(argCountAttr.object()); + if (argCount != 1) { + PyErr_Format(PyExc_TypeError, "Callback (%S) has %d parameter(s), expected one.", + callback, argCount); return false; } // Make sure the callback never gets deallocated |
