Skip to content

Commit 7d8e13f

Browse files
committed
preserve lazy import of _colorize
1 parent f0d9823 commit 7d8e13f

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

Lib/profiling/sampling/pstats_collector.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import collections
22
import marshal
33
import pstats
4-
from _colorize import get_colors
4+
lazy from _colorize import get_colors
55

66
from .collector import Collector, extract_lineno
77
from .constants import MICROSECONDS_PER_SECOND, PROFILING_MODE_CPU
88

9-
ANSIColors = get_colors()
10-
119
class PstatsCollector(Collector):
1210
aggregating = True
1311

@@ -179,6 +177,8 @@ def print_stats(self, sort=-1, limit=None, show_summary=True, mode=None):
179177
}
180178

181179
# Print header with colors and proper alignment
180+
ANSIColors = get_colors()
181+
182182
print(f"{ANSIColors.BOLD_BLUE}Profile Stats:{ANSIColors.RESET}")
183183

184184
header_nsamples = f"{ANSIColors.BOLD_BLUE}{'nsamples':>{col_widths['nsamples']}}{ANSIColors.RESET}"
@@ -270,6 +270,8 @@ def _determine_best_unit(max_value):
270270

271271
def _print_summary(self, stats_list, total_samples):
272272
"""Print summary of interesting functions."""
273+
ANSIColors = get_colors()
274+
273275
print(
274276
f"\n{ANSIColors.BOLD_BLUE}Summary of Interesting Functions:{ANSIColors.RESET}"
275277
)

Lib/profiling/sampling/sample.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
import sysconfig
77
import time
88
from collections import deque
9-
from _colorize import get_colors
9+
lazy from _colorize import get_colors
1010

1111
from .binary_collector import BinaryCollector
1212

13-
ANSIColors = get_colors()
14-
1513
@contextlib.contextmanager
1614
def _pause_threads(unwinder, blocking):
1715
"""Context manager to pause/resume threads around sampling if blocking is True."""
@@ -273,6 +271,8 @@ def _print_realtime_stats(self):
273271
) # Max time = Min Hz
274272

275273
# Build cache stats string if stats collection is enabled
274+
ANSIColors = get_colors()
275+
276276
cache_stats_str = ""
277277
if self.collect_stats:
278278
try:
@@ -306,6 +306,8 @@ def _print_unwinder_stats(self):
306306
except RuntimeError:
307307
return # Stats not enabled
308308

309+
ANSIColors = get_colors()
310+
309311
print(f"\n{ANSIColors.BOLD_BLUE}{'='*50}{ANSIColors.RESET}")
310312
print(f"{ANSIColors.BOLD_BLUE}Unwinder Statistics:{ANSIColors.RESET}")
311313

@@ -400,6 +402,8 @@ def _print_binary_stats(self, collector):
400402
except (ValueError, RuntimeError):
401403
return # Collector closed or stats unavailable
402404

405+
ANSIColors = get_colors()
406+
403407
print(f" {ANSIColors.CYAN}Binary Encoding:{ANSIColors.RESET}")
404408

405409
repeat_records = stats.get('repeat_records', 0)

0 commit comments

Comments
 (0)