Skip to content

Commit f366f3e

Browse files
committed
qt gui: be more resilient against import issues of QtMultimedia
The user should still be able to open the Preferences dialog, despite the camera functionality being broken. see #9949 ``` 6.36 | E | gui.qt.exception_window.Exception_Hook | exception caught by crash reporter Traceback (most recent call last): File "electrum\gui\qt\main_window.py", line 2672, in settings_dialog File "electrum\gui\qt\settings_dialog.py", line 229, in __init__ File "electrum\gui\qt\qrreader\__init__.py", line 96, in find_system_cameras File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 935, in _load_unlocked File "PyInstaller\loader\pyimod02_importers.py", line 450, in exec_module File "electrum\gui\qt\qrreader\qtmultimedia\__init__.py", line 28, in <module> File "<frozen importlib._bootstrap>", line 1360, in _find_and_load File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 935, in _load_unlocked File "PyInstaller\loader\pyimod02_importers.py", line 450, in exec_module File "electrum\gui\qt\qrreader\qtmultimedia\camera_dialog.py", line 32, in <module> RuntimeError: PyQt6.QtMultimedia cannot import type '���d�⌂' from PyQt6.QtCore ```
1 parent 694c03e commit f366f3e

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

electrum/gui/qt/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@
4444

4545
import PyQt6.QtCore as QtCore
4646

47+
from electrum.logging import Logger, get_logger
48+
_logger = get_logger(__name__)
49+
4750
try:
4851
# Preload QtMultimedia at app start, if available.
4952
# We use QtMultimedia on some platforms for camera-handling, and
5053
# lazy-loading it later led to some crashes. Maybe due to bugs in PyQt. (see #7725)
5154
from PyQt6.QtMultimedia import QMediaDevices; del QMediaDevices
52-
except ImportError as e:
55+
except (ImportError, RuntimeError) as e:
56+
_logger.debug(f"failed to import optional dependency: PyQt6.QtMultimedia. exc={repr(e)}")
5357
pass # failure is ok; it is an optional dependency.
58+
else:
59+
_logger.debug(f"successfully preloaded optional dependency: PyQt6.QtMultimedia")
5460

5561
if sys.platform == "linux" and os.environ.get("APPIMAGE"):
5662
# For AppImage, we default to xcb qt backend, for better support of older system.
@@ -67,7 +73,6 @@
6773
standardize_path)
6874
from electrum.wallet import Wallet, Abstract_Wallet
6975
from electrum.wallet_db import WalletRequiresSplit, WalletRequiresUpgrade, WalletUnfinished
70-
from electrum.logging import Logger
7176
from electrum.gui import BaseElectrumGui
7277
from electrum.simple_config import SimpleConfig
7378
from electrum.wizard import WizardViewState

electrum/gui/qt/qrreader/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ def find_system_cameras() -> Mapping[str, str]:
9494
if sys.platform == 'darwin' or sys.platform in ('windows', 'win32'):
9595
try:
9696
from .qtmultimedia import find_system_cameras
97-
except ImportError as e:
97+
except (ImportError, RuntimeError) as e:
98+
_logger.exception('error importing .qtmultimedia')
9899
return {}
99100
else:
100101
return find_system_cameras()
@@ -143,7 +144,7 @@ def _scan_qrcode_using_qtmultimedia(
143144
) -> None:
144145
try:
145146
from .qtmultimedia import QrReaderCameraDialog, CameraError
146-
except ImportError as e:
147+
except (ImportError, RuntimeError) as e:
147148
icon = QMessageBox.Icon.Warning
148149
title = _("QR Reader Error")
149150
message = _("QR reader failed to load. This may happen if "

electrum/gui/qt/qrreader/qtmultimedia/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2323
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2424
# SOFTWARE.
25+
#
26+
# -----
27+
#
28+
# Note: This module is risky to import. At the very least, ImportError and
29+
# RuntimeError needs to be handled at import time!
2530

2631
from typing import Mapping
2732

0 commit comments

Comments
 (0)