2018-06-23 16:59:23 +00:00
|
|
|
#include "PythonAPI.h"
|
|
|
|
#include "PythonManager.h"
|
|
|
|
|
|
|
|
#include <marshal.h>
|
2018-06-24 09:51:16 +00:00
|
|
|
#include <QDebug>
|
2018-06-23 16:59:23 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include "QtResImporter.h"
|
|
|
|
#include "plugins/CutterPythonPlugin.h"
|
|
|
|
|
2018-07-04 20:50:02 +00:00
|
|
|
static PythonManager *uniqueInstance = nullptr;
|
2018-06-23 16:59:23 +00:00
|
|
|
|
|
|
|
PythonManager *PythonManager::getInstance()
|
|
|
|
{
|
2018-07-04 20:50:02 +00:00
|
|
|
if (!uniqueInstance) {
|
|
|
|
uniqueInstance = new PythonManager();
|
|
|
|
}
|
2018-06-23 16:59:23 +00:00
|
|
|
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 (!customPythonHome.isNull()) {
|
|
|
|
qInfo() << "PYTHONHOME =" << customPythonHome;
|
|
|
|
pythonHome = Py_DecodeLocale(customPythonHome.toLocal8Bit().constData(), nullptr);
|
|
|
|
Py_SetPythonHome(pythonHome);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 15:03:36 +00:00
|
|
|
extern "C" PyObject *PyInit_CutterBindings();
|
|
|
|
|
2018-06-23 16:59:23 +00:00
|
|
|
void PythonManager::initialize()
|
|
|
|
{
|
|
|
|
initPythonHome();
|
|
|
|
|
|
|
|
PyImport_AppendInittab("_cutter", &PyInit_api);
|
2019-02-03 15:03:36 +00:00
|
|
|
#ifdef CUTTER_ENABLE_JUPYTER
|
2018-06-23 16:59:23 +00:00
|
|
|
PyImport_AppendInittab("cutter_internal", &PyInit_api_internal);
|
2019-02-03 15:03:36 +00:00
|
|
|
#endif
|
2018-06-23 16:59:23 +00:00
|
|
|
PyImport_AppendInittab("_qtres", &PyInit_qtres);
|
2019-02-03 15:03:36 +00:00
|
|
|
PyImport_AppendInittab("CutterBindings", &PyInit_CutterBindings);
|
2018-06-23 16:59:23 +00:00
|
|
|
Py_Initialize();
|
|
|
|
PyEval_InitThreads();
|
|
|
|
|
2019-02-03 11:31:59 +00:00
|
|
|
RegQtResImporter();
|
|
|
|
|
2018-06-23 16:59:23 +00:00
|
|
|
// Import other modules
|
|
|
|
cutterPluginModule = QtResImport("cutter_plugin");
|
|
|
|
|
2018-06-24 09:51:16 +00:00
|
|
|
saveThread();
|
2018-06-23 16:59:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 13:00:40 +00:00
|
|
|
void PythonManager::shutdown()
|
|
|
|
{
|
|
|
|
emit willShutDown();
|
|
|
|
|
|
|
|
restoreThread();
|
|
|
|
|
|
|
|
Py_Finalize();
|
|
|
|
|
|
|
|
if (pythonHome) {
|
|
|
|
PyMem_RawFree(pythonHome);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 16:59:23 +00:00
|
|
|
void PythonManager::addPythonPath(char *path) {
|
2018-06-24 09:51:16 +00:00
|
|
|
restoreThread();
|
2018-06-23 16:59:23 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
PyEval_CallFunction(append, "(s)", path);
|
|
|
|
|
2018-06-24 09:51:16 +00:00
|
|
|
saveThread();
|
2018-06-23 16:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-24 09:51:16 +00:00
|
|
|
CutterPythonPlugin* PythonManager::loadPlugin(const char *pluginName) {
|
2018-06-23 16:59:23 +00:00
|
|
|
CutterPythonPlugin *plugin = nullptr;
|
|
|
|
if (!cutterPluginModule) {
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
|
2018-06-24 09:51:16 +00:00
|
|
|
restoreThread();
|
2018-06-23 16:59:23 +00:00
|
|
|
PyObject *pluginModule = PyImport_ImportModule(pluginName);
|
|
|
|
if (!pluginModule) {
|
2018-07-04 20:50:02 +00:00
|
|
|
qWarning() << "Couldn't load the plugin" << QString(pluginName);
|
2018-06-24 09:51:16 +00:00
|
|
|
PyErr_PrintEx(10);
|
|
|
|
} else {
|
|
|
|
plugin = new CutterPythonPlugin(pluginModule);
|
2018-06-23 16:59:23 +00:00
|
|
|
}
|
2018-06-24 09:51:16 +00:00
|
|
|
saveThread();
|
2018-06-23 16:59:23 +00:00
|
|
|
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
|
2018-06-24 09:51:16 +00:00
|
|
|
void PythonManager::restoreThread()
|
2018-06-23 16:59:23 +00:00
|
|
|
{
|
|
|
|
if (pyThreadState) {
|
|
|
|
PyEval_RestoreThread(pyThreadState);
|
|
|
|
}
|
2018-06-24 09:51:16 +00:00
|
|
|
}
|
2018-06-23 16:59:23 +00:00
|
|
|
|
2018-06-24 09:51:16 +00:00
|
|
|
void PythonManager::saveThread()
|
|
|
|
{
|
2018-06-23 16:59:23 +00:00
|
|
|
pyThreadState = PyEval_SaveThread();
|
|
|
|
}
|