Skip to content

Commit cb6ed34

Browse files
committed
truth to be told, this should be layer above
1 parent da3e754 commit cb6ed34

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

Lib/profiling/sampling/collector.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ def normalize_location(location):
2020
"""Normalize location to a 4-tuple format.
2121
2222
Args:
23-
location: tuple (lineno, end_lineno, col_offset, end_col_offset) or None
23+
location: tuple (lineno, end_lineno, col_offset, end_col_offset),
24+
an integer line number, or None
2425
2526
Returns:
2627
tuple: (lineno, end_lineno, col_offset, end_col_offset)
2728
"""
2829
if location is None:
2930
return DEFAULT_LOCATION
31+
if isinstance(location, int):
32+
return (location, location, -1, -1)
3033
return location
3134

3235

Lib/profiling/sampling/jsonl_collector.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
PROFILING_MODE_GIL,
1313
PROFILING_MODE_WALL,
1414
)
15+
from .collector import normalize_location
1516
from .stack_collector import StackTraceCollector
1617

1718

@@ -117,7 +118,7 @@ def _build_end_record(self):
117118

118119
def _get_or_create_frame_id(self, filename, location, funcname):
119120
synthetic = location is None
120-
location_fields = self._normalize_export_location(location)
121+
location_fields = self._location_to_export_fields(location)
121122
func_str_id = self._intern_string(funcname)
122123
path_str_id = self._intern_string(filename)
123124

@@ -160,34 +161,19 @@ def _intern_string(self, value):
160161
return string_id
161162

162163
@staticmethod
163-
def _normalize_export_location(location):
164-
if location is None:
165-
return {"line": 0}
166-
167-
if isinstance(location, int):
168-
return {"line": max(location, 0)}
169-
170-
if not isinstance(location, tuple):
171-
lineno = getattr(location, "lineno", 0)
172-
location = (
173-
lineno,
174-
getattr(location, "end_lineno", lineno),
175-
getattr(location, "col_offset", -1),
176-
getattr(location, "end_col_offset", -1),
177-
)
164+
def _location_to_export_fields(location):
165+
lineno, end_lineno, col_offset, end_col_offset = normalize_location(
166+
location
167+
)
178168

179-
lineno, end_lineno, col_offset, end_col_offset = location
180-
if not isinstance(lineno, int) or lineno <= 0:
181-
return {"line": 0}
182-
183-
normalized = {"line": lineno}
184-
if isinstance(end_lineno, int) and end_lineno > 0:
185-
normalized["end_line"] = end_lineno
186-
if isinstance(col_offset, int) and col_offset >= 0:
187-
normalized["col"] = col_offset
188-
if isinstance(end_col_offset, int) and end_col_offset >= 0:
189-
normalized["end_col"] = end_col_offset
190-
return normalized
169+
fields = {"line": lineno}
170+
if end_lineno > 0:
171+
fields["end_line"] = end_lineno
172+
if col_offset >= 0:
173+
fields["col"] = col_offset
174+
if end_col_offset >= 0:
175+
fields["end_col"] = end_col_offset
176+
return fields
191177

192178
def _iter_final_agg_entries(self):
193179
for frame_record in self._frames:

Lib/test/test_profiling/test_sampling_profiler/test_collectors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,11 @@ def test_extract_lineno_from_none(self):
19551955
"""Test extracting lineno from None (synthetic frames)."""
19561956
self.assertEqual(extract_lineno(None), 0)
19571957

1958+
def test_normalize_location_with_int(self):
1959+
"""Test normalize_location expands a legacy integer line number."""
1960+
result = normalize_location(42)
1961+
self.assertEqual(result, (42, 42, -1, -1))
1962+
19581963
def test_normalize_location_with_location_info(self):
19591964
"""Test normalize_location passes through LocationInfo."""
19601965
loc = LocationInfo(10, 15, 0, 5)

0 commit comments

Comments
 (0)