From 1ecde8880bef44f0480f88de30ea8b17b6aa4c79 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Tue, 23 Sep 2025 09:20:53 +0000 Subject: [PATCH] minor cleanups (9 files) --- mmgen/addrfile.py | 2 +- mmgen/baseconv.py | 6 +++++- mmgen/filename.py | 2 +- mmgen/main_msg.py | 13 ++++++------- mmgen/main_regtest.py | 12 ++++++------ mmgen/proto/btc/tw/ctl.py | 8 +++----- test/clean.py | 1 + test/objtest.py | 7 +++---- test/tooltest2.py | 3 ++- 9 files changed, 28 insertions(+), 26 deletions(-) diff --git a/mmgen/addrfile.py b/mmgen/addrfile.py index 417a4364..ac355f65 100755 --- a/mmgen/addrfile.py +++ b/mmgen/addrfile.py @@ -343,7 +343,7 @@ class PasswordFile(AddrFile): p = self.parent if p.pw_fmt in ('bip39', 'xmrseed'): - ret = lines.pop(0).split(None, p.pw_len+1) + ret = lines.pop(0).split(None, p.pw_len + 1) if len(ret) > p.pw_len+1: m1 = f'extraneous text {ret[p.pw_len+1]!r} found after password' m2 = '[bare comments not allowed in BIP39 password files]' diff --git a/mmgen/baseconv.py b/mmgen/baseconv.py index fdde5bf5..db94b52f 100755 --- a/mmgen/baseconv.py +++ b/mmgen/baseconv.py @@ -120,6 +120,10 @@ class baseconv: If 'seed', output length will be mapped from input length using data in seedlen_map. If an integer, the string, hex string or byte output will be padded to this length. """ + + def do_die(): + die('BaseConversionPadError', f"{pad!r}: illegal value for 'pad' (must be None, 'seed' or int)") + if pad is None: return 0 elif type(pad) is int: @@ -127,7 +131,7 @@ class baseconv: elif pad == 'seed': return seed_pad_func() else: - die('BaseConversionPadError', f"{pad!r}: illegal value for 'pad' (must be None, 'seed' or int)") + do_die() def tohex(self, words_arg, /, *, pad=None): "convert string or list data of instance base to a hexadecimal string" diff --git a/mmgen/filename.py b/mmgen/filename.py index 1a87269d..aa60e25b 100755 --- a/mmgen/filename.py +++ b/mmgen/filename.py @@ -42,7 +42,7 @@ class File: import stat if stat.S_ISBLK(st.st_mode): - if sys.platform in ('win32',): + if sys.platform == 'win32': die(2, 'Access to raw block devices not supported on platform {sys.platform!r}') mode = (os.O_RDONLY, os.O_RDWR)[bool(write)] try: diff --git a/mmgen/main_msg.py b/mmgen/main_msg.py index 0b3d4b08..fc58e8ff 100755 --- a/mmgen/main_msg.py +++ b/mmgen/main_msg.py @@ -207,23 +207,22 @@ if len(cmd_args) < 2: cfg._usage() op = cmd_args.pop(0) +arg1 = cmd_args.pop(0) if cfg.msghash_type and op != 'create': die(1, '--msghash-type option may only be used with the "create" command') async def main(): if op == 'create': - if len(cmd_args) < 2: + if not cmd_args: cfg._usage() - MsgOps.create(cmd_args[0], ' '.join(cmd_args[1:])) + MsgOps.create(arg1, ' '.join(cmd_args)) elif op == 'sign': - if len(cmd_args) < 1: - cfg._usage() - await MsgOps.sign(cmd_args[0], cmd_args[1:]) + await MsgOps.sign(arg1, cmd_args[:]) elif op in ('verify', 'export'): - if len(cmd_args) not in (1, 2): + if len(cmd_args) not in (0, 1): cfg._usage() - await getattr(MsgOps, op)(cmd_args[0], addr=cmd_args[1] if len(cmd_args) == 2 else None) + await getattr(MsgOps, op)(arg1, addr=cmd_args[0] if cmd_args else None) else: die(1, f'{op!r}: unrecognized operation') diff --git a/mmgen/main_regtest.py b/mmgen/main_regtest.py index c994fd01..4d9a4a2a 100755 --- a/mmgen/main_regtest.py +++ b/mmgen/main_regtest.py @@ -64,17 +64,17 @@ cmd_args = cfg._args from .proto.btc.regtest import MMGenRegtest def check_num_args(): - m = getattr(MMGenRegtest, cmd_args[0]) + cmd, *args = cmd_args + m = getattr(MMGenRegtest, cmd) margs = m.__code__.co_varnames[1:m.__code__.co_argcount] mdfls = m.__defaults__ or () amin = len(margs) - len(mdfls) amax = len(margs) - args = cmd_args[1:] - m = "{}: too {} arguments for command '%s' (must have no {} than {})" % cmd_args[0] + fs = '{}: too {} arguments for command ‘{}’ (must have no {} than {})' if len(args) < amin: - die(1, m.format(args, 'few', 'less', amin)) - elif len(cmd_args[1:]) > amax: - die(1, m.format(args, 'many', 'more', amax)) + die(1, fs.format(args, 'few', cmd, 'less', amin)) + elif len(args) > amax: + die(1, fs.format(args, 'many', cmd, 'more', amax)) if not cmd_args: cfg._usage() diff --git a/mmgen/proto/btc/tw/ctl.py b/mmgen/proto/btc/tw/ctl.py index 22475093..da9d8711 100755 --- a/mmgen/proto/btc/tw/ctl.py +++ b/mmgen/proto/btc/tw/ctl.py @@ -77,15 +77,13 @@ class BitcoinTwCtl(TwCtl): return b if res else tip def gen_chunks(start, stop, tip): - n = start if endless: stop = tip elif stop > tip: die(1, f'{stop}: stop value is higher than chain tip') - - while n <= stop: - yield (n, min(n+99, stop)) - n += 100 + while start <= stop: + yield (start, min(start + 99, stop)) + start += 100 last_block = await do_scan(gen_chunks(start, stop, self.rpc.blockcount), self.rpc.blockcount) diff --git a/test/clean.py b/test/clean.py index 4bb27c5a..1a9135f0 100755 --- a/test/clean.py +++ b/test/clean.py @@ -33,6 +33,7 @@ opts_data = { cfg = Config( opts_data = opts_data, + need_proto = False, init_opts = {'skip_cfg_file': True}) from test.overlay import get_overlay_tree_dir diff --git a/test/objtest.py b/test/objtest.py index 712c6b11..56138cc7 100755 --- a/test/objtest.py +++ b/test/objtest.py @@ -65,10 +65,9 @@ from test.include.common import set_globals set_globals(cfg) def run_test(mod, test, arg, input_data, arg1, exc_name): - arg_copy = arg - kwargs = {} - ret_chk = arg - ret_idx = None + + arg_copy, ret_chk, ret_idx, kwargs = (arg, arg, None, {}) + if input_data == 'good' and isinstance(arg, tuple): arg, ret_chk = arg if isinstance(arg, dict): # pass one arg + kwargs to constructor diff --git a/test/tooltest2.py b/test/tooltest2.py index 0a3db748..76d5c1bb 100755 --- a/test/tooltest2.py +++ b/test/tooltest2.py @@ -157,12 +157,13 @@ def check_output(out, chk): out = str(out).encode() if isinstance(out, str): out = out.encode() - err_fs = "Output ({!r}) doesn't match expected output ({!r})" try: outd = out.decode() except: outd = None + err_fs = "Output ({!r}) doesn't match expected output ({!r})" + if type(chk).__name__ == 'function': assert chk(outd), f'{chk.__name__}({outd}) failed!' elif isinstance(chk, dict):