Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
Description
Feature or enhancement
Regular file objects returned by theopen()
building havename
andname
andmode
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 in5a83292
if (nameisNoneandhasattr(fileobj,"name")and Line 1669 in5a83292
ifhasattr(fileobj,"mode"): Line 455 in5a83292
if'b'notingetattr(file,'mode','b'): Line 201 in5a83292
mode=getattr(fileobj,'mode','rb') cpython/Lib/asyncio/base_events.py
Line 982 in5a83292
if'b'notingetattr(file,'mode','b'): Line 654 in5a83292
ifhasattr(f,'mode'):
There are several file-like objects in compressing modulesgzip
,bz2
andlama
and archiving moduleszipfile
andtarfile
. They usually implement the raw or buffered file protocols fromio
, but not always havename
andmode
attributes, and when they have, they not always have common semantic.
GzipFile
, unlike toBZ2File
andLZMAFile
, hasname
andmode
attributes, butmode
is an integer, that confusestarfile
(see#62775).
ZipExtFile
has themode
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