From 988e6217127c28d9f32f5d65644da83f1a667127 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 26 Oct 2026 13:36:48 +0000 Subject: [PATCH] various exception-related changes and cleanups --- mmgen/fileutil.py | 13 +++++-------- mmgen/proto/btc/regtest.py | 17 +++++++---------- pyproject.toml | 1 - test/cmdtest.py | 5 ++++- test/cmdtest_d/autosign.py | 8 ++++---- test/cmdtest_d/regtest.py | 2 +- 6 files changed, 21 insertions(+), 25 deletions(-) diff --git a/mmgen/fileutil.py b/mmgen/fileutil.py index d3208630..7634ce06 100644 --- a/mmgen/fileutil.py +++ b/mmgen/fileutil.py @@ -35,14 +35,11 @@ from .util import ( def check_or_create_dir(cfg, path): try: os.listdir(path) - except: - if cfg.test_suite and os.path.exists(path): # path is a link or regular file - from subprocess import run - run(['rm', '-rf', str(path)]) - try: - os.makedirs(path, 0o700) - except: - die(2, f'ERROR: unable to read or create path ‘{path}’') + except FileNotFoundError: + os.makedirs(path, 0o700) # will fail if file is a broken link + except NotADirectoryError: # path is a good link or regular file + os.unlink(path) + os.makedirs(path, 0o700) def check_binary(args): from subprocess import run, DEVNULL diff --git a/mmgen/proto/btc/regtest.py b/mmgen/proto/btc/regtest.py index f56b56c0..7b2ce314 100644 --- a/mmgen/proto/btc/regtest.py +++ b/mmgen/proto/btc/regtest.py @@ -28,24 +28,21 @@ from ...objmethods import MMGenObject from ...daemon import CoinDaemon def create_data_dir(cfg, data_dir): + rt_dir = os.path.join(data_dir, 'regtest') try: - os.stat(os.path.join(data_dir, 'regtest')) - except: - pass + os.stat(rt_dir) + except FileNotFoundError: + os.makedirs(rt_dir) else: from ...ui import keypress_confirm if keypress_confirm( cfg, - f'Delete your existing MMGen regtest setup at {data_dir!r} and create a new one?'): - shutil.rmtree(data_dir) + f'Delete your existing MMGen regtest setup at {rt_dir!r} and create a new one?'): + shutil.rmtree(rt_dir) + os.makedirs(rt_dir) else: die(1, 'Exiting') - try: - os.makedirs(data_dir) - except: - pass - class MMGenRegtest(MMGenObject): rpc_user = 'bobandalice' diff --git a/pyproject.toml b/pyproject.toml index 26e2c3b2..671b9650 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,6 @@ indent-style = "tab" ignore = [ "B006", # Do not use mutable data structures for argument defaults "BLE001", # Do not catch blind exception: `Exception` - "E722", # bare except "I001", # Import block is un-sorted or un-formatted "N999", # Invalid module name: 'import'... "PLW1510", # `subprocess.run` without explicit `check` argument diff --git a/test/cmdtest.py b/test/cmdtest.py index 4247ccc8..eee5b4ac 100755 --- a/test/cmdtest.py +++ b/test/cmdtest.py @@ -195,7 +195,10 @@ if not po.user_opts.get('skip_deps'): for fn in (data_dir, autosign_data_dir): try: os.unlink(fn) - except: + except IsADirectoryError: + import shutil + shutil.rmtree(fn) + except FileNotFoundError: pass # step 2: opts.init will create new data_dir in test/tmp (if not po.user_opts['skip_deps']) diff --git a/test/cmdtest_d/autosign.py b/test/cmdtest_d/autosign.py index c65c295d..d8bda11d 100644 --- a/test/cmdtest_d/autosign.py +++ b/test/cmdtest_d/autosign.py @@ -245,11 +245,11 @@ class CmdTestAutosignBase(CmdTestBase): target = joinpath(getattr(self.asi, tx_dir), fn) if op != 'remove_signed': shutil.copyfile(src, target) - try: - if target.endswith('.rawtx'): + if target.endswith('.rawtx'): + try: os.unlink(target.replace('.rawtx', '.sigtx')) - except FileNotFoundError: - pass + except FileNotFoundError: + pass self.do_umount() self.remove_device() diff --git a/test/cmdtest_d/regtest.py b/test/cmdtest_d/regtest.py index 704cfe63..2aeac05c 100644 --- a/test/cmdtest_d/regtest.py +++ b/test/cmdtest_d/regtest.py @@ -1636,7 +1636,7 @@ class CmdTestRegtest(CmdTestBase, CmdTestShared): time.sleep(0.2) t = self.spawn('mmgen-regtest', self.regtest_opts + ['cli', 'unloadwallet', 'carol']) t.ok() - wdir = joinpath((await self.rt.rpc).daemon.network_datadir, 'wallets', 'carol') + wdir = joinpath((await self.rt.rpc).daemon.network_datadir, 'carol') from shutil import rmtree imsg('Deleting Carol’s tracking wallet') rmtree(wdir)