如何在pdb中调试手动输入的表达式和语句? [英] How can I debug manually typed expression and statements in pdb?

查看:236
本文介绍了如何在pdb中调试手动输入的表达式和语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在pdb(或ipdb)中,我们可以使用 <$ c执行语句并评估表达式$ c>!或 p 命令


p表达式

    评估当前上下文中的表达式并打印其值。



[!]语句



   &NBSP;&NBSP;在当前堆栈帧的上下文中执行(一行)语句。感叹号可以省略,除非语句的第一个词类似于调试器命令。要设置全局变量,您可以在同一行上使用全局命令前缀赋值命令


所以,例如,我可以在ipdb调试时,键入 p reddit.get_subreddits(),代码将在当前上下文中执行,我将看到返回值。



有没有办法我可以调试执行这样的手动输入表达式?



基本上我想做的是 s reddit.get_subreddits(),但只是执行步骤命令,并忽略表达式。



编辑:一个简单的例子



采取这个简单的功能:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $¬$ ]
如果不是weekday_index:
#如果没有提供工作日,返回所有工作日的平均值
返回总和(值)/ 7
返回平均值[weekday_index]

如果__name__ =='__main__':
while True:
import ipdb; ipdb.set_trace()#enter ipbd for debug
get_value_for_weekday(random.randint(0,7))

由于如果不是weekday_index (它应该检查 weekday_index不是无)。 / p>

我们假设我注意到,我得到 10 我期待的次数的一半。所以我添加了一个 import ipdb; ipdb.set_trace()在调用函数之前尝试调试代码。



所以我在ipdb控制台中,突然得出这样的想法,也许问题是当我通过0作为weekday_index。
我可以直接在ipdb中测试我的假设:

  ipdb> p get_value_for_weekday(0)
22

好的,所以我意识到当code> weekday_index = 0 。

我现在要做的是调试一步一步调用 get_value_for_weekday(0),所以我可以看到我错误地输入if块。



显然我可以退出ipdb,停止脚本,更改代码总是通过0 ,重新启动脚本,当我输入ipdb时,使用ipdb 步骤调试调用 s )命令。 >
但是,如果我可以只做 s get_value_for_weekday(0),就像我能够做的一样 p get_value_for_weekday(0)



有没有办法这样做?

解决方案

我想你正在寻找(d)ebug 命令,由于某些原因, a href =https://docs.python.org/3/library/pdb.html#debugger-commands =nofo llow> 调试器命令 。仅供以后参考, pdb 具有指定的一组好的命令(您可以在交互式中键入 help 提示)。在调试命令:

 (Pdb)帮助调试
调试代码
输入一个递归调试器,该调试器通过代码
参数(这是当前环境中执行的任意表达式或语句为
)。

这似乎是你做的事情。从终端使用您的示例脚本:

  python -m pdb pdbscript.py 

发出两个 n 命令后,为了使函数得到解析(我相信这是怎样的 pdb works)。您可以发出一个 debug get_value_for_weekday(0)命令来递归地执行以下功能:

 (Pdb)debug get_value_for_weekday(0)
ENTERING RECURSIVE DEBUGGER
> < string>(1)< module>()
((Pdb))s
--Call--
> /home/jim/Desktop/pdbscript.py(3)get_value_for_weekday()
- > def get_value_for_weekday(weekday_index = None):
((Pdb))n
> /home/jim/Desktop/pdbscript.py(4)get_value_for_weekday()
- >值= [10,20,20,10,30,30,30]
((Pdb))n
> /home/jim/Desktop/pdbscript.py(5)get_value_for_weekday()
- >如果不是weekday_index:
((Pdb))p weekday_index
0
((Pdb))n
> /home/jim/Desktop/pdbscript.py(7)get_value_for_weekday()
- >返回金额(值)/ 7

请注意,我觉得这个形式元调试,但它似乎是你之后。


In pdb (or ipdb) we can execute statements and evaluate expressions with the ! or p commands:

p expression
     Evaluate the expression in the current context and print its value.

[!]statement

     Execute the (one-line) statement in the context of the current stack frame. The exclamation point can be omitted unless the first word of the statement resembles a debugger command. To set a global variable, you can prefix the assignment command with a global command on the same line

So, for example, I can type p reddit.get_subreddits() while debugging in ipdb and the code will be executed in the current context and I will see the return value.

Is there a way I can debug the execution of such "manually typed" expressions?

Basically I would like to do is s reddit.get_subreddits(), but that just executes the step command and ignores the expression.

EDIT: A trivial example

Take this simple function:

import random

def get_value_for_weekday(weekday_index=None):
    values = [10, 20, 20, 10, 30, 30, 30]
    if not weekday_index:
        # If no weekday provided, return the average of all weekdays
        return sum(values) / 7
    return averages[weekday_index]

if __name__ == '__main__':
    while True:
        import ipdb; ipdb.set_trace()  # enter ipbd for debug
        get_value_for_weekday(random.randint(0, 7))

Which is bugged because of the if not weekday_index (it should check weekday_index is not None.)

Let's assume I notice I get 10 half the number of times I was expecting. So I added a import ipdb; ipdb.set_trace() before the call to the function to try and debug the code.

So I'm in the ipdb console and I suddenly get the idea that maybe the problem is when I pass 0 as weekday_index. I can test my hypothesis directly in ipdb:

ipdb> p get_value_for_weekday(0)
22

Ok, so I realize there's something wrong when weekday_index=0.
What I would like to do now is debug step by step the call to get_value_for_weekday(0), so that I could see that I erranously enter the if block.

Obviously I could exit ipdb, stop the script, change the code to always pass 0, relaunch the script and when I enter ipdb, debug the call with the ipdb step (s) command.
But wouldn't it be easier if I could just do s get_value_for_weekday(0) much the same way I was able to do p get_value_for_weekday(0)?

Is there a way do something like this?

解决方案

I think you're looking for the (d)ebug command which, for some reason, is not specified in the Debugger Commands. Just for future reference, pdb has a nice set of commands specified (which you can see by typing help in the interactive prompt). On to the debug command:

(Pdb) help debug
debug code
        Enter a recursive debugger that steps through the code
        argument (which is an arbitrary expression or statement to be
        executed in the current environment).

Which seems to do what you're after. Using your sample script from the terminal:

python -m pdb pdbscript.py

After issuing two n commands in order for the function to get parsed (I believe this is how pdb works). You can issue a debug get_value_for_weekday(0) command to recursively step in the function:

(Pdb) debug get_value_for_weekday(0)
ENTERING RECURSIVE DEBUGGER
> <string>(1)<module>()
((Pdb)) s
--Call--
> /home/jim/Desktop/pdbscript.py(3)get_value_for_weekday()
-> def get_value_for_weekday(weekday_index=None):
((Pdb)) n
> /home/jim/Desktop/pdbscript.py(4)get_value_for_weekday()
-> values = [10, 20, 20, 10, 30, 30, 30]
((Pdb)) n 
> /home/jim/Desktop/pdbscript.py(5)get_value_for_weekday()
-> if not weekday_index:
((Pdb)) p weekday_index
0
((Pdb)) n
> /home/jim/Desktop/pdbscript.py(7)get_value_for_weekday()
-> return sum(values) / 7

Do note, I feel really sketchy about this form of meta-debugging but it seems to be what you're after.

这篇关于如何在pdb中调试手动输入的表达式和语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆