mirror of https://github.com/python/cpython
bpo-44289: Keep argument file object's current position in tarfile.is_tarfile (GH-26488)
This commit is contained in:
parent
d2d1d49eac
commit
128ab092ca
|
@ -2493,7 +2493,9 @@ def is_tarfile(name):
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
if hasattr(name, "read"):
|
if hasattr(name, "read"):
|
||||||
|
pos = name.tell()
|
||||||
t = open(fileobj=name)
|
t = open(fileobj=name)
|
||||||
|
name.seek(pos)
|
||||||
else:
|
else:
|
||||||
t = open(name)
|
t = open(name)
|
||||||
t.close()
|
t.close()
|
||||||
|
|
|
@ -375,6 +375,18 @@ class CommonReadTest(ReadTest):
|
||||||
with open(self.tarname, "rb") as fobj:
|
with open(self.tarname, "rb") as fobj:
|
||||||
self.assertTrue(tarfile.is_tarfile(io.BytesIO(fobj.read())))
|
self.assertTrue(tarfile.is_tarfile(io.BytesIO(fobj.read())))
|
||||||
|
|
||||||
|
def test_is_tarfile_keeps_position(self):
|
||||||
|
# Test for issue44289: tarfile.is_tarfile() modifies
|
||||||
|
# file object's current position
|
||||||
|
with open(self.tarname, "rb") as fobj:
|
||||||
|
tarfile.is_tarfile(fobj)
|
||||||
|
self.assertEqual(fobj.tell(), 0)
|
||||||
|
|
||||||
|
with open(self.tarname, "rb") as fobj:
|
||||||
|
file_like = io.BytesIO(fobj.read())
|
||||||
|
tarfile.is_tarfile(file_like)
|
||||||
|
self.assertEqual(file_like.tell(), 0)
|
||||||
|
|
||||||
def test_empty_tarfile(self):
|
def test_empty_tarfile(self):
|
||||||
# Test for issue6123: Allow opening empty archives.
|
# Test for issue6123: Allow opening empty archives.
|
||||||
# This test checks if tarfile.open() is able to open an empty tar
|
# This test checks if tarfile.open() is able to open an empty tar
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix an issue with :meth:`~tarfile.is_tarfile` method when using *fileobj* argument: position in the *fileobj* was advanced forward which made it unreadable with :meth:`tarfile.TarFile.open`.
|
Loading…
Reference in New Issue