From 1378c09aa5de0eb2edd8966c783c204c4de52dce Mon Sep 17 00:00:00 2001 From: Jimmy Date: Sat, 16 Apr 2022 15:51:41 +1200 Subject: Manual fixups for ImportErrors lost in rewrite Fixups for commit PyQt import rewrite commit "Re-write PyQt5 imports to be via our wrapper." Import not at the top of the file where mostly moved up there. Where they were inside a try/except block they were replaced with `pass` (why!). With the current setup of having everything imported into qt.py we don't get import errors anymore anyway. So affected places have been changed to check for None. This has not currently been well tested. We might have to move some of the conditional checks into qt.py. I identified these pieces of code by looking for `^+ * pass$` in the diff. --- qutebrowser/browser/webengine/webenginetab.py | 10 ++-------- qutebrowser/misc/backendproblem.py | 15 +++++---------- qutebrowser/misc/earlyinit.py | 11 ++++++----- qutebrowser/utils/qtutils.py | 6 ++---- qutebrowser/utils/version.py | 10 ++++------ tests/end2end/conftest.py | 8 +++----- tests/helpers/testutils.py | 5 +---- tests/unit/browser/test_history.py | 5 ----- tests/unit/completion/test_models.py | 4 ---- tests/unit/config/test_configfiles.py | 4 +--- tests/unit/utils/test_version.py | 6 ++---- 11 files changed, 26 insertions(+), 58 deletions(-) diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index a0a83d7a7..38d8b5e7b 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -119,9 +119,7 @@ class _WebEngineSearchWrapHandler: if not qtutils.version_check("5.14"): return - try: - pass - except ImportError: + if not hasattr(QtWebEngineCore, 'QWebEngineFindTextResult'): # WORKAROUND for some odd PyQt/packaging bug where the # findTextResult signal is available, but QWebEngineFindTextResult # is not. Seems to happen on e.g. Gentoo. @@ -1692,11 +1690,7 @@ class WebEngineTab(browsertab.AbstractTab): page.navigation_request.connect(self._on_navigation_request) page.printRequested.connect(self._on_print_requested) - try: - pass - except ImportError: - pass - else: + if QtWebEngineWidgets.QWebEngineClientCertificateSelection: page.selectClientCertificate.connect( self._on_select_client_certificate) diff --git a/qutebrowser/misc/backendproblem.py b/qutebrowser/misc/backendproblem.py index 7cd292294..c318c2f80 100644 --- a/qutebrowser/misc/backendproblem.py +++ b/qutebrowser/misc/backendproblem.py @@ -263,21 +263,16 @@ class _BackendProblemChecker: def _try_import_backends(self) -> _BackendImports: """Check whether backends can be imported and return BackendImports.""" - # pylint: disable=unused-import results = _BackendImports() - try: - pass - except (ImportError, ValueError) as e: - results.webkit_error = str(e) - else: + if QtWebKit and QtWebKitWidgets and QtWebKit.qWebKitVersion: if not qtutils.is_new_qtwebkit(): results.webkit_error = "Unsupported legacy QtWebKit found" + else: + results.webkit_error = "Some required QtWebKit modules not found" - try: - pass - except (ImportError, ValueError) as e: - results.webengine_error = str(e) + if not QtWebEngineWidgets: + results.webengine_error = "QtWebEngineWidgets not available" return results diff --git a/qutebrowser/misc/earlyinit.py b/qutebrowser/misc/earlyinit.py index fe174991f..2c71804e7 100644 --- a/qutebrowser/misc/earlyinit.py +++ b/qutebrowser/misc/earlyinit.py @@ -78,7 +78,7 @@ def _die(message, exception=None): message: The message to display. exception: The exception object if we're handling an exception. """ - from qutebrowser.qt import QtWidgets, QtWebEngineWidgets, QtNetwork, QtCore + from qutebrowser.qt import QtWidgets, QtCore if (('--debug' in sys.argv or '--no-err-windows' in sys.argv) and exception is not None): print(file=sys.stderr) @@ -160,6 +160,7 @@ def check_pyqt(): def qt_version(qversion=None, qt_version_str=None): """Get a Qt version string based on the runtime/compiled versions.""" + from qutebrowser.qt import QtCore if qversion is None: qversion = QtCore.qVersion() if qt_version_str is None: @@ -173,6 +174,7 @@ def qt_version(qversion=None, qt_version_str=None): def check_qt_version(): """Check if the Qt version is recent enough.""" + from qutebrowser.qt import QtCore try: qt_ver = QtCore.QLibraryInfo.version().normalized() recent_qt_runtime = qt_ver >= QtCore.QVersionNumber(5, 12) # type: ignore[operator] @@ -194,9 +196,8 @@ def check_qt_version(): def check_ssl_support(): """Check if SSL support is available.""" - try: - pass - except ImportError: + from qutebrowser.qt import QtNetwork + if not QtNetwork or not QtNetwork.QSslSocket: _die("Fatal error: Your Qt is built without SSL support.") @@ -288,7 +289,7 @@ def webengine_early_import(): error messages in backendproblem.py are accurate. """ try: - pass + from qutebrowser.qt import QtWebEngineWidgets # pylint: disable=unused-import except ImportError: pass diff --git a/qutebrowser/utils/qtutils.py b/qutebrowser/utils/qtutils.py index 8c0f8a6be..bf109095e 100644 --- a/qutebrowser/utils/qtutils.py +++ b/qutebrowser/utils/qtutils.py @@ -33,10 +33,8 @@ import operator import contextlib from typing import (Any, AnyStr, TYPE_CHECKING, BinaryIO, IO, Iterator, Optional, Union, Tuple, cast) -from qutebrowser.qt import QtWebKit, QtWebEngineWidgets, QtGui -try: - pass -except ImportError: # pragma: no cover +from qutebrowser.qt import QtWebKit, QtGui +if not QtWebKit: qWebKitVersion = None # type: ignore[assignment] # noqa: N816 if TYPE_CHECKING: from qutebrowser.qt import QWebHistory diff --git a/qutebrowser/utils/version.py b/qutebrowser/utils/version.py index 6049b0708..28df9199d 100644 --- a/qutebrowser/utils/version.py +++ b/qutebrowser/utils/version.py @@ -38,13 +38,11 @@ from typing import (Mapping, Optional, Sequence, Tuple, ClassVar, Dict, cast, TYPE_CHECKING) from qutebrowser.qt import QtWidgets, QtWebKit, QtWebEngine, QtNetwork -try: - pass -except ImportError: # pragma: no cover +if not QtWebKit: qWebKitVersion = None # type: ignore[assignment] # noqa: N816 -try: - pass -except ImportError: # pragma: no cover +if QtWebEngine: + PYQT_WEBENGINE_VERSION_STR = QtWebEngine.PYQT_WEBENGINE_VERSION_STR +else: # Added in PyQt 5.13 PYQT_WEBENGINE_VERSION_STR = None # type: ignore[assignment] diff --git a/tests/end2end/conftest.py b/tests/end2end/conftest.py index 6e14495e2..d8d3b7a38 100644 --- a/tests/end2end/conftest.py +++ b/tests/end2end/conftest.py @@ -118,12 +118,10 @@ def _get_version_tag(tag): reason='Needs ' + tag, ) elif package == 'pyqtwebengine': - try: - pass - except ImportError: - running_version = QtCore.PYQT_VERSION - else: + if QtWebEngine: running_version = QtWebEngine.PYQT_WEBENGINE_VERSION + else: + running_version = QtCore.PYQT_VERSION return pytest.mark.skipif( not _check_version( op_str=match.group('operator'), diff --git a/tests/helpers/testutils.py b/tests/helpers/testutils.py index 44a299de6..40031a433 100644 --- a/tests/helpers/testutils.py +++ b/tests/helpers/testutils.py @@ -272,10 +272,7 @@ def disable_seccomp_bpf_sandbox(): This is needed for some QtWebEngine setups, with older Qt versions but newer kernels. """ - try: - pass - except ImportError: - # no QtWebEngine available + if not QtWebEngine: return False affected_versions = set() diff --git a/tests/unit/browser/test_history.py b/tests/unit/browser/test_history.py index d757d16ba..e41998b3c 100644 --- a/tests/unit/browser/test_history.py +++ b/tests/unit/browser/test_history.py @@ -320,11 +320,6 @@ class TestInit: history.init(data_tmpdir / f'test_init_{backend}', qapp) assert history.web_history.parent() is qapp - try: - pass - except ImportError: - QWebHistoryInterface = None - if backend == usertypes.Backend.QtWebKit: default_interface = QtWebKit.QWebHistoryInterface.defaultInterface() assert default_interface._history is history.web_history diff --git a/tests/unit/completion/test_models.py b/tests/unit/completion/test_models.py index 360ee9f8a..ceea3db35 100644 --- a/tests/unit/completion/test_models.py +++ b/tests/unit/completion/test_models.py @@ -31,10 +31,6 @@ from unittest import mock import hypothesis import hypothesis.strategies as hst import pytest -try: - pass -except ImportError: - pass from qutebrowser.misc import objects, guiprocess from qutebrowser.completion import completer diff --git a/tests/unit/config/test_configfiles.py b/tests/unit/config/test_configfiles.py index 460a512fa..ed7cb1714 100644 --- a/tests/unit/config/test_configfiles.py +++ b/tests/unit/config/test_configfiles.py @@ -166,9 +166,7 @@ def state_writer(data_tmpdir): @pytest.fixture def qtwe_version_patcher(monkeypatch): - try: - pass - except ImportError: + if not QtWebEngineWidgets: pytest.skip("QtWebEngine not available") def patch(ver): diff --git a/tests/unit/utils/test_version.py b/tests/unit/utils/test_version.py index d7a46ae07..d65a91082 100644 --- a/tests/unit/utils/test_version.py +++ b/tests/unit/utils/test_version.py @@ -1007,11 +1007,9 @@ class TestWebEngineVersions: if '.dev' in QtCore.PYQT_VERSION_STR: pytest.skip("dev version of PyQt5") - try: - pass - except ImportError as e: + if not QtWebEngine or not hasattr(QtWebEngine, "PYQT_WEBENGINE_VERSION"): # QtWebKit or QtWebEngine < 5.13 - pytest.skip(str(e)) + pytest.skip("QtWebEngine") if QtWebEngine.PYQT_WEBENGINE_VERSION >= 0x050F02: # Starting with Qt 5.15.2, we can only do bad guessing anyways... -- cgit v1.2.3-54-g00ecf