Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e0bd2c5

Browse files
committed
Issue21440 Use support.rmtree in test_zipfile & test_tarfile
1 parent 3080d92 commit e0bd2c5

2 files changed

Lines changed: 30 additions & 32 deletions

File tree

Lib/test/test_tarfile.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import sys
22
import os
33
import io
4-
import shutil
54
from hashlib import md5
65

76
import unittest
@@ -456,16 +455,16 @@ def test_extract_hardlink(self):
456455
# Test hardlink extraction (e.g. bug #857297).
457456
with tarfile.open(tarname, errorlevel=1, encoding="iso8859-1") as tar:
458457
tar.extract("ustar/regtype", TEMPDIR)
459-
self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/regtype"))
458+
self.addCleanup(support.unlink, os.path.join(TEMPDIR, "ustar/regtype"))
460459

461460
tar.extract("ustar/lnktype", TEMPDIR)
462-
self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/lnktype"))
461+
self.addCleanup(support.unlink, os.path.join(TEMPDIR, "ustar/lnktype"))
463462
with open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb") as f:
464463
data = f.read()
465464
self.assertEqual(md5sum(data), md5_regtype)
466465

467466
tar.extract("ustar/symtype", TEMPDIR)
468-
self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/symtype"))
467+
self.addCleanup(support.unlink, os.path.join(TEMPDIR, "ustar/symtype"))
469468
with open(os.path.join(TEMPDIR, "ustar/symtype"), "rb") as f:
470469
data = f.read()
471470
self.assertEqual(md5sum(data), md5_regtype)
@@ -498,7 +497,7 @@ def format_mtime(mtime):
498497
self.assertEqual(tarinfo.mtime, file_mtime, errmsg)
499498
finally:
500499
tar.close()
501-
shutil.rmtree(DIR)
500+
support.rmtree(DIR)
502501

503502
def test_extract_directory(self):
504503
dirtype = "ustar/dirtype"
@@ -513,7 +512,7 @@ def test_extract_directory(self):
513512
if sys.platform != "win32":
514513
self.assertEqual(os.stat(extracted).st_mode & 0o777, 0o755)
515514
finally:
516-
shutil.rmtree(DIR)
515+
support.rmtree(DIR)
517516

518517
def test_init_close_fobj(self):
519518
# Issue #7341: Close the internal file object in the TarFile
@@ -877,7 +876,7 @@ def _fs_supports_holes():
877876
fobj.seek(4096)
878877
fobj.truncate()
879878
s = os.stat(name)
880-
os.remove(name)
879+
support.unlink(name)
881880
return s.st_blocks == 0
882881
else:
883882
return False
@@ -1010,7 +1009,7 @@ def test_directory_size(self):
10101009
finally:
10111010
tar.close()
10121011
finally:
1013-
os.rmdir(path)
1012+
support.rmdir(path)
10141013

10151014
@unittest.skipUnless(hasattr(os, "link"),
10161015
"Missing hardlink implementation")
@@ -1030,8 +1029,8 @@ def test_link_size(self):
10301029
finally:
10311030
tar.close()
10321031
finally:
1033-
os.remove(target)
1034-
os.remove(link)
1032+
support.unlink(target)
1033+
support.unlink(link)
10351034

10361035
@support.skip_unless_symlink
10371036
def test_symlink_size(self):
@@ -1045,7 +1044,7 @@ def test_symlink_size(self):
10451044
finally:
10461045
tar.close()
10471046
finally:
1048-
os.remove(path)
1047+
support.unlink(path)
10491048

10501049
def test_add_self(self):
10511050
# Test for #1257255.
@@ -1092,7 +1091,7 @@ def test_exclude(self):
10921091
finally:
10931092
tar.close()
10941093
finally:
1095-
shutil.rmtree(tempdir)
1094+
support.rmtree(tempdir)
10961095

10971096
def test_filter(self):
10981097
tempdir = os.path.join(TEMPDIR, "filter")
@@ -1128,7 +1127,7 @@ def filter(tarinfo):
11281127
finally:
11291128
tar.close()
11301129
finally:
1131-
shutil.rmtree(tempdir)
1130+
support.rmtree(tempdir)
11321131

11331132
# Guarantee that stored pathnames are not modified. Don't
11341133
# remove ./ or ../ or double slashes. Still make absolute
@@ -1156,9 +1155,9 @@ def _test_pathname(self, path, cmp_path=None, dir=False):
11561155
tar.close()
11571156

11581157
if not dir:
1159-
os.remove(foo)
1158+
support.unlink(foo)
11601159
else:
1161-
os.rmdir(foo)
1160+
support.rmdir(foo)
11621161

11631162
self.assertEqual(t.name, cmp_path or path.replace(os.sep, "/"))
11641163

@@ -1189,8 +1188,8 @@ def test_extractall_symlinks(self):
11891188
finally:
11901189
tar.close()
11911190
finally:
1192-
os.unlink(temparchive)
1193-
shutil.rmtree(tempdir)
1191+
support.unlink(temparchive)
1192+
support.rmtree(tempdir)
11941193

11951194
def test_pathnames(self):
11961195
self._test_pathname("foo")
@@ -1290,7 +1289,7 @@ def test_file_mode(self):
12901289
# Test for issue #8464: Create files with correct
12911290
# permissions.
12921291
if os.path.exists(tmpname):
1293-
os.remove(tmpname)
1292+
support.unlink(tmpname)
12941293

12951294
original_umask = os.umask(0o022)
12961295
try:
@@ -1644,7 +1643,7 @@ class AppendTestBase:
16441643
def setUp(self):
16451644
self.tarname = tmpname
16461645
if os.path.exists(self.tarname):
1647-
os.remove(self.tarname)
1646+
support.unlink(self.tarname)
16481647

16491648
def _create_testtar(self, mode="w:"):
16501649
with tarfile.open(tarname, encoding="iso8859-1") as src:
@@ -2151,7 +2150,7 @@ def setUpModule():
21512150

21522151
def tearDownModule():
21532152
if os.path.exists(TEMPDIR):
2154-
shutil.rmtree(TEMPDIR)
2153+
support.rmtree(TEMPDIR)
21552154

21562155
if __name__ == "__main__":
21572156
unittest.main()

Lib/test/test_zipfile.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import sys
44
import importlib.util
55
import time
6-
import shutil
76
import struct
87
import zipfile
98
import unittest
@@ -12,7 +11,7 @@
1211
from tempfile import TemporaryFile
1312
from random import randint, random, getrandbits
1413

15-
from test.support import (TESTFN, findfile, unlink,
14+
from test.support import (TESTFN, findfile, unlink, rmtree,
1615
requires_zlib, requires_bz2, requires_lzma,
1716
captured_stdout, check_warnings)
1817

@@ -691,7 +690,7 @@ def test_write_python_directory(self):
691690
self.assertNotIn('mod2.txt', names)
692691

693692
finally:
694-
shutil.rmtree(TESTFN2)
693+
rmtree(TESTFN2)
695694

696695
def test_write_python_directory_filtered(self):
697696
os.mkdir(TESTFN2)
@@ -711,7 +710,7 @@ def test_write_python_directory_filtered(self):
711710
self.assertNotIn('mod2.py', names)
712711

713712
finally:
714-
shutil.rmtree(TESTFN2)
713+
rmtree(TESTFN2)
715714

716715
def test_write_non_pyfile(self):
717716
with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
@@ -741,7 +740,7 @@ def test_write_pyfile_bad_syntax(self):
741740
self.assertNotIn('mod1.pyo', names)
742741

743742
finally:
744-
shutil.rmtree(TESTFN2)
743+
rmtree(TESTFN2)
745744

746745

747746
class ExtractTests(unittest.TestCase):
@@ -767,7 +766,7 @@ def test_extract(self):
767766
os.remove(writtenfile)
768767

769768
# remove the test file subdirectories
770-
shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
769+
rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
771770

772771
def test_extract_all(self):
773772
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
@@ -785,7 +784,7 @@ def test_extract_all(self):
785784
os.remove(outfile)
786785

787786
# remove the test file subdirectories
788-
shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
787+
rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
789788

790789
def check_file(self, filename, content):
791790
self.assertTrue(os.path.isfile(filename))
@@ -867,12 +866,12 @@ def _test_extract_hackers_arcnames(self, hacknames):
867866
msg='extract %r: %r != %r' %
868867
(arcname, writtenfile, correctfile))
869868
self.check_file(correctfile, content)
870-
shutil.rmtree('target')
869+
rmtree('target')
871870

872871
with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
873872
zipfp.extractall(targetpath)
874873
self.check_file(correctfile, content)
875-
shutil.rmtree('target')
874+
rmtree('target')
876875

877876
correctfile = os.path.join(os.getcwd(), *fixedname.split('/'))
878877

@@ -881,12 +880,12 @@ def _test_extract_hackers_arcnames(self, hacknames):
881880
self.assertEqual(writtenfile, correctfile,
882881
msg="extract %r" % arcname)
883882
self.check_file(correctfile, content)
884-
shutil.rmtree(fixedname.split('/')[0])
883+
rmtree(fixedname.split('/')[0])
885884

886885
with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
887886
zipfp.extractall()
888887
self.check_file(correctfile, content)
889-
shutil.rmtree(fixedname.split('/')[0])
888+
rmtree(fixedname.split('/')[0])
890889

891890
os.remove(TESTFN2)
892891

@@ -1628,7 +1627,7 @@ def test_store_dir(self):
16281627
self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
16291628

16301629
def tearDown(self):
1631-
shutil.rmtree(TESTFN2)
1630+
rmtree(TESTFN2)
16321631
if os.path.exists(TESTFN):
16331632
unlink(TESTFN)
16341633

0 commit comments

Comments
 (0)