Skip to content
Myr

Forward exception information to resources registered in a context (#3058)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Rowlando13 · committed by GitHub · parents 16fe802, f2cae7a · Browse files · 3 files+151 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+151 added−3 removed

  • tests:+127 added−0 removed
  • other:+24 added−3 removed

3 files+151 added−3 removed

modified:src/click/core.py+22 added-3 removed
481 unmodified lines
482 self,483 exc_type: type[BaseException] | None,484 exc_value: BaseException | None,485 tb: TracebackType | None,486 ) -> None:486 ) -> bool | None:487 self._depth -= 1488 exit_result: bool | None = None489 if self._depth == 0:489 self.close()490 exit_result = self._close_with_exception_info(exc_type, exc_value, tb)491 pop_context()492493 return exit_result494495 @contextmanager496 def scope(self, cleanup: bool = True) -> cabc.Iterator[Context]:497 """This helper method can be used with the context object to promote498 it to the current thread local (see :func:`get_current_context`).
118 unmodified lines
617 """Invoke all close callbacks registered with618 :meth:`call_on_close`, and exit all context managers entered619 with :meth:`with_resource`.620 """618 self._exit_stack.close()621 self._close_with_exception_info(None, None, None)622623 def _close_with_exception_info(624 self,625 exc_type: type[BaseException] | None,626 exc_value: BaseException | None,627 tb: TracebackType | None,628 ) -> bool | None:629 """Unwind the exit stack by calling its :meth:`__exit__` providing the exception630 information to allow for exception handling by the various resources registered631 using :meth;`with_resource`632633 :return: Whatever ``exit_stack.__exit__()`` returns.634 """635 exit_result = self._exit_stack.__exit__(exc_type, exc_value, tb)636 # In case the context is reused, create a new exit stack.637 self._exit_stack = ExitStack()638639 return exit_result640641 @property642 def command_path(self) -> str:643 """The computed command path. This is used for the ``usage``644 information on the help page. It's automatically created by
2703 unmodified lines
modified:tests/test_context.py+127 added
1import logging2from contextlib import AbstractContextManager3from contextlib import contextmanager4from types import TracebackType56import pytest78import click
415 unmodified lines
424425 assert rv == [0]426427428def test_with_resource_exception() -> None:429 class TestContext(AbstractContextManager[list[int]]):430 _handle_exception: bool431 _base_val: int432 val: list[int]433434 def __init__(self, base_val: int = 1, *, handle_exception: bool = True) -> None:435 self._handle_exception = handle_exception436 self._base_val = base_val437438 def __enter__(self) -> list[int]:439 self.val = [self._base_val]440 return self.val441442 def __exit__(443 self,444 exc_type: type[BaseException] | None,445 exc_value: BaseException | None,446 traceback: TracebackType | None,447 ) -> bool | None:448 if not exc_type:449 self.val[0] = self._base_val - 1450 return None451452 self.val[0] = self._base_val + 1453 return self._handle_exception454455 class TestException(Exception):456 pass457458 ctx = click.Context(click.Command("test"))459460 base_val = 1461462 with ctx.scope():463 rv = ctx.with_resource(TestContext(base_val=base_val))464 assert rv[0] == base_val465466 assert rv == [base_val - 1]467468 with ctx.scope():469 rv = ctx.with_resource(TestContext(base_val=base_val))470 raise TestException()471472 assert rv == [base_val + 1]473474 with pytest.raises(TestException):475 with ctx.scope():476 rv = ctx.with_resource(477 TestContext(base_val=base_val, handle_exception=False)478 )479 raise TestException()480481482def test_with_resource_nested_exception() -> None:483 class TestContext(AbstractContextManager[list[int]]):484 _handle_exception: bool485 _base_val: int486 val: list[int]487488 def __init__(self, base_val: int = 1, *, handle_exception: bool = True) -> None:489 self._handle_exception = handle_exception490 self._base_val = base_val491492 def __enter__(self) -> list[int]:493 self.val = [self._base_val]494 return self.val495496 def __exit__(497 self,498 exc_type: type[BaseException] | None,499 exc_value: BaseException | None,500 traceback: TracebackType | None,501 ) -> bool | None:502 if not exc_type:503 self.val[0] = self._base_val - 1504 return None505506 self.val[0] = self._base_val + 1507 return self._handle_exception508509 class TestException(Exception):510 pass511512 ctx = click.Context(click.Command("test"))513 base_val = 1514 base_val_nested = 11515516 with ctx.scope():517 rv = ctx.with_resource(TestContext(base_val=base_val))518 rv_nested = ctx.with_resource(TestContext(base_val=base_val_nested))519 assert rv[0] == base_val520 assert rv_nested[0] == base_val_nested521522 assert rv == [base_val - 1]523 assert rv_nested == [base_val_nested - 1]524525 with ctx.scope():526 rv = ctx.with_resource(TestContext(base_val=base_val))527 rv_nested = ctx.with_resource(TestContext(base_val=base_val_nested))528 raise TestException()529530 # If one of the context "eats" the exceptions they will not be forwarded to other531 # parts. This is due to how ExitStack unwinding works532 assert rv_nested == [base_val_nested + 1]533 assert rv == [base_val - 1]534535 with ctx.scope():536 rv = ctx.with_resource(TestContext(base_val=base_val))537 rv_nested = ctx.with_resource(538 TestContext(base_val=base_val_nested, handle_exception=False)539 )540 raise TestException()541542 assert rv_nested == [base_val_nested + 1]543 assert rv == [base_val + 1]544545 with pytest.raises(TestException):546 rv = ctx.with_resource(TestContext(base_val=base_val, handle_exception=False))547 rv_nested = ctx.with_resource(548 TestContext(base_val=base_val_nested, handle_exception=False)549 )550 raise TestException()551552553def test_make_pass_decorator_args(runner):554 """555 Test to check that make_pass_decorator doesn't consume arguments based on556 invocation order.
116 unmodified lines
modified:CHANGES.rst+2 added
20 unmodified lines
21 option :issue:`2813`22- Show correct auto complete value for nargs option in combination with flag option :issue:`2813`23- Fix handling of quoted and escaped parameters in Fish autocompletion. :issue:`2995` :pr:`3013`24- Lazily import ``shutil``. :pr:`3023`25- Properly forward exception information to resources registered with26 ``click.core.Context.with_resource()``. :issue:`2447` :pr:`3058`2728Version 8.2.229-------------30
1262 unmodified lines