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. --- defconfig.py | 2 +- examples/merge_config.py | 2 +- genconfig.py | 2 +- kconfiglib.py | 36 ++++++++++++++++++++++++++---------- savedefconfig.py | 2 +- setconfig.py | 2 +- 6 files changed, 31 insertions(+), 15 deletions(-) diff --git a/defconfig.py b/defconfig.py index d1b1e4e..173fc7b 100755 --- a/defconfig.py +++ b/defconfig.py @@ -34,7 +34,7 @@ def main(): args = parser.parse_args() - kconf = kconfiglib.Kconfig(args.kconfig) + kconf = kconfiglib.Kconfig(args.kconfig, suppress_traceback=True) print(kconf.load_config(args.config)) print(kconf.write_config()) diff --git a/examples/merge_config.py b/examples/merge_config.py index 528e86c..2681b63 100755 --- a/examples/merge_config.py +++ b/examples/merge_config.py @@ -79,7 +79,7 @@ from kconfiglib import Kconfig, BOOL, TRISTATE, TRI_TO_STR if len(sys.argv) < 4: sys.exit("usage: merge_config.py Kconfig merged_config config1 [config2 ...]") -kconf = Kconfig(sys.argv[1]) +kconf = Kconfig(sys.argv[1], suppress_traceback=True) # Enable warnings for assignments to undefined symbols kconf.warn_assign_undef = True diff --git a/genconfig.py b/genconfig.py index 7d701ff..3482673 100755 --- a/genconfig.py +++ b/genconfig.py @@ -109,7 +109,7 @@ only supported for backwards compatibility). args = parser.parse_args() - kconf = kconfiglib.Kconfig(args.kconfig_filename) + kconf = kconfiglib.Kconfig(args.kconfig_filename, suppress_traceback=True) kconf.load_config() if args.header_path is None: 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(): diff --git a/savedefconfig.py b/savedefconfig.py index c388f1a..50850fa 100755 --- a/savedefconfig.py +++ b/savedefconfig.py @@ -40,7 +40,7 @@ def main(): args = parser.parse_args() - kconf = kconfiglib.Kconfig(args.kconfig) + kconf = kconfiglib.Kconfig(args.kconfig, suppress_traceback=True) print(kconf.load_config()) print(kconf.write_min_config(args.out)) diff --git a/setconfig.py b/setconfig.py index b689754..4e5c409 100755 --- a/setconfig.py +++ b/setconfig.py @@ -59,7 +59,7 @@ def main(): args = parser.parse_args() - kconf = kconfiglib.Kconfig(args.kconfig) + kconf = kconfiglib.Kconfig(args.kconfig, suppress_traceback=True) print(kconf.load_config()) for arg in args.assignments: -- cgit v1.2.3