From d82a1d1d6eb6bb38f4f8f1070cf2cd2f2bb2a6c1 Mon Sep 17 00:00:00 2001 From: Kay Hayen Date: Sun, 21 Mar 2021 12:31:17 +0100 Subject: [PATCH 2/6] Nuitka: Allow for compiled functions and methods too * All that is wanted here is to look up attributes of slots, e.g. code objects that Nuitka also has. * Made the lookup that was duplicated a function where looking up the name is optional. --- sources/pyside6/libpyside/pysidesignal.cpp | 109 ++++++++++++++++++--- 1 file changed, 94 insertions(+), 15 deletions(-) diff --git a/sources/pyside6/libpyside/pysidesignal.cpp b/sources/pyside6/libpyside/pysidesignal.cpp index 8533c2a48..bd9a2c41e 100644 --- a/sources/pyside6/libpyside/pysidesignal.cpp +++ b/sources/pyside6/libpyside/pysidesignal.cpp @@ -307,6 +307,76 @@ static void signalInstanceFree(void *self) Py_TYPE(pySelf)->tp_base->tp_free(self); } +// PYSIDE-1523: PyFunction_Check is not accepting compiled functions and +// PyMethod_Check is not allowing compiled methods, therefore also lookup +// "im_func" and "__code__" attributes, we allow for that with a dedicated +// function handling both. +static void extractFunctionArgumentsFromSlot(PyObject *slot, PyObject *& function, PepCodeObject *& objCode, bool &isMethod, QByteArray *functionName) +{ + isMethod = PyMethod_Check(slot); + bool isFunction = PyFunction_Check(slot); + + function = NULL; + objCode = NULL; + + if (isMethod || isFunction) { + function = isMethod ? PyMethod_GET_FUNCTION(slot) : slot; + objCode = reinterpret_cast(PyFunction_GET_CODE(function)); + + if (functionName != nullptr) { + *functionName = Shiboken::String::toCString(PepFunction_GetName(function)); + } + } else if (PyObject_HasAttrString(slot, "im_func")) { + // PYSIDE-1523: PyFunction_Check and PyMethod_Check are not accepting compiled forms, we + // just go by attributes. + isMethod = true; + + function = PyObject_GetAttrString(slot, "im_func"); + + // Not retaining a reference inline with what PyMethod_GET_FUNCTION does. + Py_DECREF(function); + + if (functionName != nullptr) { + PyObject *name = PyObject_GetAttrString(function, "__name__"); + *functionName = Shiboken::String::toCString(name); + // Not retaining a reference inline with what PepFunction_GetName does. + Py_DECREF(name); + } + + objCode = reinterpret_cast(PyObject_GetAttrString(function, "__code__")); + // Not retaining a reference inline with what PyFunction_GET_CODE does. + Py_XDECREF(objCode); + + if (objCode == nullptr) { + // Should not happen, but lets handle it gracefully, maybe Nuitka one day + // makes these optional, or somebody defined a type named like it without + // it being actually being that. + function = NULL; + } + } else if (strcmp(Py_TYPE(slot)->tp_name, "compiled_function")==0) { + isMethod = false; + function = slot; + + if (functionName != nullptr) { + PyObject *name = PyObject_GetAttrString(function, "__name__"); + *functionName = Shiboken::String::toCString(name); + // Not retaining a reference inline with what PepFunction_GetName does. + Py_DECREF(name); + } + + objCode = reinterpret_cast(PyObject_GetAttrString(function, "__code__")); + // Not retaining a reference inline with what PyFunction_GET_CODE does. + Py_XDECREF(objCode); + + if (objCode == nullptr) { + // Should not happen, but lets handle it gracefully, maybe Nuitka one day + // makes these optional, or somebody defined a type named like it without + // it being actually being that. + function = NULL; + } + } +} + static PyObject *signalInstanceConnect(PyObject *self, PyObject *args, PyObject *kwds) { PyObject *slot = nullptr; @@ -347,18 +417,18 @@ static PyObject *signalInstanceConnect(PyObject *self, PyObject *args, PyObject } else { // Check signature of the slot (method or function) to match signal int slotArgs = -1; - bool useSelf = false; - bool isMethod = PyMethod_Check(slot); - bool isFunction = PyFunction_Check(slot); bool matchedSlot = false; - QByteArray functionName; + // QByteArray functionName; PySideSignalInstance *it = source; - if (isMethod || isFunction) { - PyObject *function = isMethod ? PyMethod_GET_FUNCTION(slot) : slot; - auto *objCode = reinterpret_cast(PyFunction_GET_CODE(function)); - useSelf = isMethod; + PyObject *function = NULL; + PepCodeObject *objCode = NULL; + bool useSelf = false; + + extractFunctionArgumentsFromSlot(slot, function, objCode, useSelf, NULL); + + if (function != nullptr) { slotArgs = PepCode_GET_FLAGS(objCode) & CO_VARARGS ? -1 : PepCode_GET_ARGCOUNT(objCode); if (useSelf) slotArgs -= 1; @@ -944,15 +1014,14 @@ QString getCallbackSignature(const char *signal, QObject *receiver, PyObject *ca { QByteArray functionName; int numArgs = -1; + + PyObject *function = NULL; + PepCodeObject *objCode = NULL; bool useSelf = false; - bool isMethod = PyMethod_Check(callback); - bool isFunction = PyFunction_Check(callback); - if (isMethod || isFunction) { - PyObject *function = isMethod ? PyMethod_GET_FUNCTION(callback) : callback; - auto objCode = reinterpret_cast(PyFunction_GET_CODE(function)); - functionName = Shiboken::String::toCString(PepFunction_GetName(function)); - useSelf = isMethod; + extractFunctionArgumentsFromSlot(callback, function, objCode, useSelf, &functionName); + + if (function != nullptr) { numArgs = PepCode_GET_FLAGS(objCode) & CO_VARARGS ? -1 : PepCode_GET_ARGCOUNT(objCode); } else if (PyCFunction_Check(callback)) { const PyCFunctionObject *funcObj = reinterpret_cast(callback); @@ -1025,6 +1094,16 @@ QString codeCallbackName(PyObject *callback, const QString &funcName) if (PyMethod_Check(callback)) { PyObject *self = PyMethod_GET_SELF(callback); PyObject *func = PyMethod_GET_FUNCTION(callback); + return funcName + QString::number(quint64(self), 16) + QString::number(quint64(func), 16); + } else if (PyObject_HasAttrString(callback, "im_func") && PyObject_HasAttrString(callback, "im_self")) { + PyObject *self = PyObject_GetAttrString(callback, "im_self"); + // Not retaining a reference inline with what PyMethod_GET_SELF does. + Py_DECREF(self); + + PyObject *func = PyObject_GetAttrString(callback, "im_func"); + // Not retaining a reference inline with what PyMethod_GET_FUNCTION does. + Py_DECREF(func); + return funcName + QString::number(quint64(self), 16) + QString::number(quint64(func), 16); } return funcName + QString::number(quint64(callback), 16); -- 2.30.0.windows.2