summaryrefslogtreecommitdiff
path: root/kconfiglib.py
diff options
context:
space:
mode:
authorUlf Magnusson <ulfalizer@gmail.com>2019-12-14 18:04:09 +0100
committerUlf Magnusson <ulfalizer@gmail.com>2019-12-14 18:12:07 +0100
commit1ec91703ecdb4c369212fabe2ca0a68f040b7697 (patch)
treeda0d7e8233511dd4face9d35b23add875b86e8e3 /kconfiglib.py
parentc2414bdf64c03470061d280a7d1b5ea97bab3185 (diff)
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.
Diffstat (limited to 'kconfiglib.py')
-rw-r--r--kconfiglib.py36
1 files changed, 26 insertions, 10 deletions
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():