如何永久设置pdb中显示的行数?

2024-04-16 12:45:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以在运行时使用l来显示在pdb中要执行的当前行之前和之后的更多行,但是我们可以在脚本中或通过命令行选项永久地这样做吗?你知道吗

ywong:tmp ywong$ python tests.py
> /private/tmp/tests.py(23)<module>()
-> unittest.main()
(Pdb) l
 18             self.assertEqual(1,1)
 19
 20     if __name__ == "__main__":
 21         import pdb
 22         pdb.set_trace()
 23  ->     unittest.main()
 24         #unittest.main(testRunner=MyRunner)
[EOF]
(Pdb)

Tags: 命令行pyself脚本main选项testsunittest
1条回答
网友
1楼 · 发布于 2024-04-16 12:45:45

您可以设置自己的函数,通过实例化pdb.Pdb对象并在调用pdb交互式提示之前执行list命令来创建自定义调试器。你知道吗

您可以创建自定义调试器调用函数,如下所示:

import pdb, sys

def auto_list_debug():
    # Create an instance of the Pdb class
    my_pdb = pdb.Pdb()
    my_pdb.reset()

    # Execute your list command before invoking the interactive prompt
    my_pdb.cmdqueue.append('l')

    # Invoke the interactive prompt with the current frame
    my_pdb.interaction(sys._getframe().f_back, None)

使用此函数而不是pdb.set_trace()

for i in range(5):
    auto_list_debug()

相关问题 更多 >