@@ -77,6 +77,54 @@ static int PythonQtInstanceWrapper_nonzero(PythonQtInstanceWrapper* wrapper)
77
77
return result;
78
78
}
79
79
80
+ static Py_ssize_t PythonQtInstanceWrapper_length (PythonQtInstanceWrapper* wrapper)
81
+ {
82
+ qint64 result = -1 ;
83
+ if (wrapper->_wrappedPtr != NULL || wrapper->_obj != NULL ) {
84
+ static QByteArray memberName = " __len__" ;
85
+ PythonQtMemberInfo opSlot = wrapper->classInfo ()->member (memberName);
86
+ if (opSlot._type == PythonQtMemberInfo::Slot) {
87
+ PyObject* resultObj = PythonQtSlotFunction_CallImpl (wrapper->classInfo (), wrapper->_obj , opSlot._slot , NULL , NULL , wrapper->_wrappedPtr );
88
+ bool ok;
89
+ result = PythonQtConv::PyObjGetLongLong (resultObj, false , ok);
90
+ if (!ok) {
91
+ result = -1 ;
92
+ }
93
+ Py_XDECREF (resultObj);
94
+ }
95
+ }
96
+ return result;
97
+ }
98
+
99
+ static int PythonQtInstanceWrapper_setitem (PyObject* self, PyObject* index, PyObject* value)
100
+ {
101
+ PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)self;
102
+ PythonQtMemberInfo opSlot;
103
+ bool isSetItem = false ;
104
+ if (value) {
105
+ isSetItem = true ;
106
+ opSlot = wrapper->classInfo ()->member (" __setitem__" );
107
+ } else {
108
+ opSlot = wrapper->classInfo ()->member (" __delitem__" );
109
+ }
110
+ if (opSlot._type == PythonQtMemberInfo::Slot) {
111
+ PyObject* args = PyTuple_New (isSetItem?2 :1 );
112
+ Py_INCREF (index );
113
+ PyTuple_SET_ITEM (args, 0 , index );
114
+ if (isSetItem) {
115
+ Py_INCREF (value);
116
+ PyTuple_SET_ITEM (args, 1 , value);
117
+ }
118
+ PyObject* result = PythonQtSlotFunction_CallImpl (wrapper->classInfo (), wrapper->_obj , opSlot._slot , args, NULL , wrapper->_wrappedPtr );
119
+ if (result) {
120
+ Py_DECREF (result);
121
+ }
122
+ Py_DECREF (args);
123
+ return 0 ;
124
+ } else {
125
+ return -1 ;
126
+ }
127
+ }
80
128
81
129
static PyObject* PythonQtInstanceWrapper_binaryfunc (PyObject* self, PyObject* other, const QByteArray& opName, const QByteArray& fallbackOpName = QByteArray())
82
130
{
@@ -130,6 +178,7 @@ BINARY_OP(xor)
130
178
BINARY_OP(mod)
131
179
BINARY_OP(lshift)
132
180
BINARY_OP(rshift)
181
+ BINARY_OP(getitem)
133
182
134
183
BINARY_OP_INPLACE(add)
135
184
BINARY_OP_INPLACE(sub)
@@ -146,6 +195,19 @@ static void initializeSlots(PythonQtClassWrapper* wrap)
146
195
{
147
196
int typeSlots = wrap->classInfo ()->typeSlots ();
148
197
if (typeSlots) {
198
+
199
+ if (typeSlots & PythonQt::Type_MappingGetItem) {
200
+ wrap->_base .as_mapping .mp_subscript = (binaryfunc)PythonQtInstanceWrapper_getitem;
201
+ }
202
+ if (typeSlots & PythonQt::Type_MappingSetItem) {
203
+ wrap->_base .as_mapping .mp_ass_subscript = (objobjargproc)PythonQtInstanceWrapper_setitem;
204
+ }
205
+ if (typeSlots & (PythonQt::Type_MappingGetItem | PythonQt::Type_MappingSetItem)) {
206
+ if (typeSlots & PythonQt::Type_Length) {
207
+ wrap->_base .as_mapping .mp_length = (lenfunc)PythonQtInstanceWrapper_length;
208
+ }
209
+ }
210
+
149
211
if (typeSlots & PythonQt::Type_Add) {
150
212
wrap->_base .as_number .nb_add = (binaryfunc)PythonQtInstanceWrapper_add;
151
213
}
0 commit comments