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