本文整理汇总了Python中tarfile.ReadError方法的典型用法代码示例。如果您正苦于以下问题:Python tarfile.ReadError方法的具体用法?Python tarfile.ReadError怎么用?Python tarfile.ReadError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tarfile
的用法示例。
在下文中一共展示了tarfile.ReadError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_empty_tarfile
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def test_empty_tarfile(self):
# Test for issue6123: Allow opening empty archives.
# 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!
with tarfile.open(tmpname, self.mode.replace("r", "w")):
pass
try:
tar = tarfile.open(tmpname, self.mode)
tar.getnames()
except tarfile.ReadError:
self.fail("tarfile.open() failed on empty archive")
else:
self.assertListEqual(tar.getmembers(), [])
finally:
tar.close()
示例2: test_premature_end_of_archive
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def test_premature_end_of_archive(self):
for size in (512, 600, 1024, 1200):
with tarfile.open(tmpname, "w:") as tar:
t = tarfile.TarInfo("foo")
t.size = 1024
tar.addfile(t, StringIO.StringIO("a" * 1024))
with open(tmpname, "r+b") as fobj:
fobj.truncate(size)
with tarfile.open(tmpname) as tar:
with self.assertRaisesRegexp(tarfile.ReadError, "unexpected end of data"):
for t in tar:
pass
with tarfile.open(tmpname) as tar:
t = tar.next()
with self.assertRaisesRegexp(tarfile.ReadError, "unexpected end of data"):
tar.extract(t, TEMPDIR)
with self.assertRaisesRegexp(tarfile.ReadError, "unexpected end of data"):
tar.extractfile(t).read()
示例3: test_init_close_fobj
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def test_init_close_fobj(self):
# Issue #7341: Close the internal file object in the TarFile
# constructor in case of an error. For the test we rely on
# the fact that opening an empty file raises a ReadError.
empty = os.path.join(TEMPDIR, "empty")
with open(empty, "wb") as fobj:
fobj.write("")
try:
tar = object.__new__(tarfile.TarFile)
try:
tar.__init__(empty)
except tarfile.ReadError:
self.assertTrue(tar.fileobj.closed)
else:
self.fail("ReadError not raised")
finally:
support.unlink(empty)
示例4: _test_partial_input
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def _test_partial_input(self, mode):
class MyStringIO(StringIO.StringIO):
hit_eof = False
def read(self, n):
if self.hit_eof:
raise AssertionError("infinite loop detected in tarfile.open()")
self.hit_eof = self.pos == self.len
return StringIO.StringIO.read(self, n)
def seek(self, *args):
self.hit_eof = False
return StringIO.StringIO.seek(self, *args)
data = bz2.compress(tarfile.TarInfo("foo").tobuf())
for x in range(len(data) + 1):
try:
tarfile.open(fileobj=MyStringIO(data[:x]), mode=mode)
except tarfile.ReadError:
pass # we have no interest in ReadErrors
示例5: test_init_close_fobj
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def test_init_close_fobj(self):
# Issue #7341: Close the internal file object in the TarFile
# constructor in case of an error. For the test we rely on
# the fact that opening an empty file raises a ReadError.
empty = os.path.join(TEMPDIR, "empty")
open(empty, "wb").write("")
try:
tar = object.__new__(tarfile.TarFile)
try:
tar.__init__(empty)
except tarfile.ReadError:
self.assertTrue(tar.fileobj.closed)
else:
self.fail("ReadError not raised")
finally:
os.remove(empty)
示例6: test_premature_end_of_archive
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def test_premature_end_of_archive(self):
for size in (512, 600, 1024, 1200):
with tarfile.open(tmpname, "w:") as tar:
t = tarfile.TarInfo("foo")
t.size = 1024
tar.addfile(t, io.BytesIO(b"a" * 1024))
with open(tmpname, "r+b") as fobj:
fobj.truncate(size)
with tarfile.open(tmpname) as tar:
with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
for t in tar:
pass
with tarfile.open(tmpname) as tar:
t = tar.next()
with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
tar.extract(t, TEMPDIR)
with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
tar.extractfile(t).read()
示例7: test_init_close_fobj
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def test_init_close_fobj(self):
# Issue #7341: Close the internal file object in the TarFile
# constructor in case of an error. For the test we rely on
# the fact that opening an empty file raises a ReadError.
empty = os.path.join(TEMPDIR, "empty")
with open(empty, "wb") as fobj:
fobj.write(b"")
try:
tar = object.__new__(tarfile.TarFile)
try:
tar.__init__(empty)
except tarfile.ReadError:
self.assertTrue(tar.fileobj.closed)
else:
self.fail("ReadError not raised")
finally:
support.unlink(empty)
示例8: test_null_tarfile
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def test_null_tarfile(self):
# Test for issue6123: Allow opening empty archives.
# This test guarantees that tarfile.open() does not treat an empty
# file as an empty tar archive.
with open(tmpname, "wb"):
pass
self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, self.mode)
self.assertRaises(tarfile.ReadError, tarfile.open, tmpname)
示例9: test_fail_comp
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
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)
with open(tarname, "rb") as fobj:
self.assertRaises(tarfile.ReadError, tarfile.open,
fileobj=fobj, mode=self.mode)
示例10: _testfunc_fileobj
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def _testfunc_fileobj(self, name, mode):
try:
tar = tarfile.open(name, mode, fileobj=open(name, "rb"))
except tarfile.ReadError:
self.fail()
else:
tar.close()
示例11: _test_modes
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def _test_modes(self, testfunc):
testfunc(tarname, "r")
testfunc(tarname, "r:")
testfunc(tarname, "r:*")
testfunc(tarname, "r|")
testfunc(tarname, "r|*")
if gzip:
self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:gz")
self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|gz")
self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r:")
self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r|")
testfunc(gzipname, "r")
testfunc(gzipname, "r:*")
testfunc(gzipname, "r:gz")
testfunc(gzipname, "r|*")
testfunc(gzipname, "r|gz")
if bz2:
self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:bz2")
self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|bz2")
self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r:")
self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r|")
testfunc(bz2name, "r")
testfunc(bz2name, "r:*")
testfunc(bz2name, "r:bz2")
testfunc(bz2name, "r|*")
testfunc(bz2name, "r|bz2")
示例12: test_truncated_longname
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def test_truncated_longname(self):
longname = self.subdir + "/" + "123/" * 125 + "longname"
tarinfo = self.tar.getmember(longname)
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)
示例13: test_append_gz
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def test_append_gz(self):
self._create_testtar("w:gz")
self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
示例14: test_append_bz2
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def test_append_bz2(self):
self._create_testtar("w:bz2")
self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
# Append mode is supposed to fail if the tarfile to append to
# does not end with a zero block.
示例15: _detect_filetype
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import ReadError [as 别名]
def _detect_filetype(base_url):
try:
resp_obj = urlopen(base_url);
resp = resp_obj.read()
if "text" in resp_obj.headers['Content-Type']:
logger.info("[+] Response of '%s' is HTML. - Content-Type: %s" % (base_url, resp_obj.headers['Content-Type']))
return HttpImporter.WEB_ARCHIVE, resp
except Exception as e: # Base URL is not callable in GitHub /raw/ contents - returns 400 Error
logger.info("[!] Response of '%s' triggered '%s'" % (base_url, e))
return HttpImporter.WEB_ARCHIVE, None
resp_io = io.BytesIO(resp)
try:
tar = tarfile.open(fileobj=resp_io, mode='r:*')
logger.info("[+] Response of '%s' is a Tarball" % base_url)
return HttpImporter.TAR_ARCHIVE, tar
except tarfile.ReadError:
logger.info("Response of '%s' is not a (compressed) tarball" % base_url)
try:
zip = zipfile.ZipFile(resp_io)
logger.info("[+] Response of '%s' is a ZIP file" % base_url)
return HttpImporter.ZIP_ARCHIVE, zip
except zipfile.BadZipfile:
logger.info("Response of '%s' is not a ZIP file" % base_url)
raise IOError("Content of URL '%s' is Invalid" % base_url)