Skip to content

Commit fe5e983

Browse files
committed
bpo-29435: Allow is_tarfile to take a filelike obj
1 parent a96e06d commit fe5e983

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Lib/tarfile.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2461,9 +2461,14 @@ def __exit__(self, type, value, traceback):
24612461
def is_tarfile(name):
24622462
"""Return True if name points to a tar archive that we
24632463
are able to handle, else return False.
2464+
2465+
The name argument may be a file or file-like object.
24642466
"""
24652467
try:
2466-
t = open(name)
2468+
if hasattr(name, "read"):
2469+
t = open(fileobj=name)
2470+
else:
2471+
t = open(name)
24672472
t.close()
24682473
return True
24692474
except TarError:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Allow :func:`tarfile.is_tarfile` to be used with file and file-like
2+
objects, like :func:`zipfile.is_zipfile`. Patch by William Woodruff.

0 commit comments

Comments
 (0)