From 393e0b4339ca843b0bf2c77d77104b23f19efbc1 Mon Sep 17 00:00:00 2001 From: Danijar Hafner Date: Sun, 11 Aug 2019 11:27:32 -0400 Subject: [PATCH] Add no new logging handlers when reloading the module. User @epogrebnyak found the bug. Fixes #11. Closes #27. --- handout/__init__.py | 7 ++++--- handout/handout.py | 3 +++ handout/tests/__init__.py | 0 handout/tests/test_handout.py | 6 ++++-- 4 files changed, 11 insertions(+), 5 deletions(-) delete mode 100644 handout/tests/__init__.py diff --git a/handout/__init__.py b/handout/__init__.py index de017c7..3b1aa59 100644 --- a/handout/__init__.py +++ b/handout/__init__.py @@ -8,6 +8,7 @@ logger = logging.getLogger('handout') logger.setLevel(logging.INFO) logger.propagate = False # Global logger should not print messages again. -handler = logging.StreamHandler(sys.stdout) -handler.setFormatter(logging.Formatter('%(message)s')) -logger.addHandler(handler) +if not logger.handlers: # Reloading the module should not add another handler. + handler = logging.StreamHandler(sys.stdout) + handler.setFormatter(logging.Formatter('%(message)s')) + logger.addHandler(handler) diff --git a/handout/handout.py b/handout/handout.py index 4f9b087..b27dad0 100644 --- a/handout/handout.py +++ b/handout/handout.py @@ -16,6 +16,9 @@ def __init__(self, directory, title='Handout'): self._title = title self._blocks = collections.defaultdict(list) self._pending = [] + # The logger is configured in handout/__init__.py to make it available + # right after importing the handout package. This allows the user to change + # the logging level, message format, etc. self._logger = logging.getLogger('handout') for info in inspect.stack(): if info.filename == __file__: diff --git a/handout/tests/__init__.py b/handout/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/handout/tests/test_handout.py b/handout/tests/test_handout.py index 95f143f..2fc6a2e 100644 --- a/handout/tests/test_handout.py +++ b/handout/tests/test_handout.py @@ -1,5 +1,7 @@ import handout + def test_handout_on_title_arg_inserts_title(tmp_path): - output = handout.Handout(directory=tmp_path, title='This string')._generate(source='') - assert 'This string' in output \ No newline at end of file + doc = handout.Handout(directory=tmp_path, title='This string') + output = doc._generate(source='') + assert 'This string' in output