@@ -77,6 +77,54 @@ static int PythonQtInstanceWrapper_nonzero(PythonQtInstanceWrapper* wrapper)
7777 return result;
7878}
7979
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+ }
80128
81129static PyObject* PythonQtInstanceWrapper_binaryfunc (PyObject* self, PyObject* other, const QByteArray& opName, const QByteArray& fallbackOpName = QByteArray())
82130{
@@ -130,6 +178,7 @@ BINARY_OP(xor)
130178BINARY_OP(mod)
131179BINARY_OP(lshift)
132180BINARY_OP(rshift)
181+ BINARY_OP(getitem)
133182
134183BINARY_OP_INPLACE(add)
135184BINARY_OP_INPLACE(sub)
@@ -146,6 +195,19 @@ static void initializeSlots(PythonQtClassWrapper* wrap)
146195{
147196 int typeSlots = wrap->classInfo ()->typeSlots ();
148197 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+
149211 if (typeSlots & PythonQt::Type_Add) {
150212 wrap->_base .as_number .nb_add = (binaryfunc)PythonQtInstanceWrapper_add;
151213 }
0 commit comments