diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2024-04-21 08:46:39 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-21 08:46:39 (GMT) |
commit | 51ef89cd9acbfbfa7fd8d82f0c6baa388bb650c9 (patch) | |
tree | eb6f52baa37360fb86c7401d12c47f51ad3795af /Doc | |
parent | ccda73828473576c57d1bb31774f56542d6e8964 (diff) | |
download | cpython-51ef89cd9acbfbfa7fd8d82f0c6baa388bb650c9.zip cpython-51ef89cd9acbfbfa7fd8d82f0c6baa388bb650c9.tar.gz cpython-51ef89cd9acbfbfa7fd8d82f0c6baa388bb650c9.tar.bz2 |
gh-115961: Add name and mode attributes for compressed file-like objects (GH-116036)
* Add name and mode attributes for compressed and archived file-like objects
in modules bz2, lzma, tarfile and zipfile.
* Change the value of the mode attribute of GzipFile from integer (1 or 2)
to string ('rb' or 'wb').
* Change the value of the mode attribute of ZipExtFile from 'r' to 'rb'.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/bz2.rst | 15 | ||||
-rw-r--r-- | Doc/library/gzip.rst | 15 | ||||
-rw-r--r-- | Doc/library/lzma.rst | 16 | ||||
-rw-r--r-- | Doc/library/tarfile.rst | 4 | ||||
-rw-r--r-- | Doc/library/zipfile.rst | 10 | ||||
-rw-r--r-- | Doc/whatsnew/3.13.rst | 11 |
6 files changed, 65 insertions, 6 deletions
diff --git a/Doc/library/bz2.rst b/Doc/library/bz2.rst index 5e0aa3e..eaf0a09 100644 --- a/Doc/library/bz2.rst +++ b/Doc/library/bz2.rst @@ -91,7 +91,7 @@ The :mod:`bz2` module contains: and :meth:`~io.IOBase.truncate`. Iteration and the :keyword:`with` statement are supported. - :class:`BZ2File` also provides the following methods: + :class:`BZ2File` also provides the following methods and attributes: .. method:: peek([n]) @@ -148,6 +148,19 @@ The :mod:`bz2` module contains: .. versionadded:: 3.3 + .. attribute:: mode + + ``'rb'`` for reading and ``'wb'`` for writing. + + .. versionadded:: 3.13 + + .. attribute:: name + + The bzip2 file name. Equivalent to the :attr:`~io.FileIO.name` + attribute of the underlying :term:`file object`. + + .. versionadded:: 3.13 + .. versionchanged:: 3.1 Support for the :keyword:`with` statement was added. diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 044be8c..e6106e7 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -133,6 +133,13 @@ The module defines the following items: .. versionadded:: 3.2 + .. attribute:: mode + + ``'rb'`` for reading and ``'wb'`` for writing. + + .. versionchanged:: 3.13 + In previous versions it was an integer ``1`` or ``2``. + .. attribute:: mtime When decompressing, this attribute is set to the last timestamp in the most @@ -168,14 +175,14 @@ The module defines the following items: .. versionchanged:: 3.6 Accepts a :term:`path-like object`. - .. versionchanged:: 3.12 - Remove the ``filename`` attribute, use the :attr:`~GzipFile.name` - attribute instead. - .. deprecated:: 3.9 Opening :class:`GzipFile` for writing without specifying the *mode* argument is deprecated. + .. versionchanged:: 3.12 + Remove the ``filename`` attribute, use the :attr:`~GzipFile.name` + attribute instead. + .. function:: compress(data, compresslevel=9, *, mtime=None) diff --git a/Doc/library/lzma.rst b/Doc/library/lzma.rst index 0d69c3b..74bf267 100644 --- a/Doc/library/lzma.rst +++ b/Doc/library/lzma.rst @@ -104,7 +104,7 @@ Reading and writing compressed files and :meth:`~io.IOBase.truncate`. Iteration and the :keyword:`with` statement are supported. - The following method is also provided: + The following method and attributes are also provided: .. method:: peek(size=-1) @@ -117,6 +117,20 @@ Reading and writing compressed files file object (e.g. if the :class:`LZMAFile` was constructed by passing a file object for *filename*). + .. attribute:: mode + + ``'rb'`` for reading and ``'wb'`` for writing. + + .. versionadded:: 3.13 + + .. attribute:: name + + The lzma file name. Equivalent to the :attr:`~io.FileIO.name` + attribute of the underlying :term:`file object`. + + .. versionadded:: 3.13 + + .. versionchanged:: 3.4 Added support for the ``"x"`` and ``"xb"`` modes. diff --git a/Doc/library/tarfile.rst b/Doc/library/tarfile.rst index afcad9b..cf21dee 100644 --- a/Doc/library/tarfile.rst +++ b/Doc/library/tarfile.rst @@ -565,6 +565,10 @@ be finalized; only the internally used file object will be closed. See the .. versionchanged:: 3.3 Return an :class:`io.BufferedReader` object. + .. versionchanged:: 3.13 + The returned :class:`io.BufferedReader` object has the :attr:`!mode` + attribute which is always equal to ``'rb'``. + .. attribute:: TarFile.errorlevel :type: int diff --git a/Doc/library/zipfile.rst b/Doc/library/zipfile.rst index ee53f16..6192689 100644 --- a/Doc/library/zipfile.rst +++ b/Doc/library/zipfile.rst @@ -301,6 +301,10 @@ ZipFile Objects attempting to read or write other files in the ZIP file will raise a :exc:`ValueError`. + In both cases the file-like object has also attributes :attr:`!name`, + which is equivalent to the name of a file within the archive, and + :attr:`!mode`, which is ``'rb'`` or ``'wb'`` depending on the input mode. + When writing a file, if the file size is not known in advance but may exceed 2 GiB, pass ``force_zip64=True`` to ensure that the header format is capable of supporting large files. If the file size is known in advance, @@ -325,6 +329,12 @@ ZipFile Objects Calling :meth:`.open` on a closed ZipFile will raise a :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised. + .. versionchanged:: 3.13 + Added attributes :attr:`!name` and :attr:`!mode` for the writeable + file-like object. + The value of the :attr:`!mode` attribute for the readable file-like + object was changed from ``'r'`` to ``'rb'``. + .. method:: ZipFile.extract(member, path=None, pwd=None) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 0f0fca5..5be5620 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -206,6 +206,11 @@ Other Language Changes (Contributed by Victor Stinner in :gh:`114570`.) +* Added :attr:`!name` and :attr:`!mode` attributes for compressed + and archived file-like objects in modules :mod:`bz2`, :mod:`lzma`, + :mod:`tarfile` and :mod:`zipfile`. + (Contributed by Serhiy Storchaka in :gh:`115961`.) + * Allow controlling Expat >=2.6.0 reparse deferral (:cve:`2023-52425`) by adding five new methods: @@ -1605,6 +1610,12 @@ Changes in the Python API than directories only. Users may add a trailing slash to match only directories. +* The value of the :attr:`!mode` attribute of :class:`gzip.GzipFile` was + changed from integer (``1`` or ``2``) to string (``'rb'`` or ``'wb'``). + The value of the :attr:`!mode` attribute of the readable file-like object + returned by :meth:`zipfile.ZipFile.open` was changed from ``'r'`` to ``'rb'``. + (Contributed by Serhiy Storchaka in :gh:`115961`.) + * :c:func:`!PyCode_GetFirstFree` is an unstable API now and has been renamed to :c:func:`PyUnstable_Code_GetFirstFree`. (Contributed by Bogdan Romanyuk in :gh:`115781`.) |