Python tarfile.ReadError() Examples

The following are 30 code examples of tarfile.ReadError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tarfile , or try the search function .
Example #1
Source File: test_tarfile.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
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() 
Example #2
Source File: test_tarfile.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
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) 
Example #3
Source File: test_tarfile.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
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() 
Example #4
Source File: test_tarfile.py    From gcblue with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #5
Source File: test_tarfile.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
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() 
Example #6
Source File: test_tarfile.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
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 
Example #7
Source File: test_tarfile.py    From BinderFilter with MIT License 6 votes vote down vote up
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) 
Example #8
Source File: test_tarfile.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
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) 
Example #9
Source File: test_tarfile.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
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() 
Example #10
Source File: test_tarfile.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
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() 
Example #11
Source File: test_tarfile.py    From oss-ftp with MIT License 6 votes vote down vote up
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) 
Example #12
Source File: test_tarfile.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #13
Source File: test_tarfile.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
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() 
Example #14
Source File: tarlayerformat.py    From quay with Apache License 2.0 5 votes vote down vote up
def _tar_file_from_stream(stream):
        tar_file = None
        try:
            tar_file = tarfile.open(mode="r|*", fileobj=stream)
        except tarfile.ReadError as re:
            if str(re) != "empty file":
                raise TarLayerReadException("Could not read layer")

        return tar_file 
Example #15
Source File: readable.py    From wextracto with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def readables_from_file_path(path):
    """ Yield readables from a file system path """

    numdirs = 0
    for dirpath, dirnames, filenames in os.walk(path):
        numdirs += 1
        # Don't walk into "hidden" directories
        dirnames[:] = [n for n in dirnames if not n.startswith('.')]
        for filename in filenames:
            if filename.lower().endswith(EXT_WEXIN):
                filepath = os.path.join(dirpath, filename)
                yield Open(partial(FileIO, filepath))

    if numdirs < 1:
        if path.lower().endswith('EXT_WEXIN'):
            yield Open(partial(FileIO, path))
        else:
            try:
                tf = tarfile_open(path)
                for readable in readables_from_tarfile(tf):
                    yield readable
            except IOError as exc:
                if exc.errno != errno.ENOENT:
                    raise
                # we want to defer the ENOENT error to later
                # so we do that by yielding an Open(...)
                yield Open(partial(FileIO, path))
            except tarfile.ReadError:
                yield Open(partial(FileIO, path)) 
Example #16
Source File: test_tarfile.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
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) 
Example #17
Source File: test_tarfile.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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!
        tarfile.open(tmpname, self.mode.replace("r", "w")).close()
        try:
            tar = tarfile.open(tmpname, self.mode)
            tar.getnames()
        except tarfile.ReadError:
            self.fail("tarfile.open() failed on empty archive")
        self.assertListEqual(tar.getmembers(), []) 
Example #18
Source File: test_tarfile.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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. 
Example #19
Source File: decompression.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def _is_tar(self, response):
        archive = BytesIO(response.body)
        try:
            tar_file = tarfile.open(name=mktemp(), fileobj=archive)
        except tarfile.ReadError:
            return

        body = tar_file.extractfile(tar_file.members[0]).read()
        respcls = responsetypes.from_args(filename=tar_file.members[0].name, body=body)
        return response.replace(body=body, cls=respcls) 
Example #20
Source File: decompression.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def _is_tar(self, response):
        archive = BytesIO(response.body)
        try:
            tar_file = tarfile.open(name=mktemp(), fileobj=archive)
        except tarfile.ReadError:
            return

        body = tar_file.extractfile(tar_file.members[0]).read()
        respcls = responsetypes.from_args(filename=tar_file.members[0].name, body=body)
        return response.replace(body=body, cls=respcls) 
Example #21
Source File: upload_manager.py    From cloudify-manager with Apache License 2.0 5 votes vote down vote up
def _extract(src, dest):
            try:
                tarfile_ = tarfile.open(name=src)
            except tarfile.ReadError:
                raise UploadedCaravanManager.InvalidCaravanException(
                    'Failed to load caravan file'
                )
            try:
                # Get the top level dir
                root_dir = tarfile_.getmembers()[0]
                tarfile_.extractall(path=dest, members=tarfile_.getmembers())
            finally:
                tarfile_.close()
            return os.path.join(dest, root_dir.path) 
Example #22
Source File: test_tarfile.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def _test_error(self, data):
        with open(self.tarname, "wb") as fobj:
            fobj.write(data)
        self.assertRaises(tarfile.ReadError, self._add_testfile) 
Example #23
Source File: test_tarfile.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_append_compressed(self):
        self._create_testtar("w:" + self.suffix)
        self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a") 
Example #24
Source File: test_tarfile.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
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 = io.BytesIO(self.tar.fileobj.read(3 * 512))
        with self.assertRaises(tarfile.ReadError):
            tarfile.open(name="foo.tar", fileobj=fobj) 
Example #25
Source File: test_tarfile.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def _test_modes(self, testfunc):
        if self.suffix:
            with self.assertRaises(tarfile.ReadError):
                tarfile.open(tarname, mode="r:" + self.suffix)
            with self.assertRaises(tarfile.ReadError):
                tarfile.open(tarname, mode="r|" + self.suffix)
            with self.assertRaises(tarfile.ReadError):
                tarfile.open(self.tarname, mode="r:")
            with self.assertRaises(tarfile.ReadError):
                tarfile.open(self.tarname, mode="r|")
        testfunc(self.tarname, "r")
        testfunc(self.tarname, "r:" + self.suffix)
        testfunc(self.tarname, "r:*")
        testfunc(self.tarname, "r|" + self.suffix)
        testfunc(self.tarname, "r|*") 
Example #26
Source File: test_tarfile.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def _testfunc_fileobj(self, name, mode):
        try:
            with open(name, "rb") as f:
                tar = tarfile.open(name, mode, fileobj=f)
        except tarfile.ReadError as e:
            self.fail()
        else:
            tar.close() 
Example #27
Source File: test_tarfile.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _test_error(self, data):
        open(self.tarname, "wb").write(data)
        self.assertRaises(tarfile.ReadError, self._add_testfile) 
Example #28
Source File: test_tarfile.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_fail_comp(self):
        # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file.
        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) 
Example #29
Source File: ratarmount.py    From ratarmount with MIT License 5 votes vote down vote up
def _detectCompression( name = None, fileobj = None ):
        oldOffset = None
        if fileobj:
            assert fileobj.seekable()
            oldOffset = fileobj.tell()
            if name is None:
                name = fileobj.name

        for compression in [ '', 'bz2', 'gz', 'xz' ]:
            try:
                # Simply opening a TAR file should be fast as only the header should be read!
                tarfile.open( name = name, fileobj = fileobj, mode = 'r:' + compression )

                if compression == 'bz2' and 'IndexedBzip2File' not in globals():
                    raise Exception( "Can't open a bzip2 compressed TAR file '{}' without indexed_bzip2 module!"
                                     .format( name ) )
                elif compression == 'gz' and 'IndexedGzipFile' not in globals():
                    raise Exception( "Can't open a bzip2 compressed TAR file '{}' without indexed_gzip module!"
                                     .format( name ) )
                elif compression == 'xz':
                    raise Exception( "Can't open xz compressed TAR file '{}'!".format( name ) )

                if oldOffset is not None:
                    fileobj.seek( oldOffset )
                return compression

            except tarfile.ReadError as e:
                if oldOffset is not None:
                    fileobj.seek( oldOffset )
                pass

        raise Exception( "File '{}' does not seem to be a valid TAR file!".format( name ) ) 
Example #30
Source File: test_tarfile.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_append_gz(self):
        self._create_testtar("w:gz")
        self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")