@@ -290,19 +290,14 @@ def our_candidate_list():
290
290
def raise_OSError (* args , ** kwargs ):
291
291
raise OSError ()
292
292
293
- with support .swap_attr (io , "open" , raise_OSError ):
294
- # test again with failing io .open()
293
+ with support .swap_attr (os , "open" , raise_OSError ):
294
+ # test again with failing os .open()
295
295
with self .assertRaises (FileNotFoundError ):
296
296
tempfile ._get_default_tempdir ()
297
297
self .assertEqual (os .listdir (our_temp_directory ), [])
298
298
299
- def bad_writer (* args , ** kwargs ):
300
- fp = orig_open (* args , ** kwargs )
301
- fp .write = raise_OSError
302
- return fp
303
-
304
- with support .swap_attr (io , "open" , bad_writer ) as orig_open :
305
- # test again with failing write()
299
+ with support .swap_attr (os , "write" , raise_OSError ):
300
+ # test again with failing os.write()
306
301
with self .assertRaises (FileNotFoundError ):
307
302
tempfile ._get_default_tempdir ()
308
303
self .assertEqual (os .listdir (our_temp_directory ), [])
@@ -977,6 +972,7 @@ def test_del_on_close(self):
977
972
try :
978
973
with tempfile .NamedTemporaryFile (dir = dir ) as f :
979
974
f .write (b'blat' )
975
+ self .assertEqual (os .listdir (dir ), [])
980
976
self .assertFalse (os .path .exists (f .name ),
981
977
"NamedTemporaryFile %s exists after close" % f .name )
982
978
finally :
@@ -1016,19 +1012,6 @@ def use_closed():
1016
1012
pass
1017
1013
self .assertRaises (ValueError , use_closed )
1018
1014
1019
- def test_no_leak_fd (self ):
1020
- # Issue #21058: don't leak file descriptor when io.open() fails
1021
- closed = []
1022
- os_close = os .close
1023
- def close (fd ):
1024
- closed .append (fd )
1025
- os_close (fd )
1026
-
1027
- with mock .patch ('os.close' , side_effect = close ):
1028
- with mock .patch ('io.open' , side_effect = ValueError ):
1029
- self .assertRaises (ValueError , tempfile .NamedTemporaryFile )
1030
- self .assertEqual (len (closed ), 1 )
1031
-
1032
1015
def test_bad_mode (self ):
1033
1016
dir = tempfile .mkdtemp ()
1034
1017
self .addCleanup (os_helper .rmtree , dir )
@@ -1038,6 +1021,24 @@ def test_bad_mode(self):
1038
1021
tempfile .NamedTemporaryFile (mode = 2 , dir = dir )
1039
1022
self .assertEqual (os .listdir (dir ), [])
1040
1023
1024
+ def test_bad_encoding (self ):
1025
+ dir = tempfile .mkdtemp ()
1026
+ self .addCleanup (os_helper .rmtree , dir )
1027
+ with self .assertRaises (LookupError ):
1028
+ tempfile .NamedTemporaryFile ('w' , encoding = 'bad-encoding' , dir = dir )
1029
+ self .assertEqual (os .listdir (dir ), [])
1030
+
1031
+ def test_unexpected_error (self ):
1032
+ dir = tempfile .mkdtemp ()
1033
+ self .addCleanup (os_helper .rmtree , dir )
1034
+ with mock .patch ('tempfile._TemporaryFileWrapper' ) as mock_ntf , \
1035
+ mock .patch ('io.open' , mock .mock_open ()) as mock_open :
1036
+ mock_ntf .side_effect = KeyboardInterrupt ()
1037
+ with self .assertRaises (KeyboardInterrupt ):
1038
+ tempfile .NamedTemporaryFile (dir = dir )
1039
+ mock_open ().close .assert_called ()
1040
+ self .assertEqual (os .listdir (dir ), [])
1041
+
1041
1042
# How to test the mode and bufsize parameters?
1042
1043
1043
1044
class TestSpooledTemporaryFile (BaseTestCase ):
@@ -1068,8 +1069,10 @@ def test_del_on_close(self):
1068
1069
self .assertTrue (f ._rolled )
1069
1070
filename = f .name
1070
1071
f .close ()
1071
- self .assertFalse (isinstance (filename , str ) and os .path .exists (filename ),
1072
- "SpooledTemporaryFile %s exists after close" % filename )
1072
+ self .assertEqual (os .listdir (dir ), [])
1073
+ if not isinstance (filename , int ):
1074
+ self .assertFalse (os .path .exists (filename ),
1075
+ "SpooledTemporaryFile %s exists after close" % filename )
1073
1076
finally :
1074
1077
os .rmdir (dir )
1075
1078
@@ -1356,19 +1359,34 @@ def roundtrip(input, *args, **kwargs):
1356
1359
roundtrip ("\u039B " , "w+" , encoding = "utf-16" )
1357
1360
roundtrip ("foo\r \n " , "w+" , newline = "" )
1358
1361
1359
- def test_no_leak_fd (self ):
1360
- # Issue #21058: don't leak file descriptor when io.open() fails
1361
- closed = []
1362
- os_close = os .close
1363
- def close (fd ):
1364
- closed .append (fd )
1365
- os_close (fd )
1366
-
1367
- with mock .patch ('os.close' , side_effect = close ):
1368
- with mock .patch ('io.open' , side_effect = ValueError ):
1369
- self .assertRaises (ValueError , tempfile .TemporaryFile )
1370
- self .assertEqual (len (closed ), 1 )
1362
+ def test_bad_mode (self ):
1363
+ dir = tempfile .mkdtemp ()
1364
+ self .addCleanup (os_helper .rmtree , dir )
1365
+ with self .assertRaises (ValueError ):
1366
+ tempfile .TemporaryFile (mode = 'wr' , dir = dir )
1367
+ with self .assertRaises (TypeError ):
1368
+ tempfile .TemporaryFile (mode = 2 , dir = dir )
1369
+ self .assertEqual (os .listdir (dir ), [])
1370
+
1371
+ def test_bad_encoding (self ):
1372
+ dir = tempfile .mkdtemp ()
1373
+ self .addCleanup (os_helper .rmtree , dir )
1374
+ with self .assertRaises (LookupError ):
1375
+ tempfile .TemporaryFile ('w' , encoding = 'bad-encoding' , dir = dir )
1376
+ self .assertEqual (os .listdir (dir ), [])
1371
1377
1378
+ def test_unexpected_error (self ):
1379
+ dir = tempfile .mkdtemp ()
1380
+ self .addCleanup (os_helper .rmtree , dir )
1381
+ with mock .patch ('tempfile._O_TMPFILE_WORKS' , False ), \
1382
+ mock .patch ('os.unlink' ) as mock_unlink , \
1383
+ mock .patch ('os.open' ) as mock_open , \
1384
+ mock .patch ('os.close' ) as mock_close :
1385
+ mock_unlink .side_effect = KeyboardInterrupt ()
1386
+ with self .assertRaises (KeyboardInterrupt ):
1387
+ tempfile .TemporaryFile (dir = dir )
1388
+ mock_close .assert_called ()
1389
+ self .assertEqual (os .listdir (dir ), [])
1372
1390
1373
1391
1374
1392
# Helper for test_del_on_shutdown
0 commit comments