[Python-checkins] gh-91513: Tidied up a test and did minor refactoring around test filename gener… (GH-93265)
vsajip
webhook-mailer at python.org
Thu May 26 12:11:24 EDT 2022
https://github.com/python/cpython/commit/efc5d37671d2b9f31850a9edaa495fadae038759
commit: efc5d37671d2b9f31850a9edaa495fadae038759
branch: main
author: Vinay Sajip <vinay_sajip at yahoo.co.uk>
committer: vsajip <vinay_sajip at yahoo.co.uk>
date: 2022-05-26T17:11:15+01:00
summary:
gh-91513: Tidied up a test and did minor refactoring around test filename gener… (GH-93265)
files:
M Lib/test/test_logging.py
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 07804a2686a14..1c5e8523754ca 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -540,6 +540,12 @@ def test_specific_filters(self):
handler.removeFilter(garr)
+def make_temp_file(*args, **kwargs):
+ fd, fn = tempfile.mkstemp(*args, **kwargs)
+ os.close(fd)
+ return fn
+
+
class HandlerTest(BaseTest):
def test_name(self):
h = logging.Handler()
@@ -554,8 +560,7 @@ def test_builtin_handlers(self):
# but we can try instantiating them with various options
if sys.platform in ('linux', 'darwin'):
for existing in (True, False):
- fd, fn = tempfile.mkstemp()
- os.close(fd)
+ fn = make_temp_file()
if not existing:
os.unlink(fn)
h = logging.handlers.WatchedFileHandler(fn, encoding='utf-8', delay=True)
@@ -609,8 +614,7 @@ def test_path_objects(self):
See Issue #27493.
"""
- fd, fn = tempfile.mkstemp()
- os.close(fd)
+ fn = make_temp_file()
os.unlink(fn)
pfn = pathlib.Path(fn)
cases = (
@@ -649,8 +653,7 @@ def remove_loop(fname, tries):
self.deletion_time = None
for delay in (False, True):
- fd, fn = tempfile.mkstemp('.log', 'test_logging-3-')
- os.close(fd)
+ fn = make_temp_file('.log', 'test_logging-3-')
remover = threading.Thread(target=remove_loop, args=(fn, del_count))
remover.daemon = True
remover.start()
@@ -1596,8 +1599,7 @@ def cleanup(h1, fn):
os.remove(fn)
with self.check_no_resource_warning():
- fd, fn = tempfile.mkstemp(".log", "test_logging-X-")
- os.close(fd)
+ fn = make_temp_file(".log", "test_logging-X-")
# Replace single backslash with double backslash in windows
# to avoid unicode error during string formatting
@@ -1782,8 +1784,7 @@ def test_noserver(self):
self.root_logger.error('Nor this')
def _get_temp_domain_socket():
- fd, fn = tempfile.mkstemp(prefix='test_logging_', suffix='.sock')
- os.close(fd)
+ fn = make_temp_file(prefix='test_logging_', suffix='.sock')
# just need a name - file can't be present, or we'll get an
# 'address already in use' error.
os.remove(fn)
@@ -2135,8 +2136,7 @@ class EncodingTest(BaseTest):
def test_encoding_plain_file(self):
# In Python 2.x, a plain file object is treated as having no encoding.
log = logging.getLogger("test")
- fd, fn = tempfile.mkstemp(".log", "test_logging-1-")
- os.close(fd)
+ fn = make_temp_file(".log", "test_logging-1-")
# the non-ascii data we write to the log.
data = "foo\x80"
try:
@@ -3227,8 +3227,7 @@ def cleanup(h1, fn):
os.remove(fn)
with self.check_no_resource_warning():
- fd, fn = tempfile.mkstemp(".log", "test_logging-X-")
- os.close(fd)
+ fn = make_temp_file(".log", "test_logging-X-")
config = {
"version": 1,
@@ -4891,10 +4890,14 @@ def test_log_taskName(self):
async def log_record():
logging.warning('hello world')
+ handler = None
+ log_filename = make_temp_file('.log', 'test-logging-taskname-')
+ self.addCleanup(os.remove, log_filename)
try:
encoding = 'utf-8'
- logging.basicConfig(filename='test.log', errors='strict', encoding=encoding,
- format='%(taskName)s - %(message)s', level=logging.WARNING)
+ logging.basicConfig(filename=log_filename, errors='strict',
+ encoding=encoding, level=logging.WARNING,
+ format='%(taskName)s - %(message)s')
self.assertEqual(len(logging.root.handlers), 1)
handler = logging.root.handlers[0]
@@ -4903,13 +4906,13 @@ async def log_record():
with asyncio.Runner(debug=True) as runner:
logging.logAsyncioTasks = True
runner.run(log_record())
- finally:
- asyncio.set_event_loop_policy(None)
- handler.close()
- with open('test.log', encoding='utf-8') as f:
+ with open(log_filename, encoding='utf-8') as f:
data = f.read().strip()
- os.remove('test.log')
self.assertRegex(data, r'Task-\d+ - hello world')
+ finally:
+ asyncio.set_event_loop_policy(None)
+ if handler:
+ handler.close()
def _test_log(self, method, level=None):
@@ -5294,8 +5297,7 @@ class BaseFileTest(BaseTest):
def setUp(self):
BaseTest.setUp(self)
- fd, self.fn = tempfile.mkstemp(".log", "test_logging-2-")
- os.close(fd)
+ self.fn = make_temp_file(".log", "test_logging-2-")
self.rmfiles = []
def tearDown(self):
More information about the Python-checkins
mailing list