def iter_stack_frames()

in elasticapm/utils/stacks.py [0:0]


def iter_stack_frames(frames=None, start_frame=None, skip=0, skip_top_modules=(), config=None):
    """
    Given an optional list of frames (defaults to current stack),
    iterates over all frames that do not contain the ``__traceback_hide__``
    local variable.

    Frames can be skipped by either providing a number, or a tuple
    of module names. If the module of a frame matches one of the names
    (using `.startswith`, that frame will be skipped. This matching will only
    be done until the first frame doesn't match.

    This is useful to filter out frames that are caused by frame collection
    itself.

    :param frames: a list of frames, or None
    :param start_frame: a Frame object or None
    :param skip: number of frames to skip from the beginning
    :param skip_top_modules: tuple of strings
    :param config: agent configuration

    """
    if not frames:
        frame = start_frame if start_frame is not None else inspect.currentframe().f_back
        frames = _walk_stack(frame)
    max_frames = config.stack_trace_limit if config else -1
    stop_ignoring = False
    frames_count = 0  # can't use i, as we don't want to count skipped and ignored frames
    for i, frame in enumerate(frames):
        if max_frames != -1 and frames_count == max_frames:
            break
        if i < skip:
            continue
        f_globals = getattr(frame, "f_globals", {})
        if not stop_ignoring and f_globals.get("__name__", "").startswith(skip_top_modules):
            continue
        stop_ignoring = True
        f_locals = getattr(frame, "f_locals", {})
        if not _getitem_from_frame(f_locals, "__traceback_hide__"):
            frames_count += 1
            yield frame, frame.f_lineno