From 2b7080c2279068e755f50a739961a0112861d88f Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 3 Oct 2025 10:34:04 +0000 Subject: [PATCH] subprocess.run(): use `text` arg (10 files) --- mmgen/color.py | 2 +- mmgen/daemon.py | 4 ++-- mmgen/platform/darwin/util.py | 3 ++- scripts/create-token.py | 4 ++-- test/cmdtest_d/ethdev.py | 15 ++++++--------- test/cmdtest_d/include/proxy.py | 10 +++++----- test/gentest.py | 4 ++-- test/include/common.py | 10 +++++----- test/modtest_d/dep.py | 6 +++--- test/tooltest.py | 5 +++-- 10 files changed, 31 insertions(+), 32 deletions(-) diff --git a/mmgen/color.py b/mmgen/color.py index 054b3ccb..95ac1535 100755 --- a/mmgen/color.py +++ b/mmgen/color.py @@ -61,7 +61,7 @@ def get_terminfo_colors(term=None): cmd.append(term) try: - cmdout = run(cmd, stdout=PIPE, check=True).stdout.decode() + cmdout = run(cmd, stdout=PIPE, check=True, text=True).stdout except: set_vt100() return None diff --git a/mmgen/daemon.py b/mmgen/daemon.py index 5dfff4d4..f54a0a75 100755 --- a/mmgen/daemon.py +++ b/mmgen/daemon.py @@ -247,14 +247,14 @@ class Daemon(Lockable): @classmethod def get_exec_version_str(cls): try: - cp = run([cls.exec_fn, cls.version_info_arg], stdout=PIPE, stderr=PIPE, check=True) + cp = run([cls.exec_fn, cls.version_info_arg], stdout=PIPE, stderr=PIPE, check=True, text=True) except Exception as e: die(2, f'{e}\nUnable to execute {cls.exec_fn}') if cp.returncode: die(2, f'Unable to execute {cls.exec_fn}') else: - res = cp.stdout.decode().splitlines() + res = cp.stdout.splitlines() return (res[0] if len(res) == 1 else [s for s in res if 'ersion' in s][0]).strip() class RPCDaemon(Daemon): diff --git a/mmgen/platform/darwin/util.py b/mmgen/platform/darwin/util.py index 4365fa21..0207c28b 100755 --- a/mmgen/platform/darwin/util.py +++ b/mmgen/platform/darwin/util.py @@ -73,8 +73,9 @@ class MacOSRamDisk: cp = run( ['hdiutil', 'attach', '-nomount', f'ram://{2048 * self.size}'], stdout = PIPE, + text = True, check = True) - self.dev_name = cp.stdout.decode().strip() + self.dev_name = cp.stdout.strip() self.cfg._util.qmsg(f'Created {self.desc} {self.label.hl()} [{self.dev_name}]') run(['diskutil', 'eraseVolume', 'APFS', self.label, self.dev_name], stdout=redir, check=True) diskutil_size = self.get_diskutil_size() diff --git a/scripts/create-token.py b/scripts/create-token.py index da72d6d6..c3a67292 100755 --- a/scripts/create-token.py +++ b/scripts/create-token.py @@ -100,7 +100,7 @@ def check_solc_version(): The output is used by other programs, so write to stdout only """ try: - cp = run(['solc', '--version'], check=True, stdout=PIPE) + cp = run(['solc', '--version'], check=True, text=True, stdout=PIPE) except: msg('solc missing or could not be executed') # this must go to stderr return False @@ -109,7 +109,7 @@ def check_solc_version(): Msg('solc exited with error') return False - line = cp.stdout.decode().splitlines()[1] + line = cp.stdout.splitlines()[1] version_str = re.sub(r'Version:\s*', '', line) m = re.match(r'(\d+)\.(\d+)\.(\d+)', version_str) diff --git a/test/cmdtest_d/ethdev.py b/test/cmdtest_d/ethdev.py index 9f1c1545..328a6230 100755 --- a/test/cmdtest_d/ethdev.py +++ b/test/cmdtest_d/ethdev.py @@ -829,9 +829,8 @@ class CmdTestEthdev(CmdTestEthdevMethods, CmdTestBase, CmdTestShared): write_to_file(pwfile, '') run(['rm', '-rf', self.keystore_dir]) cmd = f'geth account new --password={pwfile} --lightkdf --keystore {self.keystore_dir}' - cp = run(cmd.split(), stdout=PIPE, stderr=PIPE) - if cp.returncode: - die(1, cp.stderr.decode()) + if (cp := run(cmd.split(), stdout=PIPE, stderr=PIPE, text=True)).returncode: + die(1, cp.stderr) def make_genesis(signer_addr, prealloc_addr): return { @@ -875,9 +874,8 @@ class CmdTestEthdev(CmdTestEthdevMethods, CmdTestBase, CmdTestShared): def init_genesis(fn): cmd = f'{d.exec_fn} init --datadir {d.datadir} {fn}' - cp = run(cmd.split(), stdout=PIPE, stderr=PIPE) - if cp.returncode: - die(1, cp.stderr.decode()) + if (cp := run(cmd.split(), stdout=PIPE, stderr=PIPE, text=True)).returncode: + die(1, cp.stderr) d.stop(quiet=True) d.remove_datadir() @@ -1349,10 +1347,9 @@ class CmdTestEthdev(CmdTestEthdevMethods, CmdTestBase, CmdTestShared): '--outdir=' + odir ] + cmd_args + [self.proto.checksummed_addr(dfl_devaddr)] imsg('Executing: {}'.format(' '.join(cmd))) - cp = run(cmd, stdout=DEVNULL, stderr=PIPE) - if cp.returncode != 0: + if (cp := run(cmd, stdout=DEVNULL, stderr=PIPE, text=True)).returncode: rmsg('solc failed with the following output:') - die(2, cp.stderr.decode()) + die(2, cp.stderr) imsg('ERC20 token {!r} compiled'.format(token_data['symbol'])) return 'ok' diff --git a/test/cmdtest_d/include/proxy.py b/test/cmdtest_d/include/proxy.py index 81ef3100..1fc90275 100755 --- a/test/cmdtest_d/include/proxy.py +++ b/test/cmdtest_d/include/proxy.py @@ -76,14 +76,14 @@ class TestProxy: if port_in_use(self.port): omsg(f'Port {self.port} already in use. Assuming SSH SOCKS server is running') else: - cp = run(a + b0 + b1, stdout=PIPE, stderr=PIPE) - if err := cp.stderr.decode(): - omsg(err) + cp = run(a + b0 + b1, stdout=PIPE, stderr=PIPE, text=True) + if cp.stderr: + omsg(cp.stderr) if cp.returncode == 0: start_proxy() - elif 'onnection refused' in err: + elif 'onnection refused' in cp.stderr: die(2, fmt(self.no_ssh_errmsg, indent=' ')) - elif 'ermission denied' in err: + elif 'ermission denied' in cp.stderr: msg(fmt(self.bad_perm_errmsg.format(' '.join(a + b2)), indent=' ', strip_char='\t')) from mmgen.ui import keypress_confirm keypress_confirm(cfg, 'Continue?', do_exit=True) diff --git a/test/gentest.py b/test/gentest.py index 2f9e5dad..7a6f59be 100755 --- a/test/gentest.py +++ b/test/gentest.py @@ -138,7 +138,7 @@ SUPPORTED EXTERNAL TOOLS: } def get_cmd_output(cmd, input=None): - return run(cmd, input=input, stdout=PIPE, stderr=DEVNULL).stdout.decode().splitlines() + return run(cmd, input=input, stdout=PIPE, stderr=DEVNULL, text=True).stdout.splitlines() saved_results = {} @@ -201,7 +201,7 @@ class GenToolKeyconv(GenTool): class GenToolZcash_mini(GenTool): desc = 'zcash-mini' def run(self, sec, vcoin): - o = get_cmd_output(['zcash-mini', '-key', '-simple'], input=(sec.wif+'\n').encode()) + o = get_cmd_output(['zcash-mini', '-key', '-simple'], input=sec.wif+'\n') return gtr(o[1], o[0], o[-1]) class GenToolPycoin(GenTool): diff --git a/test/include/common.py b/test/include/common.py index 165199ad..fd0ad693 100755 --- a/test/include/common.py +++ b/test/include/common.py @@ -324,10 +324,10 @@ tested_solc_ver = '0.8.26' def check_solc_ver(): cmd = 'python3 scripts/create-token.py --check-solc-version' try: - cp = run(cmd.split(), check=False, stdout=PIPE) + cp = run(cmd.split(), check=False, stdout=PIPE, text=True) except Exception as e: die(4, f'Unable to execute {cmd!r}: {e}') - res = cp.stdout.decode().strip() + res = cp.stdout.strip() if cp.returncode == 0: omsg( orange(f'Found supported solc version {res}') if res == tested_solc_ver else @@ -352,7 +352,7 @@ def get_ethkey(): return None def do_run(cmd, check=True): - return run(cmd, stdout=PIPE, stderr=DEVNULL, check=check) + return run(cmd, stdout=PIPE, stderr=DEVNULL, check=check, text=True) def test_exec(cmd): try: @@ -459,10 +459,10 @@ class VirtBlockDeviceLinux(VirtBlockDeviceBase): def _get_associations(self): cmd = ['sudo', 'losetup', '-n', '-O', 'NAME', '-j', str(self.img_path)] - return do_run(cmd).stdout.decode().splitlines() + return do_run(cmd).stdout.splitlines() def get_new_dev(self): - return do_run(['sudo', 'losetup', '-f']).stdout.decode().strip() + return do_run(['sudo', 'losetup', '-f']).stdout.strip() def do_create(self, size, path): do_run(['truncate', f'--size={size}', str(path)]) diff --git a/test/modtest_d/dep.py b/test/modtest_d/dep.py index 4667dc24..56f66430 100755 --- a/test/modtest_d/dep.py +++ b/test/modtest_d/dep.py @@ -151,9 +151,9 @@ class unit_tests: '--stdout', init_proto(cfg, 'eth').checksummed_addr('deadbeef'*5), ] - cp = run(cmd, stdout=PIPE, stderr=PIPE) - vmsg(cp.stderr.decode()) + cp = run(cmd, stdout=PIPE, stderr=PIPE, text=True) + vmsg(cp.stderr) if cp.returncode: - msg(cp.stderr.decode()) + msg(cp.stderr) return False return True diff --git a/test/tooltest.py b/test/tooltest.py index 220e6a25..ba573064 100755 --- a/test/tooltest.py +++ b/test/tooltest.py @@ -189,8 +189,9 @@ if cfg.testing_status: 'tooltest2.py': run( ['python3', 'test/tooltest2.py', '--list-tested-cmds'], stdout = PIPE, + text = True, check = True - ).stdout.decode().split() + ).stdout.split() } for v in cmd_data.values(): tested_in['tooltest.py'] += list(v['cmd_data'].keys()) @@ -460,7 +461,7 @@ class MMGenToolTestCmds: test_msg('command piping') if cfg.verbose: sys.stderr.write(green('Executing ') + cyan(cmd) + '\n') - res = run(cmd, stdout=PIPE, shell=True).stdout.decode().strip() + res = run(cmd, stdout=PIPE, shell=True, text=True).stdout.strip() addr = read_from_tmpfile(tcfg, 'wif2addr3.out').strip() cmp_or_die(addr, res) ok()