Skip to content

Commit 3d68cad

Browse files
authored
Work around PyQt5 bug with argument types (#3776)
Qt's getOpenFileUrls() and related Url-based functions are supposed to take a QUrl for the 'directory' argument (starting path), but until PyQt5 5.13.1 the argument was mis-typed as str.
1 parent 70be7dd commit 3d68cad

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/classes/app.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import platform
3333
import sys
3434
import traceback
35+
from distutils.version import LooseVersion
3536

3637
from PyQt5.QtCore import PYQT_VERSION_STR
3738
from PyQt5.QtCore import QT_VERSION_STR
@@ -57,7 +58,7 @@
5758
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
5859
QApplication.setAttribute(Qt.AA_ShareOpenGLContexts)
5960
except AttributeError:
60-
pass # Quietly fail for older Qt5 versions
61+
pass # Quietly fail for older Qt5 versions
6162

6263

6364
def get_app():
@@ -84,7 +85,7 @@ def __init__(self, *args, mode=None):
8485
log.info('Starting new session'.center(48))
8586

8687
from classes import settings, project_data, updates, language, ui_util, logger_libopenshot
87-
from . import openshot_rc
88+
from . import openshot_rc # noqa
8889
import openshot
8990

9091
# Re-route stdout and stderr to logger
@@ -130,6 +131,10 @@ def __init__(self, *args, mode=None):
130131
self.settings = settings.SettingStore()
131132
self.settings.load()
132133

134+
# Set up distutils Version instance for PyQt version checks
135+
self.pyqt_version = LooseVersion(PYQT_VERSION_STR)
136+
log.debug("Stored PyQt version as %s", repr(self.pyqt_version))
137+
133138
# Init and attach exception handler
134139
from classes import exceptions
135140
sys.excepthook = exceptions.ExceptionHandler
@@ -141,9 +146,12 @@ def __init__(self, *args, mode=None):
141146
_ = self._tr
142147
libopenshot_version = openshot.OPENSHOT_VERSION_FULL
143148
if mode != "unittest" and libopenshot_version < info.MINIMUM_LIBOPENSHOT_VERSION:
144-
QMessageBox.warning(None, _("Wrong Version of libopenshot Detected"),
145-
_("<b>Version %(minimum_version)s is required</b>, but %(current_version)s was detected. Please update libopenshot or download our latest installer.") %
146-
{"minimum_version": info.MINIMUM_LIBOPENSHOT_VERSION, "current_version": libopenshot_version})
149+
QMessageBox.warning(
150+
None, _("Wrong Version of libopenshot Detected"),
151+
_("<b>Version %(minimum_version)s is required</b>, but %(current_version)s was detected. Please update libopenshot or download our latest installer.") % {
152+
"minimum_version": info.MINIMUM_LIBOPENSHOT_VERSION,
153+
"current_version": libopenshot_version,
154+
})
147155
# Stop launching and exit
148156
sys.exit()
149157

@@ -175,9 +183,13 @@ def __init__(self, *args, mode=None):
175183
os.unlink(TEST_PATH_FILE)
176184
os.rmdir(TEST_PATH_DIR)
177185
except PermissionError as ex:
178-
log.error('Failed to create PERMISSION/test.osp file (likely permissions error): %s' % TEST_PATH_FILE, exc_info=1)
179-
QMessageBox.warning(None, _("Permission Error"),
180-
_("%(error)s. Please delete <b>%(path)s</b> and launch OpenShot again." % {"error": str(ex), "path": info.USER_PATH}))
186+
log.error('Failed to create file %s', TEST_PATH_FILE, exc_info=1)
187+
QMessageBox.warning(
188+
None, _("Permission Error"),
189+
_("%(error)s. Please delete <b>%(path)s</b> and launch OpenShot again." % {
190+
"error": str(ex),
191+
"path": info.USER_PATH,
192+
}))
181193
# Stop launching and exit
182194
raise
183195
sys.exit()

src/windows/main_window.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
import openshot # Python module for libopenshot (required video editing module installed separately)
5252

5353
from windows.views.timeline_webview import TimelineWebView
54-
from classes import info, ui_util, openshot_rc, settings, qt_types, updates
54+
from classes import info, ui_util, settings, qt_types, updates
55+
from classes import openshot_rc # noqa
5556
from classes.app import get_app
5657
from classes.logger import log
5758
from classes.timeline import TimelineSync
@@ -760,9 +761,20 @@ def actionImportFiles_trigger(self, event):
760761
if not recommended_path or not os.path.exists(recommended_path):
761762
recommended_path = os.path.join(info.HOME_PATH)
762763

764+
# PyQt through 5.13.0 had the 'directory' argument mis-typed as str
765+
if get_app().pyqt_version < '5.13.1':
766+
dir_type = "str"
767+
start_location = str(recommended_path)
768+
else:
769+
dir_type = "QUrl"
770+
start_location = QUrl.fromLocalFile(recommended_path)
771+
772+
log.debug("Calling getOpenFileURLs() with %s directory argument", dir_type)
763773
qurl_list = QFileDialog.getOpenFileUrls(
764-
self, _("Import Files..."),
765-
QUrl.fromLocalFile(recommended_path))[0]
774+
self,
775+
_("Import Files..."),
776+
start_location,
777+
)[0]
766778

767779
# Set cursor to waiting
768780
app.setOverrideCursor(QCursor(Qt.WaitCursor))

0 commit comments

Comments
 (0)