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

Skip to content

Commit 2d2d08d

Browse files
committed
Issue #22468: Merge gettarinfo() doc from 3.5
2 parents 7db1d16 + f817a48 commit 2d2d08d

2 files changed

Lines changed: 28 additions & 20 deletions

File tree

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
@@ -1757,11 +1757,13 @@ def getnames(self):
17571757
return [tarinfo.name for tarinfo in self.getmembers()]
17581758

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

@@ -1782,7 +1784,7 @@ def gettarinfo(self, name=None, arcname=None, fileobj=None):
17821784
# Now, fill the TarInfo object with
17831785
# information specific for the file.
17841786
tarinfo = self.tarinfo()
1785-
tarinfo.tarfile = self
1787+
tarinfo.tarfile = self # Not needed
17861788

17871789
# Use os.stat or os.lstat, depending on platform
17881790
# and if symlinks shall be resolved.
@@ -1949,10 +1951,9 @@ def add(self, name, arcname=None, recursive=True, exclude=None, *, filter=None):
19491951

19501952
def addfile(self, tarinfo, fileobj=None):
19511953
"""Add the TarInfo object `tarinfo' to the archive. If `fileobj' is
1952-
given, tarinfo.size bytes are read from it and added to the archive.
1953-
You can create TarInfo objects using gettarinfo().
1954-
On Windows platforms, `fileobj' should always be opened with mode
1955-
'rb' to avoid irritation about the file size.
1954+
given, it should be a binary file, and tarinfo.size bytes are read
1955+
from it and added to the archive. You can create TarInfo objects
1956+
directly, or by using gettarinfo().
19561957
"""
19571958
self._check("awx")
19581959

0 commit comments

Comments
 (0)