ruff SIM115 (use context manager for opening files)

This commit is contained in:
The MMGen Project 2026-08-07 09:37:47 +00:00
commit 73764a9e0d
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
5 changed files with 13 additions and 10 deletions

View file

@ -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)

View file

@ -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})')

View file

@ -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" ]

View file

@ -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):

View file

@ -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():