new MMGenSystemExit exception; remove rdie(), ydie()

This commit is contained in:
The MMGen Project 2022-02-05 13:32:56 +00:00
commit 41376eb515
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
33 changed files with 126 additions and 102 deletions

View file

@ -82,7 +82,7 @@ class Status(TxBase.Status):
elif await is_in_wallet():
die(0,f'Transaction has {r.confs} confirmation{suf(r.confs)}')
elif await is_in_utxos():
rdie(2,'ERROR: transaction is in the blockchain (but not in the tracking wallet)!')
die(4,'ERROR: transaction is in the blockchain (but not in the tracking wallet)!')
elif await is_replaced():
msg('Transaction has been replaced')
msg('Replacement transaction ' + (

View file

@ -135,7 +135,7 @@ if os.getenv('MMGEN_DEBUG') or os.getenv('MMGEN_TEST_SUITE') or os.getenv('MMGEN
def immutable_attr_init_check(self):
from .globalvars import g
if g.test_suite:
from .util import rdie
from .util import die
cls = type(self)
for attrname in sorted({a for a in self.valid_attrs if a[0] != '_'}):
for o in (cls,cls.__bases__[0]): # assume there's only one base class
@ -143,10 +143,10 @@ if os.getenv('MMGEN_DEBUG') or os.getenv('MMGEN_TEST_SUITE') or os.getenv('MMGEN
attr = o.__dict__[attrname]
break
else:
rdie(3,f'unable to find descriptor {cls.__name__}.{attrname}')
die(4,f'unable to find descriptor {cls.__name__}.{attrname}')
if type(attr).__name__ == 'ImmutableAttr':
if attrname not in self.__dict__:
rdie(3,
die(4,
f'attribute {attrname!r} of {cls.__name__} has not been initialized in constructor!')
def print_diff(a,b,from_file='',to_file='',from_json=True):

View file

@ -20,6 +20,19 @@
mmgen.exception: Exception classes for the MMGen suite
"""
class MMGenError(Exception):
def __init__(self,errno,strerror,stdout):
self.mmcode = errno
self.stdout = stdout
super().__init__(strerror)
def __repr__(self):
return f'{type(self).__name__}({self.mmcode}): {self}'
class MMGenSystemExit(MMGenError):
pass
# 1: no hl, message only
class UserNonConfirmation(Exception): mmcode = 1
class BadAgeFormat(Exception): mmcode = 1

View file

@ -57,7 +57,7 @@ def check_binary(args):
try:
run(args,stdout=DEVNULL,stderr=DEVNULL,check=True)
except:
rdie(2,f'{args[0]!r} binary missing, not in path, or not executable')
die(2,f'{args[0]!r} binary missing, not in path, or not executable')
def shred_file(fn,verbose=False):
check_binary(['shred','--version'])

View file

@ -89,7 +89,7 @@ class LEDControl:
fp.write(f'{init_val}\n')
return True
except PermissionError:
ydie(1,'\n'+fmt(f"""
die(2,'\n'+fmt(f"""
You do not have access to the {desc} file
To allow access, run the following command:

View file

@ -43,21 +43,35 @@ def launch(mod):
except EOFError:
sys.stderr.write('\nEnd of file\n')
except Exception as e:
if os.getenv('MMGEN_EXEC_WRAPPER'):
raise
else:
try: m = '{}'.format(e.args[0])
except: m = repr(e.args[0])
try:
errmsg = '{}'.format(e.args[0])
except:
errmsg = repr(e.args[0])
from .util import die,ydie,rdie
d = [ (ydie,2,'\nMMGen Unhandled Exception ({n}): {m}'),
(die, 1,'{m}'),
(ydie,2,'{m}'),
(ydie,3,'\nMMGen Error ({n}): {m}'),
(rdie,4,'\nMMGen Fatal Error ({n}): {m}')
][e.mmcode if hasattr(e,'mmcode') else 0]
from collections import namedtuple
from mmgen.color import nocolor,yellow,red
_o = namedtuple('exit_data',['color','exit_val','fs'])
d = {
1: _o(nocolor, 1, '{message}'),
2: _o(yellow, 2, '{message}'),
3: _o(yellow, 3, '\nMMGen Error ({name}): {message}'),
4: _o(red, 4, '\nMMGen Fatal Error ({name}): {message}'),
'x': _o(yellow, 5, '\nMMGen Unhandled Exception ({name}): {message}'),
}[getattr(e,'mmcode','x')]
(sys.stdout if getattr(e,'stdout',None) else sys.stderr).write(
d.color(d.fs.format(
name = type(e).__name__,
message = errmsg ))
+ '\n' )
sys.exit(d.exit_val)
d[0](d[1],d[2].format(n=type(e).__name__,m=m))
except SystemExit as e:
if os.getenv('MMGEN_EXEC_WRAPPER') and e.code != 0:
from mmgen.color import red

View file

@ -78,7 +78,7 @@ def parse_cmd_args(rpc,cmd_args):
al = (AddrList,KeyAddrList)[bool(opt.keyaddr_file)](proto,infile)
if al.al_id.mmtype in ('S','B'):
if not rpc.info('segwit_is_active'):
rdie(2,'Segwit is not active on this chain. Cannot import Segwit addresses')
die(2,'Segwit is not active on this chain. Cannot import Segwit addresses')
return al
if len(cmd_args) == 1:

View file

@ -164,7 +164,7 @@ async def check_daemons_running():
try:
await rpc_init(proto)
except SocketError as e:
ydie(1,f'{coin} daemon not running or not listening on port {proto.rpc_port}')
die(2,f'{coin} daemon not running or not listening on port {proto.rpc_port}')
def get_wallet_files():
try:

View file

@ -281,7 +281,7 @@ def process_args(cmd,cmd_args,cls):
return ( args, kwargs )
def process_result(ret,pager=False,print_result=False):
from .util import Msg,ydie,parse_bytespec
from .util import Msg,die,parse_bytespec
"""
Convert result to something suitable for output to screen and return it.
If result is bytes and not convertible to utf8, output as binary using os.write().
@ -294,7 +294,7 @@ def process_result(ret,pager=False,print_result=False):
if ret == True:
return True
elif ret in (False,None):
ydie(1,f'tool command returned {ret!r}')
die(2,f'tool command returned {ret!r}')
elif isinstance(ret,str):
return triage_result(ret)
elif isinstance(ret,int):
@ -312,7 +312,7 @@ def process_result(ret,pager=False,print_result=False):
else:
return ret
else:
ydie(1,f'tool.py: can’t handle return value of type {type(ret).__name__!r}')
die(2,f'tool.py: can’t handle return value of type {type(ret).__name__!r}')
def get_cmd_cls(cmd):
for modname,cmdlist in mods.items():

View file

@ -156,6 +156,6 @@ async def main():
bad_tx_count += 1
if bad_tx_count:
ydie(2,f'{bad_tx_count} transaction{suf(bad_tx_count)} could not be signed')
die(2,f'{bad_tx_count} transaction{suf(bad_tx_count)} could not be signed')
run_session(main())

View file

@ -69,7 +69,7 @@ class CoinProtocol(MMGenObject):
if 'tx' not in self.mmcaps and g.is_txprog:
from .util import die
die(1,f'Command {g.prog_name!r} not supported for coin {self.coin}')
die(2,f'Command {g.prog_name!r} not supported for coin {self.coin}')
if hasattr(self,'chain_names'):
self.chain_name = self.chain_names[0] # first chain name is default
@ -171,15 +171,14 @@ class CoinProtocol(MMGenObject):
if 0 < int.from_bytes(sec,'big') < self.secp256k1_ge:
return sec
else: # chance of this is less than 1 in 2^127
from .util import ydie
from .util import die,ymsg
pk = int.from_bytes(sec,'big')
if pk == 0: # chance of this is 1 in 2^256
ydie(3,'Private key is zero!')
die(4,'Private key is zero!')
elif pk == self.secp256k1_ge: # ditto
ydie(3,'Private key == secp256k1_ge!')
die(4,'Private key == secp256k1_ge!')
else:
if not g.test_suite:
from .util import ymsg
ymsg(f'Warning: private key is greater than secp256k1 group order!:\n {hexpriv}')
return (pk % self.secp256k1_ge).to_bytes(self.privkey_len,'big')

View file

@ -102,7 +102,7 @@ class MMGenRegtest(MMGenObject):
out = await self.rpc_call(*cmd_args,wallet='miner')
if len(out) != blocks:
rdie(1,'Error generating blocks')
die(4,'Error generating blocks')
gmsg(f'Mined {blocks} block{suf(blocks)}')

View file

@ -537,7 +537,7 @@ class BitcoinRPCClient(RPCClient,metaclass=AsyncInit):
await self.icall('createwallet',wallet_name=wname)
ymsg(f'Created {self.daemon.coind_name} wallet {wname!r}')
elif len(wallets) > 1: # support only one loaded wallet for now
rdie(2,f'ERROR: more than one {self.daemon.coind_name} wallet loaded: {wallets}')
die(4,f'ERROR: more than one {self.daemon.coind_name} wallet loaded: {wallets}')
wallet_checked.append(True)
def get_daemon_cfg_fn(self):
@ -665,7 +665,7 @@ class EthereumRPCClient(RPCClient,metaclass=AsyncInit):
import re
vip = re.match(self.daemon.version_pat,vi,re.ASCII)
if not vip:
ydie(1,fmt(f"""
die(2,fmt(f"""
Aborting on daemon mismatch:
Requested daemon: {self.daemon.id}
Running daemon: {vi}
@ -821,7 +821,7 @@ def handle_unsupported_daemon_version(rpc,name,warn_only):
daemon_warning('version',div=name,fmt_args=[rpc.daemon.coind_name])
else:
name = rpc.daemon.coind_name
rdie(1,'\n'+fmt(f"""
die(2,'\n'+fmt(f"""
The running {name} daemon has version {rpc.daemon_version_str}.
This version of MMGen is tested only on {name} v{rpc.daemon.coind_version_str} and below.

View file

@ -43,7 +43,8 @@ class TwAddrList(MMGenDict,TwCommon,metaclass=AsyncInit):
err = True
msg(f'Duplicate MMGen ID ({mmid}) discovered in tracking wallet!\n')
mmid_prev = mmid
if err: rdie(3,'Tracking wallet is corrupted!')
if err:
die(4,'Tracking wallet is corrupted!')
def check_addr_array_lens(acct_pairs):
err = False
@ -55,7 +56,8 @@ class TwAddrList(MMGenDict,TwCommon,metaclass=AsyncInit):
msg(f'Label {label!r}: has no associated address!')
else:
msg(f'{addrs!r}: more than one {proto.coin} address in account!')
if err: rdie(3,'Tracking wallet is corrupted!')
if err:
die(4,'Tracking wallet is corrupted!')
self.rpc = await rpc_init(proto)
self.total = proto.coin_amt('0')

View file

@ -17,7 +17,7 @@ from ..opts import opt
from .base import Base
from ..color import pink
from ..obj import get_obj,HexStr
from ..util import msg,qmsg,fmt,suf,remove_dups,get_extension,keypress_confirm,do_license_msg,line_input
from ..util import msg,qmsg,fmt,die,suf,remove_dups,get_extension,keypress_confirm,do_license_msg,line_input
from ..addr import is_mmgen_id,CoinAddr,is_coin_addr
def mmaddr2coinaddr(mmaddr,ad_w,ad_f,proto):
@ -53,9 +53,9 @@ def mmaddr2coinaddr(mmaddr,ad_w,ad_f,proto):
if not (opt.yes or keypress_confirm('Continue anyway?')):
sys.exit(1)
else:
ydie(2,wmsg('addr_not_found'))
die(2,wmsg('addr_not_found'))
else:
ydie(2,wmsg('addr_not_found_no_addrfile'))
die(2,wmsg('addr_not_found_no_addrfile'))
return CoinAddr(proto,coin_addr)
@ -208,7 +208,7 @@ class New(Base):
'ERROR: No change output specified' ))
if self.has_segwit_outputs() and not self.rpc.info('segwit_is_active'):
rdie(2,f'{g.proj_name} Segwit address requested on the command line, '
die(2,f'{g.proj_name} Segwit address requested on the command line, '
+ 'but Segwit is not active on this chain')
if not self.outputs:

View file

@ -120,11 +120,13 @@ def mdie(*args):
mmsg(*args)
sys.exit(0)
def die(ev=0,s=''):
def die(ev,s='',stdout=False):
assert isinstance(ev,int)
if s:
msg(s)
sys.exit(ev)
from .exception import MMGenSystemExit,MMGenError
if ev <= 2:
raise MMGenSystemExit(ev,s,stdout)
else:
raise MMGenError(ev,s,stdout)
def die_wait(delay,ev=0,s=''):
assert isinstance(delay,int)
@ -142,16 +144,7 @@ def die_pause(ev=0,s=''):
sys.exit(ev)
def Die(ev=0,s=''):
assert isinstance(ev,int)
if s:
Msg(s)
sys.exit(ev)
def rdie(ev=0,s=''):
die(ev,red(s))
def ydie(ev=0,s=''):
die(ev,yellow(s))
die(ev=ev,s=s,stdout=True)
def pp_fmt(d):
import pprint
@ -206,8 +199,7 @@ def remove_dups(iterable,edesc='element',desc='list',quiet=False,hide=False):
def exit_if_mswin(feature):
if g.platform == 'win':
m = capfirst(feature) + ' not supported on the MSWin / MSYS2 platform'
ydie(1,m)
die(2, capfirst(feature) + ' not supported on the MSWin / MSYS2 platform' )
def get_keccak():

View file

@ -243,7 +243,7 @@ def compile_code(code):
if cp.returncode != 0:
rmsg('Solidity compiler produced the following error:')
msg(err)
rdie(2,f'Solidity compiler exited with error (return val: {cp.returncode})')
die(4,f'Solidity compiler exited with error (return val: {cp.returncode})')
if err:
ymsg('Solidity compiler produced the following warning:')
msg(err)

View file

@ -32,7 +32,7 @@ def exec_wrapper_init(): # don't change: name is used to test if script is runni
except:
pass
def exec_wrapper_write_traceback():
def exec_wrapper_write_traceback(e):
import traceback,re
lines = traceback.format_exception(*sys.exc_info()) # returns a list
@ -45,7 +45,11 @@ def exec_wrapper_write_traceback():
lines.pop()
c = exec_wrapper_get_colors()
sys.stdout.write('{}{}'.format(c.yellow(''.join(lines)),c.red(exc)))
message = ( repr(e) if type(e).__name__ in ('MMGenError','MMGenSystemExit') else exc )
sys.stdout.write('{}{}'.format(
c.yellow( ''.join(lines) ),
c.red(message) )
+ '\n' )
with open('my.err','w') as fp:
fp.write(''.join(lines+[exc]))
@ -96,13 +100,13 @@ try:
exec(fp.read())
except SystemExit as e:
if e.code != 0 and not os.getenv('EXEC_WRAPPER_NO_TRACEBACK'):
exec_wrapper_write_traceback()
exec_wrapper_write_traceback(e)
else:
exec_wrapper_tracemalloc_log()
exec_wrapper_end_msg()
sys.exit(e.code)
except Exception as e:
exec_wrapper_write_traceback()
exec_wrapper_write_traceback(e)
retval = e.mmcode if hasattr(e,'mmcode') else e.code if hasattr(e,'code') else 1
sys.exit(retval)

View file

@ -318,7 +318,7 @@ def do_ab_test(proto,cfg,addr_type,gen1,kg2,ag,tool,cache_data):
kg1 = KeyGenerator( proto, addr_type.pubkey_type, gen1 )
if type(kg1) == type(kg2):
rdie(1,'Key generators are the same!')
die(4,'Key generators are the same!')
e = cinfo.get_entry(proto.coin,proto.network)
qmsg(green("Comparing address generators '{A}' and '{B}' for {N} {c} ({n}), addrtype {a!r}".format(

View file

@ -23,7 +23,7 @@ test/pexpect.py: pexpect implementation for MMGen test suites
import sys,os,time
from mmgen.globalvars import g
from mmgen.opts import opt
from mmgen.util import msg,msg_r,vmsg,vmsg_r,rmsg,red,yellow,green,cyan,die,rdie
from mmgen.util import msg,msg_r,vmsg,vmsg_r,rmsg,red,yellow,green,cyan,die
from .common import *
try:
@ -184,11 +184,12 @@ class MMGenPexpect(object):
f = (self.p.expect_exact,self.p.expect)[bool(regex)]
ret = f(s)
except pexpect.TIMEOUT:
if opt.debug_pexpect: raise
m1 = red(f'\nERROR. Expect {s!r} timed out. Exiting\n')
if opt.debug_pexpect:
raise
m1 = f'\nERROR. Expect {s!r} timed out. Exiting\n'
m2 = f'before: [{self.p.before}]\n'
m3 = f'sent value: [{self.sent_value}]' if self.sent_value != None else ''
rdie(1,m1+m2+m3)
die(2,m1+m2+m3)
debug_pexpect_msg(self.p)
@ -196,7 +197,7 @@ class MMGenPexpect(object):
msg_r(f' ==> {ret} ')
if ret == -1:
rdie(1,f'Error. Expect returned {ret}')
die(4,f'Error. Expect returned {ret}')
else:
if t == '':
if not nonl and not silent: vmsg('')

Some files were not shown because too many files have changed in this diff Show more