From 79027884c7d59c5c4d9a0e0457c5a771ef27b918 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Mon, 6 Jan 2025 11:43:04 +0100 Subject: [PATCH] Add spack API warning with file/line (#48416) --- lib/spack/spack/error.py | 4 ++++ lib/spack/spack/main.py | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/spack/spack/error.py b/lib/spack/spack/error.py index 63dcc7c3909..92eb5f951ab 100644 --- a/lib/spack/spack/error.py +++ b/lib/spack/spack/error.py @@ -15,6 +15,10 @@ SHOW_BACKTRACE = False +class SpackAPIWarning(UserWarning): + """Warning that formats with file and line number.""" + + class SpackError(Exception): """This is the superclass for all Spack errors. Subclasses can be found in the modules they have to do with. diff --git a/lib/spack/spack/main.py b/lib/spack/spack/main.py index 02e3c4f6557..9e107ecfeb1 100644 --- a/lib/spack/spack/main.py +++ b/lib/spack/spack/main.py @@ -503,9 +503,12 @@ def make_argument_parser(**kwargs): return parser -def send_warning_to_tty(message, *args): +def send_warning_to_tty(message, category, filename, lineno, file=None, line=None): """Redirects messages to tty.warn.""" - tty.warn(message) + if category is spack.error.SpackAPIWarning: + tty.warn(f"{filename}:{lineno}: {message}") + else: + tty.warn(message) def setup_main_options(args):