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

Skip to content

Commit 57004c6

Browse files
committed
Issue #21440: Backport changeset 4ebf97299b18 to branch 3.4, use
support.rmtree() and support.unlink() in test_zipfile & test_tarfile
1 parent 19c899c commit 57004c6

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
@@ -480,16 +479,16 @@ def test_extract_hardlink(self):
480479
# Test hardlink extraction (e.g. bug #857297).
481480
with tarfile.open(tarname, errorlevel=1, encoding="iso8859-1") as tar:
482481
tar.extract("ustar/regtype", TEMPDIR)
483-
self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/regtype"))
482+
self.addCleanup(support.unlink, os.path.join(TEMPDIR, "ustar/regtype"))
484483

485484
tar.extract("ustar/lnktype", TEMPDIR)
486-
self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/lnktype"))
485+
self.addCleanup(support.unlink, os.path.join(TEMPDIR, "ustar/lnktype"))
487486
with open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb") as f:
488487
data = f.read()
489488
self.assertEqual(md5sum(data), md5_regtype)
490489

491490
tar.extract("ustar/symtype", TEMPDIR)
492-
self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/symtype"))
491+
self.addCleanup(support.unlink, os.path.join(TEMPDIR, "ustar/symtype"))
493492
with open(os.path.join(TEMPDIR, "ustar/symtype"), "rb") as f:
494493
data = f.read()
495494
self.assertEqual(md5sum(data), md5_regtype)
@@ -522,7 +521,7 @@ def format_mtime(mtime):
522521
self.assertEqual(tarinfo.mtime, file_mtime, errmsg)
523522
finally:
524523
tar.close()
525-
shutil.rmtree(DIR)
524+
support.rmtree(DIR)
526525

527526
def test_extract_directory(self):
528527
dirtype = "ustar/dirtype"
@@ -537,7 +536,7 @@ def test_extract_directory(self):
537536
if sys.platform != "win32":
538537
self.assertEqual(os.stat(extracted).st_mode & 0o777, 0o755)
539538
finally:
540-
shutil.rmtree(DIR)
539+
support.rmtree(DIR)
541540

542541
def test_init_close_fobj(self):
543542
# Issue #7341: Close the internal file object in the TarFile
@@ -901,7 +900,7 @@ def _fs_supports_holes():
901900
fobj.seek(4096)
902901
fobj.truncate()
903902
s = os.stat(name)
904-
os.remove(name)
903+
support.unlink(name)
905904
return s.st_blocks == 0
906905
else:
907906
return False
@@ -1034,7 +1033,7 @@ def test_directory_size(self):
10341033
finally:
10351034
tar.close()
10361035
finally:
1037-
os.rmdir(path)
1036+
support.rmdir(path)
10381037

10391038
@unittest.skipUnless(hasattr(os, "link"),
10401039
"Missing hardlink implementation")
@@ -1054,8 +1053,8 @@ def test_link_size(self):
10541053
finally:
10551054
tar.close()
10561055
finally:
1057-
os.remove(target)
1058-
os.remove(link)
1056+
support.unlink(target)
1057+
support.unlink(link)
10591058

10601059
@support.skip_unless_symlink
10611060
def test_symlink_size(self):
@@ -1069,7 +1068,7 @@ def test_symlink_size(self):
10691068
finally:
10701069
tar.close()
10711070
finally:
1072-
os.remove(path)
1071+
support.unlink(path)
10731072

10741073
def test_add_self(self):
10751074
# Test for #1257255.
@@ -1116,7 +1115,7 @@ def test_exclude(self):
11161115
finally:
11171116
tar.close()
11181117
finally:
1119-
shutil.rmtree(tempdir)
1118+
support.rmtree(tempdir)
11201119

11211120
def test_filter(self):
11221121
tempdir = os.path.join(TEMPDIR, "filter")
@@ -1152,7 +1151,7 @@ def filter(tarinfo):
11521151
finally:
11531152
tar.close()
11541153
finally:
1155-
shutil.rmtree(tempdir)
1154+
support.rmtree(tempdir)
11561155

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

11821181
if not dir:
1183-
os.remove(foo)
1182+
support.unlink(foo)
11841183
else:
1185-
os.rmdir(foo)
1184+
support.rmdir(foo)
11861185

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

@@ -1213,8 +1212,8 @@ def test_extractall_symlinks(self):
12131212
finally:
12141213
tar.close()
12151214
finally:
1216-
os.unlink(temparchive)
1217-
shutil.rmtree(tempdir)
1215+
support.unlink(temparchive)
1216+
support.rmtree(tempdir)
12181217

12191218
def test_pathnames(self):
12201219
self._test_pathname("foo")
@@ -1314,7 +1313,7 @@ def test_file_mode(self):
13141313
# Test for issue #8464: Create files with correct
13151314
# permissions.
13161315
if os.path.exists(tmpname):
1317-
os.remove(tmpname)
1316+
support.unlink(tmpname)
13181317

13191318
original_umask = os.umask(0o022)
13201319
try:
@@ -1668,7 +1667,7 @@ class AppendTestBase:
16681667
def setUp(self):
16691668
self.tarname = tmpname
16701669
if os.path.exists(self.tarname):
1671-
os.remove(self.tarname)
1670+
support.unlink(self.tarname)
16721671

16731672
def _create_testtar(self, mode="w:"):
16741673
with tarfile.open(tarname, encoding="iso8859-1") as src:
@@ -2175,7 +2174,7 @@ def setUpModule():
21752174

21762175
def tearDownModule():
21772176
if os.path.exists(TEMPDIR):
2178-
shutil.rmtree(TEMPDIR)
2177+
support.rmtree(TEMPDIR)
21792178

21802179
if __name__ == "__main__":
21812180
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

@@ -1643,7 +1642,7 @@ def test_store_dir(self):
16431642
self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
16441643

16451644
def tearDown(self):
1646-
shutil.rmtree(TESTFN2)
1645+
rmtree(TESTFN2)
16471646
if os.path.exists(TESTFN):
16481647
unlink(TESTFN)
16491648

0 commit comments

Comments
 (0)