From 1ec91703ecdb4c369212fabe2ca0a68f040b7697 Mon Sep 17 00:00:00 2001 From: Ulf Magnusson Date: Sat, 14 Dec 2019 18:04:09 +0100 Subject: Add Kconfig.__init__() helper flag for suppressing tracebacks Tools that don't use standard_kconfig() currently generate spammy tracebacks for e.g. syntax errors. Add a suppress_traceback flag to Kconfig.__init__() for catching "expected" exceptions and printing them to stderr and exiting with status 1. Use it to make all tools consistently hide tracebacks. --- kconfiglib.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'kconfiglib.py') diff --git a/kconfiglib.py b/kconfiglib.py index 6199e8b..d152b76 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -865,7 +865,7 @@ class Kconfig(object): # def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True, - encoding="utf-8"): + encoding="utf-8", suppress_traceback=False): """ Creates a new Kconfig object by parsing Kconfig files. Note that Kconfig files are not the same as .config files (which store @@ -930,7 +930,31 @@ class Kconfig(object): anyway. Related PEP: https://www.python.org/dev/peps/pep-0538/ + + suppress_traceback (default: False): + Helper for tools. When True, any EnvironmentError or KconfigError + generated during parsing is caught, the exception message is printed + to stderr, and sys.exit(1) is called (which generates SystemExit). + + This hides the Python traceback for "expected" errors like syntax + errors in Kconfig files. + + Other exceptions besides EnvironmentError and KconfigError are still + propagated when suppress_traceback is True. """ + try: + return self._init(filename, warn, warn_to_stderr, encoding) + except (EnvironmentError, KconfigError) as e: + if suppress_traceback: + # Some long exception messages have extra newlines for better + # formatting when reported as an unhandled exception. Strip + # them here. + sys.exit(str(e).strip()) + raise + + def _init(self, filename, warn, warn_to_stderr, encoding): + # See __init__() + self._encoding = encoding self.srctree = os.getenv("srctree", "") @@ -6182,15 +6206,7 @@ def standard_kconfig(description=None): nargs="?", help="Kconfig file (default: Kconfig)") - args = parser.parse_args() - - # Suppress backtraces for expected exceptions - try: - return Kconfig(args.kconfig) - except (EnvironmentError, KconfigError) as e: - # Some long exception messages have extra newlines for better - # formatting when reported as an unhandled exception. Strip them here. - sys.exit(str(e).strip()) + return Kconfig(parser.parse_args().kconfig, suppress_traceback=True) def standard_config_filename(): -- cgit v1.2.3