minor fixes and cleanups

This commit is contained in:
The MMGen Project 2021-09-03 20:17:24 +00:00
commit 424c9f1593
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
3 changed files with 30 additions and 24 deletions

View file

@ -27,7 +27,12 @@ if os.getenv('MMGEN_DEBUG') or os.getenv('MMGEN_TEST_SUITE') or os.getenv('MMGEN
def Pexit(*args):
pexit(*args,out=sys.stdout)
def print_stack_trace(message=None):
def print_stack_trace(message=None,fh=[],nl='\n',sep='\n '):
if not fh:
fh.append(open(f'devtools.trace.{os.getpid()}','w'))
nl = ''
tb = [t for t in traceback.extract_stack() if t.filename[:1] != '<'][:-1]
fs = '{}:{}: in {}:\n {}'
out = [
@ -38,11 +43,9 @@ if os.getenv('MMGEN_DEBUG') or os.getenv('MMGEN_TEST_SUITE') or os.getenv('MMGEN
t.line or '(none)')
for t in tb ]
sys.stderr.write(
'STACK TRACE {}:\n '.format(message or '[unnamed]') +
'\n '.join(out) + '\n' )
open('devtools.trace','w').write('\n'.join(out))
text = f'{nl}STACK TRACE {message or "[unnamed]"}:{sep}{sep.join(out)}\n'
sys.stderr.write(text)
fh[0].write(text)
class MMGenObject(object):

View file

@ -504,7 +504,7 @@ class CoinAmt(Decimal,Hilite,InitErrors): # abstract class
def fmt(self,fs=None,color=False,suf='',prec=1000):
if fs == None:
fs = self.amt_fs
s = str(int(self)) if int(self) == self else self.normalize().__format__('f')
s = self.__str__()
if '.' in fs:
p1,p2 = list(map(int,fs.split('.',1)))
ss = s.split('.',1)
@ -518,13 +518,10 @@ class CoinAmt(Decimal,Hilite,InitErrors): # abstract class
return self.colorize(ret,color=color)
def hl(self,color=True):
return self.__str__(color=color)
return self.colorize(self.__str__(),color=color)
def __str__(self,color=False): # format simply, no exponential notation
return self.colorize(
str(int(self)) if int(self) == self else
self.normalize().__format__('f'),
color=color)
def __str__(self): # format simply, with no exponential notation
return str(int(self)) if int(self) == self else self.normalize().__format__('f')
def __repr__(self):
return "{}('{}')".format(type(self).__name__,self.__str__())
@ -533,7 +530,7 @@ class CoinAmt(Decimal,Hilite,InitErrors): # abstract class
"""
we must allow other to be int(0) to use the sum() builtin
"""
if other != 0 and type(other) not in ( type(self), DecimalNegateResult ):
if type(other) not in ( type(self), DecimalNegateResult ) and other != 0:
raise ValueError(
f'operand {other} of incorrect type ({type(other).__name__} != {type(self).__name__})')
return type(self)(Decimal.__add__(self,other,*args,**kwargs))
@ -558,7 +555,7 @@ class CoinAmt(Decimal,Hilite,InitErrors): # abstract class
def __mul__(self,other,*args,**kwargs):
return type(self)('{:0.{p}f}'.format(
Decimal.__mul__(self,Decimal(other,*args,**kwargs),*args,**kwargs),
Decimal.__mul__(self,Decimal(other),*args,**kwargs),
p = self.max_prec
))
@ -566,7 +563,7 @@ class CoinAmt(Decimal,Hilite,InitErrors): # abstract class
def __truediv__(self,other,*args,**kwargs):
return type(self)('{:0.{p}f}'.format(
Decimal.__truediv__(self,Decimal(other,*args,**kwargs),*args,**kwargs),
Decimal.__truediv__(self,Decimal(other),*args,**kwargs),
p = self.max_prec
))

View file

@ -4,11 +4,6 @@
REFDIR='test/ref'
SUDO='sudo'
[ -e "$CORE_REPO_ROOT/src/test/data/tx_valid.json" ] || {
echo "CORE_REPO_ROOT not set, or does not point to Bitcoin Core repository"
exit 1
}
if [ "$(uname -m)" == 'armv7l' ]; then
ARM32=1
elif uname -a | grep -q 'MSYS'; then
@ -175,9 +170,9 @@ check() {
[ "$BRANCH" ] || { echo 'No branch specified. Exiting'; exit; }
[ "$(git diff $BRANCH)" == "" ] || {
echo "Unmerged changes from branch '$BRANCH'. Exiting"
exit
exit 1
}
git diff $BRANCH >/dev/null 2>&1 || exit
git diff $BRANCH >/dev/null 2>&1 || exit 1
}
uninstall() {
set +e
@ -558,12 +553,23 @@ run_tests() {
done
}
check_core_repo() {
[ -e "$CORE_REPO_ROOT/src/test/data/tx_valid.json" ] || {
echo "CORE_REPO_ROOT not set, or does not point to Bitcoin Core repository"
exit 1
}
}
check_args() {
for i in $tests; do
echo "$dfl_tests $extra_tests" | grep -q "\<$i\>" || { echo "$i: unrecognized argument"; exit; }
echo "$dfl_tests $extra_tests" | grep -q "\<$i\>" || {
echo "$i: unrecognized argument"
exit 1
}
done
}
check_core_repo
check_args
[ "$LIST_CMDS" ] || echo "Running tests: $tests"
START=$(date +%s)