serhiy-storchaka ยท GitHub

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

Read the original on github.com โ†—