Skip to content

Commit 2a3cf83

Browse files
committed
Move log cleanup to finally
1 parent 4192f51 commit 2a3cf83

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

annotated_logger/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ def annotate_logs(
653653
# Between the overloads and the two inner method definitions,
654654
# there's not much I can do to reduce the complexity more.
655655
# So, ignoring the complexity metric
656-
def annotate_logs( # noqa: C901
656+
def annotate_logs( # noqa: C901 PLR0915
657657
self,
658658
logger_name: str | None = None,
659659
*,
@@ -787,7 +787,8 @@ def wrap_function(*args: P.args, **kwargs: P.kwargs) -> R:
787787
if post_call and not post_call_attempted:
788788
_attempt_post_call(post_call, logger, *new_args, **new_kwargs) # pyright: ignore[reportCallIssue]
789789
raise
790-
logging.root.manager.loggerDict.pop(logger.logger.name, None)
790+
finally:
791+
logging.root.manager.loggerDict.pop(logger.logger.name, None)
791792
return result
792793

793794
return wrap_function

test/test_memory.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import contextlib
34
import logging
45

56
import pytest
@@ -10,17 +11,21 @@
1011

1112

1213
class TestMemory:
14+
@pytest.mark.parametrize("denominator", [2, 0])
1315
@pytest.mark.limit_memory("10 MB")
14-
def test_repeated_calls_do_not_accumulate_memory(self):
15-
calc = example.calculator.Calculator(1, 2)
16+
def test_repeated_calls_do_not_accumulate_memory(self, denominator):
17+
calc = example.calculator.Calculator(1, denominator)
1618
for _ in range(10000):
17-
calc.add()
19+
with contextlib.suppress(ZeroDivisionError):
20+
calc.divide()
1821

19-
def test_repeated_calls_do_not_accumulate_loggers(self):
20-
calc = example.calculator.Calculator(1, 2)
22+
@pytest.mark.parametrize("denominator", [2, 0])
23+
def test_repeated_calls_do_not_accumulate_loggers(self, denominator):
24+
calc = example.calculator.Calculator(1, denominator)
2125
starting_loggers = len(logging.root.manager.loggerDict)
2226
for _ in range(1000):
23-
calc.add()
27+
with contextlib.suppress(ZeroDivisionError):
28+
calc.divide()
2429

2530
ending_loggers = len(logging.root.manager.loggerDict)
2631
assert starting_loggers == ending_loggers

0 commit comments

Comments
 (0)