Issue 32655: File mode should be a constant (original) (raw)

Created on 2018-01-24 17:31 by nagayev, last changed 2022-04-11 14:58 by admin.

Messages (4)
msg310620 - (view) Author: Марат Нагаев (nagayev) * Date: 2018-01-24 17:41
Hey guys, I find bug in Python. If you'll try to file stream using open function you can edit file mode: Watch git: https://gist.github.com/nagayev/7d17ead7b3bc2b06f2445fb5d9122a5c In fact,mode don't changed,so mode shoul be constant like a encoding of the file and raise AttributeError if you'll edit it.
msg310622 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-01-24 18:13
That isn't a bug. Python doesn't protect you from doing the wrong thing, in general. On the other hand, it might be a worthwhile improvement to make it read-only in this case. Especially since, as you point out, other seemingly similar attributes of this object are read-only.
msg310667 - (view) Author: Nitish (nitishch) * Date: 2018-01-25 09:04
It appears some files like Lib/tokenize.py changes the mode of TextIOWrapper: text = TextIOWrapper(buffer, encoding, line_buffering=True) text.mode = 'r' setting of mode via _PyObject_SetAttrId(wrapper, &PyId_mode, modeobj) is done in some source files too. What I don't understand is since TextIOWrapper doesn't store the mode explicitly, this only adds 'mode' variable to the the object's dict. So why bother setting the mode in all these files?
msg344010 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2019-05-30 23:07
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())
History
Date User Action Args
2022-04-11 14:58:57 admin set github: 76836
2019-05-30 23:07:47 berker.peksag link issue36271 superseder
2019-05-30 23:07:00 berker.peksag set versions: + Python 3.9, - Python 3.7nosy: + berker.peksag, pitroumessages: + stage: test needed
2018-01-25 09:04:21 nitishch set nosy: + nitishchmessages: +
2018-01-24 18:13:01 r.david.murray set type: behavior -> enhancementmessages: + nosy: + r.david.murray
2018-01-24 17:41:06 nagayev set messages: + title: File mode shou -> File mode should be a constant
2018-01-24 17:31:36 nagayev create