You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Patch to print symbolic value of errno in OSError.__str__() #47169
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:
assignee=Noneclosed_at=Nonecreated_at=<Date2008-05-19.20:14:34.567>labels= ['interpreter-core', 'type-feature', '3.10']
title='Patch to print symbolic value of errno in OSError.__str__()'updated_at=<Date2021-03-22.19:43:06.919>user='https://bugs.python.org/ygingras'
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.
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.
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).
AlexanderBelopolsky commented on May 28, 2010
AlexanderBelopolskymannequin
Mannequin
More actions
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.
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).
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 bpo-2651.
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.
changed the title [-]Patch to print symbolic value or errno in EnvironmentError.__str__()[/-][+]Patch to print symbolic value of errno in OSError.__str__()[/+]on Jul 28, 2019
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
How does this patch deal with subclasses? Some modules abuse the OSError.errno attribute to record something other than Posix's errno, for instance socket.gaierror (negative error codes in my Linux experience), socket.herror (positive codes). There's also SSLError (see #67776), but I think that overrides the error message so wouldn't be affected.
On the other hand a symbolic errno would be valid for other subclasses, especially the "global" built-in ones like BlockingIOError and ConnectionError.
Activity
ygingras commentedon May 19, 2008
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.
avassalotti commentedon May 19, 2008
Overall, the patch looks good. I made a couple of fixes to make the
changes more robust.
amauryfa commentedon May 28, 2010
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.
pitrou commentedon May 28, 2010
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).
AlexanderBelopolsky commentedon May 28, 2010
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:
abalkin commentedon Jul 22, 2010
This looks like something that can become part of PEP-3151.
ambv commentedon Aug 8, 2010
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 bpo-2651.
pitrou commentedon Aug 8, 2010
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.
pitrou commentedon May 16, 2012
This would still be a helpful improvement.
[-]Patch to print symbolic value or errno in EnvironmentError.__str__()[/-][+]Patch to print symbolic value of errno in OSError.__str__()[/+]eryksun commentedon Jul 31, 2019
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
It could also support additional error codes that are common or used internally, such as
Modules/_winapi.c:
ZackerySpytz commentedon Aug 6, 2019
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.
vadmium commentedon Mar 14, 2024
How does this patch deal with subclasses? Some modules abuse the OSError.errno attribute to record something other than Posix's errno, for instance socket.gaierror (negative error codes in my Linux experience), socket.herror (positive codes). There's also SSLError (see #67776), but I think that overrides the error message so wouldn't be affected.
On the other hand a symbolic errno would be valid for other subclasses, especially the "global" built-in ones like BlockingIOError and ConnectionError.
sadrasabouri commentedon May 18, 2026
[from PyCon-US26 CPython sprint] I'm taking a stab at this.