From c27c464a876e8b167200fb26113986a496b754c6 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Tue, 2 Jun 2020 19:33:15 +0000 Subject: [PATCH] obj.py: InitErrors: allow selected exceptions to be passed through --- mmgen/obj.py | 5 ++++- mmgen/tw.py | 3 +-- test/objtest.py | 4 ++++ test/objtest_py_d/ot_btc_mainnet.py | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/mmgen/obj.py b/mmgen/obj.py index ef4a388e..30bf1a9a 100755 --- a/mmgen/obj.py +++ b/mmgen/obj.py @@ -133,7 +133,9 @@ class InitErrors: if m2: errmsg = repr(m2) + '\n' + errmsg - if hasattr(cls,'exc'): + if hasattr(cls,'passthru_excs') and type(e) in cls.passthru_excs: + raise + elif hasattr(cls,'exc'): raise cls.exc(errmsg) else: raise ObjectInitError(errmsg) @@ -684,6 +686,7 @@ class TwMMGenID(str,Hilite,InitErrors,MMGenObject): # non-displaying container for TwMMGenID,TwComment class TwLabel(str,InitErrors,MMGenObject): exc = BadTwLabel + passthru_excs = (BadTwComment,) def __new__(cls,proto,text): if type(text) == cls: return text diff --git a/mmgen/tw.py b/mmgen/tw.py index ca4bb825..f064829f 100755 --- a/mmgen/tw.py +++ b/mmgen/tw.py @@ -33,8 +33,7 @@ def CUR_RIGHT(n): return '\033[{}C'.format(n) def get_tw_label(proto,s): """ - We must fail only on malformed comment, not on empty label. - Otherwise, listaddresses showempty=1 will fail. + raise an exception on a malformed comment, return None on an empty or invalid label """ try: return TwLabel(proto,s) diff --git a/test/objtest.py b/test/objtest.py index 2e8e2887..785c98ac 100755 --- a/test/objtest.py +++ b/test/objtest.py @@ -72,6 +72,10 @@ def run_test(test,arg,input_data,arg1,exc_name): ret_chk = arg['ret'] del arg['ret'] del arg_copy['ret'] + if 'exc_name' in arg: + exc_name = arg['exc_name'] + del arg['exc_name'] + del arg_copy['exc_name'] if 'ret_idx' in arg: ret_idx = arg['ret_idx'] del arg['ret_idx'] diff --git a/test/objtest_py_d/ot_btc_mainnet.py b/test/objtest_py_d/ot_btc_mainnet.py index 2ecf97ec..828efb33 100755 --- a/test/objtest_py_d/ot_btc_mainnet.py +++ b/test/objtest_py_d/ot_btc_mainnet.py @@ -181,7 +181,7 @@ tests = { {'text':tw_pfx+' x', 'proto':proto}, {'text':tw_pfx+'я x', 'proto':proto}, {'text':utf8_ctrl[:40], 'proto':proto}, - {'text':'F00BAA12:S:1 '+ utf8_ctrl[:40], 'proto':proto, }, + {'text':'F00BAA12:S:1 ' + utf8_ctrl[:40], 'proto':proto, 'exc_name': 'BadTwComment'}, ), 'good': ( {'text':'F00BAA12:99 a comment', 'proto':proto, 'ret':'F00BAA12:L:99 a comment'},