Skip to content

Commit 3f1fc37

Browse files
Move TestHandler to logging_helper.
1 parent 739599e commit 3f1fc37

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

Lib/test/support/logging_helper.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import logging.handlers
2+
3+
class TestHandler(logging.handlers.BufferingHandler):
4+
def __init__(self, matcher):
5+
# BufferingHandler takes a "capacity" argument
6+
# so as to know when to flush. As we're overriding
7+
# shouldFlush anyway, we can set a capacity of zero.
8+
# You can call flush() manually to clear out the
9+
# buffer.
10+
logging.handlers.BufferingHandler.__init__(self, 0)
11+
self.matcher = matcher
12+
13+
def shouldFlush(self):
14+
return False
15+
16+
def emit(self, record):
17+
self.format(record)
18+
self.buffer.append(record.__dict__)
19+
20+
def matches(self, **kwargs):
21+
"""
22+
Look for a saved dict whose keys/values match the supplied arguments.
23+
"""
24+
result = False
25+
for d in self.buffer:
26+
if self.matcher.matches(d, **kwargs):
27+
result = True
28+
break
29+
return result

Lib/test/test_logging.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import tempfile
4444
from test.support.script_helper import assert_python_ok, assert_python_failure
4545
from test import support
46+
from test.support.logging_helper import TestHandler
4647
import textwrap
4748
import threading
4849
import time
@@ -3477,35 +3478,6 @@ def test_logrecord_class(self):
34773478
])
34783479

34793480

3480-
class TestHandler(logging.handlers.BufferingHandler):
3481-
def __init__(self, matcher):
3482-
# BufferingHandler takes a "capacity" argument
3483-
# so as to know when to flush. As we're overriding
3484-
# shouldFlush anyway, we can set a capacity of zero.
3485-
# You can call flush() manually to clear out the
3486-
# buffer.
3487-
logging.handlers.BufferingHandler.__init__(self, 0)
3488-
self.matcher = matcher
3489-
3490-
def shouldFlush(self):
3491-
return False
3492-
3493-
def emit(self, record):
3494-
self.format(record)
3495-
self.buffer.append(record.__dict__)
3496-
3497-
def matches(self, **kwargs):
3498-
"""
3499-
Look for a saved dict whose keys/values match the supplied arguments.
3500-
"""
3501-
result = False
3502-
for d in self.buffer:
3503-
if self.matcher.matches(d, **kwargs):
3504-
result = True
3505-
break
3506-
return result
3507-
3508-
35093481
class QueueHandlerTest(BaseTest):
35103482
# Do not bother with a logger name group.
35113483
expected_log_pat = r"^[\w.]+ -> (\w+): (\d+)$"

0 commit comments

Comments
 (0)