@@ -483,12 +483,15 @@ def __exit__(
483483 exc_type : type [BaseException ] | None ,
484484 exc_value : BaseException | None ,
485485 tb : TracebackType | None ,
486- ) -> None :
486+ ) -> bool | None :
487487 self ._depth -= 1
488+ exit_result : bool | None = None
488489 if self ._depth == 0 :
489- self .close ( )
490+ exit_result = self ._close_with_exception_info ( exc_type , exc_value , tb )
490491 pop_context ()
491492
493+ return exit_result
494+
492495 @contextmanager
493496 def scope (self , cleanup : bool = True ) -> cabc .Iterator [Context ]:
494497 """This helper method can be used with the context object to promote
@@ -615,10 +618,26 @@ def close(self) -> None:
615618 :meth:`call_on_close`, and exit all context managers entered
616619 with :meth:`with_resource`.
617620 """
618- self ._exit_stack .close ()
621+ self ._close_with_exception_info (None , None , None )
622+
623+ 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 exception
630+ information to allow for exception handling by the various resources registered
631+ using :meth;`with_resource`
632+
633+ :return: Whatever ``exit_stack.__exit__()`` returns.
634+ """
635+ exit_result = self ._exit_stack .__exit__ (exc_type , exc_value , tb )
619636 # In case the context is reused, create a new exit stack.
620637 self ._exit_stack = ExitStack ()
621638
639+ return exit_result
640+
622641 @property
623642 def command_path (self ) -> str :
624643 """The computed command path. This is used for the ``usage``
0 commit comments