summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrzej Mateja <mateja.and@gmail.com>2022-02-09 16:19:16 (GMT)
committerGitHub <noreply@github.com>2022-02-09 16:19:16 (GMT)
commit128ab092cad984b73a117f58fa0e9b4105051a04 (patch)
treec3fc300d66918d8bc177b79dca93cec060fbd26a
parentd2d1d49eaccaa83eb8873ba15f2fc9562143bc56 (diff)
downloadcpython-128ab092cad984b73a117f58fa0e9b4105051a04.zip
cpython-128ab092cad984b73a117f58fa0e9b4105051a04.tar.gz
cpython-128ab092cad984b73a117f58fa0e9b4105051a04.tar.bz2
bpo-44289: Keep argument file object's current position in tarfile.is_tarfile (GH-26488)
-rwxr-xr-xLib/tarfile.py2
-rw-r--r--Lib/test/test_tarfile.py12
-rw-r--r--Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst1
3 files changed, 15 insertions, 0 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index e187da2..e795100 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -2493,7 +2493,9 @@ def is_tarfile(name):
"""
try:
if hasattr(name, "read"):
+ pos = name.tell()
t = open(fileobj=name)
+ name.seek(pos)
else:
t = open(name)
t.close()
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 1357df5..66c1931 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -375,6 +375,18 @@ class CommonReadTest(ReadTest):
with open(self.tarname, "rb") as fobj:
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):
# Test for issue6123: Allow opening empty archives.
# This test checks if tarfile.open() is able to open an empty tar
diff --git a/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst b/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst
new file mode 100644
index 0000000..164138f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-06-02-19-47-46.bpo-44289.xC5kuV.rst
@@ -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`.