MCPcopy Index your code
hub / github.com/python/cpython / _process_stack

Method _process_stack

Lib/profiling/sampling/gecko_collector.py:452–508  ·  view source on GitHub ↗

Process a stack and return the stack index.

(self, thread_data, frames)

Source from the content-addressed store, hash-verified

450 })
451
452 def _process_stack(self, thread_data, frames):
453 """Process a stack and return the stack index."""
454 if not frames:
455 return None
456
457 # Cache references to avoid repeated dictionary lookups
458 stack_cache = thread_data["_stackCache"]
459 stack_table = thread_data["stackTable"]
460 stack_frames = stack_table["frame"]
461 stack_prefix = stack_table["prefix"]
462 stack_category = stack_table["category"]
463 stack_subcategory = stack_table["subcategory"]
464
465 # Build stack bottom-up (from root to leaf)
466 prefix_stack_idx = None
467
468 for frame_tuple in reversed(frames):
469 # frame_tuple is (filename, location, funcname, opcode)
470 # location is (lineno, end_lineno, col_offset, end_col_offset) or just lineno
471 filename, location, funcname, opcode = frame_tuple
472 if isinstance(location, tuple):
473 lineno, end_lineno, col_offset, end_col_offset = location
474 else:
475 # Legacy format: location is just lineno
476 lineno = location
477 col_offset = -1
478 end_col_offset = -1
479
480 # Get or create function
481 func_idx = self._get_or_create_func(
482 thread_data, filename, funcname, lineno
483 )
484
485 # Get or create frame (include column for precise source location)
486 frame_idx = self._get_or_create_frame(
487 thread_data, func_idx, lineno, col_offset
488 )
489
490 # Check stack cache
491 stack_key = (frame_idx, prefix_stack_idx)
492 if stack_key in stack_cache:
493 prefix_stack_idx = stack_cache[stack_key]
494 else:
495 # Create new stack entry
496 stack_idx = len(stack_frames)
497 stack_frames.append(frame_idx)
498 stack_prefix.append(prefix_stack_idx)
499
500 # Determine category
501 category = self._get_category(filename)
502 stack_category.append(category)
503 stack_subcategory.append(DEFAULT_SUBCATEGORY)
504
505 stack_cache[stack_key] = stack_idx
506 prefix_stack_idx = stack_idx
507
508 return prefix_stack_idx
509

Callers 1

collectMethod · 0.95

Calls 4

_get_or_create_funcMethod · 0.95
_get_or_create_frameMethod · 0.95
_get_categoryMethod · 0.95
appendMethod · 0.45

Tested by

no test coverage detected