嗯,这里是一个排序缓慢方法。它可以被修改用于观察局部变量变化(仅通过名称)。以下是它的工作方式:我们执行sys.settrace并分析obj.attr每一步的值。棘手的部分是我们在执行行之前收到'line'事件(某行已执行)。所以,当我们注意到obj.attr已经改变时,我们已经在下一行,并且我们不能得到以前的行框架(因为没有为每一行复制框架,它们被修改)。因此,在每一行事件中,我将traceback.format_stack保存为watcher.prev_st,如果在下次调用trace_command时值已更改,我们将保存的堆栈跟踪打印到文件。在每一行上保存回溯操作都是相当昂贵的操作,因此您必须将include关键字设置为您的项目目录列表(或者仅仅是项目的根目录),以免看到其他库如何处理它们的内容和浪费中央处理器。
watcher.py
import traceback
class Watcher(object):
def __init__(self, obj=None, attr=None, log_file='log.txt', include=[], enabled=False):
"""
Debugger that watches for changes in object attributes
obj - object to be watched
attr - string, name of attribute
log_file - string, where to write output
include - list of strings, debug files only in these directories.
Set i