diff options
author | Christian Tismer <tismer@stackless.com> | 2019-08-22 18:57:11 +0200 |
---|---|---|
committer | Christian Tismer <tismer@stackless.com> | 2019-08-29 15:44:07 +0200 |
commit | d4acbacd7a655e3ff9e17663a23ed0d822b84850 (patch) | |
tree | cfe5b2666c5e98c848b22ef7b493c775cff5aebc /sources/shiboken2/libshiboken/signature.cpp | |
parent | 4d63dfffb661115f58cca60c80c4649ba982e01b (diff) |
signature: Support typing.Optional[T] and refine a bit
The signature was missing "typing.Optional[T]" which has to be wrapped
around any argument with a default value of "None".
This is the only case where the repr of a type looks different than
it was written, because it renders as "typing.Union[T, NoneType]".
Solving that by redefining a few typing structures was way too
hard and too error prone. It was finally solved by a regex replacemet
that is run as a post process in generate_pyi.py .
The enumerations are now even more complete, since toplevel enums
are also included. This had the effect that enums with Python
keywords were revealed, and so the function "createEnumItem" had
to be modified.
The order of creation was also changed to avoid name clashes.
The overall structure was improved, and instead of parsing the
generated signatures to find out if something is a class method,
this is now very cleanly implemented as an inquiry to get_signature().
I tried to make sense of the flags structure that comes with many
enums. PyQt5 has a standard set of "__...__" methods without useful
signature information. I could mimick that as well, but that would
create a whole lot of pointless extra information. We should decide
later if it makes sense to include that. Right now the flags
structures show the class name, only.
This patch will be merged with the 5.14 branch. The additions of this
patch could fortunately be placed into areas which do almost not
overlap with the 5.14 signature additions.
Change-Id: Ie513e15917b04d746ab597fb7a9eb1fd766f7c73
Fixes: PYSIDE-1079
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/shiboken2/libshiboken/signature.cpp')
-rw-r--r-- | sources/shiboken2/libshiboken/signature.cpp | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/sources/shiboken2/libshiboken/signature.cpp b/sources/shiboken2/libshiboken/signature.cpp index 2cade65e0..0afdbcdc9 100644 --- a/sources/shiboken2/libshiboken/signature.cpp +++ b/sources/shiboken2/libshiboken/signature.cpp @@ -99,7 +99,7 @@ CreateSignature(PyObject *props, PyObject *key) { /* * Here is the new function to create all signatures. It simply calls - * into Python and creates a signature object for a dummy-function. + * into Python and creates a signature object directly. * This is so much simpler than using all the attributes explicitly * to support '_signature_is_functionlike()'. */ @@ -313,7 +313,7 @@ pyside_tp_get___signature__(PyObject *obtype_mod, const char *modifier) // forward static PyObject * -GetSignature_Cached(PyObject *props, const char *sig_kind, const char *modifier); +GetSignature_Cached(PyObject *props, const char *func_kind, const char *modifier); static PyObject * GetTypeKey(PyObject *ob) @@ -381,16 +381,16 @@ GetSignature_Function(PyObject *obfunc, const char *modifier) Py_RETURN_NONE; int flags = PyCFunction_GET_FLAGS(obfunc); - const char *sig_kind; + const char *func_kind; if (PyModule_Check(obtype_mod)) - sig_kind = "function"; + func_kind = "function"; else if (flags & METH_CLASS) - sig_kind = "classmethod"; + func_kind = "classmethod"; else if (flags & METH_STATIC) - sig_kind = "staticmethod"; + func_kind = "staticmethod"; else - sig_kind = "method"; - return GetSignature_Cached(props, sig_kind, modifier); + func_kind = "method"; + return GetSignature_Cached(props, func_kind, modifier); } static PyObject * @@ -427,11 +427,15 @@ GetSignature_TypeMod(PyObject *ob, const char *modifier) } static PyObject * -GetSignature_Cached(PyObject *props, const char *sig_kind, const char *modifier) +GetSignature_Cached(PyObject *props, const char *func_kind, const char *modifier) { + // Special case: We want to know the func_kind. + if (modifier && strcmp(modifier, "__func_kind__") == 0) + return Py_BuildValue("s", func_kind); + Shiboken::AutoDecRef key(modifier == nullptr - ? Py_BuildValue("s", sig_kind) - : Py_BuildValue("(ss)", sig_kind, modifier)); + ? Py_BuildValue("s", func_kind) + : Py_BuildValue("(ss)", func_kind, modifier)); PyObject *value = PyDict_GetItem(props, key); if (value == nullptr) { // we need to compute a signature object |