Skip to content

Patch to print symbolic value of errno in OSError.__str__() #47169

Open

Description

@ygingras
mannequin
BPO 2920
Nosy @amauryfa, @abalkin, @pitrou, @avassalotti, @ambv, @eryksun, @ZackerySpytz
PRs
  • gh-47169: Use the symbolic errno name in OSError error messages #14988
  • Files
  • python-26-sympolic-errno.diff: patch for symbolic errno
  • symbolic-errno.diff
  • 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 = None
    closed_at = None
    created_at = <Date 2008-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 = <Date 2021-03-22.19:43:06.919>
    user = 'https://bugs.python.org/ygingras'

    bugs.python.org fields:

    activity = <Date 2021-03-22.19:43:06.919>
    actor = 'eryksun'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2008-05-19.20:14:34.567>
    creator = 'ygingras'
    dependencies = []
    files = ['10377', '10378']
    hgrepos = []
    issue_num = 2920
    keywords = ['patch']
    message_count = 11.0
    messages = ['67074', '67077', '106651', '106654', '106655', '111206', '113333', '113334', '160879', '348820', '349135']
    nosy_count = 8.0
    nosy_names = ['amaury.forgeotdarc', 'belopolsky', 'pitrou', 'alexandre.vassalotti', 'ygingras', 'lukasz.langa', 'eryksun', 'ZackerySpytz']
    pr_nums = ['14988']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue2920'
    versions = ['Python 3.10']

    Linked PRs

    Activity

    1. ygingras commented on May 19, 2008

      ygingrasmannequin
      MannequinAuthor

      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.

    2. avassalotti commented on May 19, 2008

      @avassalotti
      Member

      Overall, the patch looks good. I made a couple of fixes to make the
      changes more robust.

    3. self-assigned this
      on May 28, 2010
    4. amauryfa commented on May 28, 2010

      @amauryfa
      Contributor

      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.

    5. pitrou commented on May 28, 2010

      @pitrou
      Member

      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).

    6. AlexanderBelopolsky commented on May 28, 2010

      AlexanderBelopolskymannequin
      Mannequin

      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\>


    7. abalkin commented on Jul 22, 2010

      @abalkin
      Member

      This looks like something that can become part of PEP-3151.

    8. removed their assignment
      on Jul 22, 2010
    9. ambv commented on Aug 8, 2010

      @ambv
      Contributor

      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.

    10. pitrou commented on Aug 8, 2010

      @pitrou
      Member

      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.

    11. pitrou commented on May 16, 2012

      @pitrou
      Member

      This would still be a helpful improvement.

    12. 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
    13. eryksun commented on Jul 31, 2019

      @eryksun
      Contributor

      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
      
    14. ZackerySpytz commented on Aug 6, 2019

      ZackerySpytzmannequin
      Mannequin

      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.

    15. added and removed on Mar 22, 2021
    16. transferred this issue fromon Apr 10, 2022
    17. added
      3.13only security fixes
      and removed on Jan 5, 2024
    18. vadmium commented on Mar 14, 2024

      @vadmium
      Member

      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.

    19. sadrasabouri commented on May 18, 2026

      @sadrasabouri

      [from PyCon-US26 CPython sprint] I'm taking a stab at this.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.13only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions