diff options
author | Eike Ziller <eike.ziller@qt.io> | 2018-09-12 15:01:46 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2018-09-14 09:46:29 +0000 |
commit | 876ff8853f42b4ea6fae60adf994f531898cba57 (patch) | |
tree | e0a7e9fc9d6694b1d8a4660a89d4478e5c0caf36 /plugins | |
parent | 6452612845d7edebbef26a0d2b483c129ab3a073 (diff) |
Fix encapsulation of plugins
Fixes that triggering "About Python Extensions" action resulted in
python runtime error "TypeError: must be type, not None" when
referencing PySide2.QtWidgets.
To improve encapsulation of plugins, all modules that they loaded
were deleted after their main.py finished running. This breaks
if these modules were accessed later e.g. triggered by actions.
Instead of this hack, ensure encapsulation of plugins by making them
actual Python packages.
Qt Creator's python extension path is added to python's module search
path, and extensions are simply imported as packages.
The python extension's main.py simply becomes a standard python
module __init__.py.
This also means that python extensions can depend on, and use other
python extensions' functionality by importing them with "import".
Change-Id: Ibe74c24e337b321007f5fa19c97bd35a5c1f0375
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/pythonextensions/pythonextensionsplugin.cpp | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/plugins/pythonextensions/pythonextensionsplugin.cpp b/plugins/pythonextensions/pythonextensionsplugin.cpp index 078a78c..f0ddcc5 100644 --- a/plugins/pythonextensions/pythonextensionsplugin.cpp +++ b/plugins/pythonextensions/pythonextensionsplugin.cpp @@ -164,8 +164,10 @@ QString PythonExtensionsPlugin::pythonPackagePath() void PythonExtensionsPlugin::initializePythonBindings() { // Add our custom module directory - if (extensionDir().exists()) + if (extensionDir().exists()) { + PyUtil::addToSysPath(extensionDir().path().toStdString()); PyUtil::addToSysPath(pythonPackagePath().toStdString()); + } // Initialize the Python context and register global QtCreator variable if (!PyUtil::bindShibokenModuleObject("PythonExtension", "QtCreator")) { qWarning() << "Python bindings could not be initialized"; @@ -268,23 +270,11 @@ void PythonExtensionsPlugin::initializePythonExtensions() // Run the extension initialization code for (const QString &extension : extension_list) { qDebug() << "Trying to initialize extension" << extension; - - QFile extension_main(extension_dir.absolutePath() + "/" + extension + "/main.py"); - if (extension_main.open(QIODevice::ReadOnly)) { - QTextStream in(&extension_main); - QString extension_code = in.readAll(); - if (!PyUtil::runScriptWithPath( - extension_code.toStdString(), - QString(extension_dir.absolutePath() + "/" + extension).toStdString() - )) { - qWarning() << "Failed to initialize extension" << extension; - Core::MessageManager::write(Constants::MESSAGE_MANAGER_PREFIX + tr("Failed to initialize extension ") + extension); - } else { - m_loadedExtensions << extension; - } + if (!PyUtil::runScript("import " + extension.toStdString())) { + qWarning() << "Failed to initialize extension" << extension; + Core::MessageManager::write(Constants::MESSAGE_MANAGER_PREFIX + tr("Failed to initialize extension ") + extension); } else { - qWarning() << "Failed to load main.py for extension" << extension; - Core::MessageManager::write(Constants::MESSAGE_MANAGER_PREFIX + tr("Failed to load main.py for extension ") + extension); + m_loadedExtensions << extension; } } |