[issue44289] tarfile.is_tarfile() modifies file object's current position

2021-06-02 Thread Roundup Robot


Change by Roundup Robot :


--
keywords: +patch
nosy: +python-dev
nosy_count: 1.0 -> 2.0
pull_requests: +25084
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/26488

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44289] tarfile.is_tarfile() modifies file object's current position

2021-06-02 Thread Andrzej Mateja


New submission from Andrzej Mateja :

Since Python 3.9 tarfile.is_tarfile accepts not only paths but also files and 
file-like objects (bpo-29435).

Verification if a file or file-like object is a tar file modifies file object's 
current position.

Imagine a function listing names of all tar archive members but checking first 
if this is a valid tar archive. When its argument is a str or pathlib.Path this 
is quite straightforward. If the argument is a file of file-like object then 
current position must be reset or TarFile.getmembers() returns empty list.

import tarfile


def list_tar(archive):
if tarfile.is_tarfile(archive):
kwargs = {'fileobj' if hasattr(archive, 'read') else 'name': archive}
t = tarfile.open(**kwargs)
return [member.name for member in t.getmembers()]
return []


if __name__ == '__main__':
path = 'archive.tar.gz'
print(list_tar(path))
print(list_tar(open(path, 'rb')))


['spam.py', 'ham.py', 'bacon.py', 'eggs.py']
[]

--
components: Library (Lib)
messages: 394922
nosy: mateja.and
priority: normal
severity: normal
status: open
title: tarfile.is_tarfile() modifies file object's current position
type: behavior
versions: Python 3.10, Python 3.11, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com