diff options
author | Robert O'Shea <PurityLake@users.noreply.github.com> | 2023-05-23 20:44:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-23 20:44:40 (GMT) |
commit | 50fce89d123b25e53fa8a0303a169e8887154a0e (patch) | |
tree | faad5026839dab7357dec012a5621450b2311a87 /Lib/tarfile.py | |
parent | 097b7830cd67f039ff36ba4fa285d82d26e25e84 (diff) | |
download | cpython-50fce89d123b25e53fa8a0303a169e8887154a0e.zip cpython-50fce89d123b25e53fa8a0303a169e8887154a0e.tar.gz cpython-50fce89d123b25e53fa8a0303a169e8887154a0e.tar.bz2 |
gh-102120: [TarFile] Add an iter function that doesn't cache (GH-102128)
Diffstat (limited to 'Lib/tarfile.py')
-rwxr-xr-x | Lib/tarfile.py | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 7781a43..df4e41f 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1633,7 +1633,7 @@ class TarFile(object): def __init__(self, name=None, mode="r", fileobj=None, format=None, tarinfo=None, dereference=None, ignore_zeros=None, encoding=None, errors="surrogateescape", pax_headers=None, debug=None, - errorlevel=None, copybufsize=None): + errorlevel=None, copybufsize=None, stream=False): """Open an (uncompressed) tar archive `name'. `mode' is either 'r' to read from an existing archive, 'a' to append data to an existing file or 'w' to create a new file overwriting an existing one. `mode' @@ -1665,6 +1665,8 @@ class TarFile(object): self.name = os.path.abspath(name) if name else None self.fileobj = fileobj + self.stream = stream + # Init attributes. if format is not None: self.format = format @@ -2631,7 +2633,9 @@ class TarFile(object): break if tarinfo is not None: - self.members.append(tarinfo) + # if streaming the file we do not want to cache the tarinfo + if not self.stream: + self.members.append(tarinfo) else: self._loaded = True @@ -2682,11 +2686,12 @@ class TarFile(object): def _load(self): """Read through the entire archive file and look for readable - members. + members. This should not run if the file is set to stream. """ - while self.next() is not None: - pass - self._loaded = True + if not self.stream: + while self.next() is not None: + pass + self._loaded = True def _check(self, mode=None): """Check if TarFile is still open, and if the operation's mode |