Skip to content

bpo-29435: Allow is_tarfile to take a filelike obj #18090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 23, 2020
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
5 changes: 4 additions & 1 deletion Doc/library/tarfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ Some facts and figures:
.. function:: is_tarfile(name)

Return :const:`True` if *name* is a tar archive file, that the :mod:`tarfile`
module can read.
module can read. *name* may be a file or file-like object too.
Copy link
Member

Choose a reason for hiding this comment

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

Change last sentence to:

*name* may be a :class:`str`, file, or file-like object.


.. versionchanged:: 3.9
Support for file and file-like objects.


The :mod:`tarfile` module defines the following exceptions:
Expand Down
7 changes: 6 additions & 1 deletion Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2461,9 +2461,14 @@ def __exit__(self, type, value, traceback):
def is_tarfile(name):
"""Return True if name points to a tar archive that we
are able to handle, else return False.

The name argument may be a file or file-like object.
Copy link
Member

Choose a reason for hiding this comment

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

Change last sentence to:

'name' should be a string, file, or file-like object.

"""
try:
t = open(name)
if hasattr(name, "read"):
t = open(fileobj=name)
else:
t = open(name)
t.close()
return True
except TarError:
Expand Down
32 changes: 32 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,38 @@ class LzmaListTest(LzmaTest, ListTest):

class CommonReadTest(ReadTest):

def test_is_tarfile_erroneous(self):
with open(tmpname, "wb"):
pass

# is_tarfile works on filenames
self.assertFalse(tarfile.is_tarfile(tmpname))

# is_tarfile works on path-like objects
self.assertFalse(tarfile.is_tarfile(pathlib.Path(tmpname)))

# is_tarfile works on file objects
with open(tmpname, "rb") as fobj:
self.assertFalse(tarfile.is_tarfile(fobj))

# is_tarfile works on file-like objects
self.assertFalse(tarfile.is_tarfile(io.BytesIO(b"invalid")))

def test_is_tarfile_valid(self):
# is_tarfile works on filenames
self.assertTrue(tarfile.is_tarfile(self.tarname))

# is_tarfile works on path-like objects
self.assertTrue(tarfile.is_tarfile(pathlib.Path(self.tarname)))

# is_tarfile works on file objects
with open(self.tarname, "rb") as fobj:
self.assertTrue(tarfile.is_tarfile(fobj))

# is_tarfile works on file-like objects
with open(self.tarname, "rb") as fobj:
self.assertTrue(tarfile.is_tarfile(io.BytesIO(fobj.read())))

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,2 @@
Allow :func:`tarfile.is_tarfile` to be used with file and file-like
objects, like :func:`zipfile.is_zipfile`. Patch by William Woodruff.