From 8ce110e06912604f7fed880d88b255d3c3200dd6 Mon Sep 17 00:00:00 2001 From: Jordan Galby <67924449+Jordan474@users.noreply.github.com> Date: Wed, 6 Dec 2023 14:58:14 +0100 Subject: bootstrap: Don't catch Ctrl-C (#41449) --- lib/spack/llnl/util/lang.py | 9 ++++++--- lib/spack/spack/bootstrap/core.py | 4 ++-- lib/spack/spack/test/llnl/util/lang.py | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/spack/llnl/util/lang.py b/lib/spack/llnl/util/lang.py index 607b093de8..796aecc502 100644 --- a/lib/spack/llnl/util/lang.py +++ b/lib/spack/llnl/util/lang.py @@ -1047,9 +1047,9 @@ class GroupedExceptionHandler: """Whether any exceptions were handled.""" return bool(self.exceptions) - def forward(self, context: str) -> "GroupedExceptionForwarder": + def forward(self, context: str, base: type = BaseException) -> "GroupedExceptionForwarder": """Return a contextmanager which extracts tracebacks and prefixes a message.""" - return GroupedExceptionForwarder(context, self) + return GroupedExceptionForwarder(context, self, base) def _receive_forwarded(self, context: str, exc: Exception, tb: List[str]): self.exceptions.append((context, exc, tb)) @@ -1072,15 +1072,18 @@ class GroupedExceptionForwarder: """A contextmanager to capture exceptions and forward them to a GroupedExceptionHandler.""" - def __init__(self, context: str, handler: GroupedExceptionHandler): + def __init__(self, context: str, handler: GroupedExceptionHandler, base: type): self._context = context self._handler = handler + self._base = base def __enter__(self): return None def __exit__(self, exc_type, exc_value, tb): if exc_value is not None: + if not issubclass(exc_type, self._base): + return False self._handler._receive_forwarded(self._context, exc_value, traceback.format_tb(tb)) # Suppress any exception from being re-raised: diff --git a/lib/spack/spack/bootstrap/core.py b/lib/spack/spack/bootstrap/core.py index 5f73c7bfaf..5bfec8da6e 100644 --- a/lib/spack/spack/bootstrap/core.py +++ b/lib/spack/spack/bootstrap/core.py @@ -386,7 +386,7 @@ def ensure_module_importable_or_raise(module: str, abstract_spec: Optional[str] exception_handler = GroupedExceptionHandler() for current_config in bootstrapping_sources(): - with exception_handler.forward(current_config["name"]): + with exception_handler.forward(current_config["name"], Exception): source_is_enabled_or_raise(current_config) current_bootstrapper = create_bootstrapper(current_config) if current_bootstrapper.try_import(module, abstract_spec): @@ -441,7 +441,7 @@ def ensure_executables_in_path_or_raise( exception_handler = GroupedExceptionHandler() for current_config in bootstrapping_sources(): - with exception_handler.forward(current_config["name"]): + with exception_handler.forward(current_config["name"], Exception): source_is_enabled_or_raise(current_config) current_bootstrapper = create_bootstrapper(current_config) if current_bootstrapper.try_search_path(executables, abstract_spec): diff --git a/lib/spack/spack/test/llnl/util/lang.py b/lib/spack/spack/test/llnl/util/lang.py index 2cce9f155e..863a327844 100644 --- a/lib/spack/spack/test/llnl/util/lang.py +++ b/lib/spack/spack/test/llnl/util/lang.py @@ -321,3 +321,18 @@ line xxx, in test_grouped_exception """ ).format(__file__) ) + + +def test_grouped_exception_base_type(): + h = llnl.util.lang.GroupedExceptionHandler() + + with h.forward("catch-runtime-error", RuntimeError): + raise NotImplementedError() + + with pytest.raises(NotImplementedError): + with h.forward("catch-value-error", ValueError): + raise NotImplementedError() + + message = h.grouped_message(with_tracebacks=False) + assert "catch-runtime-error" in message + assert "catch-value-error" not in message -- cgit v1.2.3-60-g2f50