Repository navigation
Add name and mode attributes to compressed file-like objects #115961
Description
Feature or enhancement
Regular file objects returned by the open() building have name and name and mode attributes. Some code may use these optional attributes for different purposes, for example to distinguish readable files from writable files, binary files from text files, or just for formatting the repr or error messages. For example:
Line 1666 in 5a83292
if (name is None and hasattr(fileobj, "name") and Line 1669 in 5a83292
if hasattr(fileobj, "mode"): Line 455 in 5a83292
if 'b' not in getattr(file, 'mode', 'b'): Line 201 in 5a83292
mode = getattr(fileobj, 'mode', 'rb') cpython/Lib/asyncio/base_events.py
Line 982 in 5a83292
if 'b' not in getattr(file, 'mode', 'b'): Line 654 in 5a83292
if hasattr(f, 'mode'):
There are several file-like objects in compressing modules gzip, bz2 and lama and archiving modules zipfile and tarfile. They usually implement the raw or buffered file protocols from io, but not always have name and mode attributes, and when they have, they not always have common semantic.
GzipFile, unlike to BZ2File and LZMAFile, has name and mode attributes, but mode is an integer, that confuses tarfile (see #62775).
ZipExtFile has the mode attribute which is always 'r'. It is a legacy from Python 2, when it could also be 'U' or 'rU' for files with universal newlines. But this mode was removed long time ago, and since it is a binary file, its mode should be 'rb'.
See also #68446, #91373, #91374. I opened this issue because consider it all the parts of larger image.
Linked PRs
- gh-115961: Improve tests for compressed file-like objects #115963
- [3.12] gh-115961: Improve tests for compressed file-like objects (GH-115963) #116032
- gh-115961: Add name and mode attributes for compressed file-like objects #116036
- [3.11] gh-115961: Improve tests for compressed file-like objects (GH-115963) #116039
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
- StatusShow more project fieldsDone
- StatusShow more project fieldsDone
Activity
pythongh-115961: Improve tests for compressed file-like objects
serhiy-storchaka commentedon Feb 26, 2024
The first step is to add tests for existing attributes and methods for file-like objects created in different way, in different modes and states: #115963. It also fixes some bugs in tests.
gh-115961: Improve tests for compressed file-like objects (GH-115963)
pythongh-115961: Improve tests for compressed file-like objects (pyth…
pythongh-115961: Add name and mode attributes for compressed file-lik…
serhiy-storchaka commentedon Feb 28, 2024
And the second step -- add new attributes and change the existing attributes to match the general rule.
It includes two minor breaking changes:
'rb'or'wb'instead of1or2.'rb'instead of'r'.These changes look rather as bugfixes (see #62775), but I do not think that it is worth to backport them.
pythongh-115961: Add name and mode attributes for compressed file-lik…
[3.12] gh-115961: Improve tests for compressed file-like objects (GH-…
[3.11] pythongh-115961: Improve tests for compressed file-like objects (
[3.11] gh-115961: Improve tests for compressed file-like objects (GH-…
pythongh-115961: Improve tests for compressed file-like objects (pyth…
serhiy-storchaka commentedon Mar 13, 2024
Open questions:
namebe available for closed streams? Currently it is available for gzip, but for bz2 and lzma it raises a ValueError.modebe always "wb" for writable streams, or let it be "ab" or "xb" if the stream was opened with the corresponding mode argument?pythongh-115961: Improve tests for compressed file-like objects (pyth…
pythongh-115961: Improve tests for compressed file-like objects (pyth…
gh-115961: Add name and mode attributes for compressed file-like obje…