devtools: print_stack_trace(): fix

This commit is contained in:
The MMGen Project 2026-08-10 09:54:40 +00:00
commit 4f05c6b9f7
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
3 changed files with 9 additions and 7 deletions

View file

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

View file

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

View file

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