Skip to content

Commit 3946358

Browse files
Copilotbittner
andcommitted
Improve handler cleanup by collecting handlers before removal
Co-authored-by: bittner <[email protected]>
1 parent 2f3ef3c commit 3946358

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

cli_test_helpers/decorators.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ def __exit__(self, exc_type, exc_val, exc_tb):
3535
"""Restore logging handlers after exiting the context."""
3636
# Close and remove any handlers that were added during the context
3737
if hasattr(self, "_old_handlers"):
38-
for handler in logging.root.handlers[:]:
39-
# Close handlers that weren't in the original list
40-
if handler not in self._old_handlers:
41-
handler.close()
42-
logging.root.handlers.remove(handler)
38+
# Collect handlers to close (those not in the original list)
39+
handlers_to_close = [
40+
handler for handler in logging.root.handlers
41+
if handler not in self._old_handlers
42+
]
43+
# Close and remove them
44+
for handler in handlers_to_close:
45+
handler.close()
46+
logging.root.handlers.remove(handler)
4347
# Restore the original handlers
4448
logging.root.handlers[:] = self._old_handlers
4549
# Call parent __exit__ if it exists (for cooperative inheritance)

0 commit comments

Comments
 (0)