-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathPythonManager.cpp
More file actions
210 lines (175 loc) · 5.71 KB
/
PythonManager.cpp
File metadata and controls
210 lines (175 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <cassert>
#include "PythonAPI.h"
#include "PythonManager.h"
#include "Cutter.h"
#include <QDebug>
#include <QFile>
#include <QDebug>
#include <QCoreApplication>
#include <QDir>
#ifdef CUTTER_ENABLE_PYTHON_BINDINGS
# include <shiboken.h>
# include <pyside.h>
# ifdef HAVE_PYSIDECLEANUP
// This header is introduced in PySide 6
# include <pysidecleanup.h>
# endif
# include <signalmanager.h>
#endif
#include "QtResImporter.h"
static PythonManager *uniqueInstance = nullptr;
PythonManager *PythonManager::getInstance()
{
if (!uniqueInstance) {
uniqueInstance = new PythonManager();
}
return uniqueInstance;
}
PythonManager::PythonManager() {}
PythonManager::~PythonManager() {}
void PythonManager::initPythonHome()
{
#if defined(APPIMAGE) || defined(MACOS_PYTHON_FRAMEWORK_BUNDLED)
if (customPythonHome.isNull()) {
auto pythonHomeDir = QDir(QCoreApplication::applicationDirPath());
# ifdef APPIMAGE
// Executable is in appdir/bin
pythonHomeDir.cdUp();
qInfo() << "Setting PYTHONHOME =" << pythonHomeDir.absolutePath() << " for AppImage.";
# else // MACOS_PYTHON_FRAMEWORK_BUNDLED
// @executable_path/../Frameworks/Python.framework/Versions/Current
pythonHomeDir.cd("../Frameworks/Python.framework/Versions/Current");
qInfo() << "Setting PYTHONHOME =" << pythonHomeDir.absolutePath()
<< " for macOS Application Bundle.";
# endif
customPythonHome = pythonHomeDir.absolutePath();
}
#endif
#if (PY_MAJOR_VERSION <= 3) && (PY_MICRO_VERSION < 11)
if (!customPythonHome.isNull()) {
qInfo() << "PYTHONHOME =" << customPythonHome;
pythonHome = Py_DecodeLocale(customPythonHome.toLocal8Bit().constData(), nullptr);
// This function is deprecated starting from Python 3.11
Py_SetPythonHome(pythonHome);
}
#else
PyConfig config;
PyConfig_InitPythonConfig(&config);
if (!customPythonHome.isNull()) {
qInfo() << "PYTHONHOME =" << customPythonHome;
PyConfig_SetBytesString(&config, &config.home, customPythonHome);
}
#endif
}
#ifdef CUTTER_ENABLE_PYTHON_BINDINGS
extern "C" PyObject *PyInit_CutterBindings();
#endif
void PythonManager::initialize()
{
initPythonHome();
PyImport_AppendInittab("_cutter", &PyInit_api);
PyImport_AppendInittab("_qtres", &PyInit_qtres);
#ifdef CUTTER_ENABLE_PYTHON_BINDINGS
PyImport_AppendInittab("CutterBindings", &PyInit_CutterBindings);
#endif
#if (PY_MAJOR_VERSION <= 3) && (PY_MICRO_VERSION < 11)
Py_Initialize();
#else
Py_InitializeFromConfig(&config);
#endif
// This function is deprecated does nothing starting from Python 3.9
#if (PY_MAJOR_VERSION <= 3) && (PY_MICRO_VERSION < 9)
PyEval_InitThreads();
#endif
#if (PY_MAJOR_VERSION <= 3) && (PY_MICRO_VERSION >= 11)
PyConfig_Clear(&config);
#endif
pyThreadStateCounter = 1; // we have the thread now => 1
RegQtResImporter();
saveThread();
}
#ifdef CUTTER_ENABLE_PYTHON_BINDINGS
static void pySideDestructionVisitor(SbkObject *pyObj, void *data)
{
void **realData = reinterpret_cast<void **>(data);
auto pyQApp = reinterpret_cast<SbkObject *>(realData[0]);
auto pyQObjectType = reinterpret_cast<PyTypeObject *>(realData[1]);
if (pyObj == pyQApp || !PyObject_TypeCheck(pyObj, pyQObjectType)) {
return;
}
if (!Shiboken::Object::hasOwnership(pyObj) || !Shiboken::Object::isValid(pyObj, false)) {
return;
}
const char *reprStr = "";
PyObject *repr = PyObject_Repr(reinterpret_cast<PyObject *>(pyObj));
PyObject *reprBytes;
if (repr) {
reprBytes = PyUnicode_AsUTF8String(repr);
reprStr = PyBytes_AsString(reprBytes);
}
qWarning() << "Warning: QObject from Python remaining (leaked from plugin?):" << reprStr;
if (repr) {
Py_DecRef(reprBytes);
Py_DecRef(repr);
}
Shiboken::Object::setValidCpp(pyObj, false);
Py_BEGIN_ALLOW_THREADS Shiboken::callCppDestructor<QObject>(
Shiboken::Object::cppPointer(pyObj, pyQObjectType));
Py_END_ALLOW_THREADS
};
#endif
void PythonManager::shutdown()
{
emit willShutDown();
restoreThread();
#ifdef CUTTER_ENABLE_PYTHON_BINDINGS
// This is necessary to prevent a segfault when the CutterCore instance is deleted after the
// Shiboken::BindingManager
Core()->setProperty("_PySideInvalidatePtr", QVariant());
// see PySide::destroyQCoreApplication()
PySide::SignalManager::instance().clear();
Shiboken::BindingManager &bm = Shiboken::BindingManager::instance();
SbkObject *pyQApp = bm.retrieveWrapper(QCoreApplication::instance());
PyTypeObject *pyQObjectType = Shiboken::Conversions::getPythonTypeObject("QObject*");
void *data[2] = { pyQApp, pyQObjectType };
bm.visitAllPyObjects(&pySideDestructionVisitor, &data);
PySide::runCleanupFunctions();
#endif
if (pythonHome) {
PyMem_Free(pythonHome);
}
Py_Finalize();
}
void PythonManager::addPythonPath(char *path)
{
restoreThread();
PyObject *sysModule = PyImport_ImportModule("sys");
if (!sysModule) {
return;
}
PyObject *pythonPath = PyObject_GetAttrString(sysModule, "path");
if (!pythonPath) {
return;
}
PyObject *append = PyObject_GetAttrString(pythonPath, "append");
if (!append) {
return;
}
PyObject_CallFunction(append, "(s)", path);
saveThread();
}
void PythonManager::restoreThread()
{
pyThreadStateCounter++;
if (pyThreadStateCounter == 1 && pyThreadState) {
PyEval_RestoreThread(pyThreadState);
}
}
void PythonManager::saveThread()
{
pyThreadStateCounter--;
assert(pyThreadStateCounter >= 0);
if (pyThreadStateCounter == 0) {
pyThreadState = PyEval_SaveThread();
}
}