Issue 14161: python2 file repr does not escape filename (original) (raw)

Created on 2012-02-29 16:43 by Ronny.Pfannschmidt, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue14161.diff ezio.melotti,2012-03-01 08:01 Patch against 2.7.
Messages (12)
msg154649 - (view) Author: Ronny Pfannschmidt (Ronny.Pfannschmidt) Date: 2012-02-29 16:43
behaviour: >>> name = 'woo\raa' >>> open(name, 'w') aa', mode 'w' at 0x295a8a0> expected: >>> name = 'woo\raa' >>> open(name, 'w') <open file 'woo\raa', mode 'w' at 0x295a8a0> note: don't ask why i tried this chunk of code in the first place
msg154674 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-03-01 05:47
Funny one :D Reproduced on linux: >>> open('/tmp/t\nest', 'w') <open file '/tmp/t est', mode 'w' at 0x7f11268a19c0> file_repr in Objects/fileobject.c calls http://docs.python.org/c-api/unicode#PyUnicode_AsUnicodeEscapeString, equivalent to encode('unicode-escape'), so backslashes should be escaped.
msg154675 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-03-01 05:58
Duh, obviously that code branch is used only for unicode paths: >>> open('/tmp/t\nest', 'w') <open file '/tmp/t est', mode 'w' at 0x7f6f0f3dd9c0> >>> open(u'/tmp/t\nest', 'w') <open file u'/tmp/t\nest', mode 'w' at 0x7f6f0f3dda50> There does not seem to be something similar in http://docs.python.org/c-api/string, so I guess one would have to create intermediary objects to decode str to unicode, transform with unicode-escape and convert back to str.
msg154679 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012-03-01 08:01
The attached patch seems to do the trick (not sure if it's the best way to fix the issue though): >>> open('woo\raa') <open file 'woo\raa', mode 'r' at 0xb77c2aa8> >>> open('woo\ra\'a', 'w') <open file "woo\ra'a", mode 'w' at 0xb77c2b88> >>> open('woo\ra\'a"', 'w') <open file 'woo\ra\'a"', mode 'w' at 0xb77c2b18> >>> It's more or less equivalent to: - return "<open file '%s', mode '%s' at %p>" % (fname, mode, addr) + return "<open file %s, mode '%s' at %p>" % (repr(fname), mode, addr)
msg154702 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-03-01 15:49
1. PyObject_Repr() should IMO be preferred (it's the abstract, high-level function). 2. You must check the result for NULL before calling PyString_AsString() on it.
msg154810 - (view) Author: Philip Jenvey (pjenvey) * (Python committer) Date: 2012-03-02 23:55
I think you want to decref the result of PyObject_Repr after the fact, too
msg155418 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-03-11 23:17
New changeset 6c1964dee98b by Ezio Melotti in branch '2.7': #14161: fix the __repr__ of file objects to escape the file name. http://hg.python.org/cpython/rev/6c1964dee98b
msg155420 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-03-11 23:29
New changeset 86c749151660 by Ezio Melotti in branch '2.7': #14161: fix compile error under Windows. http://hg.python.org/cpython/rev/86c749151660
msg155422 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-03-12 00:09
New changeset 6b1fad34d893 by Ezio Melotti in branch '2.7': #14161: fix test failures on Windows. http://hg.python.org/cpython/rev/6b1fad34d893
msg171688 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012-10-01 01:38
Not sure why this is still open -- probably I was just waiting for the buildbots and forgot it open. The issue seems fixed, so I'm going to close it now.
msg171817 - (view) Author: Ronny Pfannschmidt (Ronny.Pfannschmidt) Date: 2012-10-02 17:00
wtf? you made it possible to return NULL in some case
msg171818 - (view) Author: Ronny Pfannschmidt (Ronny.Pfannschmidt) Date: 2012-10-02 17:10
sorry for the buzz, i got myself up to date on the c api now why is there still the unicode case? the PyObject_Repr variant should work fine in both cases
History
Date User Action Args
2022-04-11 14:57:27 admin set github: 58369
2012-10-02 17:10:08 Ronny.Pfannschmidt set messages: +
2012-10-02 17:00:19 Ronny.Pfannschmidt set messages: +
2012-10-01 01:38:27 ezio.melotti set status: open -> closedmessages: + assignee: ezio.melottiresolution: fixedstage: test needed -> resolved
2012-03-12 00:09:17 python-dev set messages: +
2012-03-11 23:29:03 python-dev set messages: +
2012-03-11 23:17:17 python-dev set nosy: + python-devmessages: +
2012-03-02 23:55:54 pjenvey set nosy: + pjenveymessages: +
2012-03-01 15:49:01 pitrou set messages: +
2012-03-01 08:01:16 ezio.melotti set files: + issue14161.diffkeywords: + patchmessages: + stage: needs patch -> test needed
2012-03-01 05:58:17 eric.araujo set messages: +
2012-03-01 05:47:12 eric.araujo set messages: +
2012-02-29 18:08:59 ezio.melotti set nosy: + pitrou, ezio.melotti, eric.araujostage: needs patchcomponents: + Interpreter Coreversions: - Python 2.6
2012-02-29 16:43:29 Ronny.Pfannschmidt create