Title: python2 file __repr__ does not escape filename
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: Ronny.Pfannschmidt, eric.araujo, ezio.melotti, pitrou, pjenvey, python-dev
Priority: normal Keywords: patch

Created on 2012-02-29 16:43 by Ronny.Pfannschmidt, last changed 2012-10-02 17:10 by Ronny.Pfannschmidt. This issue is now closed.

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
>>> name = 'woo\raa'
>>> open(name, 'w')
aa', mode 'w' at 0x295a8a0>

>>> name = 'woo\raa'
>>> open(name, 'w')
<open file 'woo\raa', mode 'w' at 0x295a8a0>

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, 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, 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.
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.
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.
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
Date User Action Args
2012-10-02 17:10:08Ronny.Pfannschmidtsetmessages: + msg171818
2012-10-02 17:00:19Ronny.Pfannschmidtsetmessages: + msg171817
2012-10-01 01:38:27ezio.melottisetstatus: open -> closed
messages: + msg171688

assignee: ezio.melotti
resolution: fixed
stage: test needed -> resolved
2012-03-12 00:09:17python-devsetmessages: + msg155422
2012-03-11 23:29:03python-devsetmessages: + msg155420
2012-03-11 23:17:17python-devsetnosy: + python-dev
messages: + msg155418
2012-03-02 23:55:54pjenveysetnosy: + pjenvey
messages: + msg154810
2012-03-01 15:49:01pitrousetmessages: + msg154702
2012-03-01 08:01:16ezio.melottisetfiles: + issue14161.diff
keywords: + patch
messages: + msg154679

stage: needs patch -> test needed
2012-03-01 05:58:17eric.araujosetmessages: + msg154675
2012-03-01 05:47:12eric.araujosetmessages: + msg154674
2012-02-29 18:08:59ezio.melottisetnosy: + pitrou, ezio.melotti, eric.araujo
stage: needs patch

components: + Interpreter Core
versions: - Python 2.6
2012-02-29 16:43:29Ronny.Pfannschmidtcreate