FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-156810: Write the profiler's collapsed-stack export as UTF-8 by tonghuaroot · Pull Request #156811 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 2 additions & 1 deletion Lib/profiling/sampling/stack_collector.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def export(self, filename):

lines.sort(key=lambda x: (-x[1], x[0]))

with open(filename, "w") as f:
with open(filename, "w",
encoding="utf-8", errors="surrogatepass") as f:
for stack, count in lines:
f.write(f"{stack} {count}\n")
print(f"Collapsed stack output written to {filename}")
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,28 @@ def test_collapsed_stack_collector_export(self):
self.assertIn(stack1_expected, lines)
self.assertIn(stack2_expected, lines)

def test_collapsed_stack_collector_export_non_ascii_names(self):
# gh-156810: frame names are written verbatim, so the output must be
# opened with an encoding that can represent non-ASCII and
# surrogate-escaped (undecodable-path) names.
collapsed_out = tempfile.NamedTemporaryFile(delete=False)
self.addCleanup(close_and_unlink, collapsed_out)

collector = CollapsedStackCollector(1000)
frame = MockFrameInfo("/tmp/ba\udc80d.py", 5, "计算")
collector.collect([
MockInterpreterInfo(0, [MockThreadInfo(1, [frame])])
])

with captured_stdout(), captured_stderr():
collector.export(collapsed_out.name)

with open(collapsed_out.name, encoding="utf-8",
errors="surrogatepass") as f:
content = f.read()
self.assertIn("计算", content)
self.assertIn("ba\udc80d.py", content)

def test_flamegraph_collector_basic(self):
"""Test basic FlamegraphCollector functionality."""
collector = FlamegraphCollector(1000)
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a :exc:`UnicodeEncodeError` crash in the sampling profiler's
collapsed-stack export (``--collapsed``) when a sampled frame's function or
file name contains non-ASCII or surrogate-escaped characters. The output file
is now written as UTF-8.
Loading

Back | FazBrowse Home | New Git URL