Skip to content

Commit 43dd7fe

Browse files
committed
gh-154335: Cover profiler color policy and update NEWS
1 parent 7d8e13f commit 43dd7fe

4 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎Lib/profiling/sampling/pstats_collector.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .collector import Collector, extract_lineno
77
from .constants import MICROSECONDS_PER_SECOND, PROFILING_MODE_CPU
88

9+
910
class PstatsCollector(Collector):
1011
aggregating = True
1112

‎Lib/profiling/sampling/sample.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from .binary_collector import BinaryCollector
1212

13+
1314
@contextlib.contextmanager
1415
def _pause_threads(unwinder, blocking):
1516
"""Context manager to pause/resume threads around sampling if blocking is True."""

‎Lib/test/test_profiling/test_sampling_profiler/test_profiler.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"Test only runs when _remote_debugging is available"
1818
)
1919

20-
from test.support import force_not_colorized_test_class
20+
from test.support import force_colorized, force_not_colorized_test_class
2121

2222

2323
def print_sampled_stats(stats, sort=-1, limit=None, show_summary=True, sample_interval_usec=100):
@@ -484,6 +484,15 @@ def test_print_sampled_stats_basic(self):
484484
self.assertIn("func1", result)
485485
self.assertIn("func2", result)
486486
self.assertIn("func3", result)
487+
self.assertNotIn("\x1b[", result)
488+
489+
@force_colorized
490+
def test_print_sampled_stats_colorized(self):
491+
with io.StringIO() as output, mock.patch("sys.stdout", output):
492+
print_sampled_stats(self.mock_stats)
493+
result = output.getvalue()
494+
495+
self.assertIn("\x1b[1;34mProfile Stats:", result)
487496

488497
def test_print_sampled_stats_sorting(self):
489498
"""Test different sorting options."""
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
Allow disabling terminal coloring for Tachyon's pstats collector.
1+
The :mod:`profiling.sampling` profiler now respects ``NO_COLOR``,
2+
``FORCE_COLOR``, and ``PYTHON_COLORS`` and avoids colored output when
3+
not writing to a terminal.

0 commit comments

Comments
 (0)