Issue 12762: OSError msg should display symbolic error codes (original) (raw)

Created on 2011-08-16 17:09 by jwilk, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg142207 - (view) Author: Jakub Wilk (jwilk) Date: 2011-08-16 17:09
It is a surprisingly common error in 3rd party code to write something like this: try: eggs() except OSError, e: if e.errno == 17: ham() This is wrong, because according to POSIX[0], “only […] symbolic names should be used in programs, since the actual value of the error number is unspecified.” I was wondering why Python programmers keep writing such unportable code - e.g. I've never seen C code that would compare errno variable with a hardcoded integer. I came into conclution that the Python interpreter itself is (partially) to blame. Currently exception message generated from errno looks like this: "[Errno 2] No such file or directory: '/punt'" It would be better if the message was: "[ENOENT] No such file or directory: '/punt'" or, if the above is too hard to implement, even: "No such file or directory: '/punt'"
msg142208 - (view) Author: Jakub Wilk (jwilk) Date: 2011-08-16 17:09
And the lost footnote is: [0] http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_03.html#tag_02_03
msg143178 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2011-08-29 20:24
I tend to agree that the errno is much less useful than the symbolic name. The former is useful and will be available as an attribute, but the latter should be used in the str. The change will probably break scads of doctests, but is probably worth it. :) BTW, this came up in the PEP 3151 discussions, but I agree it's orthogonal to that PEP.
msg187282 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2013-04-18 19:25
It doesn't bother me that Python programmers can write unportable code, what with consenting adults and all that. Further the implementation of PEP 3151 in 3.3 allows specific exceptions to be caught, e.g. FileNotFoundError. I can't see anybody allowing the exception number being changed to the symbolic string or even omitted completely, so I'd recommend this is closed unless there's an extremely good reason not to do so.
msg187288 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-04-18 20:20
The enum module that is likely to land in 3.4 will allow us to fix this. We can discuss whether we want to just display the name, or both the name and the value. Omitting it would indeed be bad, IMO.
msg389330 - (view) Author: Zackery Spytz (ZackerySpytz) * (Python triager) Date: 2021-03-22 16:35
This is a duplicate of bpo-2920 (which has a PR).
History
Date User Action Args
2022-04-11 14:57:20 admin set github: 56971
2021-03-22 16:35:26 ZackerySpytz set status: open -> closedsuperseder: Patch to print symbolic value of errno in OSError.__str__()nosy: + ZackerySpytzmessages: + resolution: duplicatestage: needs patch -> resolved
2021-03-22 16:15:07 iritkatriel set title: EnvironmentError_str contributes to unportable code -> OSError msg should display symbolic error codesversions: + Python 3.10, - Python 3.4
2014-02-03 18:32:48 BreamoreBoy set nosy: - BreamoreBoy
2013-04-18 20:20:06 r.david.murray set versions: + Python 3.4nosy: + r.david.murraymessages: + type: enhancementstage: needs patch
2013-04-18 19:25:18 BreamoreBoy set nosy: + BreamoreBoymessages: +
2011-10-30 13:03:18 ezio.melotti set nosy: + pitrou
2011-08-29 20:24:11 barry set nosy: + barrymessages: +
2011-08-16 17:09:44 jwilk set messages: +
2011-08-16 17:09:05 jwilk create