@@ -1365,13 +1365,17 @@ def __exit__(self, *exc_info):
1365
1365
d .clear ()
1366
1366
d .update (c )
1367
1367
1368
+
1368
1369
class TestTemporaryDirectory (BaseTestCase ):
1369
1370
"""Test TemporaryDirectory()."""
1370
1371
1371
- def do_create (self , dir = None , pre = "" , suf = "" , recurse = 1 , dirs = 1 , files = 1 ):
1372
+ def do_create (self , dir = None , pre = "" , suf = "" , recurse = 1 , dirs = 1 , files = 1 ,
1373
+ ignore_cleanup_errors = False ):
1372
1374
if dir is None :
1373
1375
dir = tempfile .gettempdir ()
1374
- tmp = tempfile .TemporaryDirectory (dir = dir , prefix = pre , suffix = suf )
1376
+ tmp = tempfile .TemporaryDirectory (
1377
+ dir = dir , prefix = pre , suffix = suf ,
1378
+ ignore_cleanup_errors = ignore_cleanup_errors )
1375
1379
self .nameCheck (tmp .name , dir , pre , suf )
1376
1380
self .do_create2 (tmp .name , recurse , dirs , files )
1377
1381
return tmp
@@ -1410,6 +1414,30 @@ def test_explicit_cleanup(self):
1410
1414
finally :
1411
1415
os .rmdir (dir )
1412
1416
1417
+ def test_explict_cleanup_ignore_errors (self ):
1418
+ """Test that cleanup doesn't return an error when ignoring them."""
1419
+ with tempfile .TemporaryDirectory () as working_dir :
1420
+ temp_dir = self .do_create (
1421
+ dir = working_dir , ignore_cleanup_errors = True )
1422
+ temp_path = pathlib .Path (temp_dir .name )
1423
+ self .assertTrue (temp_path .exists (),
1424
+ f"TemporaryDirectory { temp_path !s} does not exist" )
1425
+ with open (temp_path / "a_file.txt" , "w+t" ) as open_file :
1426
+ open_file .write ("Hello world!\n " )
1427
+ temp_dir .cleanup ()
1428
+ self .assertEqual (len (list (temp_path .glob ("*" ))),
1429
+ int (sys .platform .startswith ("win" )),
1430
+ "Unexpected number of files in "
1431
+ f"TemporaryDirectory { temp_path !s} " )
1432
+ self .assertEqual (
1433
+ temp_path .exists (),
1434
+ sys .platform .startswith ("win" ),
1435
+ f"TemporaryDirectory { temp_path !s} existance state unexpected" )
1436
+ temp_dir .cleanup ()
1437
+ self .assertFalse (
1438
+ temp_path .exists (),
1439
+ f"TemporaryDirectory { temp_path !s} exists after cleanup" )
1440
+
1413
1441
@os_helper .skip_unless_symlink
1414
1442
def test_cleanup_with_symlink_to_a_directory (self ):
1415
1443
# cleanup() should not follow symlinks to directories (issue #12464)
@@ -1444,6 +1472,27 @@ def test_del_on_collection(self):
1444
1472
finally :
1445
1473
os .rmdir (dir )
1446
1474
1475
+ @support .cpython_only
1476
+ def test_del_on_collection_ignore_errors (self ):
1477
+ """Test that ignoring errors works when TemporaryDirectory is gced."""
1478
+ with tempfile .TemporaryDirectory () as working_dir :
1479
+ temp_dir = self .do_create (
1480
+ dir = working_dir , ignore_cleanup_errors = True )
1481
+ temp_path = pathlib .Path (temp_dir .name )
1482
+ self .assertTrue (temp_path .exists (),
1483
+ f"TemporaryDirectory { temp_path !s} does not exist" )
1484
+ with open (temp_path / "a_file.txt" , "w+t" ) as open_file :
1485
+ open_file .write ("Hello world!\n " )
1486
+ del temp_dir
1487
+ self .assertEqual (len (list (temp_path .glob ("*" ))),
1488
+ int (sys .platform .startswith ("win" )),
1489
+ "Unexpected number of files in "
1490
+ f"TemporaryDirectory { temp_path !s} " )
1491
+ self .assertEqual (
1492
+ temp_path .exists (),
1493
+ sys .platform .startswith ("win" ),
1494
+ f"TemporaryDirectory { temp_path !s} existance state unexpected" )
1495
+
1447
1496
def test_del_on_shutdown (self ):
1448
1497
# A TemporaryDirectory may be cleaned up during shutdown
1449
1498
with self .do_create () as dir :
@@ -1476,6 +1525,43 @@ def test_del_on_shutdown(self):
1476
1525
self .assertNotIn ("Exception " , err )
1477
1526
self .assertIn ("ResourceWarning: Implicitly cleaning up" , err )
1478
1527
1528
+ def test_del_on_shutdown_ignore_errors (self ):
1529
+ """Test ignoring errors works when a tempdir is gc'ed on shutdown."""
1530
+ with tempfile .TemporaryDirectory () as working_dir :
1531
+ code = """if True:
1532
+ import pathlib
1533
+ import sys
1534
+ import tempfile
1535
+ import warnings
1536
+
1537
+ temp_dir = tempfile.TemporaryDirectory(
1538
+ dir={working_dir!r}, ignore_cleanup_errors=True)
1539
+ sys.stdout.buffer.write(temp_dir.name.encode())
1540
+
1541
+ temp_dir_2 = pathlib.Path(temp_dir.name) / "test_dir"
1542
+ temp_dir_2.mkdir()
1543
+ with open(temp_dir_2 / "test0.txt", "w") as test_file:
1544
+ test_file.write("Hello world!")
1545
+ open_file = open(temp_dir_2 / "open_file.txt", "w")
1546
+ open_file.write("Hello world!")
1547
+
1548
+ warnings.filterwarnings("always", category=ResourceWarning)
1549
+ """ .format (working_dir = working_dir )
1550
+ __ , out , err = script_helper .assert_python_ok ("-c" , code )
1551
+ temp_path = pathlib .Path (out .decode ().strip ())
1552
+ self .assertEqual (len (list (temp_path .glob ("*" ))),
1553
+ int (sys .platform .startswith ("win" )),
1554
+ "Unexpected number of files in "
1555
+ f"TemporaryDirectory { temp_path !s} " )
1556
+ self .assertEqual (
1557
+ temp_path .exists (),
1558
+ sys .platform .startswith ("win" ),
1559
+ f"TemporaryDirectory { temp_path !s} existance state unexpected" )
1560
+ err = err .decode ('utf-8' , 'backslashreplace' )
1561
+ self .assertNotIn ("Exception" , err )
1562
+ self .assertNotIn ("Error" , err )
1563
+ self .assertIn ("ResourceWarning: Implicitly cleaning up" , err )
1564
+
1479
1565
def test_exit_on_shutdown (self ):
1480
1566
# Issue #22427
1481
1567
with self .do_create () as dir :
0 commit comments