[Python-checkins] gh-91078: Return None from TarFile.next when the tarfile is empty (GH-91850)
ethanfurman
webhook-mailer at python.org
Sat Nov 26 12:57:10 EST 2022
https://github.com/python/cpython/commit/78365b8e283c78e23725748500f48dd2c2ca1161
commit: 78365b8e283c78e23725748500f48dd2c2ca1161
branch: main
author: Sam Ezeh <sam.z.ezeh at gmail.com>
committer: ethanfurman <ethan at stoneleaf.us>
date: 2022-11-26T09:57:05-08:00
summary:
gh-91078: Return None from TarFile.next when the tarfile is empty (GH-91850)
Co-authored-by: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Library/2022-04-23-03-46-37.gh-issue-91078.87-hkp.rst
M Lib/tarfile.py
M Lib/test/test_tarfile.py
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 42100e9a3943..b47015f5cb6b 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -2339,6 +2339,8 @@ def next(self):
# 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 0868d5d6e909..213932069201 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -734,6 +734,18 @@ def test_zlib_error_does_not_leak(self):
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 000000000000..e05d5e2a1314
--- /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.
More information about the Python-checkins
mailing list