mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
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.
This commit is contained in:
parent
16994912c9
commit
515fce4fc4
7 changed files with 105 additions and 109 deletions
|
@ -15,7 +15,6 @@ import hashlib
|
|||
import importlib
|
||||
import importlib.util
|
||||
import locale
|
||||
import logging.handlers
|
||||
import os
|
||||
import platform
|
||||
import re
|
||||
|
@ -99,8 +98,6 @@ __all__ = [
|
|||
"open_urlresource",
|
||||
# processes
|
||||
'temp_umask', "reap_children",
|
||||
# logging
|
||||
"TestHandler",
|
||||
# threads
|
||||
"threading_setup", "threading_cleanup", "reap_threads", "start_threads",
|
||||
# miscellaneous
|
||||
|
@ -2368,37 +2365,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):
|
||||
|
||||
|
|
29
Lib/test/support/logging_helper.py
Normal file
29
Lib/test/support/logging_helper.py
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue