Skip to content

Update usages of PySys_FormatStderr with locale-dependent strerror #128022

Description

@picnixz

strerror is locale-dependent but PySys_FormatStderr is not. The usage is

cpython/Modules/main.c

Lines 377 to 379 in 7303f06

// TODO(picnixz): strerror() is locale dependent but not PySys_FormatStderr().
PySys_FormatStderr("%S: can't open file %R: [Errno %d] %s\n",
program_name, filename, errno, strerror(errno));
and we should update it to be locale-dependent. This is a follow-up to #126746 (comment).

This would be quite hard to test as pointed out by Serhiy, so I won't work on this for now. Others may pick that task.

Activity

  1. added
    type-featureA feature request or enhancement
    type-bugAn unexpected behavior, bug, or error
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    and removed
    type-featureA feature request or enhancement
    on Dec 17, 2024
  2. vstinner commented on Dec 17, 2024

    @vstinner
    Member

    There is a simple fix: remove strerrror().

  3. vstinner commented on Dec 17, 2024

    @vstinner
    Member

    At this point, the LC_MESSAGES locale is still the default C locale, the error message is not localized, and so is very likely an English message which is ASCII friendly.

    Maybe the exception would be if Python is embedded and Py_Main() (or any other function to embed Python) is called with LC_MESSAGES configured to something else.

  4. picnixz commented on Dec 17, 2024

    @picnixz
    MemberAuthor

    At this point, the LC_MESSAGES locale is still the default C locale, the error message is not localized, and so is very likely an English message which is ASCII friendly.

    Oh! I thought it was already localized. So maybe it's a non-issue? I didn't investigate more in details since I'm not well-versed in embedded Python, hence the TODO I left. But if it's a non-issue, we can try:

    • try to use strerror() normally -> hopefully nothing fails
    • if we have some UnicodeDecodeError -> clear it and remove strerror()
    • format a new exception without the use of strerror() now.

    If it's too much for something that is too rare, I think we should remove the "TODO(picnixz)" message (I can remove it as part of #128025 if the latter is not merged yet).

  5. removed
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on May 7, 2025
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

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions