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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Lib/profiling/sampling/heatmap_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,14 +785,14 @@ def _generate_file_html(self, output_path: Path, filename: str,
line_counts: Dict[int, int], self_counts: Dict[int, int],
file_stat: FileStats):
"""Generate HTML for a single source file with heatmap coloring."""
# Read source file
source_lines = [f"# Source file not available: {filename}"]
try:
source_lines = Path(filename).read_text(encoding='utf-8', errors='replace').splitlines()
except (IOError, OSError) as e:
if not (filename.startswith('<') or filename.startswith('[') or
filename in ('~', '...', '.') or len(filename) < 2):
print(f"Warning: Could not read source file {filename}: {e}")
source_lines = [f"# Source file not available: {filename}"]
path = Path(filename)
if path.is_file():
source_lines = path.read_text(
encoding='utf-8', errors='replace').splitlines()
except (IOError, OSError):
pass

# Generate HTML for each line
max_samples = max(line_counts.values()) if line_counts else 1
Expand Down
41 changes: 36 additions & 5 deletions Lib/test/test_profiling/test_heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shutil
import tempfile
import unittest
from unittest import mock
from collections import namedtuple
from pathlib import Path

Expand Down Expand Up @@ -615,13 +616,43 @@ def test_export_file_html_has_line_numbers(self):
html_files = [f for f in os.listdir(output_path)
if f.startswith('file_') and f.endswith('.html')]

if html_files:
with open(os.path.join(output_path, html_files[0]), 'r', encoding='utf-8') as f:
content = f.read()
self.assertEqual(len(html_files), 1)
with open(os.path.join(output_path, html_files[0]), 'r', encoding='utf-8') as f:
content = f.read()

# Should have line-related content
self.assertIn('line-', content)

def test_export_skips_nonexistent_source(self):
self.check_unavailable_source(os.path.join(self.test_dir, 'missing.py'))

# Should have line-related content
self.assertIn('line-', content)
def test_export_skips_directory_source(self):
self.check_unavailable_source(self.test_dir)

@unittest.skipUnless(hasattr(os, 'mkfifo'), 'requires os.mkfifo')
def test_export_skips_fifo_source(self):
filename = os.path.join(self.test_dir, 'source.fifo')
os.mkfifo(filename)
self.check_unavailable_source(filename)

def check_unavailable_source(self, filename):
collector = HeatmapCollector(sample_interval_usec=100)
frames = [(filename, (1, 1, -1, -1), 'f', None)]
collector.process_frames(frames, thread_id=1)
output_path = Path(self.test_dir) / 'unavailable_source'
read_text = Path.read_text

def check_read(path, *args, **kwargs):
self.assertNotEqual(path, Path(filename),
'Non-regular source must not be opened')
return read_text(path, *args, **kwargs)

with (captured_stdout(), captured_stderr(),
mock.patch.object(Path, 'read_text', check_read)):
collector.export(output_path)
content = (output_path / collector.file_index[filename]).read_text(
encoding='utf-8')
self.assertIn('Source file not available', content)

class MockFrameInfo:
"""Mock FrameInfo for testing.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Skip non-regular files in the ``profiling.sampling`` heatmap exporter
instead of reading them unconditionally. Patch by tonghuaroot.
Loading