Skip to content

gh-154335: Allow disabling terminal colors in Tachyon's pstats_collector module#154344

Open
pfackeldey wants to merge 5 commits into
python:mainfrom
pfackeldey:tachyon/fix_pstats_coloring
Open

gh-154335: Allow disabling terminal colors in Tachyon's pstats_collector module#154344
pfackeldey wants to merge 5 commits into
python:mainfrom
pfackeldey:tachyon/fix_pstats_coloring

Conversation

@pfackeldey

@pfackeldey pfackeldey commented Jul 21, 2026

Copy link
Copy Markdown

This PR uses _colorize.get_colors() instead of the _colorize.ANSIColors enum. That way, one can disable terminal coloring through ANSI escape codes with e.g. setting certain env vars, such as NO_COLOR=1.

I couldn't find an appropriate place for a test (and also I wasn't sure what's the best way to test this). Please let me know what your preference is regarding a test, and I'm happy to add it.

@pfackeldey
pfackeldey requested a review from pablogsal as a code owner July 21, 2026 10:32
@python-cla-bot

python-cla-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@edvilme

edvilme commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Maybe this could be extended to include Lib/profiling/sampling/sample.py as well?

@pfackeldey

Copy link
Copy Markdown
Author

Maybe this could be extended to include Lib/profiling/sampling/sample.py as well?

Good point, I missed this one. I'll update the PR asap 👍

@pfackeldey

pfackeldey commented Jul 23, 2026

Copy link
Copy Markdown
Author

Dear @pablogsal and @edvilme,
do you have by chance an idea how to deal with the failing test case for iOS and Android?

It seems that for iOS and Android the coloring changes somehow the behavior of this test, likely here, but I don't fully understand it yet, and I'm not sure how I can reproduce the failure for debugging locally.

I found the fix! the no-color path (on iOS and Android) will not skip the header line of the pstats output in the test case, which it should though to populate data_lines correctly. The following patch fixes it:

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 2f5a5e27328..6b999e04e3d 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)

I'm happy to add this if you agree that this is the correct approach.

Let me know what you think, Peter

import marshal
import pstats
lazy from _colorize import ANSIColors
from _colorize import get_colors

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be better to lazily call this import, and instead of reasigning to ANSIColors, we just call get_colors() directly? Or define ANSIColors = get_colors() in places where it will be used

#149318

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure 👍 I'm happy to change this.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 7d8e13f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants