diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-04-25 08:35:18 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-25 08:35:18 (GMT) |
commit | 515fce4fc4bb0d2db97b17df275cf90640017f56 (patch) | |
tree | 472b80622f4f7e7a8307c3f4db4122eef549dbd9 /Lib/test/support/logging_helper.py | |
parent | 16994912c93e8e5db7365d48b75d67d3f70dd7b2 (diff) | |
download | cpython-515fce4fc4bb0d2db97b17df275cf90640017f56.zip cpython-515fce4fc4bb0d2db97b17df275cf90640017f56.tar.gz cpython-515fce4fc4bb0d2db97b17df275cf90640017f56.tar.bz2 |
bpo-40275: Avoid importing logging in test.support (GH-19601)
Import logging lazily in assertLogs() in unittest.
Move TestHandler from test.support to logging_helper.
Diffstat (limited to 'Lib/test/support/logging_helper.py')
-rw-r--r-- | Lib/test/support/logging_helper.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/test/support/logging_helper.py b/Lib/test/support/logging_helper.py new file mode 100644 index 0000000..12fcca4 --- /dev/null +++ b/Lib/test/support/logging_helper.py @@ -0,0 +1,29 @@ +import logging.handlers + +class TestHandler(logging.handlers.BufferingHandler): + def __init__(self, matcher): + # BufferingHandler takes a "capacity" argument + # so as to know when to flush. As we're overriding + # shouldFlush anyway, we can set a capacity of zero. + # You can call flush() manually to clear out the + # buffer. + logging.handlers.BufferingHandler.__init__(self, 0) + self.matcher = matcher + + def shouldFlush(self): + return False + + def emit(self, record): + self.format(record) + self.buffer.append(record.__dict__) + + def matches(self, **kwargs): + """ + Look for a saved dict whose keys/values match the supplied arguments. + """ + result = False + for d in self.buffer: + if self.matcher.matches(d, **kwargs): + result = True + break + return result |