Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Avoid loading conflicting version of PyQt

Freya Lupen38bbeb8a1882
diff --git a/plugins/extensions/pykrita/plugin/PykritaModule.cpp b/plugins/extensions/pykrita/plugin/PykritaModule.cpp
index 33bb6e9801..2f49a803f0 100644
--- a/plugins/extensions/pykrita/plugin/PykritaModule.cpp
+++ b/plugins/extensions/pykrita/plugin/PykritaModule.cpp
@@ -31,6 +31,12 @@ namespace PYKRITA
Py_INCREF(Py_None);
return Py_None;
}
+
+ PyObject* qt_major_version(PyObject* /*self*/, PyObject* args)
+ {
+ Q_UNUSED(args);
+ return PyLong_FromLong(QT_VERSION_MAJOR);
+ }
} // namespace PYKRITA
namespace
@@ -42,6 +48,12 @@ namespace
, METH_VARARGS
, "True KDE way to show debug info"
}
+ , {
+ "qt_major_version"
+ , &PYKRITA::qt_major_version
+ , METH_VARARGS
+ , "Qt major version number"
+ }
, { 0, 0, 0, 0 }
};
} // anonymous namespace
diff --git a/plugins/extensions/pykrita/plugin/krita/__init__.py b/plugins/extensions/pykrita/plugin/krita/__init__.py
index 0f8c229c7a..9ce40f2cd2 100644
--- a/plugins/extensions/pykrita/plugin/krita/__init__.py
+++ b/plugins/extensions/pykrita/plugin/krita/__init__.py
@@ -8,6 +8,29 @@ import pykrita
import os
import sys
+# Try to make sure there's no conflicting version of PyQt on sys.path, because
+# loading that would load a conflicting version of Qt and crash Krita when used.
+# However, this will also cause any neighboring modules to be removed.
+import importlib.util
+pyqt_wrong = "PyQt" + ("6" if pykrita.qt_major_version() == 5 else "5")
+if (spec := importlib.util.find_spec(pyqt_wrong)):
+ parent_folder = os.path.dirname(os.path.dirname(spec.origin))
+ if sys.path.__contains__(parent_folder):
+ print(f"Found {pyqt_wrong} in '{parent_folder}', "
+ "removing from Python's search path to avoid crashes.", file=sys.stderr)
+ sys.path.remove(parent_folder)
+
+# Look for PyQt
+try:
+ from PyQt6 import QtCore
+except ImportError:
+ try:
+ from PyQt5 import QtCore
+ except ImportError:
+ print("Python cannot find the Qt5 bindings.", file=sys.stderr)
+ print("Please make sure, that the needed packages are installed.", file=sys.stderr)
+ raise
+
from .api import *
from .decorators import *
from .dockwidgetfactory import *
@@ -20,17 +43,6 @@ krita_path = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, krita_path)
print("%s added to PYTHONPATH" % krita_path, file=sys.stderr)
-# Look for PyQt
-try:
- from PyQt6 import QtCore
-except:
- try:
- from PyQt5 import QtCore
- except ImportError:
- print("Python cannot find the Qt5 bindings.", file=sys.stderr)
- print("Please make sure, that the needed packages are installed.", file=sys.stderr)
- raise
-
# Shows nice looking error dialog if an unhandled exception occurs.
import excepthook
excepthook.install()