[pypy-commit] pypy stdlib-2.7.8: further improvements for test_tarfile
bdkearns
noreply at buildbot.pypy.org
Mon Aug 25 23:04:55 CEST 2014
Author: Brian Kearns <bdkearns at gmail.com>
Branch: stdlib-2.7.8
Changeset: r73059:0ebbdd28755b
Date: 2014-08-25 17:00 -0400
http://bitbucket.org/pypy/pypy/changeset/0ebbdd28755b/
Log: further improvements for test_tarfile
diff --git a/lib-python/2.7/test/test_tarfile.py b/lib-python/2.7/test/test_tarfile.py
--- a/lib-python/2.7/test/test_tarfile.py
+++ b/lib-python/2.7/test/test_tarfile.py
@@ -42,7 +42,8 @@
mode = "r:"
def setUp(self):
- self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1")
+ self.tar = tarfile.open(self.tarname, mode=self.mode,
+ encoding="iso8859-1")
def tearDown(self):
self.tar.close()
@@ -148,13 +149,15 @@
self._test_fileobj_link("ustar/lnktype", "ustar/regtype")
def test_fileobj_link2(self):
- self._test_fileobj_link("./ustar/linktest2/lnktype", "ustar/linktest1/regtype")
+ self._test_fileobj_link("./ustar/linktest2/lnktype",
+ "ustar/linktest1/regtype")
def test_fileobj_symlink1(self):
self._test_fileobj_link("ustar/symtype", "ustar/regtype")
def test_fileobj_symlink2(self):
- self._test_fileobj_link("./ustar/linktest2/symtype", "ustar/linktest1/regtype")
+ self._test_fileobj_link("./ustar/linktest2/symtype",
+ "ustar/linktest1/regtype")
def test_issue14160(self):
self._test_fileobj_link("symtype2", "ustar/regtype")
@@ -241,7 +244,8 @@
# This test checks if tarfile.open() is able to open an empty tar
# archive successfully. Note that an empty tar archive is not the
# same as an empty file!
- tarfile.open(tmpname, self.mode.replace("r", "w")).close()
+ with tarfile.open(tmpname, self.mode.replace("r", "w")):
+ pass
try:
tar = tarfile.open(tmpname, self.mode)
tar.getnames()
@@ -256,7 +260,8 @@
# Test for issue6123: Allow opening empty archives.
# This test guarantees that tarfile.open() does not treat an empty
# file as an empty tar archive.
- open(tmpname, "wb").close()
+ with open(tmpname, "wb"):
+ pass
self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, self.mode)
self.assertRaises(tarfile.ReadError, tarfile.open, tmpname)
@@ -280,15 +285,17 @@
for char in ('\0', 'a'):
# Test if EOFHeaderError ('\0') and InvalidHeaderError ('a')
# are ignored correctly.
- fobj = _open(tmpname, "wb")
- fobj.write(char * 1024)
- fobj.write(tarfile.TarInfo("foo").tobuf())
- fobj.close()
+ with _open(tmpname, "wb") as fobj:
+ fobj.write(char * 1024)
+ fobj.write(tarfile.TarInfo("foo").tobuf())
tar = tarfile.open(tmpname, mode="r", ignore_zeros=True)
- self.assertListEqual(tar.getnames(), ["foo"],
- "ignore_zeros=True should have skipped the %r-blocks" % char)
- tar.close()
+ try:
+ self.assertListEqual(tar.getnames(), ["foo"],
+ "ignore_zeros=True should have skipped the %r-blocks" %
+ char)
+ finally:
+ tar.close()
class MiscReadTest(CommonReadTest):
@@ -312,8 +319,8 @@
data = fobj.read()
fobj = StringIO.StringIO(data)
fobj.name = ""
- tar = tarfile.open(fileobj=fobj, mode=self.mode)
- self.assertEqual(tar.name, None)
+ with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
+ self.assertEqual(tar.name, None)
def test_illegal_mode_arg(self):
with open(tmpname, 'wb'):
@@ -330,12 +337,14 @@
# Skip the first member and store values from the second member
# of the testtar.
tar = tarfile.open(self.tarname, mode=self.mode)
- tar.next()
- t = tar.next()
- name = t.name
- offset = t.offset
- data = tar.extractfile(t).read()
- tar.close()
+ try:
+ tar.next()
+ t = tar.next()
+ name = t.name
+ offset = t.offset
+ data = tar.extractfile(t).read()
+ finally:
+ tar.close()
# Open the testtar and seek to the offset of the second member.
if self.mode.endswith(":gz"):
@@ -344,27 +353,28 @@
_open = bz2.BZ2File
else:
_open = open
- fobj = _open(self.tarname, "rb")
- fobj.seek(offset)
+ with _open(self.tarname, "rb") as fobj:
+ fobj.seek(offset)
- # Test if the tarfile starts with the second member.
- tar = tar.open(self.tarname, mode="r:", fileobj=fobj)
- t = tar.next()
- self.assertEqual(t.name, name)
- # Read to the end of fileobj and test if seeking back to the
- # beginning works.
- tar.getmembers()
- self.assertEqual(tar.extractfile(t).read(), data,
- "seek back did not work")
- tar.close()
+ # Test if the tarfile starts with the second member.
+ tar = tar.open(self.tarname, mode="r:", fileobj=fobj)
+ t = tar.next()
+ self.assertEqual(t.name, name)
+ # Read to the end of fileobj and test if seeking back to the
+ # beginning works.
+ tar.getmembers()
+ self.assertEqual(tar.extractfile(t).read(), data,
+ "seek back did not work")
+ tar.close()
def test_fail_comp(self):
# For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file.
if self.mode == "r:":
self.skipTest('needs a gz or bz2 mode')
self.assertRaises(tarfile.ReadError, tarfile.open, tarname, self.mode)
- fobj = open(tarname, "rb")
- self.assertRaises(tarfile.ReadError, tarfile.open, fileobj=fobj, mode=self.mode)
+ with open(tarname, "rb") as fobj:
+ self.assertRaises(tarfile.ReadError, tarfile.open,
+ fileobj=fobj, mode=self.mode)
def test_v7_dirtype(self):
# Test old style dirtype member (bug #1336623):
@@ -418,15 +428,17 @@
# Test if extractall() correctly restores directory permissions
# and times (see issue1735).
tar = tarfile.open(tarname, encoding="iso8859-1")
- directories = [t for t in tar if t.isdir()]
- tar.extractall(TEMPDIR, directories)
- for tarinfo in directories:
- path = os.path.join(TEMPDIR, tarinfo.name)
- if sys.platform != "win32":
- # Win32 has no support for fine grained permissions.
- self.assertEqual(tarinfo.mode & 0777, os.stat(path).st_mode & 0777)
- self.assertEqual(tarinfo.mtime, os.path.getmtime(path))
- tar.close()
+ try:
+ directories = [t for t in tar if t.isdir()]
+ tar.extractall(TEMPDIR, directories)
+ for tarinfo in directories:
+ path = os.path.join(TEMPDIR, tarinfo.name)
+ if sys.platform != "win32":
+ # Win32 has no support for fine grained permissions.
+ self.assertEqual(tarinfo.mode & 0777, os.stat(path).st_mode & 0777)
+ self.assertEqual(tarinfo.mtime, os.path.getmtime(path))
+ finally:
+ tar.close()
def test_init_close_fobj(self):
# Issue #7341: Close the internal file object in the TarFile
@@ -434,7 +446,7 @@
# the fact that opening an empty file raises a ReadError.
empty = os.path.join(TEMPDIR, "empty")
with open(empty, "wb") as fobj:
- fobj.write("")
+ fobj.write(b"")
try:
tar = object.__new__(tarfile.TarFile)
@@ -474,42 +486,50 @@
def test_compare_members(self):
tar1 = tarfile.open(tarname, encoding="iso8859-1")
- tar2 = self.tar
+ try:
+ tar2 = self.tar
- while True:
- t1 = tar1.next()
- t2 = tar2.next()
- if t1 is None:
- break
- self.assertTrue(t2 is not None, "stream.next() failed.")
+ while True:
+ t1 = tar1.next()
+ t2 = tar2.next()
+ if t1 is None:
+ break
+ self.assertIsNotNone(t2, "stream.next() failed.")
- if t2.islnk() or t2.issym():
- self.assertRaises(tarfile.StreamError, tar2.extractfile, t2)
- continue
+ if t2.islnk() or t2.issym():
+ with self.assertRaises(tarfile.StreamError):
+ tar2.extractfile(t2)
+ continue
- v1 = tar1.extractfile(t1)
- v2 = tar2.extractfile(t2)
- if v1 is None:
- continue
- self.assertTrue(v2 is not None, "stream.extractfile() failed")
- self.assertTrue(v1.read() == v2.read(), "stream extraction failed")
-
- tar1.close()
+ v1 = tar1.extractfile(t1)
+ v2 = tar2.extractfile(t2)
+ if v1 is None:
+ continue
+ self.assertIsNotNone(v2, "stream.extractfile() failed")
+ self.assertEqual(v1.read(), v2.read(),
+ "stream extraction failed")
+ finally:
+ tar1.close()
class DetectReadTest(unittest.TestCase):
def _testfunc_file(self, name, mode):
try:
- tarfile.open(name, mode)
- except tarfile.ReadError:
+ tar = tarfile.open(name, mode)
+ except tarfile.ReadError as e:
self.fail()
+ else:
+ tar.close()
def _testfunc_fileobj(self, name, mode):
try:
- tarfile.open(name, mode, fileobj=open(name, "rb"))
- except tarfile.ReadError:
+ with open(name, "rb") as f:
+ tar = tarfile.open(name, mode, fileobj=f)
+ except tarfile.ReadError as e:
self.fail()
+ else:
+ tar.close()
def _test_modes(self, testfunc):
testfunc(tarname, "r")
@@ -636,7 +656,8 @@
def test_find_pax_umlauts(self):
self.tar.close()
- self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1")
+ self.tar = tarfile.open(self.tarname, mode=self.mode,
+ encoding="iso8859-1")
tarinfo = self.tar.getmember("pax/umlauts-�������")
self._test_member(tarinfo, size=7011, chksum=md5_regtype)
@@ -667,17 +688,18 @@
offset = tarinfo.offset
self.tar.fileobj.seek(offset)
fobj = StringIO.StringIO(self.tar.fileobj.read(3 * 512))
- self.assertRaises(tarfile.ReadError, tarfile.open, name="foo.tar", fileobj=fobj)
+ with self.assertRaises(tarfile.ReadError):
+ tarfile.open(name="foo.tar", fileobj=fobj)
def test_header_offset(self):
# Test if the start offset of the TarInfo object includes
# the preceding extended header.
longname = self.subdir + "/" + "123/" * 125 + "longname"
offset = self.tar.getmember(longname).offset
- fobj = open(tarname)
- fobj.seek(offset)
- tarinfo = tarfile.TarInfo.frombuf(fobj.read(512))
- self.assertEqual(tarinfo.type, self.longnametype)
+ with open(tarname, "rb") as fobj:
+ fobj.seek(offset)
+ tarinfo = tarfile.TarInfo.frombuf(fobj.read(512))
+ self.assertEqual(tarinfo.type, self.longnametype)
class GNUReadTest(LongnameTest):
@@ -759,44 +781,48 @@
# a trailing '\0'.
name = "0123456789" * 10
tar = tarfile.open(tmpname, self.mode)
- t = tarfile.TarInfo(name)
- tar.addfile(t)
- tar.close()
+ try:
+ t = tarfile.TarInfo(name)
+ tar.addfile(t)
+ finally:
+ tar.close()
tar = tarfile.open(tmpname)
- self.assertTrue(tar.getnames()[0] == name,
- "failed to store 100 char filename")
- tar.close()
+ try:
+ self.assertTrue(tar.getnames()[0] == name,
+ "failed to store 100 char filename")
+ finally:
+ tar.close()
def test_tar_size(self):
# Test for bug #1013882.
tar = tarfile.open(tmpname, self.mode)
- path = os.path.join(TEMPDIR, "file")
- fobj = open(path, "wb")
- fobj.write("aaa")
- fobj.close()
- tar.add(path)
- tar.close()
+ try:
+ path = os.path.join(TEMPDIR, "file")
+ with open(path, "wb") as fobj:
+ fobj.write(b"aaa")
+ tar.add(path)
+ finally:
+ tar.close()
self.assertTrue(os.path.getsize(tmpname) > 0,
"tarfile is empty")
# The test_*_size tests test for bug #1167128.
def test_file_size(self):
tar = tarfile.open(tmpname, self.mode)
+ try:
+ path = os.path.join(TEMPDIR, "file")
+ with open(path, "wb"):
+ pass
+ tarinfo = tar.gettarinfo(path)
+ self.assertEqual(tarinfo.size, 0)
- path = os.path.join(TEMPDIR, "file")
- fobj = open(path, "wb")
- fobj.close()
- tarinfo = tar.gettarinfo(path)
- self.assertEqual(tarinfo.size, 0)
-
- fobj = open(path, "wb")
- fobj.write("aaa")
- fobj.close()
- tarinfo = tar.gettarinfo(path)
- self.assertEqual(tarinfo.size, 3)
-
- tar.close()
+ with open(path, "wb") as fobj:
+ fobj.write(b"aaa")
+ tarinfo = tar.gettarinfo(path)
+ self.assertEqual(tarinfo.size, 3)
+ finally:
+ tar.close()
def test_directory_size(self):
path = os.path.join(TEMPDIR, "directory")
@@ -811,40 +837,41 @@
finally:
os.rmdir(path)
+ @unittest.skipUnless(hasattr(os, "link"),
+ "Missing hardlink implementation")
def test_link_size(self):
- if hasattr(os, "link"):
- link = os.path.join(TEMPDIR, "link")
- target = os.path.join(TEMPDIR, "link_target")
- fobj = open(target, "wb")
- fobj.write("aaa")
- fobj.close()
- os.link(target, link)
+ link = os.path.join(TEMPDIR, "link")
+ target = os.path.join(TEMPDIR, "link_target")
+ with open(target, "wb") as fobj:
+ fobj.write(b"aaa")
+ os.link(target, link)
+ try:
+ tar = tarfile.open(tmpname, self.mode)
try:
- tar = tarfile.open(tmpname, self.mode)
- try:
- # Record the link target in the inodes list.
- tar.gettarinfo(target)
- tarinfo = tar.gettarinfo(link)
- self.assertEqual(tarinfo.size, 0)
- finally:
- tar.close()
+ # Record the link target in the inodes list.
+ tar.gettarinfo(target)
+ tarinfo = tar.gettarinfo(link)
+ self.assertEqual(tarinfo.size, 0)
finally:
- os.remove(target)
- os.remove(link)
+ tar.close()
+ finally:
+ os.remove(target)
+ os.remove(link)
+ @unittest.skipUnless(hasattr(os, "symlink"),
+ "Missing symlink implementation")
def test_symlink_size(self):
- if hasattr(os, "symlink"):
- path = os.path.join(TEMPDIR, "symlink")
- os.symlink("link_target", path)
+ path = os.path.join(TEMPDIR, "symlink")
+ os.symlink("link_target", path)
+ try:
+ tar = tarfile.open(tmpname, self.mode)
try:
- tar = tarfile.open(tmpname, self.mode)
- try:
- tarinfo = tar.gettarinfo(path)
- self.assertEqual(tarinfo.size, 0)
- finally:
- tar.close()
+ tarinfo = tar.gettarinfo(path)
+ self.assertEqual(tarinfo.size, 0)
finally:
- os.remove(path)
+ tar.close()
+ finally:
+ os.remove(path)
def test_add_self(self):
# Test for #1257255.
@@ -939,12 +966,16 @@
os.mkdir(foo)
tar = tarfile.open(tmpname, self.mode)
- tar.add(foo, arcname=path)
- tar.close()
+ try:
+ tar.add(foo, arcname=path)
+ finally:
+ tar.close()
tar = tarfile.open(tmpname, "r")
- t = tar.next()
- tar.close()
+ try:
+ t = tar.next()
+ finally:
+ tar.close()
if not dir:
os.remove(foo)
@@ -1386,7 +1417,8 @@
tar.close()
def test_unicode_filename_error(self):
- tar = tarfile.open(tmpname, "w", format=self.format, encoding="ascii", errors="strict")
+ tar = tarfile.open(tmpname, "w", format=self.format,
+ encoding="ascii", errors="strict")
try:
tarinfo = tarfile.TarInfo()
@@ -1406,7 +1438,8 @@
tar.close()
def test_unicode_argument(self):
- tar = tarfile.open(tarname, "r", encoding="iso8859-1", errors="strict")
+ tar = tarfile.open(tarname, "r",
+ encoding="iso8859-1", errors="strict")
try:
for t in tar:
self.assertTrue(type(t.name) is str)
@@ -1688,7 +1721,8 @@
# the regular files they point to.
def _test_link_extraction(self, name):
self.tar.extract(name, TEMPDIR)
- data = open(os.path.join(TEMPDIR, name), "rb").read()
+ with open(os.path.join(TEMPDIR, name), "rb") as f:
+ data = f.read()
self.assertEqual(md5sum(data), md5_regtype)
def test_hardlink_extraction1(self):
More information about the pypy-commit
mailing list