Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2484,7 +2484,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()
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ def test_is_tarfile_valid(self):
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
Copy link
Contributor

@akulakov akulakov Feb 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In documentation, bold highlight (with asterisks) is generally used for parameter names, for example see https://github.com/python/cpython/edit/main/Doc/whatsnew/3.10.rst line 913

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Thanks. I've also noticed that here and there monospace is used and I'm not sure why.

https://github.com/python/cpython/blame/main/Doc/whatsnew/3.10.rst#L994
https://github.com/python/cpython/blame/main/Doc/whatsnew/3.10.rst#L1015

Fixed already. Happy to follow guidelines.