Skip to content

Commit e69658b

Browse files
gh-160976: Preserve source file path in Gecko sampling collector (#160982)
1 parent 2dde49a commit e69658b

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎Lib/profiling/sampling/gecko_collector.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ def _get_or_create_func(self, thread_data, filename, funcname, lineno):
612612
func_resources.append(resource_idx)
613613

614614
if is_python:
615-
filename_idx = self._intern_string(os.path.basename(filename))
615+
filename_idx = self._intern_string(filename)
616616
func_filenames.append(filename_idx)
617617
func_line_numbers.append(lineno)
618618
else:

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,51 @@ def test_gecko_collector_basic(self):
741741
self.assertGreater(stack_table["length"], 0)
742742
self.assertGreater(len(stack_table["frame"]), 0)
743743

744+
def test_gecko_collector_preserves_source_file_paths(self):
745+
"""Test that GeckoCollector preserves source file paths and distinguishes
746+
files with the same basename in different directories."""
747+
collector = GeckoCollector(1000)
748+
file_a = os.path.join("pkg_a", "__init__.py")
749+
file_b = os.path.join("pkg_b", "__init__.py")
750+
751+
test_frames = [
752+
MockInterpreterInfo(
753+
0,
754+
[
755+
MockThreadInfo(
756+
1,
757+
[
758+
MockFrameInfo(file_a, 10, "func_a"),
759+
MockFrameInfo(file_b, 20, "func_b"),
760+
],
761+
status=THREAD_STATUS_MAIN_THREAD,
762+
)
763+
],
764+
)
765+
]
766+
collector.collect(test_frames)
767+
profile_data = collector._build_profile()
768+
769+
string_array = profile_data["shared"]["stringArray"]
770+
func_table = profile_data["threads"][0]["funcTable"]
771+
772+
self.assertEqual(func_table["length"], 2)
773+
774+
funcs_by_name = {
775+
string_array[func_table["name"][i]]: (
776+
func_table["fileName"][i],
777+
string_array[func_table["fileName"][i]],
778+
)
779+
for i in range(func_table["length"])
780+
}
781+
782+
file_idx_a, resolved_file_a = funcs_by_name["func_a"]
783+
file_idx_b, resolved_file_b = funcs_by_name["func_b"]
784+
785+
self.assertEqual(resolved_file_a, file_a)
786+
self.assertEqual(resolved_file_b, file_b)
787+
self.assertNotEqual(file_idx_a, file_idx_b)
788+
744789
def test_gecko_collector_async_aware(self):
745790
collector = GeckoCollector(1000)
746791

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the Gecko reporter (``--gecko``) in :mod:`profiling.sampling` to record
2+
full source file paths.

0 commit comments

Comments
 (0)