]> granicus.if.org Git - python/commitdiff
Issues #22468, #21996, #22208: Clarify gettarinfo() and TarInfo usage
authorMartin Panter <vadmium+py@gmail.com>
Fri, 19 Feb 2016 23:34:56 +0000 (23:34 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Fri, 19 Feb 2016 23:34:56 +0000 (23:34 +0000)
* Make it more obvious gettarinfo() is based on stat(), and that non-ordinary
  files may need special care
* Filename taken from fileobj.name; suggest dummy arcname as a workaround
* Indicate TarInfo may be used directly, not just via gettarinfo()

Doc/library/tarfile.rst
Lib/tarfile.py

index 2135db482af8a367c41c9f2af6c26129ecf434d9..019c6850dadc8739d7df01008614f5a95dabb11f 100644 (file)
@@ -438,20 +438,29 @@ be finalized; only the internally used file object will be closed. See the
 
    Add the :class:`TarInfo` object *tarinfo* to the archive. If *fileobj* is given,
    ``tarinfo.size`` bytes are read from it and added to the archive.  You can
-   create :class:`TarInfo` objects using :meth:`gettarinfo`.
+   create :class:`TarInfo` objects directly, or by using :meth:`gettarinfo`.
 
    .. note::
-
       On Windows platforms, *fileobj* should always be opened with mode ``'rb'`` to
       avoid irritation about the file size.
 
 
 .. method:: TarFile.gettarinfo(name=None, arcname=None, fileobj=None)
 
-   Create a :class:`TarInfo` object for either the file *name* or the file object
-   *fileobj* (using :func:`os.fstat` on its file descriptor).  You can modify some
-   of the :class:`TarInfo`'s attributes before you add it using :meth:`addfile`.
-   If given, *arcname* specifies an alternative name for the file in the archive.
+   Create a :class:`TarInfo` object from the result of :func:`os.stat` or
+   equivalent on an existing file.  The file is either named by *name*, or
+   specified as a file object *fileobj* with a file descriptor.  If
+   given, *arcname* specifies an alternative name for the file in the
+   archive, otherwise, the name is taken from *fileobj*’s
+   :attr:`~file.name` attribute, or the *name* argument.
+
+   You can modify some
+   of the :class:`TarInfo`’s attributes before you add it using :meth:`addfile`.
+   If the file object is not an ordinary file object positioned at the
+   beginning of the file, attributes such as :attr:`~TarInfo.size` may need
+   modifying.  This is the case for objects such as :class:`~gzip.GzipFile`.
+   The :attr:`~TarInfo.name` may also be modified, in which case *arcname*
+   could be a dummy string.
 
 
 .. method:: TarFile.close()
index 66e5393afe5452c7cb298622e1922e06c6fd3c78..be688005f456d6a60e4f2d3c758fc03300ac6b14 100644 (file)
@@ -1844,11 +1844,12 @@ class TarFile(object):
         return [tarinfo.name for tarinfo in self.getmembers()]
 
     def gettarinfo(self, name=None, arcname=None, fileobj=None):
-        """Create a TarInfo object for either the file `name' or the file
-           object `fileobj' (using os.fstat on its file descriptor). You can
-           modify some of the TarInfo's attributes before you add it using
-           addfile(). If given, `arcname' specifies an alternative name for the
-           file in the archive.
+        """Create a TarInfo object from the result of os.stat or equivalent
+           on an existing file. The file is either named by `name', or
+           specified as a file object `fileobj' with a file descriptor. If
+           given, `arcname' specifies an alternative name for the file in the
+           archive, otherwise, the name is taken from the 'name' attribute of
+           'fileobj', or the 'name' argument.
         """
         self._check("aw")
 
@@ -1869,7 +1870,7 @@ class TarFile(object):
         # Now, fill the TarInfo object with
         # information specific for the file.
         tarinfo = self.tarinfo()
-        tarinfo.tarfile = self
+        tarinfo.tarfile = self  # Not needed
 
         # Use os.stat or os.lstat, depending on platform
         # and if symlinks shall be resolved.
@@ -2034,7 +2035,7 @@ class TarFile(object):
     def addfile(self, tarinfo, fileobj=None):
         """Add the TarInfo object `tarinfo' to the archive. If `fileobj' is
            given, tarinfo.size bytes are read from it and added to the archive.
-           You can create TarInfo objects using gettarinfo().
+           You can create TarInfo objects directly, or by using gettarinfo().
            On Windows platforms, `fileobj' should always be opened with mode
            'rb' to avoid irritation about the file size.
         """