This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: "+" ignored when determining file mode in gzip.GzipFile.__init__
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: __starrify__, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-09-26 00:49 by __starrify__, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 16405 closed python-dev, 2019-09-26 00:51
Messages (2)
msg353244 - (view) Author: __starrify__ (__starrify__) * Date: 2019-09-26 00:49
Currently (as of df69e75) it checks only the initial letter (via `mode.startswith`) of "mode" in gzip.GzipFile.__init__. See also: https://github.com/python/cpython/blob/df69e75/Lib/gzip.py#L183

I'd personally suggest that it takes also "+" into consideration, which shall be regarded as writable.

One common (hm.. at least I've observed more than once recently) case where people may make mistakes is to use a tempfile.NamedTemporaryFile as the file object without specifying explicitly a mode, while expecting the GzipFile to be writable.

A quick example (in Python 3.7.11):
```
>>> import tempfile
>>> tempfile.NamedTemporaryFile().mode
'rb+'
>>> import gzip
>>> gzip.GzipFile(fileobj=tempfile.NamedTemporaryFile(mode='rb+')).writable()
False
>>> gzip.GzipFile(fileobj=tempfile.NamedTemporaryFile(mode='wb')).writable()
True
```

Unfortunately this change request may be backward-incompatible, since previously modes like "r+b" have been treated as read-only. See also: https://github.com/python/cpython/blob/df69e75/Lib/test/test_gzip.py#L464
msg353280 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-09-26 09:13
Yes, this is a breaking change, it will break the code that reads gzipped temporary files.

Note also that even if this change be accepted, you could not use this feature until Python 3.9 be released. But you can write to temporary files in all Python versions if specify an explicit mode.

So the benefit is small, but there is a risk of breaking existing code.

There is an opposite proposition: deprecate guessing the GzipFile mode (issue28286).
History
Date User Action Args
2022-04-11 14:59:20adminsetgithub: 82462
2019-09-26 09:13:57serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg353280

resolution: rejected
stage: patch review -> resolved
2019-09-26 00:51:01python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request15986
2019-09-26 00:49:11__starrify__create