##// END OF EJS Templates
added initial support for public member variable access via getters/setters...
florianlink -
r113:ab319fdfc38d
parent child
Show More
@@ -1,620 +1,641
1 /*
1 /*
2 *
2 *
3 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
3 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
4 *
4 *
5 * This library is free software; you can redistribute it and/or
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
8 * version 2.1 of the License, or (at your option) any later version.
9 *
9 *
10 * This library is distributed in the hope that it will be useful,
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
13 * Lesser General Public License for more details.
14 *
14 *
15 * Further, this software is distributed without any warranty that it is
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
20 * other software, or any other product whatsoever.
21 *
21 *
22 * You should have received a copy of the GNU Lesser General Public
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
25 *
26 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
26 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
27 * 28359 Bremen, Germany or:
27 * 28359 Bremen, Germany or:
28 *
28 *
29 * http://www.mevis.de
29 * http://www.mevis.de
30 *
30 *
31 */
31 */
32
32
33 //----------------------------------------------------------------------------------
33 //----------------------------------------------------------------------------------
34 /*!
34 /*!
35 // \file PythonQtInstanceWrapper.cpp
35 // \file PythonQtInstanceWrapper.cpp
36 // \author Florian Link
36 // \author Florian Link
37 // \author Last changed by $Author: florian $
37 // \author Last changed by $Author: florian $
38 // \date 2006-05
38 // \date 2006-05
39 */
39 */
40 //----------------------------------------------------------------------------------
40 //----------------------------------------------------------------------------------
41
41
42 #include "PythonQtInstanceWrapper.h"
42 #include "PythonQtInstanceWrapper.h"
43 #include <QObject>
43 #include <QObject>
44 #include "PythonQt.h"
44 #include "PythonQt.h"
45 #include "PythonQtSlot.h"
45 #include "PythonQtSlot.h"
46 #include "PythonQtClassInfo.h"
46 #include "PythonQtClassInfo.h"
47 #include "PythonQtConversion.h"
47 #include "PythonQtConversion.h"
48 #include "PythonQtClassWrapper.h"
48 #include "PythonQtClassWrapper.h"
49
49
50 PythonQtClassInfo* PythonQtInstanceWrapperStruct::classInfo()
50 PythonQtClassInfo* PythonQtInstanceWrapperStruct::classInfo()
51 {
51 {
52 // take the class info from our type object
52 // take the class info from our type object
53 return ((PythonQtClassWrapper*)ob_type)->_classInfo;
53 return ((PythonQtClassWrapper*)ob_type)->_classInfo;
54 }
54 }
55
55
56 static void PythonQtInstanceWrapper_deleteObject(PythonQtInstanceWrapper* self, bool force = false) {
56 static void PythonQtInstanceWrapper_deleteObject(PythonQtInstanceWrapper* self, bool force = false) {
57
57
58 // is this a C++ wrapper?
58 // is this a C++ wrapper?
59 if (self->_wrappedPtr) {
59 if (self->_wrappedPtr) {
60 //mlabDebugConst("Python","c++ wrapper removed " << self->_wrappedPtr << " " << self->_obj->className() << " " << self->classInfo()->wrappedClassName().latin1());
60 //mlabDebugConst("Python","c++ wrapper removed " << self->_wrappedPtr << " " << self->_obj->className() << " " << self->classInfo()->wrappedClassName().latin1());
61
61
62 PythonQt::priv()->removeWrapperPointer(self->_wrappedPtr);
62 PythonQt::priv()->removeWrapperPointer(self->_wrappedPtr);
63 // we own our qobject, so we delete it now:
63 // we own our qobject, so we delete it now:
64 delete self->_obj;
64 delete self->_obj;
65 self->_obj = NULL;
65 self->_obj = NULL;
66 if (force || self->classInfo()->hasOwnerMethodButNoOwner(self->_wrappedPtr) || self->_ownedByPythonQt) {
66 if (force || self->classInfo()->hasOwnerMethodButNoOwner(self->_wrappedPtr) || self->_ownedByPythonQt) {
67 int type = self->classInfo()->metaTypeId();
67 int type = self->classInfo()->metaTypeId();
68 if (self->_useQMetaTypeDestroy && type>=0) {
68 if (self->_useQMetaTypeDestroy && type>=0) {
69 // use QMetaType to destroy the object
69 // use QMetaType to destroy the object
70 QMetaType::destroy(type, self->_wrappedPtr);
70 QMetaType::destroy(type, self->_wrappedPtr);
71 } else {
71 } else {
72 PythonQtSlotInfo* slot = self->classInfo()->destructor();
72 PythonQtSlotInfo* slot = self->classInfo()->destructor();
73 if (slot) {
73 if (slot) {
74 void* args[2];
74 void* args[2];
75 args[0] = NULL;
75 args[0] = NULL;
76 args[1] = &self->_wrappedPtr;
76 args[1] = &self->_wrappedPtr;
77 slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, slot->slotIndex(), args);
77 slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, slot->slotIndex(), args);
78 self->_wrappedPtr = NULL;
78 self->_wrappedPtr = NULL;
79 } else {
79 } else {
80 if (type>=0) {
80 if (type>=0) {
81 // use QMetaType to destroy the object
81 // use QMetaType to destroy the object
82 QMetaType::destroy(type, self->_wrappedPtr);
82 QMetaType::destroy(type, self->_wrappedPtr);
83 } else {
83 } else {
84 // TODO: warn about not being able to destroy the object?
84 // TODO: warn about not being able to destroy the object?
85 }
85 }
86 }
86 }
87 }
87 }
88 }
88 }
89 } else {
89 } else {
90 //mlabDebugConst("Python","qobject wrapper removed " << self->_obj->className() << " " << self->classInfo()->wrappedClassName().latin1());
90 //mlabDebugConst("Python","qobject wrapper removed " << self->_obj->className() << " " << self->classInfo()->wrappedClassName().latin1());
91 if (self->_objPointerCopy) {
91 if (self->_objPointerCopy) {
92 PythonQt::priv()->removeWrapperPointer(self->_objPointerCopy);
92 PythonQt::priv()->removeWrapperPointer(self->_objPointerCopy);
93 }
93 }
94 if (self->_obj) {
94 if (self->_obj) {
95 if (force || self->_ownedByPythonQt) {
95 if (force || self->_ownedByPythonQt) {
96 if (force || !self->_obj->parent()) {
96 if (force || !self->_obj->parent()) {
97 delete self->_obj;
97 delete self->_obj;
98 }
98 }
99 } else {
99 } else {
100 if (self->_obj->parent()==NULL) {
100 if (self->_obj->parent()==NULL) {
101 // tell someone who is interested that the qobject is no longer wrapped, if it has no parent
101 // tell someone who is interested that the qobject is no longer wrapped, if it has no parent
102 PythonQt::qObjectNoLongerWrappedCB(self->_obj);
102 PythonQt::qObjectNoLongerWrappedCB(self->_obj);
103 }
103 }
104 }
104 }
105 }
105 }
106 }
106 }
107 self->_obj = NULL;
107 self->_obj = NULL;
108 }
108 }
109
109
110 static void PythonQtInstanceWrapper_dealloc(PythonQtInstanceWrapper* self)
110 static void PythonQtInstanceWrapper_dealloc(PythonQtInstanceWrapper* self)
111 {
111 {
112 PythonQtInstanceWrapper_deleteObject(self);
112 PythonQtInstanceWrapper_deleteObject(self);
113 self->_obj.~QPointer<QObject>();
113 self->_obj.~QPointer<QObject>();
114 self->ob_type->tp_free((PyObject*)self);
114 self->ob_type->tp_free((PyObject*)self);
115 }
115 }
116
116
117 static PyObject* PythonQtInstanceWrapper_new(PyTypeObject *type, PyObject * /*args*/, PyObject * /*kwds*/)
117 static PyObject* PythonQtInstanceWrapper_new(PyTypeObject *type, PyObject * /*args*/, PyObject * /*kwds*/)
118 {
118 {
119 //PythonQtClassWrapper *classType = (PythonQtClassWrapper*)type;
119 //PythonQtClassWrapper *classType = (PythonQtClassWrapper*)type;
120 PythonQtInstanceWrapper *self;
120 PythonQtInstanceWrapper *self;
121 static PyObject* emptyTuple = NULL;
121 static PyObject* emptyTuple = NULL;
122 if (emptyTuple==NULL) {
122 if (emptyTuple==NULL) {
123 emptyTuple = PyTuple_New(0);
123 emptyTuple = PyTuple_New(0);
124 }
124 }
125
125
126 self = (PythonQtInstanceWrapper*)PyBaseObject_Type.tp_new(type, emptyTuple, NULL);
126 self = (PythonQtInstanceWrapper*)PyBaseObject_Type.tp_new(type, emptyTuple, NULL);
127
127
128 if (self != NULL) {
128 if (self != NULL) {
129 new (&self->_obj) QPointer<QObject>();
129 new (&self->_obj) QPointer<QObject>();
130 self->_wrappedPtr = NULL;
130 self->_wrappedPtr = NULL;
131 self->_ownedByPythonQt = false;
131 self->_ownedByPythonQt = false;
132 self->_useQMetaTypeDestroy = false;
132 self->_useQMetaTypeDestroy = false;
133 self->_isShellInstance = false;
133 self->_isShellInstance = false;
134 }
134 }
135 return (PyObject *)self;
135 return (PyObject *)self;
136 }
136 }
137
137
138 int PythonQtInstanceWrapper_init(PythonQtInstanceWrapper * self, PyObject * args, PyObject * kwds)
138 int PythonQtInstanceWrapper_init(PythonQtInstanceWrapper * self, PyObject * args, PyObject * kwds)
139 {
139 {
140 if (args == PythonQtPrivate::dummyTuple()) {
140 if (args == PythonQtPrivate::dummyTuple()) {
141 // we are called from the internal PythonQt API, so our data will be filled later on...
141 // we are called from the internal PythonQt API, so our data will be filled later on...
142 return 0;
142 return 0;
143 }
143 }
144
144
145 // we are called from python, try to construct our object
145 // we are called from python, try to construct our object
146 if (self->classInfo()->constructors()) {
146 if (self->classInfo()->constructors()) {
147 void* directCPPPointer = NULL;
147 void* directCPPPointer = NULL;
148 PythonQtSlotFunction_CallImpl(self->classInfo(), NULL, self->classInfo()->constructors(), args, kwds, NULL, &directCPPPointer);
148 PythonQtSlotFunction_CallImpl(self->classInfo(), NULL, self->classInfo()->constructors(), args, kwds, NULL, &directCPPPointer);
149 if (PyErr_Occurred()) {
149 if (PyErr_Occurred()) {
150 return -1;
150 return -1;
151 }
151 }
152 if (directCPPPointer) {
152 if (directCPPPointer) {
153 // change ownershipflag to be owned by PythonQt
153 // change ownershipflag to be owned by PythonQt
154 self->_ownedByPythonQt = true;
154 self->_ownedByPythonQt = true;
155 self->_useQMetaTypeDestroy = false;
155 self->_useQMetaTypeDestroy = false;
156 if (self->classInfo()->isCPPWrapper()) {
156 if (self->classInfo()->isCPPWrapper()) {
157 self->_wrappedPtr = directCPPPointer;
157 self->_wrappedPtr = directCPPPointer;
158 // TODO xxx: if there is a wrapper factory, we might want to generate a wrapper for our class?!
158 // TODO xxx: if there is a wrapper factory, we might want to generate a wrapper for our class?!
159 } else {
159 } else {
160 self->setQObject((QObject*)directCPPPointer);
160 self->setQObject((QObject*)directCPPPointer);
161 }
161 }
162 // register with PythonQt
162 // register with PythonQt
163 PythonQt::priv()->addWrapperPointer(directCPPPointer, self);
163 PythonQt::priv()->addWrapperPointer(directCPPPointer, self);
164
164
165 PythonQtShellSetInstanceWrapperCB* cb = self->classInfo()->shellSetInstanceWrapperCB();
165 PythonQtShellSetInstanceWrapperCB* cb = self->classInfo()->shellSetInstanceWrapperCB();
166 if (cb) {
166 if (cb) {
167 // if we are a derived python class, we set the wrapper
167 // if we are a derived python class, we set the wrapper
168 // to activate the shell class, otherwise we just ignore that it is a shell...
168 // to activate the shell class, otherwise we just ignore that it is a shell...
169 // we detect it be checking if the type does not have PythonQtInstanceWrapper_Type as direct base class,
169 // we detect it be checking if the type does not have PythonQtInstanceWrapper_Type as direct base class,
170 // which is the case for all non-python derived types
170 // which is the case for all non-python derived types
171 if (((PyObject*)self)->ob_type->tp_base != &PythonQtInstanceWrapper_Type) {
171 if (((PyObject*)self)->ob_type->tp_base != &PythonQtInstanceWrapper_Type) {
172 // set the wrapper and remember that we have a shell instance!
172 // set the wrapper and remember that we have a shell instance!
173 (*cb)(directCPPPointer, self);
173 (*cb)(directCPPPointer, self);
174 self->_isShellInstance = true;
174 self->_isShellInstance = true;
175 }
175 }
176 }
176 }
177 }
177 }
178 } else {
178 } else {
179 QString error = QString("No constructors available for ") + self->classInfo()->className();
179 QString error = QString("No constructors available for ") + self->classInfo()->className();
180 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
180 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
181 return -1;
181 return -1;
182 }
182 }
183 return 0;
183 return 0;
184 }
184 }
185
185
186 static PyObject *PythonQtInstanceWrapper_classname(PythonQtInstanceWrapper* obj)
186 static PyObject *PythonQtInstanceWrapper_classname(PythonQtInstanceWrapper* obj)
187 {
187 {
188 return PyString_FromString(obj->ob_type->tp_name);
188 return PyString_FromString(obj->ob_type->tp_name);
189 }
189 }
190
190
191 static PyObject *PythonQtInstanceWrapper_help(PythonQtInstanceWrapper* obj)
191 static PyObject *PythonQtInstanceWrapper_help(PythonQtInstanceWrapper* obj)
192 {
192 {
193 return PythonQt::self()->helpCalled(obj->classInfo());
193 return PythonQt::self()->helpCalled(obj->classInfo());
194 }
194 }
195
195
196 static PyObject *PythonQtInstanceWrapper_delete(PythonQtInstanceWrapper * self)
196 static PyObject *PythonQtInstanceWrapper_delete(PythonQtInstanceWrapper * self)
197 {
197 {
198 PythonQtInstanceWrapper_deleteObject(self, true);
198 PythonQtInstanceWrapper_deleteObject(self, true);
199 Py_INCREF(Py_None);
199 Py_INCREF(Py_None);
200 return Py_None;
200 return Py_None;
201 }
201 }
202
202
203
203
204 static PyMethodDef PythonQtInstanceWrapper_methods[] = {
204 static PyMethodDef PythonQtInstanceWrapper_methods[] = {
205 {"className", (PyCFunction)PythonQtInstanceWrapper_classname, METH_NOARGS,
205 {"className", (PyCFunction)PythonQtInstanceWrapper_classname, METH_NOARGS,
206 "Return the classname of the object"
206 "Return the classname of the object"
207 },
207 },
208 {"help", (PyCFunction)PythonQtInstanceWrapper_help, METH_NOARGS,
208 {"help", (PyCFunction)PythonQtInstanceWrapper_help, METH_NOARGS,
209 "Shows the help of available methods for this class"
209 "Shows the help of available methods for this class"
210 },
210 },
211 {"delete", (PyCFunction)PythonQtInstanceWrapper_delete, METH_NOARGS,
211 {"delete", (PyCFunction)PythonQtInstanceWrapper_delete, METH_NOARGS,
212 "Deletes the C++ object (at your own risk, my friend!)"
212 "Deletes the C++ object (at your own risk, my friend!)"
213 },
213 },
214 {NULL, NULL, 0, NULL} /* Sentinel */
214 {NULL, NULL, 0, NULL} /* Sentinel */
215 };
215 };
216
216
217
217
218 static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
218 static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
219 {
219 {
220 const char *attributeName;
220 const char *attributeName;
221 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
221 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
222
222
223 if ((attributeName = PyString_AsString(name)) == NULL) {
223 if ((attributeName = PyString_AsString(name)) == NULL) {
224 return NULL;
224 return NULL;
225 }
225 }
226
226
227 if (qstrcmp(attributeName, "__dict__")==0) {
227 if (qstrcmp(attributeName, "__dict__")==0) {
228 PyObject* dict = PyBaseObject_Type.tp_getattro(obj, name);
228 PyObject* dict = PyBaseObject_Type.tp_getattro(obj, name);
229 dict = PyDict_Copy(dict);
229 dict = PyDict_Copy(dict);
230
230
231 if (wrapper->_obj) {
231 if (wrapper->_obj) {
232 // only the properties are missing, the rest is already available from
232 // only the properties are missing, the rest is already available from
233 // PythonQtClassWrapper...
233 // PythonQtClassWrapper...
234 QStringList l = wrapper->classInfo()->propertyList();
234 QStringList l = wrapper->classInfo()->propertyList();
235 foreach (QString name, l) {
235 foreach (QString name, l) {
236 PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
236 PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
237 if (o) {
237 if (o) {
238 PyDict_SetItemString(dict, name.toLatin1().data(), o);
238 PyDict_SetItemString(dict, name.toLatin1().data(), o);
239 Py_DECREF(o);
239 Py_DECREF(o);
240 } else {
240 } else {
241 std::cerr << "PythonQtInstanceWrapper: something is wrong, could not get attribute " << name.toLatin1().data();
241 std::cerr << "PythonQtInstanceWrapper: something is wrong, could not get attribute " << name.toLatin1().data();
242 }
242 }
243 }
243 }
244
244
245 QList<QByteArray> dynamicProps = wrapper->_obj->dynamicPropertyNames();
245 QList<QByteArray> dynamicProps = wrapper->_obj->dynamicPropertyNames();
246 foreach (QByteArray name, dynamicProps) {
246 foreach (QByteArray name, dynamicProps) {
247 PyObject* o = PyObject_GetAttrString(obj, name.data());
247 PyObject* o = PyObject_GetAttrString(obj, name.data());
248 if (o) {
248 if (o) {
249 PyDict_SetItemString(dict, name.data(), o);
249 PyDict_SetItemString(dict, name.data(), o);
250 Py_DECREF(o);
250 Py_DECREF(o);
251 } else {
251 } else {
252 std::cerr << "PythonQtInstanceWrapper: dynamic property could not be read " << name.data();
252 std::cerr << "PythonQtInstanceWrapper: dynamic property could not be read " << name.data();
253 }
253 }
254 }
254 }
255 }
255 }
256 // Note: we do not put children into the dict, is would look confusing?!
256 // Note: we do not put children into the dict, is would look confusing?!
257 return dict;
257 return dict;
258 }
258 }
259
259
260 // first look in super, to return derived methods from base object first
260 // first look in super, to return derived methods from base object first
261 PyObject* superAttr = PyBaseObject_Type.tp_getattro(obj, name);
261 PyObject* superAttr = PyBaseObject_Type.tp_getattro(obj, name);
262 if (superAttr) {
262 if (superAttr) {
263 return superAttr;
263 return superAttr;
264 }
264 }
265 PyErr_Clear();
265 PyErr_Clear();
266
266
267 // mlabDebugConst("Python","get " << attributeName);
267 // mlabDebugConst("Python","get " << attributeName);
268
268
269 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
269 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
270 switch (member._type) {
270 switch (member._type) {
271 case PythonQtMemberInfo::Property:
271 case PythonQtMemberInfo::Property:
272 if (wrapper->_obj) {
272 if (wrapper->_obj) {
273 if (member._property.userType() != QVariant::Invalid) {
273 if (member._property.userType() != QVariant::Invalid) {
274 return PythonQtConv::QVariantToPyObject(member._property.read(wrapper->_obj));
274 return PythonQtConv::QVariantToPyObject(member._property.read(wrapper->_obj));
275 } else {
275 } else {
276 Py_INCREF(Py_None);
276 Py_INCREF(Py_None);
277 return Py_None;
277 return Py_None;
278 }
278 }
279 } else {
279 } else {
280 QString error = QString("Trying to read property '") + attributeName + "' from a destroyed " + wrapper->classInfo()->className() + " object";
280 QString error = QString("Trying to read property '") + attributeName + "' from a destroyed " + wrapper->classInfo()->className() + " object";
281 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
281 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
282 return NULL;
282 return NULL;
283 }
283 }
284 break;
284 break;
285 case PythonQtMemberInfo::Slot:
285 case PythonQtMemberInfo::Slot:
286 return PythonQtSlotFunction_New(member._slot, obj, NULL);
286 return PythonQtSlotFunction_New(member._slot, obj, NULL);
287 break;
287 break;
288 case PythonQtMemberInfo::EnumValue:
288 case PythonQtMemberInfo::EnumValue:
289 {
289 {
290 PyObject* enumValue = member._enumValue;
290 PyObject* enumValue = member._enumValue;
291 Py_INCREF(enumValue);
291 Py_INCREF(enumValue);
292 return enumValue;
292 return enumValue;
293 }
293 }
294 break;
294 break;
295 case PythonQtMemberInfo::EnumWrapper:
295 case PythonQtMemberInfo::EnumWrapper:
296 {
296 {
297 PyObject* enumWrapper = member._enumWrapper;
297 PyObject* enumWrapper = member._enumWrapper;
298 Py_INCREF(enumWrapper);
298 Py_INCREF(enumWrapper);
299 return enumWrapper;
299 return enumWrapper;
300 }
300 }
301 break;
301 break;
302 case PythonQtMemberInfo::NotFound:
302 case PythonQtMemberInfo::NotFound:
303 {
304 // check for a getter_
305 PythonQtMemberInfo member = wrapper->classInfo()->member(QByteArray("getter_") + attributeName);
306 if (member._type == PythonQtMemberInfo::Slot) {
307 return PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, member._slot, NULL, NULL, wrapper->_wrappedPtr);
308 }
309
303 // handle dynamic properties
310 // handle dynamic properties
304 if (wrapper->_obj) {
311 if (wrapper->_obj) {
305 QVariant v = wrapper->_obj->property(attributeName);
312 QVariant v = wrapper->_obj->property(attributeName);
306 if (v.isValid()) {
313 if (v.isValid()) {
307 return PythonQtConv::QVariantToPyObject(v);
314 return PythonQtConv::QVariantToPyObject(v);
308 }
315 }
309 }
316 }
317 }
310 break;
318 break;
311 default:
319 default:
312 // is an invalid type, go on
320 // is an invalid type, go on
313 break;
321 break;
314 }
322 }
315
323
316 // look for the internal methods (className(), help())
324 // look for the internal methods (className(), help())
317 PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName);
325 PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName);
318 if (internalMethod) {
326 if (internalMethod) {
319 return internalMethod;
327 return internalMethod;
320 }
328 }
321 PyErr_Clear();
329 PyErr_Clear();
322
330
323 if (wrapper->_obj) {
331 if (wrapper->_obj) {
324 // look for a child
332 // look for a child
325 QObjectList children = wrapper->_obj->children();
333 QObjectList children = wrapper->_obj->children();
326 for (int i = 0; i < children.count(); i++) {
334 for (int i = 0; i < children.count(); i++) {
327 QObject *child = children.at(i);
335 QObject *child = children.at(i);
328 if (child->objectName() == attributeName) {
336 if (child->objectName() == attributeName) {
329 return PythonQt::priv()->wrapQObject(child);
337 return PythonQt::priv()->wrapQObject(child);
330 }
338 }
331 }
339 }
332 }
340 }
333
341
334 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
342 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
335 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
343 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
336 return NULL;
344 return NULL;
337 }
345 }
338
346
339 static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
347 static int PythonQtInstanceWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
340 {
348 {
341 QString error;
349 QString error;
342 char *attributeName;
350 const char *attributeName;
343 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
351 PythonQtInstanceWrapper *wrapper = (PythonQtInstanceWrapper *)obj;
344
352
345 if ((attributeName = PyString_AsString(name)) == NULL)
353 if ((attributeName = PyString_AsString(name)) == NULL)
346 return -1;
354 return -1;
347
355
348 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
356 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
349 if (member._type == PythonQtMemberInfo::Property) {
357 if (member._type == PythonQtMemberInfo::Property) {
350
358
351 if (!wrapper->_obj) {
359 if (!wrapper->_obj) {
352 error = QString("Trying to set property '") + attributeName + "' on a destroyed " + wrapper->classInfo()->className() + " object";
360 error = QString("Trying to set property '") + attributeName + "' on a destroyed " + wrapper->classInfo()->className() + " object";
353 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
361 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
354 return -1;
362 return -1;
355 }
363 }
356
364
357 QMetaProperty prop = member._property;
365 QMetaProperty prop = member._property;
358 if (prop.isWritable()) {
366 if (prop.isWritable()) {
359 QVariant v;
367 QVariant v;
360 if (prop.isEnumType()) {
368 if (prop.isEnumType()) {
361 // this will give us either a string or an int, everything else will probably be an error
369 // this will give us either a string or an int, everything else will probably be an error
362 v = PythonQtConv::PyObjToQVariant(value);
370 v = PythonQtConv::PyObjToQVariant(value);
363 } else {
371 } else {
364 int t = prop.userType();
372 int t = prop.userType();
365 v = PythonQtConv::PyObjToQVariant(value, t);
373 v = PythonQtConv::PyObjToQVariant(value, t);
366 }
374 }
367 bool success = false;
375 bool success = false;
368 if (v.isValid()) {
376 if (v.isValid()) {
369 success = prop.write(wrapper->_obj, v);
377 success = prop.write(wrapper->_obj, v);
370 }
378 }
371 if (success) {
379 if (success) {
372 return 0;
380 return 0;
373 } else {
381 } else {
374 error = QString("Property '") + attributeName + "' of type '" +
382 error = QString("Property '") + attributeName + "' of type '" +
375 prop.typeName() + "' does not accept an object of type "
383 prop.typeName() + "' does not accept an object of type "
376 + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")";
384 + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")";
377 }
385 }
378 } else {
386 } else {
379 error = QString("Property '") + attributeName + "' of " + obj->ob_type->tp_name + " object is not writable";
387 error = QString("Property '") + attributeName + "' of " + obj->ob_type->tp_name + " object is not writable";
380 }
388 }
381 } else if (member._type == PythonQtMemberInfo::Slot) {
389 } else if (member._type == PythonQtMemberInfo::Slot) {
382 error = QString("Slot '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object";
390 error = QString("Slot '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object";
383 } else if (member._type == PythonQtMemberInfo::EnumValue) {
391 } else if (member._type == PythonQtMemberInfo::EnumValue) {
384 error = QString("EnumValue '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object";
392 error = QString("EnumValue '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object";
385 } else if (member._type == PythonQtMemberInfo::EnumWrapper) {
393 } else if (member._type == PythonQtMemberInfo::EnumWrapper) {
386 error = QString("Enum '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object";
394 error = QString("Enum '") + attributeName + "' can not be overwritten on " + obj->ob_type->tp_name + " object";
387 } else if (member._type == PythonQtMemberInfo::NotFound) {
395 } else if (member._type == PythonQtMemberInfo::NotFound) {
396 // check for a setter_
397 PythonQtMemberInfo setter = wrapper->classInfo()->member(QByteArray("setter_") + attributeName);
398 if (setter._type == PythonQtMemberInfo::Slot) {
399 // call the setter and ignore the result value
400 void* result;
401 PyObject* args = PyTuple_New(1);
402 Py_INCREF(value);
403 PyTuple_SET_ITEM(args, 0, value);
404 PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, setter._slot, args, NULL, wrapper->_wrappedPtr, &result);
405 Py_DECREF(args);
406 return 0;
407 }
408
388 // handle dynamic properties
409 // handle dynamic properties
389 if (wrapper->_obj) {
410 if (wrapper->_obj) {
390 QVariant prop = wrapper->_obj->property(attributeName);
411 QVariant prop = wrapper->_obj->property(attributeName);
391 if (prop.isValid()) {
412 if (prop.isValid()) {
392 QVariant v = PythonQtConv::PyObjToQVariant(value);
413 QVariant v = PythonQtConv::PyObjToQVariant(value);
393 if (v.isValid()) {
414 if (v.isValid()) {
394 wrapper->_obj->setProperty(attributeName, v);
415 wrapper->_obj->setProperty(attributeName, v);
395 return 0;
416 return 0;
396 } else {
417 } else {
397 error = QString("Dynamic property '") + attributeName + "' does not accept an object of type "
418 error = QString("Dynamic property '") + attributeName + "' does not accept an object of type "
398 + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")";
419 + QString(value->ob_type->tp_name) + " (" + PythonQtConv::PyObjGetRepresentation(value) + ")";
399 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
420 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
400 return -1;
421 return -1;
401 }
422 }
402 }
423 }
403 }
424 }
404
425
405 // if we are a derived python class, we allow setting attributes.
426 // if we are a derived python class, we allow setting attributes.
406 // if we are a direct CPP wrapper, we do NOT allow it, since
427 // if we are a direct CPP wrapper, we do NOT allow it, since
407 // it would be confusing to allow it because a wrapper will go away when it is not seen by python anymore
428 // it would be confusing to allow it because a wrapper will go away when it is not seen by python anymore
408 // and when it is recreated from a CPP pointer the attributes are gone...
429 // and when it is recreated from a CPP pointer the attributes are gone...
409 if (obj->ob_type->tp_base != &PythonQtInstanceWrapper_Type) {
430 if (obj->ob_type->tp_base != &PythonQtInstanceWrapper_Type) {
410 return PyBaseObject_Type.tp_setattro(obj,name,value);
431 return PyBaseObject_Type.tp_setattro(obj,name,value);
411 } else {
432 } else {
412 error = QString("'") + attributeName + "' does not exist on " + obj->ob_type->tp_name + " and creating new attributes on C++ objects is not allowed";
433 error = QString("'") + attributeName + "' does not exist on " + obj->ob_type->tp_name + " and creating new attributes on C++ objects is not allowed";
413 }
434 }
414 }
435 }
415
436
416 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
437 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
417 return -1;
438 return -1;
418 }
439 }
419
440
420 static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
441 static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
421 {
442 {
422 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
443 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
423 const char* typeName = obj->ob_type->tp_name;
444 const char* typeName = obj->ob_type->tp_name;
424 QObject *qobj = wrapper->_obj;
445 QObject *qobj = wrapper->_obj;
425 if (wrapper->_wrappedPtr) {
446 if (wrapper->_wrappedPtr) {
426 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
447 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
427 if (!str.isEmpty()) {
448 if (!str.isEmpty()) {
428 return PyString_FromFormat("%s", str.toLatin1().constData());
449 return PyString_FromFormat("%s", str.toLatin1().constData());
429 } else
450 } else
430 if (wrapper->_obj) {
451 if (wrapper->_obj) {
431 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
452 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
432 } else {
453 } else {
433 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
454 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
434 }
455 }
435 } else {
456 } else {
436 return PyString_FromFormat("%s (QObject %p)", typeName, qobj);
457 return PyString_FromFormat("%s (QObject %p)", typeName, qobj);
437 }
458 }
438 }
459 }
439
460
440 static PyObject * PythonQtInstanceWrapper_repr(PyObject * obj)
461 static PyObject * PythonQtInstanceWrapper_repr(PyObject * obj)
441 {
462 {
442 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
463 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
443 const char* typeName = obj->ob_type->tp_name;
464 const char* typeName = obj->ob_type->tp_name;
444
465
445 QObject *qobj = wrapper->_obj;
466 QObject *qobj = wrapper->_obj;
446 if (wrapper->_wrappedPtr) {
467 if (wrapper->_wrappedPtr) {
447 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
468 QString str = PythonQtConv::CPPObjectToString(wrapper->classInfo()->metaTypeId(), wrapper->_wrappedPtr);
448 if (!str.isEmpty()) {
469 if (!str.isEmpty()) {
449 return PyString_FromFormat("%s(%s, %p)", typeName, str.toLatin1().constData(), wrapper->_wrappedPtr);
470 return PyString_FromFormat("%s(%s, %p)", typeName, str.toLatin1().constData(), wrapper->_wrappedPtr);
450 } else
471 } else
451 if (wrapper->_obj) {
472 if (wrapper->_obj) {
452 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
473 return PyString_FromFormat("%s (C++ Object %p wrapped by %s %p))", typeName, wrapper->_wrappedPtr, wrapper->_obj->metaObject()->className(), qobj);
453 } else {
474 } else {
454 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
475 return PyString_FromFormat("%s (C++ Object %p)", typeName, wrapper->_wrappedPtr);
455 }
476 }
456 } else {
477 } else {
457 return PyString_FromFormat("%s (%s %p)", typeName, wrapper->classInfo()->className(), qobj);
478 return PyString_FromFormat("%s (%s %p)", typeName, wrapper->classInfo()->className(), qobj);
458 }
479 }
459 }
480 }
460
481
461 static int PythonQtInstanceWrapper_compare(PyObject * obj1, PyObject * obj2)
482 static int PythonQtInstanceWrapper_compare(PyObject * obj1, PyObject * obj2)
462 {
483 {
463 if (PyObject_TypeCheck(obj1, &PythonQtInstanceWrapper_Type) &&
484 if (PyObject_TypeCheck(obj1, &PythonQtInstanceWrapper_Type) &&
464 PyObject_TypeCheck(obj2, &PythonQtInstanceWrapper_Type)) {
485 PyObject_TypeCheck(obj2, &PythonQtInstanceWrapper_Type)) {
465
486
466 PythonQtInstanceWrapper* w1 = (PythonQtInstanceWrapper*)obj1;
487 PythonQtInstanceWrapper* w1 = (PythonQtInstanceWrapper*)obj1;
467 PythonQtInstanceWrapper* w2 = (PythonQtInstanceWrapper*)obj2;
488 PythonQtInstanceWrapper* w2 = (PythonQtInstanceWrapper*)obj2;
468 // check pointers directly first:
489 // check pointers directly first:
469 if (w1->_wrappedPtr != NULL) {
490 if (w1->_wrappedPtr != NULL) {
470 if (w1->_wrappedPtr == w2->_wrappedPtr) {
491 if (w1->_wrappedPtr == w2->_wrappedPtr) {
471 return 0;
492 return 0;
472 }
493 }
473 } else if (w1->_obj == w2->_obj) {
494 } else if (w1->_obj == w2->_obj) {
474 return 0;
495 return 0;
475 }
496 }
476 const char* class1 = w1->classInfo()->className();
497 const char* class1 = w1->classInfo()->className();
477 const char* class2 = w2->classInfo()->className();
498 const char* class2 = w2->classInfo()->className();
478 if (strcmp(class1, class2) == 0) {
499 if (strcmp(class1, class2) == 0) {
479 // same class names, so we can try the operator_equal
500 // same class names, so we can try the operator_equal
480 PythonQtMemberInfo info = w1->classInfo()->member("operator_equal");
501 PythonQtMemberInfo info = w1->classInfo()->member("operator_equal");
481 if (info._type == PythonQtMemberInfo::Slot) {
502 if (info._type == PythonQtMemberInfo::Slot) {
482 bool result = false;
503 bool result = false;
483 void* obj1 = w1->_wrappedPtr;
504 void* obj1 = w1->_wrappedPtr;
484 if (!obj1) {
505 if (!obj1) {
485 obj1 = w1->_obj;
506 obj1 = w1->_obj;
486 }
507 }
487 if (!obj1) { return -1; }
508 if (!obj1) { return -1; }
488 void* obj2 = w2->_wrappedPtr;
509 void* obj2 = w2->_wrappedPtr;
489 if (!obj2) {
510 if (!obj2) {
490 obj2 = w2->_obj;
511 obj2 = w2->_obj;
491 }
512 }
492 if (!obj2) { return -1; }
513 if (!obj2) { return -1; }
493 if (info._slot->isInstanceDecorator()) {
514 if (info._slot->isInstanceDecorator()) {
494 // call on decorator QObject
515 // call on decorator QObject
495 void* args[3];
516 void* args[3];
496 args[0] = &result;
517 args[0] = &result;
497 args[1] = &obj1; // this is a pointer, so it needs a pointer to a pointer
518 args[1] = &obj1; // this is a pointer, so it needs a pointer to a pointer
498 args[2] = obj2; // this is a reference, so it needs the direct pointer
519 args[2] = obj2; // this is a reference, so it needs the direct pointer
499 info._slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
520 info._slot->decorator()->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
500 return result?0:-1;
521 return result?0:-1;
501 } else {
522 } else {
502 // call directly on QObject
523 // call directly on QObject
503 if (w1->_obj && w2->_obj) {
524 if (w1->_obj && w2->_obj) {
504 void* args[2];
525 void* args[2];
505 args[0] = &result;
526 args[0] = &result;
506 args[1] = obj2; // this is a reference, so it needs the direct pointer
527 args[1] = obj2; // this is a reference, so it needs the direct pointer
507 w1->_obj->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
528 w1->_obj->qt_metacall(QMetaObject::InvokeMetaMethod, info._slot->slotIndex(), args);
508 }
529 }
509 }
530 }
510 }
531 }
511 }
532 }
512 }
533 }
513 return -1;
534 return -1;
514 }
535 }
515
536
516 static int PythonQtInstanceWrapper_nonzero(PyObject *obj)
537 static int PythonQtInstanceWrapper_nonzero(PyObject *obj)
517 {
538 {
518 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
539 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)obj;
519 return (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1;
540 return (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1;
520 }
541 }
521
542
522
543
523 static long PythonQtInstanceWrapper_hash(PythonQtInstanceWrapper *obj)
544 static long PythonQtInstanceWrapper_hash(PythonQtInstanceWrapper *obj)
524 {
545 {
525 if (obj->_wrappedPtr != NULL) {
546 if (obj->_wrappedPtr != NULL) {
526 return reinterpret_cast<long>(obj->_wrappedPtr);
547 return reinterpret_cast<long>(obj->_wrappedPtr);
527 } else {
548 } else {
528 QObject* qobj = obj->_obj; // get pointer from QPointer wrapper
549 QObject* qobj = obj->_obj; // get pointer from QPointer wrapper
529 return reinterpret_cast<long>(qobj);
550 return reinterpret_cast<long>(qobj);
530 }
551 }
531 }
552 }
532
553
533
554
534
555
535 // we override nb_nonzero, so that one can do 'if' expressions to test for a NULL ptr
556 // we override nb_nonzero, so that one can do 'if' expressions to test for a NULL ptr
536 static PyNumberMethods PythonQtInstanceWrapper_as_number = {
557 static PyNumberMethods PythonQtInstanceWrapper_as_number = {
537 0, /* nb_add */
558 0, /* nb_add */
538 0, /* nb_subtract */
559 0, /* nb_subtract */
539 0, /* nb_multiply */
560 0, /* nb_multiply */
540 0, /* nb_divide */
561 0, /* nb_divide */
541 0, /* nb_remainder */
562 0, /* nb_remainder */
542 0, /* nb_divmod */
563 0, /* nb_divmod */
543 0, /* nb_power */
564 0, /* nb_power */
544 0, /* nb_negative */
565 0, /* nb_negative */
545 0, /* nb_positive */
566 0, /* nb_positive */
546 0, /* nb_absolute */
567 0, /* nb_absolute */
547 PythonQtInstanceWrapper_nonzero, /* nb_nonzero */
568 PythonQtInstanceWrapper_nonzero, /* nb_nonzero */
548 0, /* nb_invert */
569 0, /* nb_invert */
549 0, /* nb_lshift */
570 0, /* nb_lshift */
550 0, /* nb_rshift */
571 0, /* nb_rshift */
551 0, /* nb_and */
572 0, /* nb_and */
552 0, /* nb_xor */
573 0, /* nb_xor */
553 0, /* nb_or */
574 0, /* nb_or */
554 0, /* nb_coerce */
575 0, /* nb_coerce */
555 0, /* nb_int */
576 0, /* nb_int */
556 0, /* nb_long */
577 0, /* nb_long */
557 0, /* nb_float */
578 0, /* nb_float */
558 0, /* nb_oct */
579 0, /* nb_oct */
559 0, /* nb_hex */
580 0, /* nb_hex */
560 0, /* nb_inplace_add */
581 0, /* nb_inplace_add */
561 0, /* nb_inplace_subtract */
582 0, /* nb_inplace_subtract */
562 0, /* nb_inplace_multiply */
583 0, /* nb_inplace_multiply */
563 0, /* nb_inplace_divide */
584 0, /* nb_inplace_divide */
564 0, /* nb_inplace_remainder */
585 0, /* nb_inplace_remainder */
565 0, /* nb_inplace_power */
586 0, /* nb_inplace_power */
566 0, /* nb_inplace_lshift */
587 0, /* nb_inplace_lshift */
567 0, /* nb_inplace_rshift */
588 0, /* nb_inplace_rshift */
568 0, /* nb_inplace_and */
589 0, /* nb_inplace_and */
569 0, /* nb_inplace_xor */
590 0, /* nb_inplace_xor */
570 0, /* nb_inplace_or */
591 0, /* nb_inplace_or */
571 0, /* nb_floor_divide */
592 0, /* nb_floor_divide */
572 0, /* nb_true_divide */
593 0, /* nb_true_divide */
573 0, /* nb_inplace_floor_divide */
594 0, /* nb_inplace_floor_divide */
574 0, /* nb_inplace_true_divide */
595 0, /* nb_inplace_true_divide */
575 };
596 };
576
597
577 PyTypeObject PythonQtInstanceWrapper_Type = {
598 PyTypeObject PythonQtInstanceWrapper_Type = {
578 PyObject_HEAD_INIT(&PythonQtClassWrapper_Type)
599 PyObject_HEAD_INIT(&PythonQtClassWrapper_Type)
579 0, /*ob_size*/
600 0, /*ob_size*/
580 "PythonQt.PythonQtInstanceWrapper", /*tp_name*/
601 "PythonQt.PythonQtInstanceWrapper", /*tp_name*/
581 sizeof(PythonQtInstanceWrapper), /*tp_basicsize*/
602 sizeof(PythonQtInstanceWrapper), /*tp_basicsize*/
582 0, /*tp_itemsize*/
603 0, /*tp_itemsize*/
583 (destructor)PythonQtInstanceWrapper_dealloc, /*tp_dealloc*/
604 (destructor)PythonQtInstanceWrapper_dealloc, /*tp_dealloc*/
584 0, /*tp_print*/
605 0, /*tp_print*/
585 0, /*tp_getattr*/
606 0, /*tp_getattr*/
586 0, /*tp_setattr*/
607 0, /*tp_setattr*/
587 PythonQtInstanceWrapper_compare, /*tp_compare*/
608 PythonQtInstanceWrapper_compare, /*tp_compare*/
588 PythonQtInstanceWrapper_repr, /*tp_repr*/
609 PythonQtInstanceWrapper_repr, /*tp_repr*/
589 &PythonQtInstanceWrapper_as_number, /*tp_as_number*/
610 &PythonQtInstanceWrapper_as_number, /*tp_as_number*/
590 0, /*tp_as_sequence*/
611 0, /*tp_as_sequence*/
591 0, /*tp_as_mapping*/
612 0, /*tp_as_mapping*/
592 (hashfunc)PythonQtInstanceWrapper_hash, /*tp_hash */
613 (hashfunc)PythonQtInstanceWrapper_hash, /*tp_hash */
593 0, /*tp_call*/
614 0, /*tp_call*/
594 PythonQtInstanceWrapper_str, /*tp_str*/
615 PythonQtInstanceWrapper_str, /*tp_str*/
595 PythonQtInstanceWrapper_getattro, /*tp_getattro*/
616 PythonQtInstanceWrapper_getattro, /*tp_getattro*/
596 PythonQtInstanceWrapper_setattro, /*tp_setattro*/
617 PythonQtInstanceWrapper_setattro, /*tp_setattro*/
597 0, /*tp_as_buffer*/
618 0, /*tp_as_buffer*/
598 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
619 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
599 "PythonQtInstanceWrapper object", /* tp_doc */
620 "PythonQtInstanceWrapper object", /* tp_doc */
600 0, /* tp_traverse */
621 0, /* tp_traverse */
601 0, /* tp_clear */
622 0, /* tp_clear */
602 0, /* tp_richcompare */
623 0, /* tp_richcompare */
603 0, /* tp_weaklistoffset */
624 0, /* tp_weaklistoffset */
604 0, /* tp_iter */
625 0, /* tp_iter */
605 0, /* tp_iternext */
626 0, /* tp_iternext */
606 0, /* tp_methods */
627 0, /* tp_methods */
607 0, /* tp_members */
628 0, /* tp_members */
608 0, /* tp_getset */
629 0, /* tp_getset */
609 0, /* tp_base */
630 0, /* tp_base */
610 0, /* tp_dict */
631 0, /* tp_dict */
611 0, /* tp_descr_get */
632 0, /* tp_descr_get */
612 0, /* tp_descr_set */
633 0, /* tp_descr_set */
613 0, /* tp_dictoffset */
634 0, /* tp_dictoffset */
614 (initproc)PythonQtInstanceWrapper_init, /* tp_init */
635 (initproc)PythonQtInstanceWrapper_init, /* tp_init */
615 0, /* tp_alloc */
636 0, /* tp_alloc */
616 PythonQtInstanceWrapper_new, /* tp_new */
637 PythonQtInstanceWrapper_new, /* tp_new */
617 };
638 };
618
639
619 //-------------------------------------------------------
640 //-------------------------------------------------------
620
641
@@ -1,511 +1,511
1 /*
1 /*
2 *
2 *
3 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
3 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
4 *
4 *
5 * This library is free software; you can redistribute it and/or
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
8 * version 2.1 of the License, or (at your option) any later version.
9 *
9 *
10 * This library is distributed in the hope that it will be useful,
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
13 * Lesser General Public License for more details.
14 *
14 *
15 * Further, this software is distributed without any warranty that it is
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
20 * other software, or any other product whatsoever.
21 *
21 *
22 * You should have received a copy of the GNU Lesser General Public
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
25 *
26 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
26 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
27 * 28359 Bremen, Germany or:
27 * 28359 Bremen, Germany or:
28 *
28 *
29 * http://www.mevis.de
29 * http://www.mevis.de
30 *
30 *
31 */
31 */
32
32
33 //----------------------------------------------------------------------------------
33 //----------------------------------------------------------------------------------
34 /*!
34 /*!
35 // \file PythonQtSlot.cpp
35 // \file PythonQtSlot.cpp
36 // \author Florian Link
36 // \author Florian Link
37 // \author Last changed by $Author: florian $
37 // \author Last changed by $Author: florian $
38 // \date 2006-05
38 // \date 2006-05
39 */
39 */
40 //----------------------------------------------------------------------------------
40 //----------------------------------------------------------------------------------
41
41
42 #include "PythonQt.h"
42 #include "PythonQt.h"
43 #include "PythonQtSlot.h"
43 #include "PythonQtSlot.h"
44 #include "PythonQtInstanceWrapper.h"
44 #include "PythonQtInstanceWrapper.h"
45 #include "PythonQtClassInfo.h"
45 #include "PythonQtClassInfo.h"
46 #include "PythonQtMisc.h"
46 #include "PythonQtMisc.h"
47 #include "PythonQtConversion.h"
47 #include "PythonQtConversion.h"
48 #include <iostream>
48 #include <iostream>
49
49
50 #define PYTHONQT_MAX_ARGS 32
50 #define PYTHONQT_MAX_ARGS 32
51
51
52
52
53 bool PythonQtCallSlot(PythonQtClassInfo* classInfo, QObject* objectToCall, PyObject* args, bool strict, PythonQtSlotInfo* info, void* firstArgument, PyObject** pythonReturnValue, void** directReturnValuePointer)
53 bool PythonQtCallSlot(PythonQtClassInfo* classInfo, QObject* objectToCall, PyObject* args, bool strict, PythonQtSlotInfo* info, void* firstArgument, PyObject** pythonReturnValue, void** directReturnValuePointer)
54 {
54 {
55 static unsigned int recursiveEntry = 0;
55 static unsigned int recursiveEntry = 0;
56
56
57 if (directReturnValuePointer) {
57 if (directReturnValuePointer) {
58 *directReturnValuePointer = NULL;
58 *directReturnValuePointer = NULL;
59 }
59 }
60 // store the current storage position, so that we can get back to this state after a slot is called
60 // store the current storage position, so that we can get back to this state after a slot is called
61 // (do this locally, so that we have all positions on the stack
61 // (do this locally, so that we have all positions on the stack
62 PythonQtValueStoragePosition globalValueStoragePos;
62 PythonQtValueStoragePosition globalValueStoragePos;
63 PythonQtValueStoragePosition globalPtrStoragePos;
63 PythonQtValueStoragePosition globalPtrStoragePos;
64 PythonQtValueStoragePosition globalVariantStoragePos;
64 PythonQtValueStoragePosition globalVariantStoragePos;
65 PythonQtConv::global_valueStorage.getPos(globalValueStoragePos);
65 PythonQtConv::global_valueStorage.getPos(globalValueStoragePos);
66 PythonQtConv::global_ptrStorage.getPos(globalPtrStoragePos);
66 PythonQtConv::global_ptrStorage.getPos(globalPtrStoragePos);
67 PythonQtConv::global_variantStorage.getPos(globalVariantStoragePos);
67 PythonQtConv::global_variantStorage.getPos(globalVariantStoragePos);
68
68
69 recursiveEntry++;
69 recursiveEntry++;
70
70
71 // the arguments that are passed to qt_metacall
71 // the arguments that are passed to qt_metacall
72 void* argList[PYTHONQT_MAX_ARGS];
72 void* argList[PYTHONQT_MAX_ARGS];
73 PyObject* result = NULL;
73 PyObject* result = NULL;
74 int argc = info->parameterCount();
74 int argc = info->parameterCount();
75 const QList<PythonQtSlotInfo::ParameterInfo>& params = info->parameters();
75 const QList<PythonQtSlotInfo::ParameterInfo>& params = info->parameters();
76
76
77 const PythonQtSlotInfo::ParameterInfo& returnValueParam = params.at(0);
77 const PythonQtSlotInfo::ParameterInfo& returnValueParam = params.at(0);
78 // set return argument to NULL
78 // set return argument to NULL
79 argList[0] = NULL;
79 argList[0] = NULL;
80
80
81 bool ok = true;
81 bool ok = true;
82 bool skipFirst = false;
82 bool skipFirst = false;
83 if (info->isInstanceDecorator()) {
83 if (info->isInstanceDecorator()) {
84 skipFirst = true;
84 skipFirst = true;
85
85
86 // for decorators on CPP objects, we take the cpp ptr, for QObjects we take the QObject pointer
86 // for decorators on CPP objects, we take the cpp ptr, for QObjects we take the QObject pointer
87 void* arg1 = firstArgument;
87 void* arg1 = firstArgument;
88 if (!arg1) {
88 if (!arg1) {
89 arg1 = objectToCall;
89 arg1 = objectToCall;
90 }
90 }
91 if (arg1) {
91 if (arg1) {
92 // upcast to correct parent class
92 // upcast to correct parent class
93 arg1 = ((char*)arg1)+info->upcastingOffset();
93 arg1 = ((char*)arg1)+info->upcastingOffset();
94 }
94 }
95
95
96 argList[1] = &arg1;
96 argList[1] = &arg1;
97 if (ok) {
97 if (ok) {
98 for (int i = 2; i<argc && ok; i++) {
98 for (int i = 2; i<argc && ok; i++) {
99 const PythonQtSlotInfo::ParameterInfo& param = params.at(i);
99 const PythonQtSlotInfo::ParameterInfo& param = params.at(i);
100 //std::cout << param.name.data() << " " << param.typeId << (param.isPointer?"*":"") << (param.isConst?" const":"") << std::endl;
100 //std::cout << param.name.data() << " " << param.typeId << (param.isPointer?"*":"") << (param.isConst?" const":"") << std::endl;
101 argList[i] = PythonQtConv::ConvertPythonToQt(param, PyTuple_GET_ITEM(args, i-2), strict, classInfo);
101 argList[i] = PythonQtConv::ConvertPythonToQt(param, PyTuple_GET_ITEM(args, i-2), strict, classInfo);
102 if (argList[i]==NULL) {
102 if (argList[i]==NULL) {
103 ok = false;
103 ok = false;
104 break;
104 break;
105 }
105 }
106 }
106 }
107 }
107 }
108 } else {
108 } else {
109 for (int i = 1; i<argc && ok; i++) {
109 for (int i = 1; i<argc && ok; i++) {
110 const PythonQtSlotInfo::ParameterInfo& param = params.at(i);
110 const PythonQtSlotInfo::ParameterInfo& param = params.at(i);
111 //std::cout << param.name.data() << " " << param.typeId << (param.isPointer?"*":"") << (param.isConst?" const":"") << std::endl;
111 //std::cout << param.name.data() << " " << param.typeId << (param.isPointer?"*":"") << (param.isConst?" const":"") << std::endl;
112 argList[i] = PythonQtConv::ConvertPythonToQt(param, PyTuple_GET_ITEM(args, i-1), strict, classInfo);
112 argList[i] = PythonQtConv::ConvertPythonToQt(param, PyTuple_GET_ITEM(args, i-1), strict, classInfo);
113 if (argList[i]==NULL) {
113 if (argList[i]==NULL) {
114 ok = false;
114 ok = false;
115 break;
115 break;
116 }
116 }
117 }
117 }
118 }
118 }
119
119
120 if (ok) {
120 if (ok) {
121 // parameters are ok, now create the qt return value which is assigned to by metacall
121 // parameters are ok, now create the qt return value which is assigned to by metacall
122 if (returnValueParam.typeId != QMetaType::Void) {
122 if (returnValueParam.typeId != QMetaType::Void) {
123 // create empty default value for the return value
123 // create empty default value for the return value
124 if (!directReturnValuePointer) {
124 if (!directReturnValuePointer) {
125 // create empty default value for the return value
125 // create empty default value for the return value
126 argList[0] = PythonQtConv::CreateQtReturnValue(returnValueParam);
126 argList[0] = PythonQtConv::CreateQtReturnValue(returnValueParam);
127 if (argList[0]==NULL) {
127 if (argList[0]==NULL) {
128 // return value could not be created, maybe we have a registered class with a default constructor, so that we can construct the pythonqt wrapper object and
128 // return value could not be created, maybe we have a registered class with a default constructor, so that we can construct the pythonqt wrapper object and
129 // pass its internal pointer
129 // pass its internal pointer
130 PythonQtClassInfo* info = PythonQt::priv()->getClassInfo(returnValueParam.name);
130 PythonQtClassInfo* info = PythonQt::priv()->getClassInfo(returnValueParam.name);
131 if (info && info->pythonQtClassWrapper()) {
131 if (info && info->pythonQtClassWrapper()) {
132 PyObject* emptyTuple = PyTuple_New(0);
132 PyObject* emptyTuple = PyTuple_New(0);
133 // 1) default construct an empty object as python object (owned by PythonQt), by calling the meta class with empty arguments
133 // 1) default construct an empty object as python object (owned by PythonQt), by calling the meta class with empty arguments
134 result = PyObject_Call((PyObject*)info->pythonQtClassWrapper(), emptyTuple, NULL);
134 result = PyObject_Call((PyObject*)info->pythonQtClassWrapper(), emptyTuple, NULL);
135 if (result) {
135 if (result) {
136 argList[0] = ((PythonQtInstanceWrapper*)result)->_wrappedPtr;
136 argList[0] = ((PythonQtInstanceWrapper*)result)->_wrappedPtr;
137 }
137 }
138 Py_DECREF(emptyTuple);
138 Py_DECREF(emptyTuple);
139 }
139 }
140 }
140 }
141 } else {
141 } else {
142 // we can use our pointer directly!
142 // we can use our pointer directly!
143 argList[0] = directReturnValuePointer;
143 argList[0] = directReturnValuePointer;
144 }
144 }
145 }
145 }
146
146
147 // invoke the slot via metacall
147 // invoke the slot via metacall
148 (info->decorator()?info->decorator():objectToCall)->qt_metacall(QMetaObject::InvokeMetaMethod, info->slotIndex(), argList);
148 (info->decorator()?info->decorator():objectToCall)->qt_metacall(QMetaObject::InvokeMetaMethod, info->slotIndex(), argList);
149
149
150 // handle the return value (which in most cases still needs to be converted to a Python object)
150 // handle the return value (which in most cases still needs to be converted to a Python object)
151 if (argList[0] || returnValueParam.typeId == QMetaType::Void) {
151 if (argList[0] || returnValueParam.typeId == QMetaType::Void) {
152 if (directReturnValuePointer) {
152 if (directReturnValuePointer) {
153 result = NULL;
153 result = NULL;
154 } else {
154 } else {
155 // the resulting object maybe present already, because we created it above at 1)...
155 // the resulting object maybe present already, because we created it above at 1)...
156 if (!result) {
156 if (!result) {
157 result = PythonQtConv::ConvertQtValueToPython(returnValueParam, argList[0]);
157 result = PythonQtConv::ConvertQtValueToPython(returnValueParam, argList[0]);
158 }
158 }
159 }
159 }
160 } else {
160 } else {
161 QString e = QString("Called ") + info->fullSignature() + ", return type '" + returnValueParam.name + "' is ignored because it is unknown to PythonQt. Probably you should register it using qRegisterMetaType() or add a default constructor decorator to the class.";
161 QString e = QString("Called ") + info->fullSignature() + ", return type '" + returnValueParam.name + "' is ignored because it is unknown to PythonQt. Probably you should register it using qRegisterMetaType() or add a default constructor decorator to the class.";
162 PyErr_SetString(PyExc_ValueError, e.toLatin1().data());
162 PyErr_SetString(PyExc_ValueError, e.toLatin1().data());
163 result = NULL;
163 result = NULL;
164 }
164 }
165 }
165 }
166 recursiveEntry--;
166 recursiveEntry--;
167
167
168 // reset the parameter storage position to the stored pos to "pop" the parameter stack
168 // reset the parameter storage position to the stored pos to "pop" the parameter stack
169 PythonQtConv::global_valueStorage.setPos(globalValueStoragePos);
169 PythonQtConv::global_valueStorage.setPos(globalValueStoragePos);
170 PythonQtConv::global_ptrStorage.setPos(globalPtrStoragePos);
170 PythonQtConv::global_ptrStorage.setPos(globalPtrStoragePos);
171 PythonQtConv::global_variantStorage.setPos(globalVariantStoragePos);
171 PythonQtConv::global_variantStorage.setPos(globalVariantStoragePos);
172
172
173 *pythonReturnValue = result;
173 *pythonReturnValue = result;
174 // NOTE: it is important to only return here, otherwise the stack will not be popped!!!
174 // NOTE: it is important to only return here, otherwise the stack will not be popped!!!
175 return result || (directReturnValuePointer && *directReturnValuePointer);
175 return result || (directReturnValuePointer && *directReturnValuePointer);
176 }
176 }
177
177
178 //-----------------------------------------------------------------------------------
178 //-----------------------------------------------------------------------------------
179
179
180 static PythonQtSlotFunctionObject *pythonqtslot_free_list = NULL;
180 static PythonQtSlotFunctionObject *pythonqtslot_free_list = NULL;
181
181
182 PyObject *PythonQtSlotFunction_Call(PyObject *func, PyObject *args, PyObject *kw)
182 PyObject *PythonQtSlotFunction_Call(PyObject *func, PyObject *args, PyObject *kw)
183 {
183 {
184 PythonQtSlotFunctionObject* f = (PythonQtSlotFunctionObject*)func;
184 PythonQtSlotFunctionObject* f = (PythonQtSlotFunctionObject*)func;
185 PythonQtSlotInfo* info = f->m_ml;
185 PythonQtSlotInfo* info = f->m_ml;
186 if (PyObject_TypeCheck(f->m_self, &PythonQtInstanceWrapper_Type)) {
186 if (PyObject_TypeCheck(f->m_self, &PythonQtInstanceWrapper_Type)) {
187 PythonQtInstanceWrapper* self = (PythonQtInstanceWrapper*) f->m_self;
187 PythonQtInstanceWrapper* self = (PythonQtInstanceWrapper*) f->m_self;
188 if (!info->isClassDecorator() && (self->_obj==NULL && self->_wrappedPtr==NULL)) {
188 if (!info->isClassDecorator() && (self->_obj==NULL && self->_wrappedPtr==NULL)) {
189 QString error = QString("Trying to call '") + f->m_ml->slotName() + "' on a destroyed " + self->classInfo()->className() + " object";
189 QString error = QString("Trying to call '") + f->m_ml->slotName() + "' on a destroyed " + self->classInfo()->className() + " object";
190 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
190 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
191 return NULL;
191 return NULL;
192 } else {
192 } else {
193 return PythonQtSlotFunction_CallImpl(self->classInfo(), self->_obj, info, args, kw, self->_wrappedPtr);
193 return PythonQtSlotFunction_CallImpl(self->classInfo(), self->_obj, info, args, kw, self->_wrappedPtr);
194 }
194 }
195 } else if (f->m_self->ob_type == &PythonQtClassWrapper_Type) {
195 } else if (f->m_self->ob_type == &PythonQtClassWrapper_Type) {
196 PythonQtClassWrapper* type = (PythonQtClassWrapper*) f->m_self;
196 PythonQtClassWrapper* type = (PythonQtClassWrapper*) f->m_self;
197 if (info->isClassDecorator()) {
197 if (info->isClassDecorator()) {
198 return PythonQtSlotFunction_CallImpl(type->classInfo(), NULL, info, args, kw);
198 return PythonQtSlotFunction_CallImpl(type->classInfo(), NULL, info, args, kw);
199 } else {
199 } else {
200 // otherwise, it is an unbound call and we have an instanceDecorator or normal slot...
200 // otherwise, it is an unbound call and we have an instanceDecorator or normal slot...
201 Py_ssize_t argc = PyTuple_Size(args);
201 Py_ssize_t argc = PyTuple_Size(args);
202 if (argc>0) {
202 if (argc>0) {
203 PyObject* firstArg = PyTuple_GET_ITEM(args, 0);
203 PyObject* firstArg = PyTuple_GET_ITEM(args, 0);
204 if (PyObject_TypeCheck(firstArg, (PyTypeObject*)&PythonQtInstanceWrapper_Type)
204 if (PyObject_TypeCheck(firstArg, (PyTypeObject*)&PythonQtInstanceWrapper_Type)
205 && ((PythonQtInstanceWrapper*)firstArg)->classInfo()->inherits(type->classInfo())) {
205 && ((PythonQtInstanceWrapper*)firstArg)->classInfo()->inherits(type->classInfo())) {
206 PythonQtInstanceWrapper* self = (PythonQtInstanceWrapper*)firstArg;
206 PythonQtInstanceWrapper* self = (PythonQtInstanceWrapper*)firstArg;
207 if (!info->isClassDecorator() && (self->_obj==NULL && self->_wrappedPtr==NULL)) {
207 if (!info->isClassDecorator() && (self->_obj==NULL && self->_wrappedPtr==NULL)) {
208 QString error = QString("Trying to call '") + f->m_ml->slotName() + "' on a destroyed " + self->classInfo()->className() + " object";
208 QString error = QString("Trying to call '") + f->m_ml->slotName() + "' on a destroyed " + self->classInfo()->className() + " object";
209 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
209 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
210 return NULL;
210 return NULL;
211 }
211 }
212 // strip the first argument...
212 // strip the first argument...
213 PyObject* newargs = PyTuple_GetSlice(args, 1, argc);
213 PyObject* newargs = PyTuple_GetSlice(args, 1, argc);
214 PyObject* result = PythonQtSlotFunction_CallImpl(self->classInfo(), self->_obj, info, newargs, kw, self->_wrappedPtr);
214 PyObject* result = PythonQtSlotFunction_CallImpl(self->classInfo(), self->_obj, info, newargs, kw, self->_wrappedPtr);
215 Py_DECREF(newargs);
215 Py_DECREF(newargs);
216 return result;
216 return result;
217 } else {
217 } else {
218 // first arg is not of correct type!
218 // first arg is not of correct type!
219 QString error = "slot " + info->fullSignature() + " requires " + type->classInfo()->className() + " instance as first argument, got " + firstArg->ob_type->tp_name;
219 QString error = "slot " + info->fullSignature() + " requires " + type->classInfo()->className() + " instance as first argument, got " + firstArg->ob_type->tp_name;
220 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
220 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
221 return NULL;
221 return NULL;
222 }
222 }
223 } else {
223 } else {
224 // wrong number of args
224 // wrong number of args
225 QString error = "slot " + info->fullSignature() + " requires " + type->classInfo()->className() + " instance as first argument.";
225 QString error = "slot " + info->fullSignature() + " requires " + type->classInfo()->className() + " instance as first argument.";
226 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
226 PyErr_SetString(PyExc_ValueError, error.toLatin1().data());
227 return NULL;
227 return NULL;
228 }
228 }
229 }
229 }
230 }
230 }
231 return NULL;
231 return NULL;
232 }
232 }
233
233
234 PyObject *PythonQtSlotFunction_CallImpl(PythonQtClassInfo* classInfo, QObject* objectToCall, PythonQtSlotInfo* info, PyObject *args, PyObject * /*kw*/, void* firstArg, void** directReturnValuePointer)
234 PyObject *PythonQtSlotFunction_CallImpl(PythonQtClassInfo* classInfo, QObject* objectToCall, PythonQtSlotInfo* info, PyObject *args, PyObject * /*kw*/, void* firstArg, void** directReturnValuePointer)
235 {
235 {
236 int argc = PyTuple_Size(args);
236 int argc = args?PyTuple_Size(args):0;
237
237
238 #ifdef PYTHONQT_DEBUG
238 #ifdef PYTHONQT_DEBUG
239 std::cout << "called " << info->metaMethod()->typeName() << " " << info->metaMethod()->signature() << std::endl;
239 std::cout << "called " << info->metaMethod()->typeName() << " " << info->metaMethod()->signature() << std::endl;
240 #endif
240 #endif
241
241
242 PyObject* r = NULL;
242 PyObject* r = NULL;
243 bool ok = false;
243 bool ok = false;
244 if (directReturnValuePointer) {
244 if (directReturnValuePointer) {
245 *directReturnValuePointer = NULL;
245 *directReturnValuePointer = NULL;
246 }
246 }
247 if (info->nextInfo()) {
247 if (info->nextInfo()) {
248 // overloaded slot call, try on all slots with strict conversion first
248 // overloaded slot call, try on all slots with strict conversion first
249 bool strict = true;
249 bool strict = true;
250 PythonQtSlotInfo* i = info;
250 PythonQtSlotInfo* i = info;
251 while (i) {
251 while (i) {
252 bool skipFirst = i->isInstanceDecorator();
252 bool skipFirst = i->isInstanceDecorator();
253 if (i->parameterCount()-1-(skipFirst?1:0) == argc) {
253 if (i->parameterCount()-1-(skipFirst?1:0) == argc) {
254 PyErr_Clear();
254 PyErr_Clear();
255 ok = PythonQtCallSlot(classInfo, objectToCall, args, strict, i, firstArg, &r, directReturnValuePointer);
255 ok = PythonQtCallSlot(classInfo, objectToCall, args, strict, i, firstArg, &r, directReturnValuePointer);
256 if (PyErr_Occurred() || ok) break;
256 if (PyErr_Occurred() || ok) break;
257 }
257 }
258 i = i->nextInfo();
258 i = i->nextInfo();
259 if (!i) {
259 if (!i) {
260 if (strict) {
260 if (strict) {
261 // one more run without being strict
261 // one more run without being strict
262 strict = false;
262 strict = false;
263 i = info;
263 i = info;
264 }
264 }
265 }
265 }
266 }
266 }
267 if (!ok && !PyErr_Occurred()) {
267 if (!ok && !PyErr_Occurred()) {
268 QString e = QString("Could not find matching overload for given arguments:\n" + PythonQtConv::PyObjGetString(args) + "\n The following slots are available:\n");
268 QString e = QString("Could not find matching overload for given arguments:\n" + PythonQtConv::PyObjGetString(args) + "\n The following slots are available:\n");
269 PythonQtSlotInfo* i = info;
269 PythonQtSlotInfo* i = info;
270 while (i) {
270 while (i) {
271 e += QString(i->fullSignature()) + "\n";
271 e += QString(i->fullSignature()) + "\n";
272 i = i->nextInfo();
272 i = i->nextInfo();
273 }
273 }
274 PyErr_SetString(PyExc_ValueError, e.toLatin1().data());
274 PyErr_SetString(PyExc_ValueError, e.toLatin1().data());
275 }
275 }
276 } else {
276 } else {
277 // simple (non-overloaded) slot call
277 // simple (non-overloaded) slot call
278 bool skipFirst = info->isInstanceDecorator();
278 bool skipFirst = info->isInstanceDecorator();
279 if (info->parameterCount()-1-(skipFirst?1:0) == argc) {
279 if (info->parameterCount()-1-(skipFirst?1:0) == argc) {
280 PyErr_Clear();
280 PyErr_Clear();
281 ok = PythonQtCallSlot(classInfo, objectToCall, args, false, info, firstArg, &r, directReturnValuePointer);
281 ok = PythonQtCallSlot(classInfo, objectToCall, args, false, info, firstArg, &r, directReturnValuePointer);
282 if (!ok && !PyErr_Occurred()) {
282 if (!ok && !PyErr_Occurred()) {
283 QString e = QString("Called ") + info->fullSignature() + " with wrong arguments: " + PythonQtConv::PyObjGetString(args);
283 QString e = QString("Called ") + info->fullSignature() + " with wrong arguments: " + PythonQtConv::PyObjGetString(args);
284 PyErr_SetString(PyExc_ValueError, e.toLatin1().data());
284 PyErr_SetString(PyExc_ValueError, e.toLatin1().data());
285 }
285 }
286 } else {
286 } else {
287 QString e = QString("Called ") + info->fullSignature() + " with wrong number of arguments: " + PythonQtConv::PyObjGetString(args);
287 QString e = QString("Called ") + info->fullSignature() + " with wrong number of arguments: " + PythonQtConv::PyObjGetString(args);
288 PyErr_SetString(PyExc_ValueError, e.toLatin1().data());
288 PyErr_SetString(PyExc_ValueError, e.toLatin1().data());
289 }
289 }
290 }
290 }
291
291
292 return r;
292 return r;
293 }
293 }
294
294
295 PyObject *
295 PyObject *
296 PythonQtSlotFunction_New(PythonQtSlotInfo *ml, PyObject *self, PyObject *module)
296 PythonQtSlotFunction_New(PythonQtSlotInfo *ml, PyObject *self, PyObject *module)
297 {
297 {
298 PythonQtSlotFunctionObject *op;
298 PythonQtSlotFunctionObject *op;
299 op = pythonqtslot_free_list;
299 op = pythonqtslot_free_list;
300 if (op != NULL) {
300 if (op != NULL) {
301 pythonqtslot_free_list = (PythonQtSlotFunctionObject *)(op->m_self);
301 pythonqtslot_free_list = (PythonQtSlotFunctionObject *)(op->m_self);
302 PyObject_INIT(op, &PythonQtSlotFunction_Type);
302 PyObject_INIT(op, &PythonQtSlotFunction_Type);
303 }
303 }
304 else {
304 else {
305 op = PyObject_GC_New(PythonQtSlotFunctionObject, &PythonQtSlotFunction_Type);
305 op = PyObject_GC_New(PythonQtSlotFunctionObject, &PythonQtSlotFunction_Type);
306 if (op == NULL)
306 if (op == NULL)
307 return NULL;
307 return NULL;
308 }
308 }
309 op->m_ml = ml;
309 op->m_ml = ml;
310 Py_XINCREF(self);
310 Py_XINCREF(self);
311 op->m_self = self;
311 op->m_self = self;
312 Py_XINCREF(module);
312 Py_XINCREF(module);
313 op->m_module = module;
313 op->m_module = module;
314 PyObject_GC_Track(op);
314 PyObject_GC_Track(op);
315 return (PyObject *)op;
315 return (PyObject *)op;
316 }
316 }
317
317
318 PythonQtSlotInfo*
318 PythonQtSlotInfo*
319 PythonQtSlotFunction_GetSlotInfo(PyObject *op)
319 PythonQtSlotFunction_GetSlotInfo(PyObject *op)
320 {
320 {
321 if (!PythonQtSlotFunction_Check(op)) {
321 if (!PythonQtSlotFunction_Check(op)) {
322 PyErr_BadInternalCall();
322 PyErr_BadInternalCall();
323 return NULL;
323 return NULL;
324 }
324 }
325 return ((PythonQtSlotFunctionObject *)op) -> m_ml;
325 return ((PythonQtSlotFunctionObject *)op) -> m_ml;
326 }
326 }
327
327
328 PyObject *
328 PyObject *
329 PythonQtSlotFunction_GetSelf(PyObject *op)
329 PythonQtSlotFunction_GetSelf(PyObject *op)
330 {
330 {
331 if (!PythonQtSlotFunction_Check(op)) {
331 if (!PythonQtSlotFunction_Check(op)) {
332 PyErr_BadInternalCall();
332 PyErr_BadInternalCall();
333 return NULL;
333 return NULL;
334 }
334 }
335 return ((PythonQtSlotFunctionObject *)op) -> m_self;
335 return ((PythonQtSlotFunctionObject *)op) -> m_self;
336 }
336 }
337
337
338 /* Methods (the standard built-in methods, that is) */
338 /* Methods (the standard built-in methods, that is) */
339
339
340 static void
340 static void
341 meth_dealloc(PythonQtSlotFunctionObject *m)
341 meth_dealloc(PythonQtSlotFunctionObject *m)
342 {
342 {
343 PyObject_GC_UnTrack(m);
343 PyObject_GC_UnTrack(m);
344 Py_XDECREF(m->m_self);
344 Py_XDECREF(m->m_self);
345 Py_XDECREF(m->m_module);
345 Py_XDECREF(m->m_module);
346 m->m_self = (PyObject *)pythonqtslot_free_list;
346 m->m_self = (PyObject *)pythonqtslot_free_list;
347 pythonqtslot_free_list = m;
347 pythonqtslot_free_list = m;
348 }
348 }
349
349
350 static PyObject *
350 static PyObject *
351 meth_get__doc__(PythonQtSlotFunctionObject * /*m*/, void * /*closure*/)
351 meth_get__doc__(PythonQtSlotFunctionObject * /*m*/, void * /*closure*/)
352 {
352 {
353 Py_INCREF(Py_None);
353 Py_INCREF(Py_None);
354 return Py_None;
354 return Py_None;
355 }
355 }
356
356
357 static PyObject *
357 static PyObject *
358 meth_get__name__(PythonQtSlotFunctionObject *m, void * /*closure*/)
358 meth_get__name__(PythonQtSlotFunctionObject *m, void * /*closure*/)
359 {
359 {
360 return PyString_FromString(m->m_ml->metaMethod()->signature());
360 return PyString_FromString(m->m_ml->metaMethod()->signature());
361 }
361 }
362
362
363 static int
363 static int
364 meth_traverse(PythonQtSlotFunctionObject *m, visitproc visit, void *arg)
364 meth_traverse(PythonQtSlotFunctionObject *m, visitproc visit, void *arg)
365 {
365 {
366 int err;
366 int err;
367 if (m->m_self != NULL) {
367 if (m->m_self != NULL) {
368 err = visit(m->m_self, arg);
368 err = visit(m->m_self, arg);
369 if (err)
369 if (err)
370 return err;
370 return err;
371 }
371 }
372 if (m->m_module != NULL) {
372 if (m->m_module != NULL) {
373 err = visit(m->m_module, arg);
373 err = visit(m->m_module, arg);
374 if (err)
374 if (err)
375 return err;
375 return err;
376 }
376 }
377 return 0;
377 return 0;
378 }
378 }
379
379
380 static PyObject *
380 static PyObject *
381 meth_get__self__(PythonQtSlotFunctionObject *m, void * /*closure*/)
381 meth_get__self__(PythonQtSlotFunctionObject *m, void * /*closure*/)
382 {
382 {
383 PyObject *self;
383 PyObject *self;
384 if (PyEval_GetRestricted()) {
384 if (PyEval_GetRestricted()) {
385 PyErr_SetString(PyExc_RuntimeError,
385 PyErr_SetString(PyExc_RuntimeError,
386 "method.__self__ not accessible in restricted mode");
386 "method.__self__ not accessible in restricted mode");
387 return NULL;
387 return NULL;
388 }
388 }
389 self = m->m_self;
389 self = m->m_self;
390 if (self == NULL)
390 if (self == NULL)
391 self = Py_None;
391 self = Py_None;
392 Py_INCREF(self);
392 Py_INCREF(self);
393 return self;
393 return self;
394 }
394 }
395
395
396 static PyGetSetDef meth_getsets [] = {
396 static PyGetSetDef meth_getsets [] = {
397 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
397 {"__doc__", (getter)meth_get__doc__, NULL, NULL},
398 {"__name__", (getter)meth_get__name__, NULL, NULL},
398 {"__name__", (getter)meth_get__name__, NULL, NULL},
399 {"__self__", (getter)meth_get__self__, NULL, NULL},
399 {"__self__", (getter)meth_get__self__, NULL, NULL},
400 {NULL, NULL, NULL,NULL},
400 {NULL, NULL, NULL,NULL},
401 };
401 };
402
402
403 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 6
403 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 6
404 #define PY_WRITE_RESTRICTED WRITE_RESTRICTED
404 #define PY_WRITE_RESTRICTED WRITE_RESTRICTED
405 #endif
405 #endif
406
406
407 #define OFF(x) offsetof(PythonQtSlotFunctionObject, x)
407 #define OFF(x) offsetof(PythonQtSlotFunctionObject, x)
408
408
409 static PyMemberDef meth_members[] = {
409 static PyMemberDef meth_members[] = {
410 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
410 {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED},
411 {NULL}
411 {NULL}
412 };
412 };
413
413
414 static PyObject *
414 static PyObject *
415 meth_repr(PythonQtSlotFunctionObject *f)
415 meth_repr(PythonQtSlotFunctionObject *f)
416 {
416 {
417 if (f->m_self->ob_type == &PythonQtClassWrapper_Type) {
417 if (f->m_self->ob_type == &PythonQtClassWrapper_Type) {
418 PythonQtClassWrapper* self = (PythonQtClassWrapper*) f->m_self;
418 PythonQtClassWrapper* self = (PythonQtClassWrapper*) f->m_self;
419 return PyString_FromFormat("<unbound qt slot %s of %s type>",
419 return PyString_FromFormat("<unbound qt slot %s of %s type>",
420 f->m_ml->slotName().data(),
420 f->m_ml->slotName().data(),
421 self->classInfo()->className());
421 self->classInfo()->className());
422 } else {
422 } else {
423 return PyString_FromFormat("<qt slot %s of %s instance at %p>",
423 return PyString_FromFormat("<qt slot %s of %s instance at %p>",
424 f->m_ml->slotName().data(),
424 f->m_ml->slotName().data(),
425 f->m_self->ob_type->tp_name,
425 f->m_self->ob_type->tp_name,
426 f->m_self);
426 f->m_self);
427 }
427 }
428 }
428 }
429
429
430 static int
430 static int
431 meth_compare(PythonQtSlotFunctionObject *a, PythonQtSlotFunctionObject *b)
431 meth_compare(PythonQtSlotFunctionObject *a, PythonQtSlotFunctionObject *b)
432 {
432 {
433 if (a->m_self != b->m_self)
433 if (a->m_self != b->m_self)
434 return (a->m_self < b->m_self) ? -1 : 1;
434 return (a->m_self < b->m_self) ? -1 : 1;
435 if (a->m_ml == b->m_ml)
435 if (a->m_ml == b->m_ml)
436 return 0;
436 return 0;
437 if (strcmp(a->m_ml->metaMethod()->signature(), b->m_ml->metaMethod()->signature()) < 0)
437 if (strcmp(a->m_ml->metaMethod()->signature(), b->m_ml->metaMethod()->signature()) < 0)
438 return -1;
438 return -1;
439 else
439 else
440 return 1;
440 return 1;
441 }
441 }
442
442
443 static long
443 static long
444 meth_hash(PythonQtSlotFunctionObject *a)
444 meth_hash(PythonQtSlotFunctionObject *a)
445 {
445 {
446 long x,y;
446 long x,y;
447 if (a->m_self == NULL)
447 if (a->m_self == NULL)
448 x = 0;
448 x = 0;
449 else {
449 else {
450 x = PyObject_Hash(a->m_self);
450 x = PyObject_Hash(a->m_self);
451 if (x == -1)
451 if (x == -1)
452 return -1;
452 return -1;
453 }
453 }
454 y = _Py_HashPointer((void*)(a->m_ml));
454 y = _Py_HashPointer((void*)(a->m_ml));
455 if (y == -1)
455 if (y == -1)
456 return -1;
456 return -1;
457 x ^= y;
457 x ^= y;
458 if (x == -1)
458 if (x == -1)
459 x = -2;
459 x = -2;
460 return x;
460 return x;
461 }
461 }
462
462
463
463
464 PyTypeObject PythonQtSlotFunction_Type = {
464 PyTypeObject PythonQtSlotFunction_Type = {
465 PyObject_HEAD_INIT(&PyType_Type)
465 PyObject_HEAD_INIT(&PyType_Type)
466 0,
466 0,
467 "builtin_qt_slot",
467 "builtin_qt_slot",
468 sizeof(PythonQtSlotFunctionObject),
468 sizeof(PythonQtSlotFunctionObject),
469 0,
469 0,
470 (destructor)meth_dealloc, /* tp_dealloc */
470 (destructor)meth_dealloc, /* tp_dealloc */
471 0, /* tp_print */
471 0, /* tp_print */
472 0, /* tp_getattr */
472 0, /* tp_getattr */
473 0, /* tp_setattr */
473 0, /* tp_setattr */
474 (cmpfunc)meth_compare, /* tp_compare */
474 (cmpfunc)meth_compare, /* tp_compare */
475 (reprfunc)meth_repr, /* tp_repr */
475 (reprfunc)meth_repr, /* tp_repr */
476 0, /* tp_as_number */
476 0, /* tp_as_number */
477 0, /* tp_as_sequence */
477 0, /* tp_as_sequence */
478 0, /* tp_as_mapping */
478 0, /* tp_as_mapping */
479 (hashfunc)meth_hash, /* tp_hash */
479 (hashfunc)meth_hash, /* tp_hash */
480 PythonQtSlotFunction_Call, /* tp_call */
480 PythonQtSlotFunction_Call, /* tp_call */
481 0, /* tp_str */
481 0, /* tp_str */
482 PyObject_GenericGetAttr, /* tp_getattro */
482 PyObject_GenericGetAttr, /* tp_getattro */
483 0, /* tp_setattro */
483 0, /* tp_setattro */
484 0, /* tp_as_buffer */
484 0, /* tp_as_buffer */
485 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
485 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
486 0, /* tp_doc */
486 0, /* tp_doc */
487 (traverseproc)meth_traverse, /* tp_traverse */
487 (traverseproc)meth_traverse, /* tp_traverse */
488 0, /* tp_clear */
488 0, /* tp_clear */
489 0, /* tp_richcompare */
489 0, /* tp_richcompare */
490 0, /* tp_weaklistoffset */
490 0, /* tp_weaklistoffset */
491 0, /* tp_iter */
491 0, /* tp_iter */
492 0, /* tp_iternext */
492 0, /* tp_iternext */
493 0, /* tp_methods */
493 0, /* tp_methods */
494 meth_members, /* tp_members */
494 meth_members, /* tp_members */
495 meth_getsets, /* tp_getset */
495 meth_getsets, /* tp_getset */
496 0, /* tp_base */
496 0, /* tp_base */
497 0, /* tp_dict */
497 0, /* tp_dict */
498 };
498 };
499
499
500 /* Clear out the free list */
500 /* Clear out the free list */
501
501
502 void
502 void
503 PythonQtSlotFunction_Fini(void)
503 PythonQtSlotFunction_Fini(void)
504 {
504 {
505 while (pythonqtslot_free_list) {
505 while (pythonqtslot_free_list) {
506 PythonQtSlotFunctionObject *v = pythonqtslot_free_list;
506 PythonQtSlotFunctionObject *v = pythonqtslot_free_list;
507 pythonqtslot_free_list = (PythonQtSlotFunctionObject *)(v->m_self);
507 pythonqtslot_free_list = (PythonQtSlotFunctionObject *)(v->m_self);
508 PyObject_GC_Del(v);
508 PyObject_GC_Del(v);
509 }
509 }
510 }
510 }
511
511
General Comments 0
You need to be logged in to leave comments. Login now