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'.
This commit is contained in:
Serhiy Storchaka 2024-04-21 11:46:39 +03:00 committed by GitHub
parent ccda738284
commit 51ef89cd9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 246 additions and 37 deletions

View file

@ -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.

View file

@ -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)

View file

@ -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.

View file

@ -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

View file

@ -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)

View file

@ -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`.)