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
10 changes: 2 additions & 8 deletions Lib/profiling/sampling/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,27 @@ def normalize_location(location):
"""Normalize location to a 4-tuple format.
Args:
location: tuple (lineno, end_lineno, col_offset, end_col_offset),
an integer line number, or None
location: tuple (lineno, end_lineno, col_offset, end_col_offset) or None
Returns:
tuple: (lineno, end_lineno, col_offset, end_col_offset)
"""
if location is None:
return DEFAULT_LOCATION
if isinstance(location, int):
return (location, location, -1, -1)
return location


def extract_lineno(location):
"""Extract lineno from location.
Args:
location: tuple (lineno, end_lineno, col_offset, end_col_offset),
an integer line number, or None
location: tuple (lineno, end_lineno, col_offset, end_col_offset) or None
Returns:
int: The line number (0 for synthetic frames)
"""
if location is None:
return 0
if isinstance(location, int):
return location
return location[0]

def _is_internal_frame(frame):
Expand Down
17 changes: 6 additions & 11 deletions Lib/profiling/sampling/gecko_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,10 @@ def _record_stack_sample(self, thread_data, frames, tid, times, first_time):
if self.opcodes_enabled and frames:
leaf_frame = frames[0]
filename, location, funcname, opcode = leaf_frame
if isinstance(location, tuple):
lineno, _, col_offset, _ = location
if location is None:
lineno, col_offset = None, -1
else:
lineno = location
col_offset = -1
lineno, _, col_offset, _ = location

current_state = (opcode, lineno, col_offset, funcname, filename)

Expand Down Expand Up @@ -541,15 +540,11 @@ def _process_stack(self, thread_data, frames):

for frame_tuple in reversed(frames):
# frame_tuple is (filename, location, funcname, opcode)
# location is (lineno, end_lineno, col_offset, end_col_offset) or just lineno
filename, location, funcname, opcode = frame_tuple
if isinstance(location, tuple):
lineno, end_lineno, col_offset, end_col_offset = location
if location is None:
lineno, col_offset = None, -1
else:
# Legacy format: location is just lineno
lineno = location
col_offset = -1
end_col_offset = -1
lineno, _, col_offset, _ = location

# Get or create function
func_idx = self._get_or_create_func(
Expand Down
1 change: 0 additions & 1 deletion Lib/profiling/sampling/live_collector/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ def process_frames(self, frames, thread_id=None):
self._seen_locations.clear()

# Process each frame in the stack to track cumulative calls
# frame.location is (lineno, end_lineno, col_offset, end_col_offset), int, or None
for frame in frames:
lineno = extract_lineno(frame.location)
location = (frame.filename, lineno, frame.funcname)
Expand Down
1 change: 0 additions & 1 deletion Lib/profiling/sampling/pstats_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def _process_frames(self, frames, weight=1):
self._seen_locations.clear()

# Process each frame in the stack to track cumulative calls
# frame.location is int, tuple (lineno, end_lineno, col_offset, end_col_offset), or None
for frame in frames:
lineno = extract_lineno(frame.location)
location = (frame.filename, lineno, frame.funcname)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,16 @@ def make_interpreter(interp_id, threads):
return InterpreterInfo((interp_id, threads))


def extract_lineno(location):
"""Extract line number from location (tuple or int or None)."""
if location is None:
return 0 # Treat None as 0
if isinstance(location, tuple):
return location[0] if location[0] is not None else 0
return location


def extract_location(location):
"""Extract full location info as dict from location tuple or None."""
if location is None:
return {"lineno": 0, "end_lineno": 0, "column": 0, "end_column": 0}
if isinstance(location, tuple) and len(location) >= 4:
return {
"lineno": location[0] if location[0] is not None else 0,
"end_lineno": location[1] if location[1] is not None else 0,
"column": location[2] if location[2] is not None else 0,
"end_column": location[3] if location[3] is not None else 0,
}
# Fallback for old-style location
lineno = location[0] if isinstance(location, tuple) else location
return {"lineno": lineno or 0, "end_lineno": lineno or 0, "column": 0, "end_column": 0}
return {
"lineno": location[0],
"end_lineno": location[1],
"column": location[2],
"end_column": location[3],
}


def frame_to_dict(frame):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2957,20 +2957,6 @@ def test_extract_lineno_from_none(self):
"""Test extracting lineno from None (synthetic frames)."""
self.assertEqual(extract_lineno(None), 0)

def test_extract_lineno_from_int(self):
"""Test extracting lineno from a bare integer line number.
Mirrors normalize_location's int contract so callers like the
collapsed/flamegraph collectors do not crash on a bare-int location.
"""
self.assertEqual(extract_lineno(42), 42)
self.assertEqual(extract_lineno(0), 0)

def test_normalize_location_with_int(self):
"""Test normalize_location expands a legacy integer line number."""
result = normalize_location(42)
self.assertEqual(result, (42, 42, -1, -1))

def test_normalize_location_with_location_info(self):
"""Test normalize_location passes through LocationInfo."""
loc = LocationInfo(10, 15, 0, 5)
Expand Down
Loading