diff --git a/Lib/profiling/sampling/pstats_collector.py b/Lib/profiling/sampling/pstats_collector.py index 7132cffd58f094a..dd3e7ef1c02c990 100644 --- a/Lib/profiling/sampling/pstats_collector.py +++ b/Lib/profiling/sampling/pstats_collector.py @@ -1,12 +1,11 @@ import collections import marshal import pstats -lazy from _colorize import ANSIColors +lazy from _colorize import get_colors from .collector import Collector, extract_lineno from .constants import MICROSECONDS_PER_SECOND, PROFILING_MODE_CPU - class PstatsCollector(Collector): aggregating = True @@ -178,6 +177,8 @@ def print_stats(self, sort=-1, limit=None, show_summary=True, mode=None): } # Print header with colors and proper alignment + ANSIColors = get_colors() + print(f"{ANSIColors.BOLD_BLUE}Profile Stats:{ANSIColors.RESET}") header_nsamples = f"{ANSIColors.BOLD_BLUE}{'nsamples':>{col_widths['nsamples']}}{ANSIColors.RESET}" @@ -269,6 +270,8 @@ def _determine_best_unit(max_value): def _print_summary(self, stats_list, total_samples): """Print summary of interesting functions.""" + ANSIColors = get_colors() + print( f"\n{ANSIColors.BOLD_BLUE}Summary of Interesting Functions:{ANSIColors.RESET}" ) diff --git a/Lib/profiling/sampling/sample.py b/Lib/profiling/sampling/sample.py index 6cb6dd483520887..0886baed3c75b5c 100644 --- a/Lib/profiling/sampling/sample.py +++ b/Lib/profiling/sampling/sample.py @@ -6,11 +6,10 @@ import sysconfig import time from collections import deque -lazy from _colorize import ANSIColors +lazy from _colorize import get_colors from .binary_collector import BinaryCollector - @contextlib.contextmanager def _pause_threads(unwinder, blocking): """Context manager to pause/resume threads around sampling if blocking is True.""" @@ -272,6 +271,8 @@ def _print_realtime_stats(self): ) # Max time = Min Hz # Build cache stats string if stats collection is enabled + ANSIColors = get_colors() + cache_stats_str = "" if self.collect_stats: try: @@ -305,6 +306,8 @@ def _print_unwinder_stats(self): except RuntimeError: return # Stats not enabled + ANSIColors = get_colors() + print(f"\n{ANSIColors.BOLD_BLUE}{'='*50}{ANSIColors.RESET}") print(f"{ANSIColors.BOLD_BLUE}Unwinder Statistics:{ANSIColors.RESET}") @@ -399,6 +402,8 @@ def _print_binary_stats(self, collector): except (ValueError, RuntimeError): return # Collector closed or stats unavailable + ANSIColors = get_colors() + print(f" {ANSIColors.CYAN}Binary Encoding:{ANSIColors.RESET}") repeat_records = stats.get('repeat_records', 0) diff --git a/Lib/test/test_profiling/test_sampling_profiler/test_profiler.py b/Lib/test/test_profiling/test_sampling_profiler/test_profiler.py index 2f5a5e273286590..6b999e04e3d65c3 100644 --- a/Lib/test/test_profiling/test_sampling_profiler/test_profiler.py +++ b/Lib/test/test_profiling/test_sampling_profiler/test_profiler.py @@ -751,6 +751,7 @@ def test_print_sampled_stats_sort_by_name(self): and not "calls" in line # Skip summary lines and not "total time" in line # Skip summary lines and not "cumulative time" in line + and not "filename:lineno(function)" in line # Skip header line ): # Skip summary lines data_lines.append(line) diff --git a/Misc/NEWS.d/next/Library/2026-07-21-12-23-48.gh-issue-154335.SRf8Gr.rst b/Misc/NEWS.d/next/Library/2026-07-21-12-23-48.gh-issue-154335.SRf8Gr.rst new file mode 100644 index 000000000000000..c3241b0c42c7e30 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-21-12-23-48.gh-issue-154335.SRf8Gr.rst @@ -0,0 +1 @@ +Allow disabling terminal coloring for Tachyon's pstats collector.