Skip to content

Allow to set non-UTF8 exception messages #126742

Closed

Description

@picnixz

Feature or enhancement

Proposal:

This is a follow-up to the discussion in #126555 (comment).

dlerror() may return non-UTF-8 messages, possibly translated. We should be able to set exception messages according to the current locale. To that end, we'll expose some internal helper:

extern void 
_PyErr_SetLocaleStringTstate(PyThreadState *tstate, PyObject *exception, const char *string);

extern void
_PyErr_SetLocaleString(PyObject *exception, const char *string);

cc @ZeroIntensity @encukou

For now, both functions would be only declared as extern and not part of the public C API.

Linked PRs

Activity

  1. self-assigned this
    on Nov 12, 2024
  2. picnixz commented on Nov 12, 2024

    @picnixz
    MemberAuthor

    Let's actually mark it as a bugfix since we'll need to backport it in order to use it later.

  3. added
    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 Nov 12, 2024
  4. ZeroIntensity commented on Nov 12, 2024

    @ZeroIntensity
    Member

    Hmm, so is PyErr_SetLocaleString supposed to be implicitly private? I'm not the biggest fan of that--if something is supposed to be private, then we need to prefix it with _, not only so users don't touch it, but so people reading the source don't mistake it as public. If we want a variation of it that takes a thread state, we could have something like _PyErr_SetLocaleStringTstate.

  5. picnixz commented on Nov 12, 2024

    @picnixz
    MemberAuthor

    Hmm, so is PyErr_SetLocaleString supposed to be implicitly private

    For now, but maybe we can directly expose to the C API? I want to be consistent with the naming of each function.

  6. picnixz commented on Nov 12, 2024

    @picnixz
    MemberAuthor

    Nevermind, there is a precedent: _PyErr_FormatFromCauseTstate. Let's have
    _PyErr_SetLocaleStringTstate and _PyErr_SetLocaleString and both private.

  7. encukou commented on Nov 12, 2024

    @encukou
    Member

    How many places in the code would use this helper? I don't see the advantage over calling PyUnicode_DecodeLocale and PyErr_SetObject.

  8. picnixz commented on Nov 12, 2024

    @picnixz
    MemberAuthor

    There are a few occurrences of dlerror() in _ctypes.c and callproc.c and dynload_shlib as well.

    NVM: the dynload_shlib uses its own logic.

  9. 22 remaining items

  10. picnixz commented on Dec 17, 2024

    @picnixz
    MemberAuthor

    I changed the labels since we decideed to eventually treat this as a bug fix.

  11. added a commit that references this issue on Dec 18, 2024
  12. added 2 commits that reference this issue on Dec 18, 2024
  13. added 2 commits that reference this issue on Dec 18, 2024
  14. added a commit that references this issue on Dec 18, 2024
  15. added 2 commits that reference this issue on Dec 18, 2024
  16. added 2 commits that reference this issue on Dec 19, 2024
  17. added 3 commits that reference this issue on Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13only security fixes3.14bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions