# HG changeset patch # Parent ce39a4ec29067ef1aac5cfc45c8caf8222b140a2 diff --git a/Lib/tarfile.py b/Lib/tarfile.py --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -758,6 +758,13 @@ self.closed = True #class _FileInFile +class ExFileObject(io.BufferedReader): + + def __init__(self, tarfile, tarinfo): + fileobj = _FileInFile(tarfile.fileobj, tarinfo.offset_data, + tarinfo.size, tarinfo.sparse) + super().__init__(fileobj) +#class ExFileObject #------------------ # Exported Classes @@ -1443,8 +1450,7 @@ tarinfo = TarInfo # The default TarInfo class to use. - fileobject = None # The file-object for extractfile() or - # io.BufferedReader if None. + fileobject = ExFileObject # The file-object for extractfile(). def __init__(self, name=None, mode="r", fileobj=None, format=None, tarinfo=None, dereference=None, ignore_zeros=None, encoding=None, @@ -2081,12 +2087,7 @@ if tarinfo.isreg() or tarinfo.type not in SUPPORTED_TYPES: # Members with unknown types are treated as regular files. - if self.fileobject is None: - fileobj = _FileInFile(self.fileobj, tarinfo.offset_data, tarinfo.size, tarinfo.sparse) - return io.BufferedReader(fileobj) - else: - # Keep the traditional pre-3.3 API intact. - return self.fileobject(self, tarinfo) + return self.fileobject(self, tarinfo) elif tarinfo.islnk() or tarinfo.issym(): if isinstance(self.fileobj, _Stream): diff --git a/Misc/NEWS b/Misc/NEWS --- a/Misc/NEWS +++ b/Misc/NEWS @@ -20,6 +20,8 @@ Library ------- +- Issue #13815: TarFile.extractfile() now returns io.BufferedReader objects. + - Issue #14583: Fix importlib bug when a package's __init__.py would first import one of its modules then raise an error. @@ -137,8 +139,6 @@ Library ------- -- Issue #13815: TarFile.extractfile() now returns io.BufferedReader objects. - - Issue #14768: os.path.expanduser('~/a') doesn't works correctly when HOME is '/'. - Issue #14371: Support bzip2 in zipfile module. Patch by Serhiy Storchaka.