diff options
author | Sam Ezeh <sam.z.ezeh@gmail.com> | 2022-11-26 17:57:05 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-26 17:57:05 (GMT) |
commit | 78365b8e283c78e23725748500f48dd2c2ca1161 (patch) | |
tree | 3f6a48fa957262380ec71700e39737deb139f109 | |
parent | 7796d3179b71536dd1d2ca7fdbc1255bdb8cfb52 (diff) | |
download | cpython-78365b8e283c78e23725748500f48dd2c2ca1161.zip cpython-78365b8e283c78e23725748500f48dd2c2ca1161.tar.gz cpython-78365b8e283c78e23725748500f48dd2c2ca1161.tar.bz2 |
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>
-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 42100e9..b47015f 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 0868d5d..2139320 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. |