##// END OF EJS Templates
fixed binary operations where first argument is not a wrapper...
florianlink -
r136:90c7a1edb395
parent child
Show More
@@ -1,454 +1,462
1 /*
1 /*
2 *
2 *
3 * Copyright (C) 2010 MeVis Medical Solutions AG All Rights Reserved.
3 * Copyright (C) 2010 MeVis Medical Solutions AG 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 Medical Solutions AG, Universitaetsallee 29,
26 * Contact information: MeVis Medical Solutions AG, 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 PythonQtClassWrapper.cpp
35 // \file PythonQtClassWrapper.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 "PythonQtClassWrapper.h"
42 #include "PythonQtClassWrapper.h"
43 #include <QObject>
43 #include <QObject>
44
44
45 #include "PythonQt.h"
45 #include "PythonQt.h"
46 #include "PythonQtSlot.h"
46 #include "PythonQtSlot.h"
47 #include "PythonQtClassInfo.h"
47 #include "PythonQtClassInfo.h"
48 #include "PythonQtConversion.h"
48 #include "PythonQtConversion.h"
49 #include "PythonQtInstanceWrapper.h"
49 #include "PythonQtInstanceWrapper.h"
50
50
51 static PyObject* PythonQtInstanceWrapper_invert(PythonQtInstanceWrapper* wrapper)
51 static PyObject* PythonQtInstanceWrapper_invert(PythonQtInstanceWrapper* wrapper)
52 {
52 {
53 PyObject* result = NULL;
53 PyObject* result = NULL;
54 static QByteArray memberName = "__invert__";
54 static QByteArray memberName = "__invert__";
55 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(memberName);
55 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(memberName);
56 if (opSlot._type == PythonQtMemberInfo::Slot) {
56 if (opSlot._type == PythonQtMemberInfo::Slot) {
57 result = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, NULL, NULL, wrapper->_wrappedPtr);
57 result = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, NULL, NULL, wrapper->_wrappedPtr);
58 }
58 }
59 return result;
59 return result;
60 }
60 }
61
61
62 static int PythonQtInstanceWrapper_nonzero(PythonQtInstanceWrapper* wrapper)
62 static int PythonQtInstanceWrapper_nonzero(PythonQtInstanceWrapper* wrapper)
63 {
63 {
64 int result = (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1;
64 int result = (wrapper->_wrappedPtr == NULL && wrapper->_obj == NULL)?0:1;
65 if (result) {
65 if (result) {
66 static QByteArray memberName = "__nonzero__";
66 static QByteArray memberName = "__nonzero__";
67 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(memberName);
67 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(memberName);
68 if (opSlot._type == PythonQtMemberInfo::Slot) {
68 if (opSlot._type == PythonQtMemberInfo::Slot) {
69 PyObject* resultObj = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, NULL, NULL, wrapper->_wrappedPtr);
69 PyObject* resultObj = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, NULL, NULL, wrapper->_wrappedPtr);
70 if (resultObj == Py_False) {
70 if (resultObj == Py_False) {
71 result = 0;
71 result = 0;
72 }
72 }
73 Py_XDECREF(resultObj);
73 Py_XDECREF(resultObj);
74 }
74 }
75 }
75 }
76 return result;
76 return result;
77 }
77 }
78
78
79
79
80 static PyObject* PythonQtInstanceWrapper_binaryfunc(PythonQtInstanceWrapper* wrapper, PyObject* other, const QByteArray& opName, const QByteArray& fallbackOpName = QByteArray())
80 static PyObject* PythonQtInstanceWrapper_binaryfunc(PyObject* self, PyObject* other, const QByteArray& opName, const QByteArray& fallbackOpName = QByteArray())
81 {
81 {
82 // since we disabled type checking, we can receive any object as self, but we currently only support
83 // different objects on the right. Otherwise we would need to generate __radd__ etc. methods.
84 if (!PyObject_TypeCheck(self, &PythonQtInstanceWrapper_Type)) {
85 QString error = "Unsupported operation " + opName + "(" + self->ob_type->tp_name + ", " + other->ob_type->tp_name + ")";
86 PyErr_SetString(PyExc_ArithmeticError, error.toLatin1().data());
87 return NULL;
88 }
89 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)self;
82 PyObject* result = NULL;
90 PyObject* result = NULL;
83 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(opName);
91 PythonQtMemberInfo opSlot = wrapper->classInfo()->member(opName);
84 if (opSlot._type == PythonQtMemberInfo::Slot) {
92 if (opSlot._type == PythonQtMemberInfo::Slot) {
85 // TODO get rid of tuple
93 // TODO get rid of tuple
86 PyObject* args = PyTuple_New(1);
94 PyObject* args = PyTuple_New(1);
87 Py_INCREF(other);
95 Py_INCREF(other);
88 PyTuple_SET_ITEM(args, 0, other);
96 PyTuple_SET_ITEM(args, 0, other);
89 result = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, args, NULL, wrapper->_wrappedPtr);
97 result = PythonQtSlotFunction_CallImpl(wrapper->classInfo(), wrapper->_obj, opSlot._slot, args, NULL, wrapper->_wrappedPtr);
90 Py_DECREF(args);
98 Py_DECREF(args);
91 if (!result && !fallbackOpName.isEmpty()) {
99 if (!result && !fallbackOpName.isEmpty()) {
92 // try fallback if we did not get a result
100 // try fallback if we did not get a result
93 result = PythonQtInstanceWrapper_binaryfunc(wrapper, other, fallbackOpName);
101 result = PythonQtInstanceWrapper_binaryfunc(self, other, fallbackOpName);
94 }
102 }
95 }
103 }
96 return result;
104 return result;
97 }
105 }
98
106
99 #define BINARY_OP(NAME) \
107 #define BINARY_OP(NAME) \
100 static PyObject* PythonQtInstanceWrapper_ ## NAME(PythonQtInstanceWrapper* wrapper, PyObject* other) \
108 static PyObject* PythonQtInstanceWrapper_ ## NAME(PyObject* self, PyObject* other) \
101 { \
109 { \
102 static const QByteArray opName("__" #NAME "__"); \
110 static const QByteArray opName("__" #NAME "__"); \
103 return PythonQtInstanceWrapper_binaryfunc(wrapper, other, opName); \
111 return PythonQtInstanceWrapper_binaryfunc(self, other, opName); \
104 }
112 }
105
113
106 #define BINARY_OP_INPLACE(NAME) \
114 #define BINARY_OP_INPLACE(NAME) \
107 static PyObject* PythonQtInstanceWrapper_i ## NAME(PythonQtInstanceWrapper* wrapper, PyObject* other) \
115 static PyObject* PythonQtInstanceWrapper_i ## NAME(PyObject* self, PyObject* other) \
108 { \
116 { \
109 static const QByteArray opName("__i" #NAME "__"); \
117 static const QByteArray opName("__i" #NAME "__"); \
110 static const QByteArray fallbackName("__" #NAME "__"); \
118 static const QByteArray fallbackName("__" #NAME "__"); \
111 return PythonQtInstanceWrapper_binaryfunc(wrapper, other, opName, fallbackName); \
119 return PythonQtInstanceWrapper_binaryfunc(self, other, opName, fallbackName); \
112 }
120 }
113
121
114 BINARY_OP(add)
122 BINARY_OP(add)
115 BINARY_OP(sub)
123 BINARY_OP(sub)
116 BINARY_OP(mul)
124 BINARY_OP(mul)
117 BINARY_OP(div)
125 BINARY_OP(div)
118 BINARY_OP(and)
126 BINARY_OP(and)
119 BINARY_OP(or)
127 BINARY_OP(or)
120 BINARY_OP(xor)
128 BINARY_OP(xor)
121 BINARY_OP(mod)
129 BINARY_OP(mod)
122 BINARY_OP(lshift)
130 BINARY_OP(lshift)
123 BINARY_OP(rshift)
131 BINARY_OP(rshift)
124
132
125 BINARY_OP_INPLACE(add)
133 BINARY_OP_INPLACE(add)
126 BINARY_OP_INPLACE(sub)
134 BINARY_OP_INPLACE(sub)
127 BINARY_OP_INPLACE(mul)
135 BINARY_OP_INPLACE(mul)
128 BINARY_OP_INPLACE(div)
136 BINARY_OP_INPLACE(div)
129 BINARY_OP_INPLACE(and)
137 BINARY_OP_INPLACE(and)
130 BINARY_OP_INPLACE(or)
138 BINARY_OP_INPLACE(or)
131 BINARY_OP_INPLACE(xor)
139 BINARY_OP_INPLACE(xor)
132 BINARY_OP_INPLACE(mod)
140 BINARY_OP_INPLACE(mod)
133 BINARY_OP_INPLACE(lshift)
141 BINARY_OP_INPLACE(lshift)
134 BINARY_OP_INPLACE(rshift)
142 BINARY_OP_INPLACE(rshift)
135
143
136 static void initializeSlots(PythonQtClassWrapper* wrap)
144 static void initializeSlots(PythonQtClassWrapper* wrap)
137 {
145 {
138 int typeSlots = wrap->classInfo()->typeSlots();
146 int typeSlots = wrap->classInfo()->typeSlots();
139 if (typeSlots) {
147 if (typeSlots) {
140 if (typeSlots & PythonQt::Type_Add) {
148 if (typeSlots & PythonQt::Type_Add) {
141 wrap->_base.as_number.nb_add = (binaryfunc)PythonQtInstanceWrapper_add;
149 wrap->_base.as_number.nb_add = (binaryfunc)PythonQtInstanceWrapper_add;
142 }
150 }
143 if (typeSlots & PythonQt::Type_Subtract) {
151 if (typeSlots & PythonQt::Type_Subtract) {
144 wrap->_base.as_number.nb_subtract = (binaryfunc)PythonQtInstanceWrapper_sub;
152 wrap->_base.as_number.nb_subtract = (binaryfunc)PythonQtInstanceWrapper_sub;
145 }
153 }
146 if (typeSlots & PythonQt::Type_Multiply) {
154 if (typeSlots & PythonQt::Type_Multiply) {
147 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_mul;
155 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_mul;
148 }
156 }
149 if (typeSlots & PythonQt::Type_Divide) {
157 if (typeSlots & PythonQt::Type_Divide) {
150 wrap->_base.as_number.nb_divide = (binaryfunc)PythonQtInstanceWrapper_div;
158 wrap->_base.as_number.nb_divide = (binaryfunc)PythonQtInstanceWrapper_div;
151 wrap->_base.as_number.nb_true_divide = (binaryfunc)PythonQtInstanceWrapper_div;
159 wrap->_base.as_number.nb_true_divide = (binaryfunc)PythonQtInstanceWrapper_div;
152 }
160 }
153 if (typeSlots & PythonQt::Type_And) {
161 if (typeSlots & PythonQt::Type_And) {
154 wrap->_base.as_number.nb_and = (binaryfunc)PythonQtInstanceWrapper_and;
162 wrap->_base.as_number.nb_and = (binaryfunc)PythonQtInstanceWrapper_and;
155 }
163 }
156 if (typeSlots & PythonQt::Type_Or) {
164 if (typeSlots & PythonQt::Type_Or) {
157 wrap->_base.as_number.nb_or = (binaryfunc)PythonQtInstanceWrapper_or;
165 wrap->_base.as_number.nb_or = (binaryfunc)PythonQtInstanceWrapper_or;
158 }
166 }
159 if (typeSlots & PythonQt::Type_Xor) {
167 if (typeSlots & PythonQt::Type_Xor) {
160 wrap->_base.as_number.nb_xor = (binaryfunc)PythonQtInstanceWrapper_xor;
168 wrap->_base.as_number.nb_xor = (binaryfunc)PythonQtInstanceWrapper_xor;
161 }
169 }
162 if (typeSlots & PythonQt::Type_Mod) {
170 if (typeSlots & PythonQt::Type_Mod) {
163 wrap->_base.as_number.nb_remainder = (binaryfunc)PythonQtInstanceWrapper_mod;
171 wrap->_base.as_number.nb_remainder = (binaryfunc)PythonQtInstanceWrapper_mod;
164 }
172 }
165 if (typeSlots & PythonQt::Type_LShift) {
173 if (typeSlots & PythonQt::Type_LShift) {
166 wrap->_base.as_number.nb_lshift = (binaryfunc)PythonQtInstanceWrapper_lshift;
174 wrap->_base.as_number.nb_lshift = (binaryfunc)PythonQtInstanceWrapper_lshift;
167 }
175 }
168 if (typeSlots & PythonQt::Type_RShift) {
176 if (typeSlots & PythonQt::Type_RShift) {
169 wrap->_base.as_number.nb_rshift = (binaryfunc)PythonQtInstanceWrapper_rshift;
177 wrap->_base.as_number.nb_rshift = (binaryfunc)PythonQtInstanceWrapper_rshift;
170 }
178 }
171
179
172 if (typeSlots & PythonQt::Type_InplaceAdd) {
180 if (typeSlots & PythonQt::Type_InplaceAdd) {
173 wrap->_base.as_number.nb_add = (binaryfunc)PythonQtInstanceWrapper_iadd;
181 wrap->_base.as_number.nb_add = (binaryfunc)PythonQtInstanceWrapper_iadd;
174 }
182 }
175 if (typeSlots & PythonQt::Type_InplaceSubtract) {
183 if (typeSlots & PythonQt::Type_InplaceSubtract) {
176 wrap->_base.as_number.nb_subtract = (binaryfunc)PythonQtInstanceWrapper_isub;
184 wrap->_base.as_number.nb_subtract = (binaryfunc)PythonQtInstanceWrapper_isub;
177 }
185 }
178 if (typeSlots & PythonQt::Type_InplaceMultiply) {
186 if (typeSlots & PythonQt::Type_InplaceMultiply) {
179 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_imul;
187 wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_imul;
180 }
188 }
181 if (typeSlots & PythonQt::Type_InplaceDivide) {
189 if (typeSlots & PythonQt::Type_InplaceDivide) {
182 wrap->_base.as_number.nb_inplace_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
190 wrap->_base.as_number.nb_inplace_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
183 wrap->_base.as_number.nb_inplace_true_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
191 wrap->_base.as_number.nb_inplace_true_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
184 }
192 }
185 if (typeSlots & PythonQt::Type_InplaceAnd) {
193 if (typeSlots & PythonQt::Type_InplaceAnd) {
186 wrap->_base.as_number.nb_inplace_and = (binaryfunc)PythonQtInstanceWrapper_iand;
194 wrap->_base.as_number.nb_inplace_and = (binaryfunc)PythonQtInstanceWrapper_iand;
187 }
195 }
188 if (typeSlots & PythonQt::Type_InplaceOr) {
196 if (typeSlots & PythonQt::Type_InplaceOr) {
189 wrap->_base.as_number.nb_inplace_or = (binaryfunc)PythonQtInstanceWrapper_ior;
197 wrap->_base.as_number.nb_inplace_or = (binaryfunc)PythonQtInstanceWrapper_ior;
190 }
198 }
191 if (typeSlots & PythonQt::Type_InplaceXor) {
199 if (typeSlots & PythonQt::Type_InplaceXor) {
192 wrap->_base.as_number.nb_inplace_xor = (binaryfunc)PythonQtInstanceWrapper_ixor;
200 wrap->_base.as_number.nb_inplace_xor = (binaryfunc)PythonQtInstanceWrapper_ixor;
193 }
201 }
194 if (typeSlots & PythonQt::Type_InplaceMod) {
202 if (typeSlots & PythonQt::Type_InplaceMod) {
195 wrap->_base.as_number.nb_inplace_remainder = (binaryfunc)PythonQtInstanceWrapper_imod;
203 wrap->_base.as_number.nb_inplace_remainder = (binaryfunc)PythonQtInstanceWrapper_imod;
196 }
204 }
197 if (typeSlots & PythonQt::Type_InplaceLShift) {
205 if (typeSlots & PythonQt::Type_InplaceLShift) {
198 wrap->_base.as_number.nb_inplace_lshift = (binaryfunc)PythonQtInstanceWrapper_ilshift;
206 wrap->_base.as_number.nb_inplace_lshift = (binaryfunc)PythonQtInstanceWrapper_ilshift;
199 }
207 }
200 if (typeSlots & PythonQt::Type_InplaceRShift) {
208 if (typeSlots & PythonQt::Type_InplaceRShift) {
201 wrap->_base.as_number.nb_inplace_rshift = (binaryfunc)PythonQtInstanceWrapper_irshift;
209 wrap->_base.as_number.nb_inplace_rshift = (binaryfunc)PythonQtInstanceWrapper_irshift;
202 }
210 }
203 if (typeSlots & PythonQt::Type_Invert) {
211 if (typeSlots & PythonQt::Type_Invert) {
204 wrap->_base.as_number.nb_invert = (unaryfunc)PythonQtInstanceWrapper_invert;
212 wrap->_base.as_number.nb_invert = (unaryfunc)PythonQtInstanceWrapper_invert;
205 }
213 }
206 if (typeSlots & PythonQt::Type_NonZero) {
214 if (typeSlots & PythonQt::Type_NonZero) {
207 wrap->_base.as_number.nb_nonzero = (inquiry)PythonQtInstanceWrapper_nonzero;
215 wrap->_base.as_number.nb_nonzero = (inquiry)PythonQtInstanceWrapper_nonzero;
208 }
216 }
209 }
217 }
210 }
218 }
211
219
212 static PyObject* PythonQtClassWrapper_alloc(PyTypeObject *self, Py_ssize_t nitems)
220 static PyObject* PythonQtClassWrapper_alloc(PyTypeObject *self, Py_ssize_t nitems)
213 {
221 {
214 // call the default type alloc
222 // call the default type alloc
215 PyObject* obj = PyType_Type.tp_alloc(self, nitems);
223 PyObject* obj = PyType_Type.tp_alloc(self, nitems);
216
224
217 // take current class type, if we are called via newPythonQtClassWrapper()
225 // take current class type, if we are called via newPythonQtClassWrapper()
218 PythonQtClassWrapper* wrap = (PythonQtClassWrapper*)obj;
226 PythonQtClassWrapper* wrap = (PythonQtClassWrapper*)obj;
219 wrap->_classInfo = PythonQt::priv()->currentClassInfoForClassWrapperCreation();
227 wrap->_classInfo = PythonQt::priv()->currentClassInfoForClassWrapperCreation();
220 if (wrap->_classInfo) {
228 if (wrap->_classInfo) {
221 initializeSlots(wrap);
229 initializeSlots(wrap);
222 }
230 }
223
231
224 return obj;
232 return obj;
225 }
233 }
226
234
227
235
228 static int PythonQtClassWrapper_init(PythonQtClassWrapper* self, PyObject* args, PyObject* kwds)
236 static int PythonQtClassWrapper_init(PythonQtClassWrapper* self, PyObject* args, PyObject* kwds)
229 {
237 {
230 // call the default type init
238 // call the default type init
231 if (PyType_Type.tp_init((PyObject *)self, args, kwds) < 0) {
239 if (PyType_Type.tp_init((PyObject *)self, args, kwds) < 0) {
232 return -1;
240 return -1;
233 }
241 }
234
242
235 // if we have no CPP class information, try our base class
243 // if we have no CPP class information, try our base class
236 if (!self->classInfo()) {
244 if (!self->classInfo()) {
237 PyTypeObject* superType = ((PyTypeObject *)self)->tp_base;
245 PyTypeObject* superType = ((PyTypeObject *)self)->tp_base;
238
246
239 if (!superType || (superType->ob_type != &PythonQtClassWrapper_Type)) {
247 if (!superType || (superType->ob_type != &PythonQtClassWrapper_Type)) {
240 PyErr_Format(PyExc_TypeError, "type %s is not derived from PythonQtClassWrapper", ((PyTypeObject*)self)->tp_name);
248 PyErr_Format(PyExc_TypeError, "type %s is not derived from PythonQtClassWrapper", ((PyTypeObject*)self)->tp_name);
241 return -1;
249 return -1;
242 }
250 }
243
251
244 // take the class info from the superType
252 // take the class info from the superType
245 self->_classInfo = ((PythonQtClassWrapper*)superType)->classInfo();
253 self->_classInfo = ((PythonQtClassWrapper*)superType)->classInfo();
246 }
254 }
247
255
248 return 0;
256 return 0;
249 }
257 }
250
258
251 static PyObject *PythonQtClassWrapper_classname(PythonQtClassWrapper* type)
259 static PyObject *PythonQtClassWrapper_classname(PythonQtClassWrapper* type)
252 {
260 {
253 return PyString_FromString((QString("Class_") + type->classInfo()->className()).toLatin1().data());
261 return PyString_FromString((QString("Class_") + type->classInfo()->className()).toLatin1().data());
254 }
262 }
255
263
256 static PyObject *PythonQtClassWrapper_help(PythonQtClassWrapper* type)
264 static PyObject *PythonQtClassWrapper_help(PythonQtClassWrapper* type)
257 {
265 {
258 return PythonQt::self()->helpCalled(type->classInfo());
266 return PythonQt::self()->helpCalled(type->classInfo());
259 }
267 }
260
268
261 PyObject *PythonQtClassWrapper__init__(PythonQtClassWrapper *type, PyObject *args)
269 PyObject *PythonQtClassWrapper__init__(PythonQtClassWrapper *type, PyObject *args)
262 {
270 {
263 Py_ssize_t argc = PyTuple_Size(args);
271 Py_ssize_t argc = PyTuple_Size(args);
264 if (argc>0) {
272 if (argc>0) {
265 // we need to call __init__ of the instance
273 // we need to call __init__ of the instance
266 PyObject* self = PyTuple_GET_ITEM(args, 0);
274 PyObject* self = PyTuple_GET_ITEM(args, 0);
267 if (PyObject_TypeCheck(self, (PyTypeObject*)type->classInfo()->pythonQtClassWrapper())) {
275 if (PyObject_TypeCheck(self, (PyTypeObject*)type->classInfo()->pythonQtClassWrapper())) {
268 PyObject* newargs = PyTuple_New(argc-1);
276 PyObject* newargs = PyTuple_New(argc-1);
269 for (int i = 0;i<argc-1; i++) {
277 for (int i = 0;i<argc-1; i++) {
270 PyTuple_SET_ITEM(newargs, i,PyTuple_GET_ITEM(args, i+1));
278 PyTuple_SET_ITEM(newargs, i,PyTuple_GET_ITEM(args, i+1));
271 }
279 }
272 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)self;
280 PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)self;
273 int result = PythonQtInstanceWrapper_init(wrapper, newargs, NULL);
281 int result = PythonQtInstanceWrapper_init(wrapper, newargs, NULL);
274 Py_DECREF(newargs);
282 Py_DECREF(newargs);
275 if (result==0) {
283 if (result==0) {
276 Py_INCREF(Py_None);
284 Py_INCREF(Py_None);
277 return Py_None;
285 return Py_None;
278 } else {
286 } else {
279 // init failed!
287 // init failed!
280 }
288 }
281 } else {
289 } else {
282 // self not of correct type!
290 // self not of correct type!
283 }
291 }
284 } else {
292 } else {
285 // wrong number of args
293 // wrong number of args
286 }
294 }
287 return NULL;
295 return NULL;
288 }
296 }
289
297
290 static PyMethodDef PythonQtClassWrapper_methods[] = {
298 static PyMethodDef PythonQtClassWrapper_methods[] = {
291 {"__init__", (PyCFunction)PythonQtClassWrapper__init__, METH_VARARGS,
299 {"__init__", (PyCFunction)PythonQtClassWrapper__init__, METH_VARARGS,
292 "Return the classname of the object"
300 "Return the classname of the object"
293 },
301 },
294 {"className", (PyCFunction)PythonQtClassWrapper_classname, METH_NOARGS,
302 {"className", (PyCFunction)PythonQtClassWrapper_classname, METH_NOARGS,
295 "Return the classname of the object"
303 "Return the classname of the object"
296 },
304 },
297 {"help", (PyCFunction)PythonQtClassWrapper_help, METH_NOARGS,
305 {"help", (PyCFunction)PythonQtClassWrapper_help, METH_NOARGS,
298 "Shows the help of available methods for this class"
306 "Shows the help of available methods for this class"
299 },
307 },
300 {NULL, NULL, 0 , NULL} /* Sentinel */
308 {NULL, NULL, 0 , NULL} /* Sentinel */
301 };
309 };
302
310
303
311
304 static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
312 static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
305 {
313 {
306 const char *attributeName;
314 const char *attributeName;
307 PythonQtClassWrapper *wrapper = (PythonQtClassWrapper *)obj;
315 PythonQtClassWrapper *wrapper = (PythonQtClassWrapper *)obj;
308
316
309 if ((attributeName = PyString_AsString(name)) == NULL) {
317 if ((attributeName = PyString_AsString(name)) == NULL) {
310 return NULL;
318 return NULL;
311 }
319 }
312 if (obj == (PyObject*)&PythonQtInstanceWrapper_Type) {
320 if (obj == (PyObject*)&PythonQtInstanceWrapper_Type) {
313 return NULL;
321 return NULL;
314 }
322 }
315
323
316 if (qstrcmp(attributeName, "__dict__")==0) {
324 if (qstrcmp(attributeName, "__dict__")==0) {
317 PyObject* dict = ((PyTypeObject *)wrapper)->tp_dict;
325 PyObject* dict = ((PyTypeObject *)wrapper)->tp_dict;
318 if (!wrapper->classInfo()) {
326 if (!wrapper->classInfo()) {
319 Py_INCREF(dict);
327 Py_INCREF(dict);
320 return dict;
328 return dict;
321 }
329 }
322 dict = PyDict_Copy(dict);
330 dict = PyDict_Copy(dict);
323
331
324 QStringList l = wrapper->classInfo()->memberList(false);
332 QStringList l = wrapper->classInfo()->memberList(false);
325 foreach (QString name, l) {
333 foreach (QString name, l) {
326 PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
334 PyObject* o = PyObject_GetAttrString(obj, name.toLatin1().data());
327 if (o) {
335 if (o) {
328 PyDict_SetItemString(dict, name.toLatin1().data(), o);
336 PyDict_SetItemString(dict, name.toLatin1().data(), o);
329 Py_DECREF(o);
337 Py_DECREF(o);
330 } else {
338 } else {
331 // it must have been a property or child, which we do not know as a class object...
339 // it must have been a property or child, which we do not know as a class object...
332 }
340 }
333 }
341 }
334 if (wrapper->classInfo()->constructors()) {
342 if (wrapper->classInfo()->constructors()) {
335 PyObject* func = PyCFunction_New(&PythonQtClassWrapper_methods[0], obj);
343 PyObject* func = PyCFunction_New(&PythonQtClassWrapper_methods[0], obj);
336 PyDict_SetItemString(dict, "__init__", func);
344 PyDict_SetItemString(dict, "__init__", func);
337 Py_DECREF(func);
345 Py_DECREF(func);
338 }
346 }
339 for (int i = 1;i<3;i++) {
347 for (int i = 1;i<3;i++) {
340 PyObject* func = PyCFunction_New(&PythonQtClassWrapper_methods[i], obj);
348 PyObject* func = PyCFunction_New(&PythonQtClassWrapper_methods[i], obj);
341 PyDict_SetItemString(dict, PythonQtClassWrapper_methods[i].ml_name, func);
349 PyDict_SetItemString(dict, PythonQtClassWrapper_methods[i].ml_name, func);
342 Py_DECREF(func);
350 Py_DECREF(func);
343 }
351 }
344 return dict;
352 return dict;
345 }
353 }
346
354
347 if (wrapper->classInfo()) {
355 if (wrapper->classInfo()) {
348 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
356 PythonQtMemberInfo member = wrapper->classInfo()->member(attributeName);
349 if (member._type == PythonQtMemberInfo::EnumValue) {
357 if (member._type == PythonQtMemberInfo::EnumValue) {
350 PyObject* enumValue = member._enumValue;
358 PyObject* enumValue = member._enumValue;
351 Py_INCREF(enumValue);
359 Py_INCREF(enumValue);
352 return enumValue;
360 return enumValue;
353 } else if (member._type == PythonQtMemberInfo::EnumWrapper) {
361 } else if (member._type == PythonQtMemberInfo::EnumWrapper) {
354 PyObject* enumWrapper = member._enumWrapper;
362 PyObject* enumWrapper = member._enumWrapper;
355 Py_INCREF(enumWrapper);
363 Py_INCREF(enumWrapper);
356 return enumWrapper;
364 return enumWrapper;
357 } else if (member._type == PythonQtMemberInfo::Slot) {
365 } else if (member._type == PythonQtMemberInfo::Slot) {
358 // we return all slots, even the instance slots, since they are callable as unbound slots with self argument
366 // we return all slots, even the instance slots, since they are callable as unbound slots with self argument
359 return PythonQtSlotFunction_New(member._slot, obj, NULL);
367 return PythonQtSlotFunction_New(member._slot, obj, NULL);
360 }
368 }
361 }
369 }
362
370
363 // look for the interal methods (className(), help())
371 // look for the interal methods (className(), help())
364 PyObject* internalMethod = Py_FindMethod( PythonQtClassWrapper_methods, obj, (char*)attributeName);
372 PyObject* internalMethod = Py_FindMethod( PythonQtClassWrapper_methods, obj, (char*)attributeName);
365 if (internalMethod) {
373 if (internalMethod) {
366 return internalMethod;
374 return internalMethod;
367 }
375 }
368 PyErr_Clear();
376 PyErr_Clear();
369
377
370 // look in super
378 // look in super
371 PyObject* superAttr = PyType_Type.tp_getattro(obj, name);
379 PyObject* superAttr = PyType_Type.tp_getattro(obj, name);
372 if (superAttr) {
380 if (superAttr) {
373 return superAttr;
381 return superAttr;
374 }
382 }
375
383
376 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
384 QString error = QString(wrapper->classInfo()->className()) + " has no attribute named '" + QString(attributeName) + "'";
377 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
385 PyErr_SetString(PyExc_AttributeError, error.toLatin1().data());
378 return NULL;
386 return NULL;
379 }
387 }
380
388
381 static int PythonQtClassWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
389 static int PythonQtClassWrapper_setattro(PyObject *obj,PyObject *name,PyObject *value)
382 {
390 {
383 return PyType_Type.tp_setattro(obj,name,value);
391 return PyType_Type.tp_setattro(obj,name,value);
384 }
392 }
385
393
386 /*
394 /*
387 static PyObject * PythonQtClassWrapper_repr(PyObject * obj)
395 static PyObject * PythonQtClassWrapper_repr(PyObject * obj)
388 {
396 {
389 PythonQtClassWrapper* wrapper = (PythonQtClassWrapper*)obj;
397 PythonQtClassWrapper* wrapper = (PythonQtClassWrapper*)obj;
390 if (wrapper->classInfo()->isCPPWrapper()) {
398 if (wrapper->classInfo()->isCPPWrapper()) {
391 const QMetaObject* meta = wrapper->classInfo()->metaObject();
399 const QMetaObject* meta = wrapper->classInfo()->metaObject();
392 if (!meta) {
400 if (!meta) {
393 QObject* decorator = wrapper->classInfo()->decorator();
401 QObject* decorator = wrapper->classInfo()->decorator();
394 if (decorator) {
402 if (decorator) {
395 meta = decorator->metaObject();
403 meta = decorator->metaObject();
396 }
404 }
397 }
405 }
398 if (meta) {
406 if (meta) {
399 return PyString_FromFormat("%s Class (C++ wrapped by %s)", wrapper->classInfo()->className(), meta->className());
407 return PyString_FromFormat("%s Class (C++ wrapped by %s)", wrapper->classInfo()->className(), meta->className());
400 } else {
408 } else {
401 return PyString_FromFormat("%s Class (C++ unwrapped)", wrapper->classInfo()->className());
409 return PyString_FromFormat("%s Class (C++ unwrapped)", wrapper->classInfo()->className());
402 }
410 }
403 } else {
411 } else {
404 return PyString_FromFormat("%s Class", wrapper->classInfo()->className());
412 return PyString_FromFormat("%s Class", wrapper->classInfo()->className());
405 }
413 }
406 }
414 }
407
415
408 */
416 */
409
417
410 PyTypeObject PythonQtClassWrapper_Type = {
418 PyTypeObject PythonQtClassWrapper_Type = {
411 PyObject_HEAD_INIT(NULL)
419 PyObject_HEAD_INIT(NULL)
412 0, /*ob_size*/
420 0, /*ob_size*/
413 "PythonQt.PythonQtClassWrapper", /*tp_name*/
421 "PythonQt.PythonQtClassWrapper", /*tp_name*/
414 sizeof(PythonQtClassWrapper), /*tp_basicsize*/
422 sizeof(PythonQtClassWrapper), /*tp_basicsize*/
415 0, /*tp_itemsize*/
423 0, /*tp_itemsize*/
416 0, /*tp_dealloc*/
424 0, /*tp_dealloc*/
417 0, /*tp_print*/
425 0, /*tp_print*/
418 0, /*tp_getattr*/
426 0, /*tp_getattr*/
419 0, /*tp_setattr*/
427 0, /*tp_setattr*/
420 0, /*tp_compare*/
428 0, /*tp_compare*/
421 0, //PythonQtClassWrapper_repr, /*tp_repr*/
429 0, //PythonQtClassWrapper_repr, /*tp_repr*/
422 0, /*tp_as_number*/
430 0, /*tp_as_number*/
423 0, /*tp_as_sequence*/
431 0, /*tp_as_sequence*/
424 0, /*tp_as_mapping*/
432 0, /*tp_as_mapping*/
425 0, /*tp_hash */
433 0, /*tp_hash */
426 0, /*tp_call*/
434 0, /*tp_call*/
427 0, /*tp_str*/
435 0, /*tp_str*/
428 PythonQtClassWrapper_getattro, /*tp_getattro*/
436 PythonQtClassWrapper_getattro, /*tp_getattro*/
429 PythonQtClassWrapper_setattro, /*tp_setattro*/
437 PythonQtClassWrapper_setattro, /*tp_setattro*/
430 0, /*tp_as_buffer*/
438 0, /*tp_as_buffer*/
431 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
439 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
432 0, /* tp_doc */
440 0, /* tp_doc */
433 0, /* tp_traverse */
441 0, /* tp_traverse */
434 0, /* tp_clear */
442 0, /* tp_clear */
435 0, /* tp_richcompare */
443 0, /* tp_richcompare */
436 0, /* tp_weaklistoffset */
444 0, /* tp_weaklistoffset */
437 0, /* tp_iter */
445 0, /* tp_iter */
438 0, /* tp_iternext */
446 0, /* tp_iternext */
439 0, /* tp_methods */
447 0, /* tp_methods */
440 0, /* tp_members */
448 0, /* tp_members */
441 0, /* tp_getset */
449 0, /* tp_getset */
442 0, /* tp_base */
450 0, /* tp_base */
443 0, /* tp_dict */
451 0, /* tp_dict */
444 0, /* tp_descr_get */
452 0, /* tp_descr_get */
445 0, /* tp_descr_set */
453 0, /* tp_descr_set */
446 0, /* tp_dictoffset */
454 0, /* tp_dictoffset */
447 (initproc)PythonQtClassWrapper_init, /* tp_init */
455 (initproc)PythonQtClassWrapper_init, /* tp_init */
448 PythonQtClassWrapper_alloc, /* tp_alloc */
456 PythonQtClassWrapper_alloc, /* tp_alloc */
449 0, /* tp_new */
457 0, /* tp_new */
450 0, /* tp_free */
458 0, /* tp_free */
451 };
459 };
452
460
453 //-------------------------------------------------------
461 //-------------------------------------------------------
454
462
General Comments 0
You need to be logged in to leave comments. Login now