mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
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:
parent
7796d3179b
commit
78365b8e28
3 changed files with 15 additions and 0 deletions
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
:meth:`TarFile.next` now returns ``None`` when called on an empty tarfile.
|
Loading…
Add table
Add a link
Reference in a new issue