twizard hww: use exception handling to choose hw device again - electrum - Electrum Bitcoin wallet (HTM) git clone https://git.parazyd.org/electrum (DIR) Log (DIR) Files (DIR) Refs (DIR) Submodules --- (DIR) commit b6bac0182f23073b9136ebfd3aaf5a13d85f4def (DIR) parent 71eed1d4cb70b69767974d21430cfa28ba2cc6eb (HTM) Author: SomberNight <somber.night@protonmail.com> Date: Thu, 9 Apr 2020 19:44:33 +0200 wizard hww: use exception handling to choose hw device again - no need to pass args, caller knows what it wanted - avoids deepening the call stack on every rescan (nicer tracebacks, no stack overflow) Diffstat: M electrum/base_wizard.py | 40 +++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) --- (DIR) diff --git a/electrum/base_wizard.py b/electrum/base_wizard.py t@@ -60,6 +60,9 @@ class ScriptTypeNotSupported(Exception): pass class GoBack(Exception): pass +class ChooseHwDeviceAgain(Exception): pass + + class WizardStackItem(NamedTuple): action: Any args: Any t@@ -264,6 +267,15 @@ class BaseWizard(Logger): self.on_keystore(k) def choose_hw_device(self, purpose=HWD_SETUP_NEW_WALLET, *, storage: WalletStorage = None): + while True: + try: + self._choose_hw_device(purpose=purpose, storage=storage) + except ChooseHwDeviceAgain: + pass + else: + break + + def _choose_hw_device(self, *, purpose, storage: WalletStorage = None): title = _('Hardware Keystore') # check available plugins supported_plugins = self.plugins.get_hardware_support() t@@ -327,8 +339,8 @@ class BaseWizard(Logger): msg += '\n\n' msg += _('Debug message') + '\n' + debug_msg self.confirm_dialog(title=title, message=msg, - run_next=lambda x: self.choose_hw_device(purpose, storage=storage)) - return + run_next=lambda x: None) + raise ChooseHwDeviceAgain() # select device self.devices = devices choices = [] t@@ -355,27 +367,22 @@ class BaseWizard(Logger): + _('To try to fix this, we will now re-pair with your device.') + '\n' + _('Please try again.')) devmgr.unpair_id(device_info.device.id_) - self.choose_hw_device(purpose, storage=storage) - return + raise ChooseHwDeviceAgain() except OutdatedHwFirmwareException as e: if self.question(e.text_ignore_old_fw_and_continue(), title=_("Outdated device firmware")): self.plugin.set_ignore_outdated_fw() # will need to re-pair devmgr.unpair_id(device_info.device.id_) - self.choose_hw_device(purpose, storage=storage) - return + raise ChooseHwDeviceAgain() except (UserCancelled, GoBack): - self.choose_hw_device(purpose, storage=storage) - return + raise ChooseHwDeviceAgain() except UserFacingException as e: self.show_error(str(e)) - self.choose_hw_device(purpose, storage=storage) - return + raise ChooseHwDeviceAgain() except BaseException as e: self.logger.exception('') self.show_error(str(e)) - self.choose_hw_device(purpose, storage=storage) - return + raise ChooseHwDeviceAgain() if purpose == HWD_SETUP_NEW_WALLET: def f(derivation, script_type): t@@ -444,8 +451,7 @@ class BaseWizard(Logger): except BaseException as e: self.logger.exception('') self.show_error(e) - self.choose_hw_device() - return + raise ChooseHwDeviceAgain() d = { 'type': 'hardware', 'hw_type': name, t@@ -561,13 +567,11 @@ class BaseWizard(Logger): except UserCancelled: devmgr = self.plugins.device_manager devmgr.unpair_xpub(k.xpub) - self.choose_hw_device() - return + raise ChooseHwDeviceAgain() except BaseException as e: self.logger.exception('') self.show_error(str(e)) - self.choose_hw_device() - return + raise ChooseHwDeviceAgain() self.request_storage_encryption( run_next=lambda encrypt_storage: self.on_password( password,