@@ -285,19 +285,14 @@ def our_candidate_list():
285285 def raise_OSError (* args , ** kwargs ):
286286 raise OSError ()
287287
288- with support .swap_attr (io , "open" , raise_OSError ):
289- # test again with failing io .open()
288+ with support .swap_attr (os , "open" , raise_OSError ):
289+ # test again with failing os .open()
290290 with self .assertRaises (FileNotFoundError ):
291291 tempfile ._get_default_tempdir ()
292292 self .assertEqual (os .listdir (our_temp_directory ), [])
293293
294- def bad_writer (* args , ** kwargs ):
295- fp = orig_open (* args , ** kwargs )
296- fp .write = raise_OSError
297- return fp
298-
299- with support .swap_attr (io , "open" , bad_writer ) as orig_open :
300- # test again with failing write()
294+ with support .swap_attr (os , "write" , raise_OSError ):
295+ # test again with failing os.write()
301296 with self .assertRaises (FileNotFoundError ):
302297 tempfile ._get_default_tempdir ()
303298 self .assertEqual (os .listdir (our_temp_directory ), [])
@@ -978,6 +973,7 @@ def test_del_on_close(self):
978973 try :
979974 with tempfile .NamedTemporaryFile (dir = dir ) as f :
980975 f .write (b'blat' )
976+ self .assertEqual (os .listdir (dir ), [])
981977 self .assertFalse (os .path .exists (f .name ),
982978 "NamedTemporaryFile %s exists after close" % f .name )
983979 finally :
@@ -1017,19 +1013,6 @@ def use_closed():
10171013 pass
10181014 self .assertRaises (ValueError , use_closed )
10191015
1020- def test_no_leak_fd (self ):
1021- # Issue #21058: don't leak file descriptor when io.open() fails
1022- closed = []
1023- os_close = os .close
1024- def close (fd ):
1025- closed .append (fd )
1026- os_close (fd )
1027-
1028- with mock .patch ('os.close' , side_effect = close ):
1029- with mock .patch ('io.open' , side_effect = ValueError ):
1030- self .assertRaises (ValueError , tempfile .NamedTemporaryFile )
1031- self .assertEqual (len (closed ), 1 )
1032-
10331016 def test_bad_mode (self ):
10341017 dir = tempfile .mkdtemp ()
10351018 self .addCleanup (os_helper .rmtree , dir )
@@ -1039,6 +1022,24 @@ def test_bad_mode(self):
10391022 tempfile .NamedTemporaryFile (mode = 2 , dir = dir )
10401023 self .assertEqual (os .listdir (dir ), [])
10411024
1025+ def test_bad_encoding (self ):
1026+ dir = tempfile .mkdtemp ()
1027+ self .addCleanup (os_helper .rmtree , dir )
1028+ with self .assertRaises (LookupError ):
1029+ tempfile .NamedTemporaryFile ('w' , encoding = 'bad-encoding' , dir = dir )
1030+ self .assertEqual (os .listdir (dir ), [])
1031+
1032+ def test_unexpected_error (self ):
1033+ dir = tempfile .mkdtemp ()
1034+ self .addCleanup (os_helper .rmtree , dir )
1035+ with mock .patch ('tempfile._TemporaryFileWrapper' ) as mock_ntf , \
1036+ mock .patch ('io.open' , mock .mock_open ()) as mock_open :
1037+ mock_ntf .side_effect = KeyboardInterrupt ()
1038+ with self .assertRaises (KeyboardInterrupt ):
1039+ tempfile .NamedTemporaryFile (dir = dir )
1040+ mock_open ().close .assert_called ()
1041+ self .assertEqual (os .listdir (dir ), [])
1042+
10421043 # How to test the mode and bufsize parameters?
10431044
10441045class TestSpooledTemporaryFile (BaseTestCase ):
@@ -1093,8 +1094,10 @@ def test_del_on_close(self):
10931094 self .assertTrue (f ._rolled )
10941095 filename = f .name
10951096 f .close ()
1096- self .assertFalse (isinstance (filename , str ) and os .path .exists (filename ),
1097- "SpooledTemporaryFile %s exists after close" % filename )
1097+ self .assertEqual (os .listdir (dir ), [])
1098+ if not isinstance (filename , int ):
1099+ self .assertFalse (os .path .exists (filename ),
1100+ "SpooledTemporaryFile %s exists after close" % filename )
10981101 finally :
10991102 os .rmdir (dir )
11001103
@@ -1411,19 +1414,34 @@ def roundtrip(input, *args, **kwargs):
14111414 roundtrip ("\u039B " , "w+" , encoding = "utf-16" )
14121415 roundtrip ("foo\r \n " , "w+" , newline = "" )
14131416
1414- def test_no_leak_fd (self ):
1415- # Issue #21058: don't leak file descriptor when io.open() fails
1416- closed = []
1417- os_close = os .close
1418- def close (fd ):
1419- closed .append (fd )
1420- os_close (fd )
1421-
1422- with mock .patch ('os.close' , side_effect = close ):
1423- with mock .patch ('io.open' , side_effect = ValueError ):
1424- self .assertRaises (ValueError , tempfile .TemporaryFile )
1425- self .assertEqual (len (closed ), 1 )
1417+ def test_bad_mode (self ):
1418+ dir = tempfile .mkdtemp ()
1419+ self .addCleanup (os_helper .rmtree , dir )
1420+ with self .assertRaises (ValueError ):
1421+ tempfile .TemporaryFile (mode = 'wr' , dir = dir )
1422+ with self .assertRaises (TypeError ):
1423+ tempfile .TemporaryFile (mode = 2 , dir = dir )
1424+ self .assertEqual (os .listdir (dir ), [])
1425+
1426+ def test_bad_encoding (self ):
1427+ dir = tempfile .mkdtemp ()
1428+ self .addCleanup (os_helper .rmtree , dir )
1429+ with self .assertRaises (LookupError ):
1430+ tempfile .TemporaryFile ('w' , encoding = 'bad-encoding' , dir = dir )
1431+ self .assertEqual (os .listdir (dir ), [])
14261432
1433+ def test_unexpected_error (self ):
1434+ dir = tempfile .mkdtemp ()
1435+ self .addCleanup (os_helper .rmtree , dir )
1436+ with mock .patch ('tempfile._O_TMPFILE_WORKS' , False ), \
1437+ mock .patch ('os.unlink' ) as mock_unlink , \
1438+ mock .patch ('os.open' ) as mock_open , \
1439+ mock .patch ('os.close' ) as mock_close :
1440+ mock_unlink .side_effect = KeyboardInterrupt ()
1441+ with self .assertRaises (KeyboardInterrupt ):
1442+ tempfile .TemporaryFile (dir = dir )
1443+ mock_close .assert_called ()
1444+ self .assertEqual (os .listdir (dir ), [])
14271445
14281446
14291447# Helper for test_del_on_shutdown
0 commit comments