@@ -540,6 +540,12 @@ def test_specific_filters(self):
540540 handler .removeFilter (garr )
541541
542542
543+ def make_temp_file (* args , ** kwargs ):
544+ fd , fn = tempfile .mkstemp (* args , ** kwargs )
545+ os .close (fd )
546+ return fn
547+
548+
543549class HandlerTest (BaseTest ):
544550 def test_name (self ):
545551 h = logging .Handler ()
@@ -554,8 +560,7 @@ def test_builtin_handlers(self):
554560 # but we can try instantiating them with various options
555561 if sys .platform in ('linux' , 'darwin' ):
556562 for existing in (True , False ):
557- fd , fn = tempfile .mkstemp ()
558- os .close (fd )
563+ fn = make_temp_file ()
559564 if not existing :
560565 os .unlink (fn )
561566 h = logging .handlers .WatchedFileHandler (fn , encoding = 'utf-8' , delay = True )
@@ -609,8 +614,7 @@ def test_path_objects(self):
609614
610615 See Issue #27493.
611616 """
612- fd , fn = tempfile .mkstemp ()
613- os .close (fd )
617+ fn = make_temp_file ()
614618 os .unlink (fn )
615619 pfn = pathlib .Path (fn )
616620 cases = (
@@ -649,8 +653,7 @@ def remove_loop(fname, tries):
649653 self .deletion_time = None
650654
651655 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-' )
654657 remover = threading .Thread (target = remove_loop , args = (fn , del_count ))
655658 remover .daemon = True
656659 remover .start ()
@@ -1596,8 +1599,7 @@ def cleanup(h1, fn):
15961599 os .remove (fn )
15971600
15981601 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-" )
16011603
16021604 # Replace single backslash with double backslash in windows
16031605 # to avoid unicode error during string formatting
@@ -1782,8 +1784,7 @@ def test_noserver(self):
17821784 self .root_logger .error ('Nor this' )
17831785
17841786def _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' )
17871788 # just need a name - file can't be present, or we'll get an
17881789 # 'address already in use' error.
17891790 os .remove (fn )
@@ -2135,8 +2136,7 @@ class EncodingTest(BaseTest):
21352136 def test_encoding_plain_file (self ):
21362137 # In Python 2.x, a plain file object is treated as having no encoding.
21372138 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-" )
21402140 # the non-ascii data we write to the log.
21412141 data = "foo\x80 "
21422142 try :
@@ -3227,8 +3227,7 @@ def cleanup(h1, fn):
32273227 os .remove (fn )
32283228
32293229 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-" )
32323231
32333232 config = {
32343233 "version" : 1 ,
@@ -4891,10 +4890,14 @@ def test_log_taskName(self):
48914890 async def log_record ():
48924891 logging .warning ('hello world' )
48934892
4893+ handler = None
4894+ log_filename = make_temp_file ('.log' , 'test-logging-taskname-' )
4895+ self .addCleanup (os .remove , log_filename )
48944896 try :
48954897 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' )
48984901
48994902 self .assertEqual (len (logging .root .handlers ), 1 )
49004903 handler = logging .root .handlers [0 ]
@@ -4903,13 +4906,13 @@ async def log_record():
49034906 with asyncio .Runner (debug = True ) as runner :
49044907 logging .logAsyncioTasks = True
49054908 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 :
49104910 data = f .read ().strip ()
4911- os .remove ('test.log' )
49124911 self .assertRegex (data , r'Task-\d+ - hello world' )
4912+ finally :
4913+ asyncio .set_event_loop_policy (None )
4914+ if handler :
4915+ handler .close ()
49134916
49144917
49154918 def _test_log (self , method , level = None ):
@@ -5294,8 +5297,7 @@ class BaseFileTest(BaseTest):
52945297
52955298 def setUp (self ):
52965299 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-" )
52995301 self .rmfiles = []
53005302
53015303 def tearDown (self ):
0 commit comments