From 9545fba75106726098dac9defaacbbc18c311ce3 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sun, 19 Apr 2020 18:33:35 +0800 Subject: [PATCH 1/3] Add loggingutils in test.support --- Lib/test/support/__init__.py | 33 -------------------------- Lib/test/support/loggingutils.py | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 Lib/test/support/loggingutils.py diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 9f43b4071c044d..8880b06e4c1837 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 @@ -2522,38 +2521,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..57f4efabfae085 --- /dev/null +++ b/Lib/test/support/loggingutils.py @@ -0,0 +1,40 @@ +"""Utilities to support asyncio 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 From f4fd271e29cd17202813be5018d628eaa6926005 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sun, 19 Apr 2020 18:33:35 +0800 Subject: [PATCH 2/3] Add loggingutils in test.support --- Lib/test/support/__init__.py | 33 -------------------------- Lib/test/support/loggingutils.py | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 Lib/test/support/loggingutils.py diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 9f43b4071c044d..8880b06e4c1837 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 @@ -2522,38 +2521,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 From dc9741b3291d500f2e4e737e363213196f7f530d Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Sun, 19 Apr 2020 19:19:08 +0800 Subject: [PATCH 3/3] fix testcases --- Lib/test/support/__init__.py | 2 -- Lib/test/test_logging.py | 5 +++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 8880b06e4c1837..b75a337fc4ecb6 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -108,8 +108,6 @@ "bind_unix_socket", # processes 'temp_umask', "reap_children", - # logging - "TestHandler", # threads "threading_setup", "threading_cleanup", "reap_threads", "start_threads", # miscellaneous 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)