Skip to content

Commit 4bb136e

Browse files
author
florianlink
committed
fixed bug that was introduced by refactoring
added ownership handling for QList<SomeObject*> git-svn-id: http://svn.code.sf.net/p/pythonqt/code/trunk@378 ea8d5007-eb21-0410-b261-ccb3ea6e24a9
1 parent b0056bc commit 4bb136e

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

src/PythonQtConversion.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ PyObject* PythonQtConv::ConvertQtValueToPython(const PythonQtMethodInfo::Paramet
9595
listPtr = (QList<void*>*)data;
9696
}
9797
if (listPtr) {
98-
return ConvertQListOfPointerTypeToPythonList(listPtr, info.innerName);
98+
return ConvertQListOfPointerTypeToPythonList(listPtr, info);
9999
} else {
100100
return NULL;
101101
}
@@ -656,7 +656,7 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
656656
} else {
657657
ptr = alreadyAllocatedCPPObject;
658658
}
659-
ok = ConvertPythonListToQListOfPointerType(obj, (QList<void*>*)ptr, info.innerName, strict);
659+
ok = ConvertPythonListToQListOfPointerType(obj, (QList<void*>*)ptr, info, strict);
660660
if (ok) {
661661
return ptr;
662662
} else {
@@ -1189,7 +1189,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
11891189
if (info.isQList && (info.innerNamePointerCount == 1)) {
11901190
// allocate a default object of the needed type:
11911191
v = QVariant(type, (const void*)NULL);
1192-
ok = ConvertPythonListToQListOfPointerType(val, (QList<void*>*)v.constData(), info.innerName, true);
1192+
ok = ConvertPythonListToQListOfPointerType(val, (QList<void*>*)v.constData(), info, true);
11931193
if (!ok) {
11941194
v = QVariant();
11951195
}
@@ -1290,18 +1290,27 @@ PyObject* PythonQtConv::QVariantListToPyObject(const QVariantList& l) {
12901290
return result;
12911291
}
12921292

1293-
PyObject* PythonQtConv::ConvertQListOfPointerTypeToPythonList(QList<void*>* list, const QByteArray& typeName)
1293+
PyObject* PythonQtConv::ConvertQListOfPointerTypeToPythonList(QList<void*>* list, const PythonQtMethodInfo::ParameterInfo& info)
12941294
{
12951295
PyObject* result = PyTuple_New(list->count());
12961296
int i = 0;
12971297
Q_FOREACH (void* value, *list) {
1298-
PyTuple_SET_ITEM(result, i, PythonQt::priv()->wrapPtr(value, typeName));
1298+
PyObject* wrap = PythonQt::priv()->wrapPtr(value, info.innerName);
1299+
if (wrap) {
1300+
PythonQtInstanceWrapper* wrapper = (PythonQtInstanceWrapper*)wrap;
1301+
if (info.passOwnershipToCPP) {
1302+
wrapper->passOwnershipToCPP();
1303+
} else if (info.passOwnershipToPython) {
1304+
wrapper->passOwnershipToPython();
1305+
}
1306+
}
1307+
PyTuple_SET_ITEM(result, i, wrap);
12991308
i++;
13001309
}
13011310
return result;
13021311
}
13031312

1304-
bool PythonQtConv::ConvertPythonListToQListOfPointerType(PyObject* obj, QList<void*>* list, const QByteArray& type, bool /*strict*/)
1313+
bool PythonQtConv::ConvertPythonListToQListOfPointerType(PyObject* obj, QList<void*>* list, const PythonQtMethodInfo::ParameterInfo& info, bool /*strict*/)
13051314
{
13061315
bool result = false;
13071316
if (PySequence_Check(obj)) {
@@ -1314,9 +1323,16 @@ bool PythonQtConv::ConvertPythonListToQListOfPointerType(PyObject* obj, QList<vo
13141323
if (PyObject_TypeCheck(value, &PythonQtInstanceWrapper_Type)) {
13151324
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)value;
13161325
bool ok;
1317-
void* object = castWrapperTo(wrap, type, ok);
1326+
void* object = castWrapperTo(wrap, info.innerName, ok);
13181327
Py_XDECREF(value);
13191328
if (ok) {
1329+
if (object) {
1330+
if (info.passOwnershipToCPP) {
1331+
wrap->passOwnershipToCPP();
1332+
} else if (info.passOwnershipToPython) {
1333+
wrap->passOwnershipToPython();
1334+
}
1335+
}
13201336
list->append(object);
13211337
} else {
13221338
result = false;

src/PythonQtConversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ class PYTHONQT_EXPORT PythonQtConv {
194194
static void* handlePythonToQtAutoConversion(int typeId, PyObject* obj, void* alreadyAllocatedCPPObject);
195195

196196
//! converts the list of pointers of given type to Python
197-
static PyObject* ConvertQListOfPointerTypeToPythonList(QList<void*>* list, const QByteArray& type);
197+
static PyObject* ConvertQListOfPointerTypeToPythonList(QList<void*>* list, const PythonQtMethodInfo::ParameterInfo& info);
198198
//! tries to convert the python object to a QList of pointers to \c type objects, returns true on success
199-
static bool ConvertPythonListToQListOfPointerType(PyObject* obj, QList<void*>* list, const QByteArray& type, bool strict);
199+
static bool ConvertPythonListToQListOfPointerType(PyObject* obj, QList<void*>* list, const PythonQtMethodInfo::ParameterInfo& info, bool strict);
200200

201201
//! helper template method for conversion from Python to QVariantMap/Hash
202202
template <typename Map>

src/PythonQtMethodInfo.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,18 @@ void PythonQtMethodInfo::fillParameterInfo(ParameterInfo& type, const QByteArray
130130
type.passOwnershipToCPP = true;
131131
name = name.mid(27, len-28);
132132
len -= 28;
133-
}
133+
} else
134134
if (name.startsWith("PythonQtPassOwnershipToPython<")) {
135135
type.passOwnershipToPython = true;
136136
name = name.mid(30, len-31);
137137
len -= 31;
138-
}
138+
} else
139139
if (name.startsWith("PythonQtNewOwnerOfThis<")) {
140140
type.newOwnerOfThis = true;
141141
name = name.mid(23, len-24);
142142
len -= 24;
143143
}
144+
144145
if (strncmp(name.constData(), "const ", 6)==0) {
145146
name = name.mid(6);
146147
len -= 6;

src/PythonQtSlot.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,21 @@ bool PythonQtCallSlot(PythonQtClassInfo* classInfo, QObject* objectToCall, PyObj
8282
const PythonQtSlotInfo::ParameterInfo& returnValueParam = params.at(0);
8383
// set return argument to NULL
8484
argList[0] = NULL;
85-
85+
8686
bool ok = true;
8787
bool skipFirst = false;
8888
PythonQtPassThisOwnershipType passThisOwnership = IgnoreOwnership;
89+
8990
int instanceDecoOffset = 0;
91+
// it is important to keep arg1 on this scope, because it is stored in argList[1] and
92+
// would go away if it is moved into the if scope
93+
void* arg1 = NULL;
9094
if (info->isInstanceDecorator()) {
9195
skipFirst = true;
9296
instanceDecoOffset = 1;
9397

9498
// for decorators on CPP objects, we take the cpp ptr, for QObjects we take the QObject pointer
95-
void* arg1 = firstArgument;
99+
arg1 = firstArgument;
96100
if (!arg1) {
97101
arg1 = objectToCall;
98102
}

0 commit comments

Comments
 (0)