Clean up PyStdoutHandler on shutdown

This commit is contained in:
Michael Zanetti 2021-03-14 20:20:59 +01:00
parent 96ae3cd01a
commit 317467a05b
2 changed files with 14 additions and 7 deletions

View File

@ -216,6 +216,9 @@ PythonIntegrationPlugin::~PythonIntegrationPlugin()
s_plugins.take(this); s_plugins.take(this);
Py_XDECREF(m_pluginModule); Py_XDECREF(m_pluginModule);
Py_DECREF(m_nymeaModule); Py_DECREF(m_nymeaModule);
Py_XDECREF(m_logger);
Py_XDECREF(m_stdOutHandler);
Py_XDECREF(m_stdErrHandler);
Py_EndInterpreter(m_threadState); Py_EndInterpreter(m_threadState);
@ -335,23 +338,24 @@ bool PythonIntegrationPlugin::loadScript(const QString &scriptFile)
QString category = metadata.pluginName(); QString category = metadata.pluginName();
category.replace(0, 1, category[0].toUpper()); category.replace(0, 1, category[0].toUpper());
PyObject *args = Py_BuildValue("(s)", category.toUtf8().data()); PyObject *args = Py_BuildValue("(s)", category.toUtf8().data());
PyNymeaLoggingHandler *logger = reinterpret_cast<PyNymeaLoggingHandler*>(PyObject_CallObject((PyObject*)&PyNymeaLoggingHandlerType, args)); m_logger = PyObject_CallObject((PyObject*)&PyNymeaLoggingHandlerType, args);
Py_DECREF(args); Py_DECREF(args);
// Override stdout and stderr // Override stdout and stderr
args = Py_BuildValue("(si)", category.toUtf8().data(), QtMsgType::QtDebugMsg); args = Py_BuildValue("(si)", category.toUtf8().data(), QtMsgType::QtDebugMsg);
PyStdOutHandler*stdOutHandler = reinterpret_cast<PyStdOutHandler*>(PyObject_CallObject((PyObject*)&PyStdOutHandlerType, args)); m_stdOutHandler = PyObject_CallObject((PyObject*)&PyStdOutHandlerType, args);
Py_DECREF(args); Py_DECREF(args);
PySys_SetObject("stdout", (PyObject*)stdOutHandler); PySys_SetObject("stdout", m_stdOutHandler);
args = Py_BuildValue("(si)", category.toUtf8().data(), QtMsgType::QtWarningMsg); args = Py_BuildValue("(si)", category.toUtf8().data(), QtMsgType::QtWarningMsg);
PyStdOutHandler*stdErrHandler = reinterpret_cast<PyStdOutHandler*>(PyObject_CallObject((PyObject*)&PyStdOutHandlerType, args)); m_stdErrHandler = PyObject_CallObject((PyObject*)&PyStdOutHandlerType, args);
PySys_SetObject("stderr", (PyObject*)stdErrHandler); PySys_SetObject("stderr", m_stdErrHandler);
Py_DECREF(args); Py_DECREF(args);
int loggerAdded = PyModule_AddObject(m_pluginModule, "logger", reinterpret_cast<PyObject*>(logger)); int loggerAdded = PyModule_AddObject(m_pluginModule, "logger", m_logger);
if (loggerAdded != 0) { if (loggerAdded != 0) {
qCWarning(dcPythonIntegrations()) << "Failed to add the logger object"; qCWarning(dcPythonIntegrations()) << "Failed to add the logger object";
Py_DECREF(logger); Py_DECREF(m_logger);
m_logger = nullptr;
} }
// Export metadata ids into module // Export metadata ids into module

View File

@ -75,6 +75,9 @@ private:
PyObject *m_nymeaModule = nullptr; PyObject *m_nymeaModule = nullptr;
// The imported plugin module (the plugin.py) // The imported plugin module (the plugin.py)
PyObject *m_pluginModule = nullptr; PyObject *m_pluginModule = nullptr;
PyObject *m_logger = nullptr;
PyObject *m_stdOutHandler = nullptr;
PyObject *m_stdErrHandler = nullptr;
// A map of plugin instances to plugin python scripts/modules // A map of plugin instances to plugin python scripts/modules
// Make sure to hold the GIL when accessing this. // Make sure to hold the GIL when accessing this.