This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: OSErrors should use str and not repr on paths
Type: behavior Stage: resolved
Components: Windows Versions: Python 3.4, Python 3.5
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: akira, ezio.melotti, iritkatriel, pitrou, r.david.murray, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2014-09-23 20:34 by r.david.murray, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (12)
msg227389 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-23 20:34
>>> open(r'c:\bad\path')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'c:\\bad\\path'
msg227391 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-09-23 20:43
File names can contain special characters such as spaces or even newlines. str() can be ambiguous. I prefer always use repr() for file names or other user data.
msg227394 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-23 21:55
I realized that after I hit submit.

However, this is a real problem on windows: I can't pass the error message back to the UI (in this case the log file) without first munging it, because the string is not a correct representation of the filename and the user will think they've entered the filename wrong to begin with (this is what really happened and what resulting this bug getting filed).

On windows, if I understand correctly, we don't normally have the problem of non-unicode filenames, so could we use str on windows only?
msg227793 - (view) Author: Akira Li (akira) * Date: 2014-09-29 10:54
OSError has *filename* attribute. Could it be passed to the UI instead?
msg227795 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-29 12:06
No, because I'm just logging the error message.  That's the UI.
msg227938 - (view) Author: Akira Li (akira) * Date: 2014-09-30 14:54
I meant, in general, repr() is better for an error message because
it should be unambigous unlike str() but in your particular case
you could use filename attribute to format the way you like it
for your UI.
msg227939 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-09-30 14:56
Also, the repr() makes it easier to copy-paste in Python code (e.g. on the prompt).
msg227982 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-30 19:26
OK, so I have to add a filter to my logger that looks for a filename attribute on exceptions and and if it finds one...does what?  How do I reconstruct an arbitrary OSError error message using the filename parameter?
msg228057 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-10-01 06:29
вівторок, 30-вер-2014 19:26:52 ви написали:
> How do I reconstruct an arbitrary OSError error message using the filename
> parameter?

if not e.args:
    msg = ''
elif len(e.args) == 1:
    msg = str(e.args[0])
elif len(e.args) <= 5:
    msg = '[Error %s] %s' % e.args[:2]
    if len(e.args) > 2:
        msg = '%s: %r' % (msg, e.args[2]) # filename
    if len(e.args) > 4:
        msg = '%s -> %r' % (msg, e.args[4]) # filename2
    msg = str(e.args)
msg228074 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-10-01 14:41
Thank you, Serhiy.
msg396266 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-21 16:14
Should this be closed as rejected, or is there anything left to do?
msg396278 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2021-06-21 17:38
I'll close as rejected. Unfortunately, we have to make an exclusive choice here :-(
Date User Action Args
2022-04-11 14:58:08adminsetgithub: 66662
2021-06-21 17:38:59pitrousetstatus: open -> closed
resolution: rejected
stage: needs patch -> resolved
2021-06-21 17:38:49pitrousetmessages: + msg396278
2021-06-21 16:14:36iritkatrielsetnosy: + iritkatriel
messages: + msg396266
2014-10-01 14:41:07r.david.murraysetmessages: + msg228074
2014-10-01 06:29:26serhiy.storchakasetmessages: + msg228057
2014-09-30 19:26:51r.david.murraysetmessages: + msg227982
2014-09-30 14:56:50pitrousetnosy: + pitrou
messages: + msg227939
2014-09-30 14:54:55akirasetmessages: + msg227938
2014-09-29 12:06:51r.david.murraysetmessages: + msg227795
2014-09-29 10:54:59akirasetnosy: + akira
messages: + msg227793
2014-09-24 14:36:41ezio.melottisetnosy: + ezio.melotti
type: behavior
2014-09-23 21:55:54r.david.murraysetnosy: + vstinner
messages: + msg227394
2014-09-23 20:43:39serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg227391
2014-09-23 20:34:45r.david.murraycreate