Skip to content
Open
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
2 changes: 1 addition & 1 deletion Lib/profiling/sampling/gecko_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ def _get_or_create_func(self, thread_data, filename, funcname, lineno):
func_resources.append(resource_idx)

if is_python:
filename_idx = self._intern_string(os.path.basename(filename))
filename_idx = self._intern_string(filename)
func_filenames.append(filename_idx)
func_line_numbers.append(lineno)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,91 @@ def test_gecko_collector_basic(self):
self.assertGreater(stack_table["length"], 0)
self.assertGreater(len(stack_table["frame"]), 0)

def test_gecko_collector_preserves_source_file_paths(self):
"""Test that GeckoCollector preserves source file paths and distinguishes
files with the same basename in different directories."""
collector = GeckoCollector(1000)
file_a = os.path.join("pkg_a", "__init__.py")
file_b = os.path.join("pkg_b", "__init__.py")

test_frames = [
MockInterpreterInfo(
0,
[
MockThreadInfo(
1,
[
MockFrameInfo(file_a, 10, "func_a"),
MockFrameInfo(file_b, 20, "func_b"),
],
status=THREAD_STATUS_MAIN_THREAD,
)
],
)
]
collector.collect(test_frames)
profile_data = collector._build_profile()

string_array = profile_data["shared"]["stringArray"]
thread_data = profile_data["threads"][0]
func_table = thread_data["funcTable"]

self.assertEqual(func_table["length"], 2)
self.assertEqual(len(func_table["name"]), 2)
self.assertEqual(len(func_table["fileName"]), 2)
self.assertEqual(len(func_table["lineNumber"]), 2)

# Strictly validate that string table contains both full paths
self.assertIn(file_a, string_array)
self.assertIn(file_b, string_array)
expected_idx_a = string_array.index(file_a)
expected_idx_b = string_array.index(file_b)
self.assertNotEqual(
expected_idx_a,
expected_idx_b,
"Distinct file paths must have distinct string table indices",
)

# Build mapping of func_name -> (file_idx, file_name, line_no)
funcs_by_name = {}
for i in range(func_table["length"]):
name_idx = func_table["name"][i]
self.assertIsInstance(name_idx, int)
self.assertGreaterEqual(name_idx, 0)
self.assertLess(name_idx, len(string_array))
func_name = string_array[name_idx]

file_idx = func_table["fileName"][i]
self.assertIsInstance(file_idx, int)
self.assertGreaterEqual(file_idx, 0)
self.assertLess(file_idx, len(string_array))
file_name = string_array[file_idx]

line_no = func_table["lineNumber"][i]
self.assertIsInstance(line_no, int)

funcs_by_name[func_name] = (file_idx, file_name, line_no)

self.assertEqual(set(funcs_by_name.keys()), {"func_a", "func_b"})

# Strictly validate func_a filename index, path string, and line
file_idx_a, resolved_file_a, line_a = funcs_by_name["func_a"]
self.assertEqual(file_idx_a, expected_idx_a)
self.assertEqual(resolved_file_a, file_a)
self.assertEqual(line_a, 10)

# Strictly validate func_b filename index, path string, and line
file_idx_b, resolved_file_b, line_b = funcs_by_name["func_b"]
self.assertEqual(file_idx_b, expected_idx_b)
self.assertEqual(resolved_file_b, file_b)
self.assertEqual(line_b, 20)

# Ensure the filename indices and resolved paths in funcTable are distinct
self.assertNotEqual(file_idx_a, file_idx_b)
self.assertNotEqual(resolved_file_a, resolved_file_b)
self.assertNotEqual(resolved_file_a, os.path.basename(file_a))
self.assertNotEqual(resolved_file_b, os.path.basename(file_b))

def test_gecko_collector_async_aware(self):
collector = GeckoCollector(1000)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed an issue in :mod:`profiling.sampling` where the Gecko exporter (``--gecko``)
only recorded source file basenames instead of preserving the source file path.
Loading
⌂ Proxy