python - 了解 pdb.set_trace() 之后的 pdb 输出

标签 python pdb

尝试使用 pdb 调试我的基于 python 的 roguelike。我想弄清楚的是,函数handle_keys()是否像我所问的那样返回0。我有另一个函数没有按预期运行,所以我试图找出问题发生的地方。应该发生的情况是:handle_keys() 返回 0,然后 frag_grenade() 执行某些操作。

我插入了 pdb 的回溯,但我不确定这是否会显示 handle_keys() 是否返回 0:

elif key.vk == libtcod.KEY_BACKSPACE:
        game_state = 'playing'
        pdb.set_trace()
        return 0

当我在游戏中按退格键时,我从 pdb 得到以下输出:

--> return 0

我不确定这是否显示返回值或仅显示下一行代码......

非常感谢!

最佳答案

26.2. pdb — The Python Debugger — Python 2.7.15 documentation :

The typical usage to break into the debugger from a running program is to insert

import pdb; pdb.set_trace()

at the location you want to break into the debugger. You can then step through the code following this statement, and continue running without the debugger using the c command.

箭头指向当前行,即即将执行的行。

返回值的打印方式完全不同:

In [11]: def answer():
    ...:     return 42

In [13]: pdb.runeval("answer()")
> <string>(1)<module>()->None
(Pdb) s
--Call--
> <ipython-input-11-22e067ec9c24>(1)answer()
-> def answer():
(Pdb) n
> <ipython-input-11-22e067ec9c24>(2)answer()
-> return 42
(Pdb)
--Return--
> <ipython-input-11-22e067ec9c24>(2)answer()->42
-> return 42
(Pdb)

正如您所看到的,返回行被打印两次——第一次是在执行之前,然后是函数返回时。第二次,它在位置行中伴随有 --Return-- 和返回值。

关于python - 了解 pdb.set_trace() 之后的 pdb 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50862946/

相关文章:

python - 如何在列表中查找重复值并合并它们

python - 通过 Python Socket Server 发送 HTML

python - 使用 python -selenium 进行网页抓取

Python-pdb 跳过代码(如 "not execute"中)

python - ipdb中的 '*** Oldest frame'是什么意思?

python - RuntimeError : The reset parameter is False but there is no n_features_in_ attribute. 是否安装了这个估算器?

python - 无法格式化打印字符串

python - pdb 事后调试器自行完成

python - 多线程脚本的事后调试

python - PDB.run - 重新启动 pdb session