Skip to content

Commit 7ae6e13

Browse files
Copilotbittner
andcommitted
Fix list reference issue in logging handler restoration
Co-authored-by: bittner <[email protected]>
1 parent 3ea222e commit 7ae6e13

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

cli_test_helpers/decorators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def __enter__(self):
3939

4040
def __exit__(self, exc_type, exc_val, exc_tb):
4141
sys.argv = self._old
42-
logging.root.handlers = self._old_handlers
42+
# Restore logging handlers by copying contents to preserve the list reference
43+
logging.root.handlers[:] = self._old_handlers
4344

4445

4546
class EnvironContext(patch.dict):

docs/tutorial.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ this approach with mocking a target function, e.g.
9292
9393
assert mock_command.called
9494
95+
``ArgvContext`` also provides automatic logging isolation. When test
96+
frameworks like pytest set up logging handlers, they can interfere with
97+
code under test that calls ``logging.basicConfig()``. ``ArgvContext``
98+
automatically saves and clears logging handlers when entering the context,
99+
then restores them when exiting, allowing your CLI code to configure
100+
logging as expected.
101+
95102
See more |example code (argparse-cli)|_.
96103

97104
``EnvironContext``

0 commit comments

Comments
 (0)