Skip to content

Commit 5eb436c

Browse files
committed
Revert "[3.15] pythongh-153838: Skip non-regular source files in the heatmap exporter (pythonGH-153839) (python#158853)"
This reverts commit 48998df.
1 parent bdd3303 commit 5eb436c

3 files changed

Lines changed: 12 additions & 45 deletions

File tree

‎Lib/profiling/sampling/heatmap_collector.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -785,14 +785,14 @@ def _generate_file_html(self, output_path: Path, filename: str,
785785
line_counts: Dict[int, int], self_counts: Dict[int, int],
786786
file_stat: FileStats):
787787
"""Generate HTML for a single source file with heatmap coloring."""
788-
source_lines = [f"# Source file not available: {filename}"]
788+
# Read source file
789789
try:
790-
path = Path(filename)
791-
if path.is_file():
792-
source_lines = path.read_text(
793-
encoding='utf-8', errors='replace').splitlines()
794-
except (IOError, OSError):
795-
pass
790+
source_lines = Path(filename).read_text(encoding='utf-8', errors='replace').splitlines()
791+
except (IOError, OSError) as e:
792+
if not (filename.startswith('<') or filename.startswith('[') or
793+
filename in ('~', '...', '.') or len(filename) < 2):
794+
print(f"Warning: Could not read source file {filename}: {e}")
795+
source_lines = [f"# Source file not available: {filename}"]
796796

797797
# Generate HTML for each line
798798
max_samples = max(line_counts.values()) if line_counts else 1

‎Lib/test/test_profiling/test_heatmap.py‎

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import shutil
55
import tempfile
66
import unittest
7-
from unittest import mock
87
from collections import namedtuple
98
from pathlib import Path
109

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

619-
self.assertEqual(len(html_files), 1)
620-
with open(os.path.join(output_path, html_files[0]), 'r', encoding='utf-8') as f:
621-
content = f.read()
622-
623-
# Should have line-related content
624-
self.assertIn('line-', content)
625-
626-
def test_export_skips_nonexistent_source(self):
627-
self.check_unavailable_source(os.path.join(self.test_dir, 'missing.py'))
618+
if html_files:
619+
with open(os.path.join(output_path, html_files[0]), 'r', encoding='utf-8') as f:
620+
content = f.read()
628621

629-
def test_export_skips_directory_source(self):
630-
self.check_unavailable_source(self.test_dir)
622+
# Should have line-related content
623+
self.assertIn('line-', content)
631624

632-
@unittest.skipUnless(hasattr(os, 'mkfifo'), 'requires os.mkfifo')
633-
def test_export_skips_fifo_source(self):
634-
filename = os.path.join(self.test_dir, 'source.fifo')
635-
os.mkfifo(filename)
636-
self.check_unavailable_source(filename)
637-
638-
def check_unavailable_source(self, filename):
639-
collector = HeatmapCollector(sample_interval_usec=100)
640-
frames = [(filename, (1, 1, -1, -1), 'f', None)]
641-
collector.process_frames(frames, thread_id=1)
642-
output_path = Path(self.test_dir) / 'unavailable_source'
643-
read_text = Path.read_text
644-
645-
def check_read(path, *args, **kwargs):
646-
self.assertNotEqual(path, Path(filename),
647-
'Non-regular source must not be opened')
648-
return read_text(path, *args, **kwargs)
649-
650-
with (captured_stdout(), captured_stderr(),
651-
mock.patch.object(Path, 'read_text', check_read)):
652-
collector.export(output_path)
653-
content = (output_path / collector.file_index[filename]).read_text(
654-
encoding='utf-8')
655-
self.assertIn('Source file not available', content)
656625

657626
class MockFrameInfo:
658627
"""Mock FrameInfo for testing.

‎Misc/NEWS.d/next/Library/2026-07-17-12-00-00.gh-issue-153838.HmCp5s.rst‎

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)