当前位置: 首页>>代码示例>>Python>>正文


Python tarfile.ReadError方法代码示例

本文整理汇总了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() 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:18,代码来源:test_tarfile.py

示例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() 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:25,代码来源:test_tarfile.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:20,代码来源:test_tarfile.py

示例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 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:20,代码来源:test_tarfile.py

示例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) 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:19,代码来源:test_tarfile.py

示例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() 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:25,代码来源:test_tarfile.py

示例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) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:20,代码来源:test_tarfile.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:10,代码来源:test_tarfile.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:10,代码来源:test_tarfile.py

示例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() 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:9,代码来源:test_tarfile.py

示例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") 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:32,代码来源:test_tarfile.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:9,代码来源:test_tarfile.py

示例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") 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:5,代码来源:test_tarfile.py

示例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. 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:8,代码来源:test_tarfile.py

示例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) 
开发者ID:operatorequals,项目名称:httpimport,代码行数:30,代码来源:httpimport.py


注:本文中的tarfile.ReadError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。