classification
Title: tarfile.is_tarfile() modifies file object's current position
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: mateja.and, python-dev
Priority: normal Keywords: patch

Created on 2021-06-02 15:09 by mateja.and, last changed 2021-06-02 15:23 by python-dev.

Pull Requests
URL Status Linked Edit
PR 26488 open python-dev, 2021-06-02 15:23
Messages (1)
msg394922 - (view) Author: Andrzej Mateja (mateja.and) * Date: 2021-06-02 15:09
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']
[]
History
Date User Action Args
2021-06-02 15:23:44python-devsetkeywords: + patch
nosy: + python-dev

pull_requests: + pull_request25084
stage: patch review
2021-06-02 15:09:05mateja.andcreate