diff options
-rwxr-xr-x | Lib/tarfile.py | 2 | ||||
-rw-r--r-- | Lib/test/test_tarfile.py | 12 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-04-23-03-46-37.gh-issue-91078.87-hkp.rst | 1 |
3 files changed, 15 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 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 index 0000000..e05d5e2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-04-23-03-46-37.gh-issue-91078.87-hkp.rst @@ -0,0 +1 @@ +:meth:`TarFile.next` now returns ``None`` when called on an empty tarfile. |