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.

classification
Title: OSError msg should display symbolic error codes
Type: enhancement Stage: resolved
Components: Versions: Python 3.10
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Patch to print symbolic value of errno in OSError.__str__()
View: 2920
Assigned To: Nosy List: ZackerySpytz, barry, jwilk, pitrou, r.david.murray
Priority: normal Keywords:

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:20adminsetgithub: 56971
2021-03-22 16:35:26ZackerySpytzsetstatus: open -> closed

superseder: Patch to print symbolic value of errno in OSError.__str__()

nosy: + ZackerySpytz
messages: + msg389330
resolution: duplicate
stage: needs patch -> resolved
2021-03-22 16:15:07iritkatrielsettitle: EnvironmentError_str contributes to unportable code -> OSError msg should display symbolic error codes
versions: + Python 3.10, - Python 3.4
2014-02-03 18:32:48BreamoreBoysetnosy: - BreamoreBoy
2013-04-18 20:20:06r.david.murraysetversions: + Python 3.4
nosy: + r.david.murray

messages: + msg187288

type: enhancement
stage: needs patch
2013-04-18 19:25:18BreamoreBoysetnosy: + BreamoreBoy
messages: + msg187282
2011-10-30 13:03:18ezio.melottisetnosy: + pitrou
2011-08-29 20:24:11barrysetnosy: + barry
messages: + msg143178
2011-08-16 17:09:44jwilksetmessages: + msg142208
2011-08-16 17:09:05jwilkcreate