Skip to content

traceback

Frame and stack walking utilities for tracing execution context.

This module provides utilities for inspecting and walking the Python call stack, copied and adapted from the Python standard library's logging module.

Example
from py_misc_utils import traceback as tb

def inner():
  return [f.f_code.co_name for f in tb.walk_stack()]

# The frames walk from the innermost frame up to the module one.
assert inner()[0] == 'inner'

FrameCarrier

Bases: Exception

Exception used as a traceback carrier for the frame-walk fallback.

Source code in py_misc_utils/traceback.py
21
22
class FrameCarrier(Exception):
  """Exception used as a traceback carrier for the frame-walk fallback."""

get_frame(n=0)

Return the frame object n levels up from the current call stack.

Parameters:

Name Type Description Default
n

Number of frames to go back from the current frame. Defaults to 0, meaning return the caller of get_frame() - 1 is the caller's caller, and so on.

0

Returns:

Type Description

The frame object n levels up, or None if the stack is not deep enough.

Source code in py_misc_utils/traceback.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def get_frame(n=0):
  """Return the frame object n levels up from the current call stack.

  Args:
    n: Number of frames to go back from the current frame. Defaults to 0,
      meaning return the caller of get_frame() - 1 is the caller's
      caller, and so on.

  Returns:
    The frame object n levels up, or None if the stack is not deep enough.
  """
  frame = _get_frame().f_back
  while n > 0 and frame is not None:
    frame = frame.f_back
    n -= 1

  return frame

get_frame_after(filename)

Find the first frame after the specified file in the call stack.

Walks the stack starting from the current frame and returns the first frame whose code object is not in the given filename, after skipping frames that are inside that filename.

Parameters:

Name Type Description Default
filename

The source filename to skip frames for.

required

Returns:

Type Description

The first frame object after the given filename in the call stack, or None

if no such frame exists.

Source code in py_misc_utils/traceback.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def get_frame_after(filename):
  """Find the first frame after the specified file in the call stack.

  Walks the stack starting from the current frame and returns the first frame
  whose code object is not in the given filename, after skipping frames that are
  inside that filename.

  Args:
    filename: The source filename to skip frames for.

  Returns:
    The first frame object after the given filename in the call stack, or None
    if no such frame exists.
  """
  frame, inscope = _get_frame(), False
  while frame is not None:
    if not inscope:
      if filename == frame.f_code.co_filename:
        inscope = True
    elif filename != frame.f_code.co_filename:
      break

    frame = frame.f_back

  return frame

walk_stack(frame=None)

Yield frames walking up the call stack starting from the given frame.

Parameters:

Name Type Description Default
frame

The starting frame object. If None, starts from the caller's frame (one level up). Defaults to None.

None

Yields:

Type Description

Frame objects walking up the call stack, starting from the given frame and

proceeding to each successive caller until None is reached - the first

frame yielded is the given one (or the caller's frame when frame is

None).

Source code in py_misc_utils/traceback.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def walk_stack(frame=None):
  """Yield frames walking up the call stack starting from the given frame.

  Args:
    frame: The starting frame object. If None, starts from the caller's frame
      (one level up). Defaults to None.

  Yields:
    Frame objects walking up the call stack, starting from the given frame and
    proceeding to each successive caller until None is reached - the first
    frame yielded is the given one (or the caller's frame when frame is
    None).
  """
  if frame is None:
    frame = get_frame(1)

  while frame is not None:
    yield frame
    frame = frame.f_back