From 3c039f42a066a9ae43789bcce3b2dd14ba077d82 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Fri, 26 Nov 2021 11:21:22 -0500 Subject: [PATCH] report the context, if possible, on python tracebacks The interpreter tries to catch any exception and add the latest node information to it, but currently we only used that to print better formatted error messages on MesonException. Since we should theoretically have that property for most/all exceptions, let's percolate that upward, and message the user that an unexpected traceback was encountered, that it should be reported as a bug, and the helpful information of "how far into parsing this meson.build did we get before erroring out, anyway?" --- mesonbuild/mesonmain.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mesonbuild/mesonmain.py b/mesonbuild/mesonmain.py index a60f914de..06f589be9 100644 --- a/mesonbuild/mesonmain.py +++ b/mesonbuild/mesonmain.py @@ -27,7 +27,7 @@ import shutil from . import mesonlib from . import mlog from . import mconf, mdist, minit, minstall, mintro, msetup, mtest, rewriter, msubprojects, munstable_coredata, mcompile, mdevenv -from .mesonlib import MesonException +from .mesonlib import MesonException, MesonBugException from .environment import detect_msys2_arch from .wrap import wraptool @@ -144,10 +144,16 @@ class CommandLineParser: if os.environ.get('MESON_FORCE_BACKTRACE'): raise return 1 - except Exception: + except Exception as e: if os.environ.get('MESON_FORCE_BACKTRACE'): raise traceback.print_exc() + msg = 'Unhandled python exception' + if all(getattr(e, a, None) is not None for a in ['file', 'lineno', 'colno']): + e = MesonBugException(msg, e.file, e.lineno, e.colno) # type: ignore + else: + e = MesonBugException(msg) + mlog.exception(e) return 2 finally: mlog.shutdown()