From 73764a9e0d6f42158e57d0e93ed1ace3ecbfc99e Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 7 Aug 2026 09:37:47 +0000 Subject: [PATCH] ruff SIM115 (use context manager for opening files) --- mmgen/devtools.py | 3 ++- mmgen/tool/fileutil.py | 5 +---- pyproject.toml | 6 ++++-- test/modtest_d/devtools.py | 3 ++- test/modtest_d/rune.py | 6 ++++-- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/mmgen/devtools.py b/mmgen/devtools.py index 4e6c379f..6976b394 100755 --- a/mmgen/devtools.py +++ b/mmgen/devtools.py @@ -56,7 +56,8 @@ def Pexit(*args): def print_stack_trace(message=None, fh_list=[], nl='\n', sep='\n ', trim=4): if not fh_list: import os - fh_list.append(open(f'devtools.trace.{os.getpid()}', 'w')) + with open(f'devtools.trace.{os.getpid()}', 'w') as fh: + fh_list.append(fh) nl = '' res = get_stack_trace(message, nl, sep, trim) sys.stderr.write(res) diff --git a/mmgen/tool/fileutil.py b/mmgen/tool/fileutil.py index dea5c007..318787f3 100755 --- a/mmgen/tool/fileutil.py +++ b/mmgen/tool/fileutil.py @@ -113,8 +113,6 @@ class tool_cmd(tool_cmd_base): if self.cfg.outdir: outfile = make_full_path(self.cfg.outdir, outfile) - fh = open(outfile, 'wb') - blk_size = 1024 * 1024 key = Crypto(self.cfg).get_random(32) q1, q2 = (Queue(), Queue()) @@ -127,7 +125,7 @@ class tool_cmd(tool_cmd_base): for i in range(max(1, threads - 1)): Thread(target=encrypt_worker, daemon=True).start() - if True: + with open(outfile, 'wb') as fh: Thread(target=output_worker, args=(fh,), daemon=True).start() for i in range(nbytes // blk_size): @@ -140,7 +138,6 @@ class tool_cmd(tool_cmd_base): q1.join() q2.join() - fh.close() if (fsize := os.stat(outfile).st_size) != nbytes: die(3, f'{fsize}: incorrect random file size (should be {nbytes})') diff --git a/pyproject.toml b/pyproject.toml index 5f43eaf2..ebdaf61b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,6 @@ ignore = [ "S110", # `try`-`except`-`pass` detected, consider logging the exception "SIM102", # Use a single `if` statement instead of nested `if` statements "SIM114", # Combine `if` branches using logical `or` operator - "SIM115", # Use a context manager for opening files "SIM118", # Use `key in dict` instead of `key in dict.keys()` "SIM201", # Use `path.stat().st_mode & S_IWUSR | S_IRUSR != S_IWUSR | S_IRUSR` instead of `not path.stat().st_mode & S_IWUSR | S_IRUSR == S_IWUSR | S_IRUSR` "SIM210", # Use `bool(...)` instead of `True if ... else False` @@ -68,6 +67,10 @@ ignore = [ "F821", # undefined name 'cfg' "PLW0406" # self-import ] +"test/*" = [ + "SIM115", # open() with context manager + "ASYNC230" # open() in async func +] "test/misc/input_func.py" = [ "F401" ] # imported but unused "test/modtest_d/cashaddr.py" = [ "F841" ] # assigned to but never used "test/modtest_d/dep.py" = [ "F401" ] # imported but unused @@ -79,7 +82,6 @@ ignore = [ "test/*.py" = [ "F401" ] # imported but unused "test/colortest.py" = [ "F403", "F405" ] # `import *` used "test/tooltest2.py" = [ "F403", "F405" ] # `import *` used -"test/*" = [ "ASYNC230" ] # open() in async function "test/cmdtest_d/*" = [ "ASYNC251" ] # time.sleep() in async function "test/overlay/tree/*" = [ "ALL" ] diff --git a/test/modtest_d/devtools.py b/test/modtest_d/devtools.py index ed5ade67..3f25b258 100755 --- a/test/modtest_d/devtools.py +++ b/test/modtest_d/devtools.py @@ -70,7 +70,8 @@ class unit_tests: def stack_trace(self, name, ut): print_hdr('stack trace') - print_stack_trace('Test', fh_list=[open(os.devnull, 'w')], trim=0) + with open(os.devnull, 'w') as fh: + print_stack_trace('Test', fh_list=[fh], trim=0) return True def obj_pmsg(self, name, ut): diff --git a/test/modtest_d/rune.py b/test/modtest_d/rune.py index 5ad5d58d..54ddf0de 100755 --- a/test/modtest_d/rune.py +++ b/test/modtest_d/rune.py @@ -145,7 +145,8 @@ def test_tx(src, cfg, vec): match src: case 'parse': - tx_in = open(os.path.join('test/ref/thorchain', vec.fn), 'br').read() + with open(os.path.join('test/ref/thorchain', vec.fn), 'rb') as fh: + tx_in = fh.read() tx = RuneTx.loads(tx_in) if not parms.from_addr: ymsg(f'Warning: missing test vector data for {vec.fn}') @@ -215,7 +216,8 @@ class unit_tests: addr = 'thor1lukwlve7hayy66qrdkp4k7sh0emjqwergy7tl3' txhash = 'abcdef01' * 8 - txbytes = open('test/ref/thorchain/mainnet-tx-msgsend1.binpb', 'rb').read() + with open('test/ref/thorchain/mainnet-tx-msgsend1.binpb', 'rb') as fh: + txbytes = fh.read() async def main():