diff options
author | Christian Tismer <tismer@stackless.com> | 2018-07-14 15:10:56 +0200 |
---|---|---|
committer | Christian Tismer <tismer@stackless.com> | 2019-01-10 09:15:44 +0000 |
commit | a0543241df2273ad60a4c92e4ffe6e0cfb1042b9 (patch) | |
tree | 0e12bac1f772c6f8a985c63709e89231c3889e56 /sources/shiboken2/libshiboken | |
parent | 77265fcedc3411fb70e149cf9d9cd4f549de80e6 (diff) |
Produce TypeError Messages Using the Signature Module
The TypeError messages can now be produced, based upon the
signature module.
As a feature under test, we produce ValueErrors instead in
certain cases. This will probably improve, later.
We are currently investigating how much can be determined,
automatically.
Task-number: PYSIDE-795
Change-Id: Ie8a648beaf8a3bed388e3c01ba501bb36859722e
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/shiboken2/libshiboken')
-rw-r--r-- | sources/shiboken2/libshiboken/basewrapper.cpp | 40 | ||||
-rw-r--r-- | sources/shiboken2/libshiboken/basewrapper.h | 4 | ||||
-rw-r--r-- | sources/shiboken2/libshiboken/signature.cpp | 29 | ||||
-rw-r--r-- | sources/shiboken2/libshiboken/signature.h | 1 |
4 files changed, 37 insertions, 37 deletions
diff --git a/sources/shiboken2/libshiboken/basewrapper.cpp b/sources/shiboken2/libshiboken/basewrapper.cpp index e820e749b..3e1a7e8e5 100644 --- a/sources/shiboken2/libshiboken/basewrapper.cpp +++ b/sources/shiboken2/libshiboken/basewrapper.cpp @@ -596,42 +596,10 @@ void init() shibokenAlreadInitialised = true; } -void setErrorAboutWrongArguments(PyObject* args, const char* funcName, const char** cppOverloads) -{ - std::string msg; - std::string params; - if (args) { - if (PyTuple_Check(args)) { - for (Py_ssize_t i = 0, max = PyTuple_GET_SIZE(args); i < max; ++i) { - if (i) - params += ", "; - PyObject* arg = PyTuple_GET_ITEM(args, i); - params += Py_TYPE(arg)->tp_name; - } - } else { - params = Py_TYPE(args)->tp_name; - } - } - - if (!cppOverloads) { - msg = "'" + std::string(funcName) + "' called with wrong argument types: " + params; - } else { - msg = "'" + std::string(funcName) + "' called with wrong argument types:\n "; - msg += funcName; - msg += '('; - msg += params; - msg += ")\n"; - msg += "Supported signatures:"; - for (int i = 0; cppOverloads[i]; ++i) { - msg += "\n "; - msg += funcName; - msg += '('; - msg += cppOverloads[i]; - msg += ')'; - } - } - PyErr_SetString(PyExc_TypeError, msg.c_str()); - +// setErrorAboutWrongArguments now gets overload info from the signature module. +void setErrorAboutWrongArguments(PyObject *args, const char *funcName) +{ + SetError_Argument(args, funcName); } class FindBaseTypeVisitor : public HierarchyVisitor diff --git a/sources/shiboken2/libshiboken/basewrapper.h b/sources/shiboken2/libshiboken/basewrapper.h index 15682c600..e1cc64ba9 100644 --- a/sources/shiboken2/libshiboken/basewrapper.h +++ b/sources/shiboken2/libshiboken/basewrapper.h @@ -141,7 +141,9 @@ void callCppDestructor(void* cptr) * Shiboken::importModule is DEPRECATED. Use Shiboken::Module::import() instead. */ SBK_DEPRECATED(LIBSHIBOKEN_API bool importModule(const char* moduleName, PyTypeObject*** cppApiPtr)); -LIBSHIBOKEN_API void setErrorAboutWrongArguments(PyObject* args, const char* funcName, const char** cppOverloads); + +// setErrorAboutWrongArguments now gets overload info from the signature module. +LIBSHIBOKEN_API void setErrorAboutWrongArguments(PyObject* args, const char* funcName); namespace ObjectType { diff --git a/sources/shiboken2/libshiboken/signature.cpp b/sources/shiboken2/libshiboken/signature.cpp index 564e5fcef..a8874e2e0 100644 --- a/sources/shiboken2/libshiboken/signature.cpp +++ b/sources/shiboken2/libshiboken/signature.cpp @@ -74,6 +74,7 @@ typedef struct safe_globals_struc { // init part 2: run module PyObject *sigparse_func; PyObject *createsig_func; + PyObject *seterror_argument_func; } safe_globals_struc, *safe_globals; static safe_globals pyside_globals = 0; @@ -510,6 +511,9 @@ init_phase_2(safe_globals_struc *p, PyMethodDef *methods) p->createsig_func = PyObject_GetAttrString(p->helper_module, "create_signature"); if (p->createsig_func == NULL) goto error; + p->seterror_argument_func = PyObject_GetAttrString(p->helper_module, "seterror_argument"); + if (p->seterror_argument_func == NULL) + goto error; return 0; error: @@ -950,4 +954,29 @@ FinishSignatureInitialization(PyObject *module, const char *signatures) } } +void +SetError_Argument(PyObject *args, const char *func_name) +{ + /* + * This function replaces the type error construction with extra + * overloads parameter in favor of using the signature module. + * Error messages are rare, so we do it completely in Python. + */ + init_module_1(); + init_module_2(); + Shiboken::AutoDecRef res(PyObject_CallFunction( + pyside_globals->seterror_argument_func, + const_cast<char *>("(Os)"), args, func_name)); + if (res.isNull()) { + PyErr_Print(); + Py_FatalError("seterror_argument did not receive a result"); + } + PyObject *err, *msg; + if (!PyArg_UnpackTuple(res, func_name, 2, 2, &err, &msg)) { + PyErr_Print(); + Py_FatalError("unexpected failure in seterror_argument"); + } + PyErr_SetObject(err, msg); +} + } //extern "C" diff --git a/sources/shiboken2/libshiboken/signature.h b/sources/shiboken2/libshiboken/signature.h index b65317662..c850698a6 100644 --- a/sources/shiboken2/libshiboken/signature.h +++ b/sources/shiboken2/libshiboken/signature.h @@ -47,6 +47,7 @@ extern "C" LIBSHIBOKEN_API int SbkSpecial_Type_Ready(PyObject *, PyTypeObject *, const char *); //WS LIBSHIBOKEN_API void FinishSignatureInitialization(PyObject *, const char *); +LIBSHIBOKEN_API void SetError_Argument(PyObject *, const char *); } // extern "C" |