New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
python2 file __repr__ does not escape filename #58369
Comments
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: |
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. |
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. |
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:
|
|
I think you want to decref the result of PyObject_Repr after the fact, too |
New changeset 6c1964dee98b by Ezio Melotti in branch '2.7': |
New changeset 86c749151660 by Ezio Melotti in branch '2.7': |
New changeset 6b1fad34d893 by Ezio Melotti in branch '2.7': |
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. |
wtf? you made it possible to return NULL in some case |
sorry for the buzz, i got myself up to date on the c api now why is there still the unicode case? |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: