Python tar.extractall()无法识别意外的EOF

Python tar.extractall()无法识别意外的EOF,python,tar,eof,Python,Tar,Eof,Pythontarfile库未检测到损坏的tar user@host$ wc -c good.tar 143360 good.tar user@host$ head -c 130000 good.tar > cut.tar user@host$ tar -tf cut.tar ... tar: Unexpected EOF in archive tar: Error is not recoverable: exiting now 非常好,命令行工具可以识别意外的EOF user@h

Python
tarfile
库未检测到损坏的tar

user@host$ wc -c good.tar
143360 good.tar

user@host$ head -c 130000 good.tar > cut.tar

user@host$ tar -tf cut.tar 
...
tar: Unexpected EOF in archive
tar: Error is not recoverable: exiting now
非常好,命令行工具可以识别意外的EOF

user@host$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
>>> import tarfile
>>> tar=tarfile.open('cut.tar')
>>> tar.extractall()
不太好。Python库对文件进行解码,但不会引发异常

如何使用Python库检测意外的EOF?我想避免使用
子流程
模块


参数
errorlevel
没有帮助。我尝试了errorlevel=1和errorlevel=2。我写了一个变通方法。它适用于我的tar文件。我猜它不支持所有类型的对象,这些对象可以存储在tar文件中

# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals, print_function
import os
import tarfile

class TarfileWhichRaisesOnEOF(tarfile.TarFile):
    def extractall(self, path=".", members=None):
        super(TarfileWhichRaisesOnEOF, self).extractall(path, members)
        if members is None:
            members = self

        for tarinfo in members:
            if not tarinfo.isfile():
                continue
            file=os.path.join(path, tarinfo.name)
            size_real=os.path.getsize(file)
            if size_real!=tarinfo.size:
                raise tarfile.ExtractError('Extracting %s: Size does not match. According to tarinfo %s and on disk %s' % (
                    tarinfo, tarinfo.size, size_real))

这在Python3中得到了修复——无论
errorlevel设置如何,都会引发
OSError

!你应该考虑打开bug报告并提交你的报告。solution@knitti我打开了一个bug报告:遗憾的是,我不能在现有的bug报告中添加一些赏金……你用的是哪种焦油?我的没有引发错误。我使用tar(gnutar)1.27.1。python.org上的bug报告(见上文)有一个损坏的tar_,该tar_是用于测试的。这意味着Python3的更改在这里没有帮助。@guettli:你试过3.4吗?请加一条说明。我试图用Python 3.4.0提取所有()上传的tar_,它是_cut.tar。它引发了一个错误-好。只有2.7%受影响?