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: Patch to print symbolic value of errno in OSError.__str__()
Type: enhancement Stage: patch review
Components: Interpreter Core Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ZackerySpytz, alexandre.vassalotti, amaury.forgeotdarc, belopolsky, eryksun, lukasz.langa, pitrou, ygingras
Priority: normal Keywords: patch

Created on 2008-05-19 20:14 by ygingras, last changed 2022-04-11 14:56 by admin.

Files
File name Uploaded Description Edit
python-26-sympolic-errno.diff ygingras, 2008-05-19 20:14 patch for symbolic errno review
symbolic-errno.diff alexandre.vassalotti, 2008-05-19 21:14 review
Pull Requests
URL Status Linked Edit
PR 14988 open ZackerySpytz, 2019-07-28 05:57
Messages (11)
msg67074 - (view) Author: Yannick Gingras (ygingras) * Date: 2008-05-19 20:14
EnvironmentError and its subclass OSError add the value of errno in 
their error message.  This value is an integer but the specific value 
in an implementation detail and the C runtime recommends that 
programmers use the symbolic values instead.  Ex: one should use 
ENODATA instead of 61.  For the same reason, the Python interpreter 
should try to return the symbolic value or errno when possible.

The attached patch replaces errno in EnvironmentError.__str__() by its 
symbolic value when possible.
msg67077 - (view) Author: Alexandre Vassalotti (alexandre.vassalotti) * (Python committer) Date: 2008-05-19 21:14
Overall, the patch looks good. I made a couple of fixes to make the
changes more robust.
msg106651 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-05-28 12:06
I don't like the "import errno" while printing an exception...
It would be much more robust to store errorcode_dict in a static variable when python starts, and reuse it directly.
msg106654 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-05-28 12:27
Agreed with Amaury. Module import could fail for various reasons (perhaps the same ones which led to the exception being raised!), or could deadlock if the import lock is being held. Also, having __str__ fail is usually very annoying for users (especially when it's the __str__ of an exception object).

If it's too hard to import errno at startup (because of bootstrapping), I would suggest using PyImport_ImportModuleNoBlock() instead, and silence errors (just print the numeric value of errno instead).
msg106655 - (view) Author: Alexander Belopolsky (Alexander.Belopolsky) Date: 2010-05-28 12:38
The patch already checks for failed import and falls back to printing  
numerical error code. However, I don't like the import either. I will  
think about the alternatives.

On May 28, 2010, at 8:27 AM, Antoine Pitrou <report@bugs.python.org>  
wrote:

>
> Antoine Pitrou <pitrou@free.fr> added the comment:
>
> Agreed with Amaury. Module import could fail for various reasons  
> (perhaps the same ones which led to the exception being raised!), or  
> could deadlock if the import lock is being held. Also, having  
> __str__ fail is usually very annoying for users (especially when  
> it's the __str__ of an exception object).
>
> If it's too hard to import errno at startup (because of  
> bootstrapping), I would suggest using PyImport_ImportModuleNoBlock()  
> instead, and silence errors (just print the numeric value of errno  
> instead).
>
> ----------
> nosy: +pitrou
> versions: +Python 2.7, Python 3.1, Python 3.2
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue2920>
> _______________________________________
msg111206 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2010-07-22 18:27
This looks like something that can become part of PEP 3151.
msg113333 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2010-08-08 21:51
Alexander is right that this should be considered as part of PEP 3151. Plus, changes to interpreter core are subject to the moratorium.

I am -1 on this. I would propose to create a superseding issue for implementing PEP 3151 and gathering dependant issues like this one. For a similar case see #2651.
msg113334 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-08-08 21:53
PEP 3151 is already large enough and I don't want to add orthogonal changes to it. Furthermore, this change is only about the string representation of exceptions, and may be accepted for 3.2 even despite the moratorium.
msg160879 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-05-16 16:20
This would still be a helpful improvement.
msg348820 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-07-31 18:33
How about doing something similar for Windows when winerror is set? This is a common case since many os functions use the Windows API directly. Showing the symbolic error name will help to disambiguate exceptions, such as 

    FileNotFoundError (ENOENT) from:

        ERROR_FILE_NOT_FOUND
        ERROR_PATH_NOT_FOUND
        ERROR_FILENAME_EXCED_RANGE
        ERROR_BAD_PATHNAME
        ERROR_BAD_NET_NAME

    PermissionError (EACCES) from:

        ERROR_ACCESS_DENIED
        ERROR_SHARING_VIOLATION
        ERROR_LOCK_VIOLATION
        ERROR_NETWORK_ACCESS_DENIED
        ERROR_NOT_READY

It could also support additional error codes that are common or used internally, such as 

    ERROR_NOT_SUPPORTED
    ERROR_INVALID_NAME
    ERROR_MOD_NOT_FOUND
    ERROR_PRIVILEGE_NOT_HELD
    ERROR_CANT_RESOLVE_FILENAME
    ERROR_INVALID_REPARSE_DATA

Modules/_winapi.c:

    ERROR_NETNAME_DELETED
    ERROR_SEM_TIMEOUT
    ERROR_PIPE_BUSY
    ERROR_MORE_DATA
    ERROR_PIPE_CONNECTED
    ERROR_OPERATION_ABORTED
    ERROR_IO_PENDING
    ERROR_NO_SYSTEM_RESOURCES
msg349135 - (view) Author: Zackery Spytz (ZackerySpytz) * (Python triager) Date: 2019-08-06 21:26
Eryk, I'm not sure if something similar should be done for winerror. I think it's best to keep OSError_str() as simple as possible.
History
Date User Action Args
2022-04-11 14:56:34adminsetgithub: 47169
2021-03-22 19:43:06eryksunsetversions: + Python 3.10, - Python 3.9
2021-03-22 16:35:26ZackerySpytzlinkissue12762 superseder
2019-08-06 21:26:53ZackerySpytzsetmessages: + msg349135
2019-07-31 18:33:42eryksunsetnosy: + ZackerySpytz, eryksun
messages: + msg348820
2019-07-28 06:00:03ZackerySpytzsettitle: Patch to print symbolic value or errno in EnvironmentError.__str__() -> Patch to print symbolic value of errno in OSError.__str__()
versions: + Python 3.9, - Python 3.3
2019-07-28 05:57:57ZackerySpytzsetstage: needs patch -> patch review
pull_requests: + pull_request14755
2012-05-16 16:20:34pitrousetstage: patch review -> needs patch
messages: + msg160879
versions: + Python 3.3, - Python 3.2
2010-08-08 21:53:33pitrousetmessages: + msg113334
2010-08-08 21:51:21lukasz.langasetnosy: + lukasz.langa
messages: + msg113333
2010-08-07 20:24:52terry.reedysetversions: - Python 2.6, Python 3.1, Python 2.7
2010-07-22 18:27:06belopolskysetnosy: - Alexander.Belopolsky
messages: + msg111206

assignee: belopolsky ->
stage: patch review
2010-05-28 12:38:37Alexander.Belopolskysetnosy: + Alexander.Belopolsky
messages: + msg106655
2010-05-28 12:27:31pitrousetnosy: + pitrou

messages: + msg106654
versions: + Python 3.1, Python 2.7, Python 3.2
2010-05-28 12:06:10amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg106651
2010-05-28 05:21:04belopolskysetassignee: belopolsky

nosy: + belopolsky
2008-05-19 21:14:05alexandre.vassalottisetpriority: normal
files: + symbolic-errno.diff
type: enhancement
messages: + msg67077
nosy: + alexandre.vassalotti
2008-05-19 20:14:35ygingrascreate