@@ -540,6 +540,12 @@ def test_specific_filters(self):
540
540
handler .removeFilter (garr )
541
541
542
542
543
+ def make_temp_file (* args , ** kwargs ):
544
+ fd , fn = tempfile .mkstemp (* args , ** kwargs )
545
+ os .close (fd )
546
+ return fn
547
+
548
+
543
549
class HandlerTest (BaseTest ):
544
550
def test_name (self ):
545
551
h = logging .Handler ()
@@ -554,8 +560,7 @@ def test_builtin_handlers(self):
554
560
# but we can try instantiating them with various options
555
561
if sys .platform in ('linux' , 'darwin' ):
556
562
for existing in (True , False ):
557
- fd , fn = tempfile .mkstemp ()
558
- os .close (fd )
563
+ fn = make_temp_file ()
559
564
if not existing :
560
565
os .unlink (fn )
561
566
h = logging .handlers .WatchedFileHandler (fn , encoding = 'utf-8' , delay = True )
@@ -609,8 +614,7 @@ def test_path_objects(self):
609
614
610
615
See Issue #27493.
611
616
"""
612
- fd , fn = tempfile .mkstemp ()
613
- os .close (fd )
617
+ fn = make_temp_file ()
614
618
os .unlink (fn )
615
619
pfn = pathlib .Path (fn )
616
620
cases = (
@@ -649,8 +653,7 @@ def remove_loop(fname, tries):
649
653
self .deletion_time = None
650
654
651
655
for delay in (False , True ):
652
- fd , fn = tempfile .mkstemp ('.log' , 'test_logging-3-' )
653
- os .close (fd )
656
+ fn = make_temp_file ('.log' , 'test_logging-3-' )
654
657
remover = threading .Thread (target = remove_loop , args = (fn , del_count ))
655
658
remover .daemon = True
656
659
remover .start ()
@@ -1596,8 +1599,7 @@ def cleanup(h1, fn):
1596
1599
os .remove (fn )
1597
1600
1598
1601
with self .check_no_resource_warning ():
1599
- fd , fn = tempfile .mkstemp (".log" , "test_logging-X-" )
1600
- os .close (fd )
1602
+ fn = make_temp_file (".log" , "test_logging-X-" )
1601
1603
1602
1604
# Replace single backslash with double backslash in windows
1603
1605
# to avoid unicode error during string formatting
@@ -1782,8 +1784,7 @@ def test_noserver(self):
1782
1784
self .root_logger .error ('Nor this' )
1783
1785
1784
1786
def _get_temp_domain_socket ():
1785
- fd , fn = tempfile .mkstemp (prefix = 'test_logging_' , suffix = '.sock' )
1786
- os .close (fd )
1787
+ fn = make_temp_file (prefix = 'test_logging_' , suffix = '.sock' )
1787
1788
# just need a name - file can't be present, or we'll get an
1788
1789
# 'address already in use' error.
1789
1790
os .remove (fn )
@@ -2135,8 +2136,7 @@ class EncodingTest(BaseTest):
2135
2136
def test_encoding_plain_file (self ):
2136
2137
# In Python 2.x, a plain file object is treated as having no encoding.
2137
2138
log = logging .getLogger ("test" )
2138
- fd , fn = tempfile .mkstemp (".log" , "test_logging-1-" )
2139
- os .close (fd )
2139
+ fn = make_temp_file (".log" , "test_logging-1-" )
2140
2140
# the non-ascii data we write to the log.
2141
2141
data = "foo\x80 "
2142
2142
try :
@@ -3227,8 +3227,7 @@ def cleanup(h1, fn):
3227
3227
os .remove (fn )
3228
3228
3229
3229
with self .check_no_resource_warning ():
3230
- fd , fn = tempfile .mkstemp (".log" , "test_logging-X-" )
3231
- os .close (fd )
3230
+ fn = make_temp_file (".log" , "test_logging-X-" )
3232
3231
3233
3232
config = {
3234
3233
"version" : 1 ,
@@ -4891,10 +4890,14 @@ def test_log_taskName(self):
4891
4890
async def log_record ():
4892
4891
logging .warning ('hello world' )
4893
4892
4893
+ handler = None
4894
+ log_filename = make_temp_file ('.log' , 'test-logging-taskname-' )
4895
+ self .addCleanup (os .remove , log_filename )
4894
4896
try :
4895
4897
encoding = 'utf-8'
4896
- logging .basicConfig (filename = 'test.log' , errors = 'strict' , encoding = encoding ,
4897
- format = '%(taskName)s - %(message)s' , level = logging .WARNING )
4898
+ logging .basicConfig (filename = log_filename , errors = 'strict' ,
4899
+ encoding = encoding , level = logging .WARNING ,
4900
+ format = '%(taskName)s - %(message)s' )
4898
4901
4899
4902
self .assertEqual (len (logging .root .handlers ), 1 )
4900
4903
handler = logging .root .handlers [0 ]
@@ -4903,13 +4906,13 @@ async def log_record():
4903
4906
with asyncio .Runner (debug = True ) as runner :
4904
4907
logging .logAsyncioTasks = True
4905
4908
runner .run (log_record ())
4906
- finally :
4907
- asyncio .set_event_loop_policy (None )
4908
- handler .close ()
4909
- with open ('test.log' , encoding = 'utf-8' ) as f :
4909
+ with open (log_filename , encoding = 'utf-8' ) as f :
4910
4910
data = f .read ().strip ()
4911
- os .remove ('test.log' )
4912
4911
self .assertRegex (data , r'Task-\d+ - hello world' )
4912
+ finally :
4913
+ asyncio .set_event_loop_policy (None )
4914
+ if handler :
4915
+ handler .close ()
4913
4916
4914
4917
4915
4918
def _test_log (self , method , level = None ):
@@ -5294,8 +5297,7 @@ class BaseFileTest(BaseTest):
5294
5297
5295
5298
def setUp (self ):
5296
5299
BaseTest .setUp (self )
5297
- fd , self .fn = tempfile .mkstemp (".log" , "test_logging-2-" )
5298
- os .close (fd )
5300
+ self .fn = make_temp_file (".log" , "test_logging-2-" )
5299
5301
self .rmfiles = []
5300
5302
5301
5303
def tearDown (self ):
0 commit comments