Skip to content

Commit

Permalink
More potential leak fixes and build fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gregor-anich-uibk committed Oct 9, 2020
1 parent 06860d4 commit 525ea41
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 55 deletions.
70 changes: 36 additions & 34 deletions build/python.prf
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,43 @@ isEmpty( PYTHON_VERSION ) {
unix:PYTHON_VERSION=2.7
}


macx {
# for macx you need to have the Python development kit installed as framework
INCLUDEPATH += /System/Library/Frameworks/Python.framework/Headers
LIBS += -F/System/Library/Frameworks -framework Python
} else:win32 {
# for windows install a Python development kit or build Python yourself from the sources
# Make sure that you set the environment variable PYTHON_PATH to point to your
# python installation (or the python sources/header files when building from source).
# Make sure that you set the environment variable PYTHON_LIB to point to
# the directory where the python libs are located.
#
# When using the prebuild Python installer, this will be:
# set PYTHON_PATH = c:\Python26
# set PYTHON_LIB = c:\Python26\libs
#
# When using the python sources, this will be something like:
# set PYTHON_PATH = c:\yourDir\Python-2.6.1\
# set PYTHON_LIB = c:\yourDir\Python-2.6.1\PCbuild8\Win32

# check if debug or release
CONFIG(debug, debug|release) {
DEBUG_EXT = _d
} else {
DEBUG_EXT =
}

win32:INCLUDEPATH += $$(PYTHON_PATH)/PC $$(PYTHON_PATH)/include
win32:LIBS += $$(PYTHON_LIB)/python$${PYTHON_VERSION}$${DEBUG_EXT}.lib
} else:unix {
# on linux, python-config is used to autodetect Python.
# make sure that you have installed a matching python-dev package.
message(Using Python version $${PYTHON_VERSION})

macx {
# for macx you need to have the Python development kit installed as framework
INCLUDEPATH += /System/Library/Frameworks/Python.framework/Headers
LIBS += -F/System/Library/Frameworks -framework Python
} else:win32 {
# for windows install a Python development kit or build Python yourself from the sources
# Make sure that you set the environment variable PYTHON_PATH to point to your
# python installation (or the python sources/header files when building from source).
# Make sure that you set the environment variable PYTHON_LIB to point to
# the directory where the python libs are located.
#
# When using the prebuild Python installer, this will be:
# set PYTHON_PATH = c:\Python26
# set PYTHON_LIB = c:\Python26\libs
#
# When using the python sources, this will be something like:
# set PYTHON_PATH = c:\yourDir\Python-2.6.1\
# set PYTHON_LIB = c:\yourDir\Python-2.6.1\PCbuild8\Win32

# check if debug or release
CONFIG(debug, debug|release) {
DEBUG_EXT = _d
} else {
DEBUG_EXT =
}

win32:INCLUDEPATH += $$(PYTHON_PATH)/PC $$(PYTHON_PATH)/include
win32:LIBS += $$(PYTHON_LIB)/python$${PYTHON_VERSION}$${DEBUG_EXT}.lib
} else:unix {
# on linux, python-config is used to autodetect Python.
# make sure that you have installed a matching python-dev package.

system(python$${PYTHON_VERSION}-config --embed --libs) {
unix:LIBS += $$system(python$${PYTHON_VERSION}-config --embed --libs)
} else: unix:LIBS += $$system(python$${PYTHON_VERSION}-config --libs)
unix:QMAKE_CXXFLAGS += $$system(python$${PYTHON_VERSION}-config --includes)
}
unix:QMAKE_CXXFLAGS += $$system(python$${PYTHON_VERSION}-config --includes)
unix:QMAKE_LFLAGS += $$system(python$${PYTHON_VERSION}-config --ldflags)
}
51 changes: 30 additions & 21 deletions src/PythonQt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ void PythonQt_init_QtGuiBuiltin(PyObject*);
void PythonQt_init_QtCoreBuiltin(PyObject*);


static inline int PyModule_AddObject_DECREF(PyObject *module, const char *name, PyObject *value)
{
int ret = PyModule_AddObject(module, name, value);
if (ret < 0)
Py_XDECREF(value);
return ret;
}


void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)
{
if (!_self) {
Expand Down Expand Up @@ -244,9 +253,9 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)
for (unsigned int i = 0;i<16; i++) {
PyObject* obj = PyObject_GetAttrString(qtNamespace, names[i]);
if (obj) {
PyModule_AddObject(pack, names[i], obj);
PyModule_AddObject_DECREF(pack, names[i], obj);
Py_INCREF(obj);
PyModule_AddObject(pack2, names[i], obj);
PyModule_AddObject_DECREF(pack2, names[i], obj);
} else {
std::cerr << "method not found " << names[i] << std::endl;
}
Expand All @@ -268,19 +277,19 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)

for (int i = 0; i<sizeof(enumValues)/sizeof(int); i++) {
PyObject* obj = PyInt_FromLong(enumValues[i]);
PyModule_AddObject(pack, enumNames[i], obj);
PyModule_AddObject_DECREF(pack, enumNames[i], obj);
Py_INCREF(obj);
PyModule_AddObject(pack2, enumNames[i], obj);
PyModule_AddObject_DECREF(pack2, enumNames[i], obj);
}

_self->priv()->pythonQtModule().addObject("Debug", _self->priv()->_debugAPI);

Py_INCREF((PyObject*)&PythonQtSlotDecorator_Type);
Py_INCREF((PyObject*)&PythonQtSignalFunction_Type);
Py_INCREF((PyObject*)&PythonQtProperty_Type);
PyModule_AddObject(pack, "Slot", (PyObject*)&PythonQtSlotDecorator_Type);
PyModule_AddObject(pack, "Signal", (PyObject*)&PythonQtSignalFunction_Type);
PyModule_AddObject(pack, "Property", (PyObject*)&PythonQtProperty_Type);
PyModule_AddObject_DECREF(pack, "Slot", (PyObject*)&PythonQtSlotDecorator_Type);
PyModule_AddObject_DECREF(pack, "Signal", (PyObject*)&PythonQtSignalFunction_Type);
PyModule_AddObject_DECREF(pack, "Property", (PyObject*)&PythonQtProperty_Type);

}
}
Expand Down Expand Up @@ -423,11 +432,11 @@ void PythonQt::setRedirectStdInCallback(PythonQtInputChangedCB* callback, void *
((PythonQtStdInRedirect*)in.object())->_cb = callback;
((PythonQtStdInRedirect*)in.object())->_callData = callbackData;
// replace the built in file objects with our own objects
PyModule_AddObject(sys.object(), "stdin", in);
PyModule_AddObject_DECREF(sys.object(), "stdin", in);

// Backup custom 'stdin' into 'pythonqt_stdin'
Py_INCREF(in); // AddObject steals the reference, so increment it
PyModule_AddObject(sys.object(), "pythonqt_stdin", in);
PyModule_AddObject_DECREF(sys.object(), "pythonqt_stdin", in);
}

void PythonQt::setRedirectStdInCallbackEnabled(bool enabled)
Expand Down Expand Up @@ -503,7 +512,7 @@ void PythonQtPrivate::registerClass(const QMetaObject* metaobject, const char* p
PyObject* classWrapper = info->pythonQtClassWrapper();
// AddObject steals a reference, so we need to INCREF
Py_INCREF(classWrapper);
PyModule_AddObject(module, info->className(), classWrapper);
PyModule_AddObject_DECREF(module, info->className(), classWrapper);
}
if (first) {
first = false;
Expand Down Expand Up @@ -537,13 +546,13 @@ void PythonQtPrivate::createPythonQtClassWrapper(PythonQtClassInfo* info, const
outerClassInfo->addNestedClass(info);
} else {
Py_INCREF(pyobj);
PyModule_AddObject(pack, info->className(), pyobj);
PyModule_AddObject_DECREF(pack, info->className(), pyobj);
}
if (!module && package && strncmp(package, "Qt", 2) == 0) {
// since PyModule_AddObject steals the reference, we need a incref once more...
Py_INCREF(pyobj);
// put all qt objects into Qt as well
PyModule_AddObject(packageByName("Qt"), info->className(), pyobj);
PyModule_AddObject_DECREF(packageByName("Qt"), info->className(), pyobj);
}
info->setPythonQtClassWrapper(pyobj);
Py_DECREF(pyobj);
Expand Down Expand Up @@ -1071,7 +1080,7 @@ void PythonQt::addObject(PyObject* object, const QString& name, QObject* qObject
PyObject *wrappedObject = _p->wrapQObject(qObject);
if (PyModule_Check(object)) {
Py_XINCREF(wrappedObject);
PyModule_AddObject(object, QStringToPythonCharPointer(name), wrappedObject);
PyModule_AddObject_DECREF(object, QStringToPythonCharPointer(name), wrappedObject);
} else if (PyDict_Check(object)) {
PyDict_SetItemString(object, QStringToPythonCharPointer(name), wrappedObject);
} else {
Expand All @@ -1085,7 +1094,7 @@ void PythonQt::addVariable(PyObject* object, const QString& name, const QVariant
PyObject *value = PythonQtConv::QVariantToPyObject(v);
if (PyModule_Check(object)) {
Py_XINCREF(value);
PyModule_AddObject(object, QStringToPythonCharPointer(name), value);
PyModule_AddObject_DECREF(object, QStringToPythonCharPointer(name), value);
} else if (PyDict_Check(object)) {
PyDict_SetItemString(object, QStringToPythonCharPointer(name), value);
} else {
Expand Down Expand Up @@ -1697,12 +1706,12 @@ void PythonQt::overwriteSysPath(const QStringList& paths)
foreach(QString path, paths) {
nativePaths << QDir::toNativeSeparators(path);
}
PyModule_AddObject(sys, "path", PythonQtConv::QStringListToPyList(nativePaths));
PyModule_AddObject_DECREF(sys, "path", PythonQtConv::QStringListToPyList(nativePaths));
}

void PythonQt::setModuleImportPath(PyObject* module, const QStringList& paths)
{
PyModule_AddObject(module, "__path__", PythonQtConv::QStringListToPyList(paths));
PyModule_AddObject_DECREF(module, "__path__", PythonQtConv::QStringListToPyList(paths));
}

void PythonQt::stdOutRedirectCB(const QString& str)
Expand Down Expand Up @@ -1780,7 +1789,7 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
_p->_pythonQtModuleName = name;

Py_INCREF((PyObject*)&PythonQtBoolResult_Type);
PyModule_AddObject(_p->pythonQtModule().object(), "BoolResult", (PyObject*)&PythonQtBoolResult_Type);
PyModule_AddObject_DECREF(_p->pythonQtModule().object(), "BoolResult", (PyObject*)&PythonQtBoolResult_Type);
PythonQtObjectPtr sys;
sys.setNewRef(PyImport_ImportModule("sys"));

Expand All @@ -1793,8 +1802,8 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
err = PythonQtStdOutRedirectType.tp_new(&PythonQtStdOutRedirectType,NULL, NULL);
((PythonQtStdOutRedirect*)err.object())->_cb = stdErrRedirectCB;
// replace the built in file objects with our own objects
PyModule_AddObject(sys, "stdout", out);
PyModule_AddObject(sys, "stderr", err);
PyModule_AddObject_DECREF(sys, "stdout", out);
PyModule_AddObject_DECREF(sys, "stderr", err);
}

// add PythonQt to the list of builtin module names
Expand All @@ -1808,7 +1817,7 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
PyTuple_SetItem(module_names, i, val);
}
PyTuple_SetItem(module_names, old_size, PyString_FromString(name.constData()));
PyModule_AddObject(sys.object(), "builtin_module_names", module_names);
PyModule_AddObject_DECREF(sys.object(), "builtin_module_names", module_names);
}
Py_XDECREF(old_module_names);

Expand Down Expand Up @@ -1991,7 +2000,7 @@ PyObject* PythonQtPrivate::packageByName(const char* name)
_packages.insert(name, v);
// AddObject steals the reference, so increment it!
Py_INCREF(v);
PyModule_AddObject(_pythonQtModule, name, v);
PyModule_AddObject_DECREF(_pythonQtModule, name, v);
}
return v;
}
Expand Down

0 comments on commit 525ea41

Please sign in to comment.