diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-11-26 19:01:20 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-26 19:01:20 (GMT) |
commit | 193a25e89c7f44a0fed0a0534046e6f89769ab36 (patch) | |
tree | 018785a6de3c162164c981d0ce7b1cb8bc23bce2 /Lib | |
parent | fe9957241ad66fd246640b3ff785fe847ecdb766 (diff) | |
download | cpython-193a25e89c7f44a0fed0a0534046e6f89769ab36.zip cpython-193a25e89c7f44a0fed0a0534046e6f89769ab36.tar.gz cpython-193a25e89c7f44a0fed0a0534046e6f89769ab36.tar.bz2 |
gh-91078: Return None from TarFile.next when the tarfile is empty (GH-91850)
(cherry picked from commit 78365b8e283c78e23725748500f48dd2c2ca1161)
Co-authored-by: Sam Ezeh <sam.z.ezeh@gmail.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Diffstat (limited to 'Lib')
-rwxr-xr-x | Lib/tarfile.py | 2 | ||||
-rw-r--r-- | Lib/test/test_tarfile.py | 12 |
2 files changed, 14 insertions, 0 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 169c88d..87f44d9 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -2339,6 +2339,8 @@ class TarFile(object): # Advance the file pointer. if self.offset != self.fileobj.tell(): + if self.offset == 0: + return None self.fileobj.seek(self.offset - 1) if not self.fileobj.read(1): raise ReadError("unexpected end of data") diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index e0389c5..cdfd426 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -734,6 +734,18 @@ class MiscReadTestBase(CommonReadTest): with self.assertRaises(tarfile.ReadError): tarfile.open(self.tarname) + def test_next_on_empty_tarfile(self): + fd = io.BytesIO() + tf = tarfile.open(fileobj=fd, mode="w") + tf.close() + + fd.seek(0) + with tarfile.open(fileobj=fd, mode="r|") as tf: + self.assertEqual(tf.next(), None) + + fd.seek(0) + with tarfile.open(fileobj=fd, mode="r") as tf: + self.assertEqual(tf.next(), None) class MiscReadTest(MiscReadTestBase, unittest.TestCase): test_fail_comp = None |