[New-bugs-announce] [issue38281] "+" ignored when determining file mode in gzip.GzipFile.__init__

__starrify__ report at bugs.python.org
Wed Sep 25 20:49:11 EDT 2019


New submission from __starrify__ <pengyu at libstarrify.so>:

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

----------
components: Library (Lib)
messages: 353244
nosy: __starrify__
priority: normal
severity: normal
status: open
title: "+" ignored when determining file mode in gzip.GzipFile.__init__
type: enhancement
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38281>
_______________________________________


More information about the New-bugs-announce mailing list