From 7b0d527430ac0de6c221552e6b63d22936641a28 Mon Sep 17 00:00:00 2001 From: Pawel Lewicki Date: Thu, 7 Nov 2024 22:44:36 +0100 Subject: [PATCH] fix: proper error handling on __exit__ and __aexit__ of Container --- src/svcs/_core.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/svcs/_core.py b/src/svcs/_core.py index c9d1342..4e57b21 100644 --- a/src/svcs/_core.py +++ b/src/svcs/_core.py @@ -541,7 +541,7 @@ def __exit__( exc_val: BaseException | None, exc_tb: TracebackType | None, ) -> None: - self.close() + self.close(exc_type, exc_val, exc_tb) async def __aenter__(self) -> Container: return self @@ -552,7 +552,7 @@ async def __aexit__( exc_val: BaseException | None, exc_tb: TracebackType | None, ) -> None: - await self.aclose() + await self.aclose(exc_type, exc_val, exc_tb) def __del__(self) -> None: """ @@ -565,7 +565,12 @@ def __del__(self) -> None: stacklevel=1, ) - def close(self) -> None: + def close( + self, + exc_type: type[BaseException] | None = None, + exc_val: BaseException | None = None, + exc_tb: TracebackType | None = None, + ) -> None: """ Run all registered *synchronous* cleanups. @@ -589,7 +594,7 @@ def close(self) -> None: ) continue - cm.__exit__(None, None, None) + cm.__exit__(exc_type, exc_val, exc_tb) except Exception: # noqa: BLE001 log.warning( "Container clean up failed for %r.", @@ -603,7 +608,12 @@ def close(self) -> None: self._on_close.clear() self._instantiated.clear() - async def aclose(self) -> None: + async def aclose( + self, + exc_type: type[BaseException] | None = None, + exc_val: BaseException | None = None, + exc_tb: TracebackType | None = None, + ) -> None: """ Run *all* registered cleanups -- synchronous **and** asynchronous. @@ -619,9 +629,9 @@ async def aclose(self) -> None: for name, cm in reversed(self._on_close): try: if isinstance(cm, AbstractContextManager): - cm.__exit__(None, None, None) + cm.__exit__(exc_type, exc_val, exc_tb) else: - await cm.__aexit__(None, None, None) + await cm.__aexit__(exc_type, exc_val, exc_tb) except Exception: # noqa: BLE001, PERF203 log.warning(