Skip to content

Commit 6d72ce6

Browse files
author
florianlink
committed
added support for [] mapping operators
git-svn-id: http://svn.code.sf.net/p/pythonqt/code/trunk@356 ea8d5007-eb21-0410-b261-ccb3ea6e24a9
1 parent e147814 commit 6d72ce6

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

src/PythonQt.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,9 @@ class PYTHONQT_EXPORT PythonQt : public QObject {
137137
Type_InplaceLShift = 1 << 18,
138138
Type_InplaceRShift = 1 << 19,
139139

140-
// Not yet needed/nicely mappable/generated...
141-
//Type_Positive = 1 << 29,
142-
//Type_Negative = 1 << 29,
143-
//Type_Abs = 1 << 29,
144-
//Type_Hash = 1 << 29,
140+
Type_Length = 1 << 20,
141+
Type_MappingSetItem = 1 << 21,
142+
Type_MappingGetItem = 1 << 22,
145143

146144
Type_Invert = 1 << 29,
147145
Type_RichCompare = 1 << 30,

src/PythonQtClassWrapper.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -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

81129
static PyObject* PythonQtInstanceWrapper_binaryfunc(PyObject* self, PyObject* other, const QByteArray& opName, const QByteArray& fallbackOpName = QByteArray())
82130
{
@@ -130,6 +178,7 @@ BINARY_OP(xor)
130178
BINARY_OP(mod)
131179
BINARY_OP(lshift)
132180
BINARY_OP(rshift)
181+
BINARY_OP(getitem)
133182

134183
BINARY_OP_INPLACE(add)
135184
BINARY_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

Comments
 (0)