]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-91078: Return None from TarFile.next when the tarfile is empty (GH-91850)
authorSam Ezeh <sam.z.ezeh@gmail.com>
Sat, 26 Nov 2022 17:57:05 +0000 (17:57 +0000)
committerGitHub <noreply@github.com>
Sat, 26 Nov 2022 17:57:05 +0000 (09:57 -0800)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS.d/next/Library/2022-04-23-03-46-37.gh-issue-91078.87-hkp.rst [new file with mode: 0644]

index 42100e9a39436e229f4c9e6cd2bb3f9b28595217..b47015f5cb6be5166949e3b84087530b17b09c7a 100755 (executable)
@@ -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")
index 0868d5d6e90915b56c94b47df16055cff3925d94..213932069201b96ef9b10a1e06f64ab1f30ddcca 100644 (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
diff --git a/Misc/NEWS.d/next/Library/2022-04-23-03-46-37.gh-issue-91078.87-hkp.rst b/Misc/NEWS.d/next/Library/2022-04-23-03-46-37.gh-issue-91078.87-hkp.rst
new file mode 100644 (file)
index 0000000..e05d5e2
--- /dev/null
@@ -0,0 +1 @@
+:meth:`TarFile.next` now returns ``None`` when called on an empty tarfile.