From 4f05c6b9f716f61d0958931f19392648a81075b6 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 10 Aug 2026 09:54:40 +0000 Subject: [PATCH] devtools: print_stack_trace(): fix --- mmgen/devtools.py | 9 ++++----- pyproject.toml | 1 + test/modtest_d/devtools.py | 6 ++++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/mmgen/devtools.py b/mmgen/devtools.py index 6976b394..f7a32ed7 100755 --- a/mmgen/devtools.py +++ b/mmgen/devtools.py @@ -53,15 +53,14 @@ def Pdie(*args, exit_val=1): def Pexit(*args): Pdie(*args, exit_val=0) -def print_stack_trace(message=None, fh_list=[], nl='\n', sep='\n ', trim=4): +def print_stack_trace(message=None, fh_list=[], sep='\n ', trim=4): + res = get_stack_trace(message, ('\n' if fh_list else ''), sep, trim) if not fh_list: import os - with open(f'devtools.trace.{os.getpid()}', 'w') as fh: - fh_list.append(fh) - nl = '' - res = get_stack_trace(message, nl, sep, trim) + fh_list.append(open(f'devtools.trace.{os.getpid()}', 'w')) sys.stderr.write(res) fh_list[0].write(res) + fh_list[0].flush() def get_stack_trace(message=None, nl='\n', sep='\n ', trim=3): diff --git a/pyproject.toml b/pyproject.toml index 865fa4be..58cf52d8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,6 +41,7 @@ ignore = [ "mmgen/tool/file.py" = [ "B008" ] # function call in dfl args "mmgen/tool/rpc.py" = [ "RUF013" ] # PEP 484 prohibits implicit `Optional` "mmgen/contrib/keccak.py" = [ "FURB163" ] # `math.log` -> `math.log2(state.lanew)` +"mmgen/devtools.py" = [ "SIM115" ] # open() with context manager "mmgen/proto/eth/rlp/*" = [ "TRY002", # create your own exception "UP004", # class `Binary` inherits from `object` diff --git a/test/modtest_d/devtools.py b/test/modtest_d/devtools.py index 3f25b258..e84f695d 100755 --- a/test/modtest_d/devtools.py +++ b/test/modtest_d/devtools.py @@ -70,8 +70,10 @@ class unit_tests: def stack_trace(self, name, ut): print_hdr('stack trace') - with open(os.devnull, 'w') as fh: - print_stack_trace('Test', fh_list=[fh], trim=0) + print_stack_trace('Test', trim=0) + print_stack_trace('Test2', trim=2) + with open(f'devtools.trace.{os.getpid()}', 'r') as fh: + assert len([l for l in fh if l.startswith('STACK TRACE')]) == 2 return True def obj_pmsg(self, name, ut):