obj.py: InitErrors: allow selected exceptions to be passed through

This commit is contained in:
The MMGen Project 2020-06-02 19:33:15 +00:00
commit c27c464a87
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 10 additions and 4 deletions

View file

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

View file

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

View file

@ -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']

View file

@ -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'},