Skip to content

Commit e4ff76e

Browse files
Copilotbittner
andcommitted
Add defensive check and improve exception safety in logging restoration
Co-authored-by: bittner <[email protected]>
1 parent 99a1a8a commit e4ff76e

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

cli_test_helpers/decorators.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def _save_logging_handlers(self):
3333

3434
def _restore_logging_handlers(self):
3535
"""Restore previously saved logging handlers."""
36-
logging.root.handlers[:] = self._old_handlers
36+
if hasattr(self, '_old_handlers'):
37+
logging.root.handlers[:] = self._old_handlers
3738

3839

3940
class ArgvContext(LoggingIsolationMixin):
@@ -90,9 +91,8 @@ def __enter__(self):
9091
return self
9192

9293
def __exit__(self, exc_type, exc_val, exc_tb):
93-
result = super().__exit__(exc_type, exc_val, exc_tb)
9494
self._restore_logging_handlers()
95-
return result
95+
return super().__exit__(exc_type, exc_val, exc_tb)
9696

9797

9898
class RandomDirectoryContext(LoggingIsolationMixin, TemporaryDirectory):
@@ -120,7 +120,6 @@ def __enter__(self):
120120

121121
def __exit__(self, exc_type, exc_value, traceback):
122122
"""Return to the original directory before execution."""
123-
os.chdir(self.__prev_dir)
124-
result = super().__exit__(exc_type, exc_value, traceback)
125123
self._restore_logging_handlers()
126-
return result
124+
os.chdir(self.__prev_dir)
125+
return super().__exit__(exc_type, exc_value, traceback)

0 commit comments

Comments
 (0)