Skip to content

python-gdb.py crashes with UnicodeEncodeError pretty-printing non-ASCII strings in the C locale #154580

Description

@serhiy-storchaka

Bug report

Printing any Python str with a non-ASCII character crashes the python-gdb.py pretty-printer in the C locale:

$ LC_ALL=C gdb ./python
(gdb) break builtin_id
(gdb) run -c "id('🐍')"
(gdb) frame
... v=Python Exception <class 'UnicodeEncodeError'>: 'ascii' codec can't encode character '\U0001f40d' ...

Tools/gdb/libpython.py decides whether to escape a character by encoding it in ENCODING = locale.getpreferredencoding(). But gdb writes its output in its host charset, which need not match: gdb's embedded Python may have a different coercion policy (PEP 538/540). In the C locale the embedded Python coerces to UTF-8 while the host charset is ASCII, so 🐍 is judged printable, written raw, and gdb's ASCII output then raises UnicodeEncodeError.

Separately, test_gdb.test_pretty_print.test_strings also failed in non-UTF-8 multibyte locales (e.g. ja_JP.eucjp, uk_UA.koi8u), for a different test-side reason: it queried the embedded Python's getpreferredencoding() and decoded gdb's output as UTF-8.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixes3.14bugs and security fixes3.15pre-release feature fixes, bugs and security fixes3.16new features, bugs and security fixestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions