@@ -28,11 +28,15 @@ def setUp(self):
2828 # universal newline mode.
2929 self .write_modes = (("binary" , "wb" ), ("text" , "w" ))
3030
31+ def tearDown (self ):
32+ self .delete_files (self .temp_file )
33+ return super ().tearDown ()
34+
3135 # This module tests operations on the builtin file object. It is not yet complete, the tests cover read(),
3236 # read(size), readline() and write() for binary, text and universal newline modes.
3337 def test_sanity (self ):
34- onlyread_tmp = "onlyread_%d.tmp" % os .getpid ()
35- onlywrite_tmp = "onlywrite_%d.tmp" % os .getpid ()
38+ onlyread_tmp = os . path . join ( self . temporary_dir , "onlyread_%d.tmp" % os .getpid () )
39+ onlywrite_tmp = os . path . join ( self . temporary_dir , "onlywrite_%d.tmp" % os .getpid () )
3640 for i in range (5 ):
3741 ### general file robustness tests
3842 f = open (onlyread_tmp , "w" )
@@ -499,7 +503,7 @@ def return_fd2():
499503
500504 def test_sharing (self ):
501505 modes = ['w' , 'w+' , 'a+' , 'r' , 'w' ]
502- fname = 'tempfile_%d.txt' % os . getpid ()
506+ fname = self . temp_file
503507 for xx in modes :
504508 for yy in modes :
505509 x = open (fname , xx )
@@ -511,7 +515,7 @@ def test_sharing(self):
511515 os .unlink (fname )
512516
513517 def test_overwrite_readonly (self ):
514- filename = "tmp_%d.txt" % os . getpid ()
518+ filename = self . temp_file
515519 f = open (filename , "w+" )
516520 f .write ("I am read-only" )
517521 f .close ()
@@ -531,7 +535,7 @@ def test_overwrite_readonly(self):
531535 # file newline handling test
532536 @unittest .skipIf (is_posix , "this test doesn't really make sense for posix since b doesn't change the behavior" )
533537 def test_newline (self ):
534- fname = "testfile_%d.tmp" % os . getpid ()
538+ fname = self . temp_file
535539
536540 def test_newline (norm , mode ):
537541 f = open (fname , mode )
@@ -560,7 +564,7 @@ def test_newline(norm, mode):
560564
561565 def test_truncate (self ):
562566 # truncate()
563- fname = 'abc.txt'
567+ fname = self . temp_file
564568 with open (fname , 'w' ) as a :
565569 a .write ('hello world\n ' )
566570 a .truncate ()
@@ -596,29 +600,26 @@ def test_truncate(self):
596600
597601 def test_modes (self ):
598602 """test various strange mode combinations and error reporting"""
599- fname = 'test_file'
600- try :
601- with open (fname , 'w' ) as x :
602- self .assertEqual (x .mode , 'w' )
603- # don't allow empty modes
604- self .assertRaisesMessage (ValueError , "Must have exactly one of create/read/write/append mode and at most one plus" , open , 'abc' , '' )
605-
606- # mode must start with valid value
607- self .assertRaisesMessage (ValueError , "invalid mode: 'p'" , open , 'abc' , 'p' )
608-
609- # allow anything w/ U but r and w
610- err_msg = "mode U cannot be combined with 'x', 'w', 'a', or '+'" if is_cli or sys .version_info >= (3 ,7 ) else "mode U cannot be combined with x', 'w', 'a', or '+'" if sys .version_info >= (3 ,6 ) else "can't use U and writing mode at once"
611- self .assertRaisesMessage (ValueError , err_msg , open , 'abc' , 'Uw' )
612- self .assertRaisesMessage (ValueError , err_msg , open , 'abc' , 'Ua' )
613- self .assertRaisesMessage (ValueError , err_msg , open , 'abc' , 'Uw+' )
614- self .assertRaisesMessage (ValueError , err_msg , open , 'abc' , 'Ua+' )
615-
616- # check invalid modes
617- self .assertRaises (ValueError , open , fname , 'pU' )
618- self .assertRaises (ValueError , open , fname , 'pU+' )
619- self .assertRaises (ValueError , open , fname , 'rFOOBAR' )
620- finally :
621- os .unlink (fname )
603+ fname = self .temp_file
604+ with open (fname , 'w' ) as x :
605+ self .assertEqual (x .mode , 'w' )
606+ # don't allow empty modes
607+ self .assertRaisesMessage (ValueError , "Must have exactly one of create/read/write/append mode and at most one plus" , open , 'abc' , '' )
608+
609+ # mode must start with valid value
610+ self .assertRaisesMessage (ValueError , "invalid mode: 'p'" , open , 'abc' , 'p' )
611+
612+ # allow anything w/ U but r and w
613+ err_msg = "mode U cannot be combined with 'x', 'w', 'a', or '+'" if is_cli or sys .version_info >= (3 ,7 ) else "mode U cannot be combined with x', 'w', 'a', or '+'" if sys .version_info >= (3 ,6 ) else "can't use U and writing mode at once"
614+ self .assertRaisesMessage (ValueError , err_msg , open , 'abc' , 'Uw' )
615+ self .assertRaisesMessage (ValueError , err_msg , open , 'abc' , 'Ua' )
616+ self .assertRaisesMessage (ValueError , err_msg , open , 'abc' , 'Uw+' )
617+ self .assertRaisesMessage (ValueError , err_msg , open , 'abc' , 'Ua+' )
618+
619+ # check invalid modes
620+ self .assertRaises (ValueError , open , fname , 'pU' )
621+ self .assertRaises (ValueError , open , fname , 'pU+' )
622+ self .assertRaises (ValueError , open , fname , 'rFOOBAR' )
622623
623624 def test_cp16623 (self ):
624625 '''
@@ -632,7 +633,7 @@ def test_cp16623(self):
632633
633634 expected_lines = ["a" , "bbb" * 100 , "cc" ]
634635 total_threads = 50
635- file_name = os . path . join ( self .temporary_dir , "cp16623.txt" )
636+ file_name = self .temp_file
636637 with open (file_name , "w" ) as f :
637638
638639 def write_stuff ():
@@ -666,22 +667,18 @@ def write_stuff():
666667 # self.assertTrue(line in expected_lines, line)
667668
668669 def test_write_memoryview (self ):
669- try :
670- with open ('foo' , 'wb+' ) as foo :
671- b = memoryview (b'hello world' )[6 :]
672- foo .write (b )
673- with open ('foo' , 'r' ) as foo :
674- self .assertEqual (foo .readlines (), ['world' ])
675-
676- with open ('foo' , 'w+b' ) as foo :
677- b = memoryview (b'hello world' )[6 :]
678- foo .write (b )
670+ with open (self .temp_file , 'wb+' ) as foo :
671+ b = memoryview (b'hello world' )[6 :]
672+ foo .write (b )
673+ with open (self .temp_file , 'r' ) as foo :
674+ self .assertEqual (foo .readlines (), ['world' ])
679675
680- with open ('foo' , 'r' ) as foo :
681- self .assertEqual (foo .readlines (), ['world' ])
676+ with open (self .temp_file , 'w+b' ) as foo :
677+ b = memoryview (b'hello world' )[6 :]
678+ foo .write (b )
682679
683- finally :
684- self .delete_files ( " foo" )
680+ with open ( self . temp_file , 'r' ) as foo :
681+ self .assertEqual ( foo . readlines (), [ 'world' ] )
685682
686683 def test_errors (self ):
687684 with self .assertRaises (OSError ) as cm :
@@ -693,7 +690,7 @@ def test_errors(self):
693690 self .assertEqual (cm .exception .errno , (36 if is_posix else 22 ) if is_netcoreapp and not is_posix or sys .version_info >= (3 ,6 ) else 2 )
694691
695692 def test_write_bytes (self ):
696- fname = "temp_ip_%d" % os . getpid ()
693+ fname = self . temp_file
697694 f = open (fname , "wb+" )
698695 try :
699696 f .write (b"Hello\n " )
@@ -706,13 +703,11 @@ def test_write_bytes(self):
706703 os .unlink (fname )
707704
708705 def test_kw_args (self ):
709- fname = 'some_test_file_%d.txt' % os .getpid ()
710- open (file = fname , mode = 'w' ).close ()
711- os .unlink (fname )
706+ open (file = self .temp_file , mode = 'w' ).close ()
712707
713708 def test_buffering_kwparam (self ):
714709 #--Positive
715- fname = 'some_test_file_%d.txt' % os . getpid ()
710+ fname = self . temp_file
716711 for x in [- 2147483648 , - 1 , 1 , 2 , 1024 , 2147483646 , 2147483647 ]:
717712 f = open (file = fname , mode = 'w' , buffering = x )
718713 f .close ()
0 commit comments