@@ -285,19 +285,14 @@ def our_candidate_list():
285
285
def raise_OSError (* args , ** kwargs ):
286
286
raise OSError ()
287
287
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()
290
290
with self .assertRaises (FileNotFoundError ):
291
291
tempfile ._get_default_tempdir ()
292
292
self .assertEqual (os .listdir (our_temp_directory ), [])
293
293
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()
301
296
with self .assertRaises (FileNotFoundError ):
302
297
tempfile ._get_default_tempdir ()
303
298
self .assertEqual (os .listdir (our_temp_directory ), [])
@@ -978,6 +973,7 @@ def test_del_on_close(self):
978
973
try :
979
974
with tempfile .NamedTemporaryFile (dir = dir ) as f :
980
975
f .write (b'blat' )
976
+ self .assertEqual (os .listdir (dir ), [])
981
977
self .assertFalse (os .path .exists (f .name ),
982
978
"NamedTemporaryFile %s exists after close" % f .name )
983
979
finally :
@@ -1017,19 +1013,6 @@ def use_closed():
1017
1013
pass
1018
1014
self .assertRaises (ValueError , use_closed )
1019
1015
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
-
1033
1016
def test_bad_mode (self ):
1034
1017
dir = tempfile .mkdtemp ()
1035
1018
self .addCleanup (os_helper .rmtree , dir )
@@ -1039,6 +1022,24 @@ def test_bad_mode(self):
1039
1022
tempfile .NamedTemporaryFile (mode = 2 , dir = dir )
1040
1023
self .assertEqual (os .listdir (dir ), [])
1041
1024
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
+
1042
1043
# How to test the mode and bufsize parameters?
1043
1044
1044
1045
class TestSpooledTemporaryFile (BaseTestCase ):
@@ -1093,8 +1094,10 @@ def test_del_on_close(self):
1093
1094
self .assertTrue (f ._rolled )
1094
1095
filename = f .name
1095
1096
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 )
1098
1101
finally :
1099
1102
os .rmdir (dir )
1100
1103
@@ -1411,19 +1414,34 @@ def roundtrip(input, *args, **kwargs):
1411
1414
roundtrip ("\u039B " , "w+" , encoding = "utf-16" )
1412
1415
roundtrip ("foo\r \n " , "w+" , newline = "" )
1413
1416
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 ), [])
1426
1432
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 ), [])
1427
1445
1428
1446
1429
1447
# Helper for test_del_on_shutdown
0 commit comments