gh-91078: Return None from TarFile.next when the tarfile is empty (GH-91850)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
This commit is contained in:
Sam Ezeh 2022-11-26 17:57:05 +00:00 committed by GitHub
parent 7796d3179b
commit 78365b8e28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 0 deletions

View file

@ -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")

View file

@ -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

View file

@ -0,0 +1 @@
:meth:`TarFile.next` now returns ``None`` when called on an empty tarfile.