[issue32655] File mode should be a constant

Berker Peksag report at bugs.python.org
Thu May 30 19:07:00 EDT 2019


Berker Peksag <berker.peksag at gmail.com> added the comment:

Changing the value of mode also changes its repr. This seems like a bug to me. It's probably too late to change TextIOWrapper.__repr__(). I think this needs to be discussed on python-dev first.

>>> f = open("README.rst")
>>> f
<_io.TextIOWrapper name='README.rst' mode='r' encoding='UTF-8'>
>>> f.writable()
False
>>> f.mode = "w"
>>> f.writable()
False
>>> f
<_io.TextIOWrapper name='README.rst' mode='w' encoding='UTF-8'>

There's an commented-out code in the initial checkin:

https://github.com/python/cpython/commit/4fa88fa0ba35e25ad9be66ebbdaba9aca553dc8b#diff-b67be9e0a41447de808ba3b7099a44a8R2341

/*    {"mode", (getter)TextIOWrapper_mode_get, NULL, NULL},
*/

TextIOWrapper.__repr__() was updated to include 'mode' in https://github.com/python/cpython/commit/a4815caa7ccf21aa994d0e0eec66873072f0e352

See issue 36271 for a related report. The OP was confused because we don't set mode manually in gzip.open() and bz2.open() (as opposed to what we do in io.open() and tokenize.open())

----------
nosy: +berker.peksag, pitrou
stage:  -> test needed
versions: +Python 3.9 -Python 3.7

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


More information about the Python-bugs-list mailing list