diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 9f43b4071c044d..b75a337fc4ecb6 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -16,7 +16,6 @@ import importlib import importlib.util import locale -import logging.handlers import nntplib import os import platform @@ -109,8 +108,6 @@ "bind_unix_socket", # processes 'temp_umask', "reap_children", - # logging - "TestHandler", # threads "threading_setup", "threading_cleanup", "reap_threads", "start_threads", # miscellaneous @@ -2522,38 +2519,6 @@ def optim_args_from_interpreter_flags(): optimization settings in sys.flags.""" return subprocess._optim_args_from_interpreter_flags() -#============================================================ -# Support for assertions about logging. -#============================================================ - -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 - class Matcher(object): _partial_matches = ('msg', 'message') diff --git a/Lib/test/support/loggingutils.py b/Lib/test/support/loggingutils.py new file mode 100644 index 00000000000000..5ad72ba2c19828 --- /dev/null +++ b/Lib/test/support/loggingutils.py @@ -0,0 +1,40 @@ +"""Utilities to support logging in the Python regression tests.""" + +import logging.handlers + +__all__ = [ + # logging + "TestHandler", + ] + +#============================================================ +# Support for assertions about logging. +#============================================================ + +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 diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 99e74ebff60875..f213cbfd4ce04b 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -41,6 +41,7 @@ import struct import sys import tempfile +from test.support.loggingutils import TestHandler from test.support.script_helper import assert_python_ok, assert_python_failure from test import support import textwrap @@ -3523,7 +3524,7 @@ def test_formatting(self): @unittest.skipUnless(hasattr(logging.handlers, 'QueueListener'), 'logging.handlers.QueueListener required for this test') def test_queue_listener(self): - handler = support.TestHandler(support.Matcher()) + handler = TestHandler(support.Matcher()) listener = logging.handlers.QueueListener(self.queue, handler) listener.start() try: @@ -3539,7 +3540,7 @@ def test_queue_listener(self): # Now test with respect_handler_level set - handler = support.TestHandler(support.Matcher()) + handler = TestHandler(support.Matcher()) handler.setLevel(logging.CRITICAL) listener = logging.handlers.QueueListener(self.queue, handler, respect_handler_level=True)