python - 我如何逐步使用 python 调试器在每次函数调用时中断?

标签 python debugging pdb

我想密切监视从某个函数调用的函数调用链。

import pdb; pdb.set_trace()
res = api.InsertVideoEntry(video_entry, video)

我正在寻找一种方法来轻松查看 api.insertVideoEntry(video_entry, video) 调用 foo() 调用 bar() 调用 baz(),

这是一个非常粗略的图表来说明我的意思。我不需要这种形式的信息,但这是我正在寻找的那种信息。

api.insertVideoEntry()
    foo()
        bar()
            baz()
            baz2()
        log()
    finish()

最佳答案

写下来是一次有趣的学习经历。也许您可以使用此处显示的代码? This demonstration应该让您了解使用 trace 时预期的输出类型。

# Functions to trace
# ==================

def baz():
    pass

def baz2():
    pass

def bar():
    baz()
    baz2()

def log():
    pass

def foo():
    bar()
    log()

def finish():
    pass

def insertVideoEntry():
    foo()
    finish()

# Names to trace
# ==============

names = list(locals())

# Machinery for tracing
# =====================

import os
import sys

def trace(start, *names):
    def tracefunc(frame, event, arg):
        if event == 'call':
            code = frame.f_code
            name = code.co_name
            if name in names:
                level = -start
                while frame:
                    frame = frame.f_back
                    level += 1
                print('{}{}.{}()'.format(
                    '    ' * level,
                    os.path.splitext(os.path.basename(code.co_filename))[0],
                    name))
                return tracefunc
    sys.settrace(tracefunc)

# Demonstration of tracing
# ========================

trace(2, *names)

insertVideoEntry()

如果您对递归演示感兴趣,您可能会喜欢 this variation带有调用的参数读数:

import os
import sys

def main(discs):
    a, b, c = list(range(discs, 0, -1)), [], []
    line = '-' * len(repr(a))
    print(a, b, c, sep='\n')
    for source, destination in towers_of_hanoi(discs, a, b, c):
        destination.append(source.pop())
        print(line, a, b, c, sep='\n')

def towers_of_hanoi(count, source, via, destination):
    if count > 0:
        count -= 1
        yield from towers_of_hanoi(count, source, destination, via)
        yield source, destination
        yield from towers_of_hanoi(count, via, source, destination)

def trace(start, *names):
    def tracefunc(frame, event, arg):
        if event == 'call':
            code = frame.f_code
            name = code.co_name
            if name in names:
                level = -start
                args = ', '.join(repr(frame.f_locals[name]) for name in
                                 code.co_varnames[:code.co_argcount])
                while frame:
                    frame = frame.f_back
                    level += 1
                print('{}{}.{}({})'.format(
                    ' ' * (level * 4),
                    os.path.splitext(os.path.basename(code.co_filename))[0],
                    name, args))
                return tracefunc
    sys.settrace(tracefunc)

if __name__ == '__main__':
    trace(3, 'main', 'towers_of_hanoi')
    main(3)

关于python - 我如何逐步使用 python 调试器在每次函数调用时中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11656560/

相关文章:

Python 多线程池 Windows 卡住

c++ - GDB 控制台无法显示 printf 的结果

visual-studio - 减少附加和加载符号的时间

python - 为什么使用线程的脚本偶尔会打印额外的行?

python - Django 模拟补丁无法正常工作

javascript - 有哪些调试 javascript 的好技巧?

c++ - OpenGL 3.3 2D 渲染 : VAO not properly configured?

python - 使用列表生成器时 Python 3 中的 pdb 模块中可能存在错误

python - 如何在 Windows 上使用 Python + Django + PyCharm 更快地进行调试?

python - Django - .values 和 .annotate 产生意外结果