CollapsedStackCollector.export (Lib/profiling/sampling/stack_collector.py) opens its output with open(filename, "w") — no encoding=. It is the only exporter in that module missing it; FlamegraphCollector.export, the JSONL exporter and the heatmap exporter all pass encoding="utf-8". Because the collapsed format writes frame names verbatim (the JSON exporters escape non-ASCII via ensure_ascii), --collapsed export raises UnicodeEncodeError when a sampled frame's function or file name contains non-ASCII characters under a non-UTF-8 locale, or surrogate-escaped characters (from an undecodable path) on any locale:
UnicodeEncodeError: 'ascii' codec can't encode characters ...
UnicodeEncodeError: 'utf-8' codec can't encode character '\udc80' ... surrogates not allowed
Non-English identifiers are common and non-UTF-8 locales are supported, so profiling such a program with --collapsed produces no output and an uncaught error.
changed the title [-]profiling: --collapsed export crashes with UnicodeEncodeError on non-ASCII frame names[/-][+]profiling: --collapsed export fails with UnicodeEncodeError on non-ASCII frame names[/+]on Sep 8, 2026
Activity
gh-156810: Write the profiler's collapsed-stack export as UTF-8 (#156811
pythongh-156810: Write the profiler's collapsed-stack export as UTF-8 (…
[3.15] gh-156810: Write the profiler's collapsed-stack export as UTF-8 (
Revert "[3.15] pythongh-156810: Write the profiler's collapsed-stack …
[3.15] Restore deferred backports for 3.15.1, including GH-158874 (#1…