@@ -379,23 +379,18 @@ PythonQt::PythonQt(int flags, const QByteArray& pythonQtModuleName)
379
379
PythonQt::~PythonQt () {
380
380
delete _p;
381
381
_p = NULL ;
382
-
383
- Py_DECREF (&PythonQtSlotFunction_Type);
384
- Py_DECREF (&PythonQtSignalFunction_Type);
385
- Py_DECREF (&PythonQtSlotDecorator_Type);
386
- Py_DECREF (&PythonQtProperty_Type);
387
- Py_DECREF (&PythonQtBoolResult_Type);
388
- Py_DECREF (&PythonQtClassWrapper_Type);
389
- Py_DECREF (&PythonQtInstanceWrapper_Type);
390
- Py_DECREF (&PythonQtStdOutRedirectType);
391
- Py_DECREF (&PythonQtStdInRedirectType);
392
382
}
393
383
394
384
PythonQtPrivate::~PythonQtPrivate () {
395
385
delete _defaultImporter;
396
386
_defaultImporter = NULL ;
397
387
398
- qDeleteAll (_knownClassInfos);
388
+ {
389
+ QHashIterator<QByteArray, PythonQtClassInfo *> i (_knownClassInfos);
390
+ while (i.hasNext ()) {
391
+ delete i.next ().value ();
392
+ }
393
+ }
399
394
400
395
PythonQtMethodInfo::cleanupCachedMethodInfos ();
401
396
PythonQtArgumentFrame::cleanupFreeList ();
@@ -414,9 +409,7 @@ void PythonQt::setRedirectStdInCallback(PythonQtInputChangedCB* callback, void *
414
409
415
410
// Backup original 'sys.stdin' if not yet done
416
411
if ( !PyObject_HasAttrString (sys.object (), " pythonqt_original_stdin" ) ) {
417
- PyObject *stdin = PyObject_GetAttrString (sys.object (), " stdin" );
418
- PyObject_SetAttrString (sys.object (), " pythonqt_original_stdin" , stdin);
419
- Py_XDECREF (stdin);
412
+ PyObject_SetAttrString (sys.object (), " pythonqt_original_stdin" , PyObject_GetAttrString (sys.object (), " stdin" ));
420
413
}
421
414
422
415
in = PythonQtStdInRedirectType.tp_new (&PythonQtStdInRedirectType, NULL , NULL );
@@ -435,19 +428,15 @@ void PythonQt::setRedirectStdInCallbackEnabled(bool enabled)
435
428
PythonQtObjectPtr sys;
436
429
sys.setNewRef (PyImport_ImportModule (" sys" ));
437
430
438
- PythonQtObjectPtr stdin;
439
431
if (enabled) {
440
- if ( PyObject_HasAttrString (sys.object (), " pythonqt_stdin" ) ) {
441
- stdin. setNewRef ( PyObject_GetAttrString (sys.object (), " pythonqt_stdin" ));
432
+ if ( ! PyObject_HasAttrString (sys.object (), " pythonqt_stdin" ) ) {
433
+ PyObject_SetAttrString (sys. object (), " stdin " , PyObject_GetAttrString (sys.object (), " pythonqt_stdin" ));
442
434
}
443
435
} else {
444
- if ( PyObject_HasAttrString (sys.object (), " pythonqt_original_stdin" ) ) {
445
- stdin. setNewRef ( PyObject_GetAttrString (sys.object (), " pythonqt_original_stdin" ));
436
+ if ( ! PyObject_HasAttrString (sys.object (), " pythonqt_original_stdin" ) ) {
437
+ PyObject_SetAttrString (sys. object (), " stdin " , PyObject_GetAttrString (sys.object (), " pythonqt_original_stdin" ));
446
438
}
447
439
}
448
- if (stdin) {
449
- PyObject_SetAttrString (sys.object (), " stdin" , stdin);
450
- }
451
440
}
452
441
453
442
PythonQtImportFileInterface* PythonQt::importInterface ()
@@ -459,7 +448,7 @@ void PythonQt::qObjectNoLongerWrappedCB(QObject* o)
459
448
{
460
449
if (_self->_p ->_noLongerWrappedCB ) {
461
450
(*_self->_p ->_noLongerWrappedCB )(o);
462
- }
451
+ };
463
452
}
464
453
465
454
void PythonQt::setQObjectMissingAttributeCallback (PythonQtQObjectMissingAttributeCB* cb)
@@ -536,7 +525,6 @@ void PythonQtPrivate::createPythonQtClassWrapper(PythonQtClassInfo* info, const
536
525
PythonQtClassInfo* outerClassInfo = lookupClassInfoAndCreateIfNotPresent (outerClass);
537
526
outerClassInfo->addNestedClass (info);
538
527
} else {
539
- Py_INCREF (pyobj);
540
528
PyModule_AddObject (pack, info->className (), pyobj);
541
529
}
542
530
if (!module && package && strncmp (package, " Qt" , 2 ) == 0 ) {
@@ -546,7 +534,6 @@ void PythonQtPrivate::createPythonQtClassWrapper(PythonQtClassInfo* info, const
546
534
PyModule_AddObject (packageByName (" Qt" ), info->className (), pyobj);
547
535
}
548
536
info->setPythonQtClassWrapper (pyobj);
549
- Py_DECREF (pyobj);
550
537
}
551
538
552
539
PyObject* PythonQtPrivate::wrapQObject (QObject* obj)
@@ -561,7 +548,6 @@ PyObject* PythonQtPrivate::wrapQObject(QObject* obj)
561
548
// address, so probably that C++ wrapper has been deleted earlier and
562
549
// now we see a QObject with the same address.
563
550
// Do not use the old wrapper anymore.
564
- removeWrapperPointer (obj);
565
551
wrap = NULL ;
566
552
}
567
553
if (!wrap) {
@@ -747,7 +733,6 @@ PythonQtClassWrapper* PythonQtPrivate::createNewPythonQtClassWrapper(PythonQtCla
747
733
PyObject* typeDict = PyDict_New ();
748
734
PyObject* moduleName = PyObject_GetAttrString (parentModule, " __name__" );
749
735
PyDict_SetItemString (typeDict, " __module__" , moduleName);
750
- Py_DECREF (moduleName);
751
736
752
737
PyObject* args = Py_BuildValue (" OOO" , className, baseClasses, typeDict);
753
738
@@ -784,7 +769,6 @@ PyObject* PythonQtPrivate::createNewPythonQtEnumWrapper(const char* enumName, Py
784
769
PyObject* module = PyObject_GetAttrString (parentObject, " __module__" );
785
770
PyObject* typeDict = PyDict_New ();
786
771
PyDict_SetItemString (typeDict, " __module__" , module);
787
- Py_DECREF (module);
788
772
789
773
PyObject* args = Py_BuildValue (" OOO" , className, baseClasses, typeDict);
790
774
@@ -909,21 +893,17 @@ QVariant PythonQt::evalCode(PyObject* object, PyObject* pycode) {
909
893
QVariant result;
910
894
clearError ();
911
895
if (pycode) {
912
- PythonQtObjectPtr dict;
913
- PythonQtObjectPtr globals;
896
+ PyObject* dict = NULL ;
897
+ PyObject* globals = NULL ;
914
898
if (PyModule_Check (object)) {
915
899
dict = PyModule_GetDict (object);
916
900
globals = dict;
917
901
} else if (PyDict_Check (object)) {
918
902
dict = object;
919
903
globals = dict;
920
904
} else {
921
- PyObject *moduleName = PyObject_GetAttrString (object, " __module__" );
922
- PyObject *module = PyImport_ImportModule (PyString_AS_STRING (moduleName));
923
- dict.setNewRef (PyObject_GetAttrString (object, " __dict__" ));
924
- globals.setNewRef (PyObject_GetAttrString (module, " __dict__" ));
925
- Py_XDECREF (moduleName);
926
- Py_XDECREF (module);
905
+ dict = PyObject_GetAttrString (object, " __dict__" );
906
+ globals = PyObject_GetAttrString (PyImport_ImportModule (PyString_AS_STRING (PyObject_GetAttrString (object, " __module__" )))," __dict__" );
927
907
}
928
908
PyObject* r = NULL ;
929
909
if (dict) {
@@ -1068,30 +1048,24 @@ PythonQtObjectPtr PythonQt::createUniqueModule()
1068
1048
1069
1049
void PythonQt::addObject (PyObject* object, const QString& name, QObject* qObject)
1070
1050
{
1071
- PyObject *wrappedObject = _p->wrapQObject (qObject);
1072
1051
if (PyModule_Check (object)) {
1073
- Py_XINCREF (wrappedObject);
1074
- PyModule_AddObject (object, QStringToPythonCharPointer (name), wrappedObject);
1052
+ PyModule_AddObject (object, QStringToPythonCharPointer (name), _p->wrapQObject (qObject));
1075
1053
} else if (PyDict_Check (object)) {
1076
- PyDict_SetItemString (object, QStringToPythonCharPointer (name), wrappedObject );
1054
+ PyDict_SetItemString (object, QStringToPythonCharPointer (name), _p-> wrapQObject (qObject) );
1077
1055
} else {
1078
- PyObject_SetAttrString (object, QStringToPythonCharPointer (name), wrappedObject );
1056
+ PyObject_SetAttrString (object, QStringToPythonCharPointer (name), _p-> wrapQObject (qObject) );
1079
1057
}
1080
- Py_XDECREF (wrappedObject);
1081
1058
}
1082
1059
1083
1060
void PythonQt::addVariable (PyObject* object, const QString& name, const QVariant& v)
1084
1061
{
1085
- PyObject *value = PythonQtConv::QVariantToPyObject (v);
1086
1062
if (PyModule_Check (object)) {
1087
- Py_XINCREF (value);
1088
- PyModule_AddObject (object, QStringToPythonCharPointer (name), value);
1063
+ PyModule_AddObject (object, QStringToPythonCharPointer (name), PythonQtConv::QVariantToPyObject (v));
1089
1064
} else if (PyDict_Check (object)) {
1090
- PyDict_SetItemString (object, QStringToPythonCharPointer (name), value );
1065
+ PyDict_SetItemString (object, QStringToPythonCharPointer (name), PythonQtConv::QVariantToPyObject (v) );
1091
1066
} else {
1092
- PyObject_SetAttrString (object, QStringToPythonCharPointer (name), value );
1067
+ PyObject_SetAttrString (object, QStringToPythonCharPointer (name), PythonQtConv::QVariantToPyObject (v) );
1093
1068
}
1094
- Py_XDECREF (value);
1095
1069
}
1096
1070
1097
1071
void PythonQt::removeVariable (PyObject* object, const QString& name)
@@ -1133,7 +1107,7 @@ QStringList PythonQt::introspection(PyObject* module, const QString& objectname,
1133
1107
} else {
1134
1108
object = lookupObject (module, objectname);
1135
1109
if (!object && type == CallOverloads) {
1136
- PythonQtObjectPtr dict = lookupObject (module, " __builtins__" );
1110
+ PyObject* dict = lookupObject (module, " __builtins__" );
1137
1111
if (dict) {
1138
1112
object = PyDict_GetItemString (dict, QStringToPythonCharPointer (objectname));
1139
1113
}
@@ -1185,33 +1159,36 @@ QStringList PythonQt::introspectObject(PyObject* object, ObjectType type)
1185
1159
}
1186
1160
}
1187
1161
} else {
1188
- PythonQtObjectPtr keys;
1162
+ PyObject* keys = NULL ;
1189
1163
bool isDict = false ;
1190
1164
if (PyDict_Check (object)) {
1191
- keys. setNewRef ( PyDict_Keys (object) );
1165
+ keys = PyDict_Keys (object);
1192
1166
isDict = true ;
1193
1167
} else {
1194
1168
#if defined(MEVISLAB) && !defined(PY3K)
1195
1169
int oldPy3kWarningFlag = Py_Py3kWarningFlag;
1196
1170
Py_Py3kWarningFlag = 0 ; // temporarily disable Python 3 warnings
1197
- keys. setNewRef ( PyObject_Dir (object) );
1171
+ keys = PyObject_Dir (object);
1198
1172
Py_Py3kWarningFlag = oldPy3kWarningFlag;
1199
1173
#else
1200
- keys. setNewRef ( PyObject_Dir (object) );
1174
+ keys = PyObject_Dir (object);
1201
1175
#endif
1202
1176
}
1203
1177
if (keys) {
1204
1178
int count = PyList_Size (keys);
1179
+ PyObject* key;
1180
+ PyObject* value;
1181
+ QString keystr;
1205
1182
for (int i = 0 ;i<count;i++) {
1206
- PythonQtObjectPtr key = PyList_GetItem (keys,i);
1207
- PythonQtObjectPtr value;
1183
+ key = PyList_GetItem (keys,i);
1208
1184
if (isDict) {
1209
1185
value = PyDict_GetItem (object, key);
1186
+ Py_INCREF (value);
1210
1187
} else {
1211
- value. setNewRef ( PyObject_GetAttr (object, key) );
1188
+ value = PyObject_GetAttr (object, key);
1212
1189
}
1213
1190
if (!value) continue ;
1214
- QString keystr = PyString_AsString (key);
1191
+ keystr = PyString_AsString (key);
1215
1192
static const QString underscoreStr (" __tmp" );
1216
1193
if (!keystr.startsWith (underscoreStr)) {
1217
1194
switch (type) {
@@ -1256,7 +1233,9 @@ QStringList PythonQt::introspectObject(PyObject* object, ObjectType type)
1256
1233
std::cerr << " PythonQt: introspection: unknown case" << " , in " << __FILE__ << " :" << __LINE__ << std::endl;
1257
1234
}
1258
1235
}
1236
+ Py_DECREF (value);
1259
1237
}
1238
+ Py_DECREF (keys);
1260
1239
}
1261
1240
}
1262
1241
PyErr_Clear ();
@@ -1309,7 +1288,6 @@ QStringList PythonQt::introspectType(const QString& typeName, ObjectType type)
1309
1288
PyObject* typeObject = getObjectByType (typeName);
1310
1289
if (typeObject) {
1311
1290
object = PyObject_GetAttrString (typeObject, QStringToPythonCharPointer (memberName));
1312
- Py_DECREF (typeObject);
1313
1291
}
1314
1292
}
1315
1293
if (object) {
@@ -1381,7 +1359,6 @@ PyObject* PythonQt::callAndReturnPyObject(PyObject* callable, const QVariantList
1381
1359
PyObject* arg = PythonQtConv::QVariantToPyObject (it.value ());
1382
1360
if (arg) {
1383
1361
PyDict_SetItemString (pkwargs, QStringToPythonCharPointer (it.key ()), arg);
1384
- Py_DECREF (arg);
1385
1362
} else {
1386
1363
err = true ;
1387
1364
break ;
@@ -1773,7 +1750,7 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
1773
1750
}
1774
1751
#ifdef PY3K
1775
1752
PythonQtModuleDef.m_name = name.constData ();
1776
- _p->_pythonQtModule . setNewRef ( PyModule_Create (&PythonQtModuleDef) );
1753
+ _p->_pythonQtModule = PyModule_Create (&PythonQtModuleDef);
1777
1754
#else
1778
1755
_p->_pythonQtModule = Py_InitModule (name.constData (), PythonQtMethods);
1779
1756
#endif
@@ -1813,11 +1790,7 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
1813
1790
Py_XDECREF (old_module_names);
1814
1791
1815
1792
#ifdef PY3K
1816
- PyObject *modules = PyObject_GetAttrString (sys.object (), " modules" );
1817
- PyObject *nameObj = PyUnicode_FromString (name.constData ());
1818
- PyDict_SetItem (modules, nameObj, _p->_pythonQtModule .object ());
1819
- Py_XDECREF (modules);
1820
- Py_XDECREF (nameObj);
1793
+ PyDict_SetItem (PyObject_GetAttrString (sys.object (), " modules" ), PyUnicode_FromString (name.constData ()), _p->_pythonQtModule .object ());
1821
1794
#endif
1822
1795
}
1823
1796
@@ -1837,8 +1810,7 @@ QString PythonQt::getReturnTypeOfWrappedMethod(PyObject* module, const QString&
1837
1810
1838
1811
QString PythonQt::getReturnTypeOfWrappedMethod (const QString& typeName, const QString& methodName)
1839
1812
{
1840
- PythonQtObjectPtr typeObject;
1841
- typeObject.setNewRef (getObjectByType (typeName));
1813
+ PythonQtObjectPtr typeObject = getObjectByType (typeName);
1842
1814
if (typeObject.isNull ()) {
1843
1815
return " " ;
1844
1816
}
@@ -2133,12 +2105,10 @@ const QMetaObject* PythonQtPrivate::buildDynamicMetaObject(PythonQtClassWrapper*
2133
2105
builder.setClassName (((PyTypeObject*)type)->tp_name );
2134
2106
2135
2107
PyObject* dict = ((PyTypeObject*)type)->tp_dict ;
2136
- Py_ssize_t pos = 0 ;
2108
+ Py_ssize_t pos = NULL ;
2137
2109
PyObject* value = NULL ;
2138
2110
PyObject* key = NULL ;
2139
- static PyObject* qtSlots = NULL ;
2140
- if (!qtSlots)
2141
- qtSlots = PyString_FromString (" _qtSlots" );
2111
+ static PyObject* qtSlots = PyString_FromString (" _qtSlots" );
2142
2112
2143
2113
bool needsMetaObject = false ;
2144
2114
// Iterate over all members and check if they affect the QMetaObject:
@@ -2156,7 +2126,7 @@ const QMetaObject* PythonQtPrivate::buildDynamicMetaObject(PythonQtClassWrapper*
2156
2126
}
2157
2127
}
2158
2128
}
2159
- pos = 0 ;
2129
+ pos = NULL ;
2160
2130
value = NULL ;
2161
2131
key = NULL ;
2162
2132
// Now look for slots: (this is a bug in QMetaObjectBuilder, all signals need to be added first)
@@ -2190,11 +2160,10 @@ const QMetaObject* PythonQtPrivate::buildDynamicMetaObject(PythonQtClassWrapper*
2190
2160
}
2191
2161
if (PyFunction_Check (value) && PyObject_HasAttr (value, qtSlots)) {
2192
2162
// A function which has a "_qtSlots" signature list, add the slots to the meta object
2193
- PythonQtObjectPtr signatures;
2194
- signatures.setNewRef (PyObject_GetAttr (value, qtSlots));
2163
+ PyObject* signatures = PyObject_GetAttr (value, qtSlots);
2195
2164
Py_ssize_t count = PyList_Size (signatures);
2196
2165
for (Py_ssize_t i = 0 ; i < count; i++) {
2197
- PyObject* signature = PyList_GET_ITEM (signatures. object () , i);
2166
+ PyObject* signature = PyList_GET_ITEM (signatures, i);
2198
2167
QByteArray sig = PyString_AsString (signature);
2199
2168
// Split the return type and the rest of the signature,
2200
2169
// no spaces should be in the rest of the signature...
@@ -2240,11 +2209,9 @@ int PythonQtPrivate::handleMetaCall(QObject* object, PythonQtInstanceWrapper* wr
2240
2209
}
2241
2210
PythonQtProperty* prop = NULL ;
2242
2211
// Get directly from the Python class, since we don't want to get the value of the property
2243
- PythonQtObjectPtr name, maybeProp;
2244
- name.setNewRef (PyString_FromString (metaProp.name ()));
2245
- maybeProp.setNewRef (PyBaseObject_Type.tp_getattro ((PyObject*)wrapper, name));
2212
+ PyObject* maybeProp = PyBaseObject_Type.tp_getattro ((PyObject*)wrapper, PyString_FromString (metaProp.name ()));
2246
2213
if (maybeProp && PythonQtProperty_Check (maybeProp)) {
2247
- prop = (PythonQtProperty*)maybeProp. object () ;
2214
+ prop = (PythonQtProperty*)maybeProp;
2248
2215
} else {
2249
2216
return id - methodCount;
2250
2217
}
@@ -2261,7 +2228,7 @@ int PythonQtPrivate::handleMetaCall(QObject* object, PythonQtInstanceWrapper* wr
2261
2228
2262
2229
PyObject* value = prop->data ->callGetter ((PyObject*)wrapper);
2263
2230
if (value) {
2264
- void * result = PythonQtConv::ConvertPythonToQt (info, value, false , NULL , args[0 ]); // FIXME: what happens with result? free?
2231
+ void * result = PythonQtConv::ConvertPythonToQt (info, value, false , NULL , args[0 ]);
2265
2232
Py_DECREF (value);
2266
2233
return (result == NULL ? -1 : 0 );
2267
2234
} else {
@@ -2300,15 +2267,17 @@ QString PythonQtPrivate::getSignature(PyObject* object)
2300
2267
PyMethodObject* method = NULL ;
2301
2268
PyFunctionObject* func = NULL ;
2302
2269
2270
+ bool decrefMethod = false ;
2271
+
2303
2272
if (PythonQtUtils::isPythonClassType (object)) {
2304
2273
method = (PyMethodObject*)PyObject_GetAttrString (object, " __init__" );
2274
+ decrefMethod = true ;
2305
2275
} else if (object->ob_type == &PyFunction_Type) {
2306
2276
func = (PyFunctionObject*)object;
2307
2277
} else if (object->ob_type == &PyMethod_Type) {
2308
2278
method = (PyMethodObject*)object;
2309
- Py_XINCREF (method);
2310
2279
}
2311
- if (method) {
2280
+ if (method) {
2312
2281
if (PyFunction_Check (method->im_func )) {
2313
2282
func = (PyFunctionObject*)method->im_func ;
2314
2283
} else if (isMethodDescriptor ((PyObject*)method)) {
@@ -2415,7 +2384,9 @@ QString PythonQtPrivate::getSignature(PyObject* object)
2415
2384
signature = funcName + " (" + signature + " )" ;
2416
2385
}
2417
2386
2418
- Py_XDECREF (method);
2387
+ if (method && decrefMethod) {
2388
+ Py_DECREF (method);
2389
+ }
2419
2390
}
2420
2391
2421
2392
return signature;
@@ -2480,7 +2451,7 @@ PythonQtClassInfo* PythonQtPrivate::getClassInfo( const QByteArray& className )
2480
2451
if (_knownLazyClasses.contains (className)) {
2481
2452
QByteArray module = _knownLazyClasses.value (className);
2482
2453
recursion = true ;
2483
- PyImport_ImportModule (module); // FIXME: reference leaked
2454
+ PyImport_ImportModule (module);
2484
2455
recursion = false ;
2485
2456
result = _knownClassInfos.value (className);
2486
2457
if (!result) {
0 commit comments