Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f817a48

Browse files
committed
Issues #22468, #21996, #22208: Clarify gettarinfo() and TarInfo usage
* The Windows-specific binary notice was probably a Python 2 thing * Make it more obvious gettarinfo() is based on stat(), and that non-ordinary files may need special care * The file name must be text; suggest dummy arcname as a workaround * Indicate TarInfo may be used directly, not just via gettarinfo()
1 parent 92849d1 commit f817a48

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

Doc/library/tarfile.rst

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -456,21 +456,28 @@ be finalized; only the internally used file object will be closed. See the
456456
.. method:: TarFile.addfile(tarinfo, fileobj=None)
457457

458458
Add the :class:`TarInfo` object *tarinfo* to the archive. If *fileobj* is given,
459+
it should be a :term:`binary file`, and
459460
``tarinfo.size`` bytes are read from it and added to the archive. You can
460-
create :class:`TarInfo` objects using :meth:`gettarinfo`.
461-
462-
.. note::
463-
464-
On Windows platforms, *fileobj* should always be opened with mode ``'rb'`` to
465-
avoid irritation about the file size.
461+
create :class:`TarInfo` objects directly, or by using :meth:`gettarinfo`.
466462

467463

468464
.. method:: TarFile.gettarinfo(name=None, arcname=None, fileobj=None)
469465

470-
Create a :class:`TarInfo` object for either the file *name* or the :term:`file
471-
object` *fileobj* (using :func:`os.fstat` on its file descriptor). You can modify
472-
some of the :class:`TarInfo`'s attributes before you add it using :meth:`addfile`.
473-
If given, *arcname* specifies an alternative name for the file in the archive.
466+
Create a :class:`TarInfo` object from the result of :func:`os.stat` or
467+
equivalent on an existing file. The file is either named by *name*, or
468+
specified as a :term:`file object` *fileobj* with a file descriptor. If
469+
given, *arcname* specifies an alternative name for the file in the
470+
archive, otherwise, the name is taken from *fileobj*’s
471+
:attr:`~io.FileIO.name` attribute, or the *name* argument. The name
472+
should be a text string.
473+
474+
You can modify
475+
some of the :class:`TarInfo`’s attributes before you add it using :meth:`addfile`.
476+
If the file object is not an ordinary file object positioned at the
477+
beginning of the file, attributes such as :attr:`~TarInfo.size` may need
478+
modifying. This is the case for objects such as :class:`~gzip.GzipFile`.
479+
The :attr:`~TarInfo.name` may also be modified, in which case *arcname*
480+
could be a dummy string.
474481

475482

476483
.. method:: TarFile.close()

Lib/tarfile.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,11 +1754,13 @@ def getnames(self):
17541754
return [tarinfo.name for tarinfo in self.getmembers()]
17551755

17561756
def gettarinfo(self, name=None, arcname=None, fileobj=None):
1757-
"""Create a TarInfo object for either the file `name' or the file
1758-
object `fileobj' (using os.fstat on its file descriptor). You can
1759-
modify some of the TarInfo's attributes before you add it using
1760-
addfile(). If given, `arcname' specifies an alternative name for the
1761-
file in the archive.
1757+
"""Create a TarInfo object from the result of os.stat or equivalent
1758+
on an existing file. The file is either named by `name', or
1759+
specified as a file object `fileobj' with a file descriptor. If
1760+
given, `arcname' specifies an alternative name for the file in the
1761+
archive, otherwise, the name is taken from the 'name' attribute of
1762+
'fileobj', or the 'name' argument. The name should be a text
1763+
string.
17621764
"""
17631765
self._check("awx")
17641766

@@ -1779,7 +1781,7 @@ def gettarinfo(self, name=None, arcname=None, fileobj=None):
17791781
# Now, fill the TarInfo object with
17801782
# information specific for the file.
17811783
tarinfo = self.tarinfo()
1782-
tarinfo.tarfile = self
1784+
tarinfo.tarfile = self # Not needed
17831785

17841786
# Use os.stat or os.lstat, depending on platform
17851787
# and if symlinks shall be resolved.
@@ -1946,10 +1948,9 @@ def add(self, name, arcname=None, recursive=True, exclude=None, *, filter=None):
19461948

19471949
def addfile(self, tarinfo, fileobj=None):
19481950
"""Add the TarInfo object `tarinfo' to the archive. If `fileobj' is
1949-
given, tarinfo.size bytes are read from it and added to the archive.
1950-
You can create TarInfo objects using gettarinfo().
1951-
On Windows platforms, `fileobj' should always be opened with mode
1952-
'rb' to avoid irritation about the file size.
1951+
given, it should be a binary file, and tarinfo.size bytes are read
1952+
from it and added to the archive. You can create TarInfo objects
1953+
directly, or by using gettarinfo().
19531954
"""
19541955
self._check("awx")
19551956

0 commit comments

Comments
 (0)