Skip to content

Commit

Permalink
bootstrap: Don't catch Ctrl-C (spack#41449)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan474 authored Dec 6, 2023
1 parent 90aee11 commit 8ce110e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
9 changes: 6 additions & 3 deletions lib/spack/llnl/util/lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,9 +1047,9 @@ def __bool__(self):
"""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))
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions lib/spack/spack/bootstrap/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
15 changes: 15 additions & 0 deletions lib/spack/spack/test/llnl/util/lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,18 @@ def inner():
"""
).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

0 comments on commit 8ce110e

Please sign in to comment.