1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt for Python.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "dynamicqmetaobject_p.h"
41 #include "pysidesignal_p.h"
42 #include "pysideslot_p.h"
43
44 #include <shiboken.h>
45
46 #include <QtCore/QMetaObject>
47 #include <QtCore/QString>
48 #include <signature.h>
49
50 using namespace Shiboken;
51
52 struct SlotData
53 {
54 QByteArray name;
55 QByteArray args;
56 QByteArray resultType;
57 };
58
59 typedef struct
60 {
61 PyObject_HEAD
62 SlotData *slotData;
63 } PySideSlot;
64
65 extern "C"
66 {
67
68 static int slotTpInit(PyObject *, PyObject *, PyObject *);
69 static PyObject *slotCall(PyObject *, PyObject *, PyObject *);
70
71 // Class Definition -----------------------------------------------
72 static PyType_Slot PySideSlotType_slots[] = {
73 {Py_tp_call, (void *)slotCall},
74 {Py_tp_init, (void *)slotTpInit},
75 {Py_tp_new, (void *)PyType_GenericNew},
76 {Py_tp_dealloc, (void *)Sbk_object_dealloc},
77 {0, 0}
78 };
79 static PyType_Spec PySideSlotType_spec = {
80 "2:PySide2.QtCore.Slot",
81 sizeof(PySideSlot),
82 0,
83 Py_TPFLAGS_DEFAULT,
84 PySideSlotType_slots,
85 };
86
87
PySideSlotTypeF(void)88 static PyTypeObject *PySideSlotTypeF(void)
89 {
90 static PyTypeObject *type = reinterpret_cast<PyTypeObject *>(
91 SbkType_FromSpec(&PySideSlotType_spec));
92 return type;
93 }
94
slotTpInit(PyObject * self,PyObject * args,PyObject * kw)95 int slotTpInit(PyObject *self, PyObject *args, PyObject *kw)
96 {
97 static PyObject *emptyTuple = nullptr;
98 static const char *kwlist[] = {"name", "result", nullptr};
99 char *argName = nullptr;
100 PyObject *argResult = nullptr;
101
102 if (emptyTuple == 0)
103 emptyTuple = PyTuple_New(0);
104
105 if (!PyArg_ParseTupleAndKeywords(emptyTuple, kw, "|sO:QtCore.Slot",
106 const_cast<char **>(kwlist), &argName, &argResult)) {
107 return -1;
108 }
109
110 PySideSlot *data = reinterpret_cast<PySideSlot *>(self);
111 if (!data->slotData)
112 data->slotData = new SlotData;
113 for(Py_ssize_t i = 0, i_max = PyTuple_Size(args); i < i_max; i++) {
114 PyObject *argType = PyTuple_GET_ITEM(args, i);
115 const auto typeName = PySide::Signal::getTypeName(argType);
116 if (typeName.isEmpty()) {
117 PyErr_Format(PyExc_TypeError, "Unknown signal argument type: %s", Py_TYPE(argType)->tp_name);
118 return -1;
119 }
120 if (!data->slotData->args.isEmpty())
121 data->slotData->args += ',';
122 data->slotData->args += typeName;
123 }
124
125 if (argName)
126 data->slotData->name = argName;
127
128 data->slotData->resultType = argResult
129 ? PySide::Signal::getTypeName(argResult) : PySide::Signal::voidType();
130
131 return 0;
132 }
133
slotCall(PyObject * self,PyObject * args,PyObject *)134 PyObject *slotCall(PyObject *self, PyObject *args, PyObject * /* kw */)
135 {
136 static PyObject *pySlotName = nullptr;
137 PyObject *callback;
138 callback = PyTuple_GetItem(args, 0);
139 Py_INCREF(callback);
140
141 if (Py_TYPE(callback)->tp_call != nullptr) {
142 PySideSlot *data = reinterpret_cast<PySideSlot *>(self);
143
144 if (!data->slotData)
145 data->slotData = new SlotData;
146
147 if (data->slotData->name.isEmpty()) {
148 // PYSIDE-198: Use PyObject_GetAttr instead of PepFunction_GetName to support Nuitka.
149 AutoDecRef funcName(PyObject_GetAttr(callback, PyMagicName::name()));
150 data->slotData->name = String::toCString(funcName);
151 }
152 const QByteArray returnType = QMetaObject::normalizedType(data->slotData->resultType);
153 const QByteArray signature =
154 returnType + ' ' + data->slotData->name + '(' + data->slotData->args + ')';
155
156 if (!pySlotName)
157 pySlotName = String::fromCString(PYSIDE_SLOT_LIST_ATTR);
158
159 PyObject *pySignature = String::fromCString(signature);
160 PyObject *signatureList = 0;
161 if (PyObject_HasAttr(callback, pySlotName)) {
162 signatureList = PyObject_GetAttr(callback, pySlotName);
163 } else {
164 signatureList = PyList_New(0);
165 PyObject_SetAttr(callback, pySlotName, signatureList);
166 Py_DECREF(signatureList);
167 }
168
169 PyList_Append(signatureList, pySignature);
170 Py_DECREF(pySignature);
171
172 //clear data
173 delete data->slotData;
174 data->slotData = nullptr;
175 return callback;
176 }
177 return callback;
178 }
179
180 } // extern "C"
181
182 namespace PySide {
183 namespace Slot {
184
185 static const char *Slot_SignatureStrings[] = {
186 "PySide2.QtCore.Slot(*types:type,name:str=nullptr,result:str=nullptr)->typing.Callable[...,typing.Optional[str]]",
187 nullptr}; // Sentinel
188
init(PyObject * module)189 void init(PyObject *module)
190 {
191 if (InitSignatureStrings(PySideSlotTypeF(), Slot_SignatureStrings) < 0)
192 return;
193
194 Py_INCREF(PySideSlotTypeF());
195 PyModule_AddObject(module, "Slot", reinterpret_cast<PyObject *>(PySideSlotTypeF()));
196 }
197
198 } // namespace Slot
199 } // namespace PySide
200