Python调试器:步入您以交互方式调用的函数 [英] Python debugger: Stepping into a function that you have called interactively

查看:170
本文介绍了Python调试器:步入您以交互方式调用的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python很酷,但不幸的是,它的调试器不如perl -d好。



在尝试代码时,我非常常用的一件事是从调试器中调用一个函数,然后进入该函数,如下所示:

 #注意这个程序立即排除,无需呼叫FOO()
〜> cat -n /tmp/show_perl.pl
1#!/ usr / local / bin / perl
2
3 sub foo {
4 printhi \\\
;
5 printbye\\\
;
6}
7
8 exit 0;

〜> perl -d /tmp/show_perl.pl

从perl5db.pl加载数据库例程版本1.28
编辑器支持可用。

输入h或h h进行帮助,或者输入man perldebug以获得更多帮助。

main ::(/ tmp / show_perl.pl:8):exit 0;

#MAGIC HAPPENS HERE - 我进入一个功能,我正在调用互动
DB< 1> s foo()
main ::((eval 6)[/ usr / local / lib / perl5 / 5.8.6 / perl5db.pl:628]:3):
3:foo();


DB< 2>> s
main :: foo(/tmp/show_perl.pl:4):printhi \\\
;


DB< 2>> n
hi
main :: foo(/tmp/show_perl.pl:5):printbye\\\
;


DB< 2>> n
bye


DB <2> n
调试程序已终止。使用q退出或R重新启动,
使用o inhibit_exit避免程序终止后停止,
h q,h R或h O获取附加信息。

DB< 2> q

当尝试逐步处理各种不同的输入时,这是非常有用的,以了解为什么它失败。但是,在pdb或pydb中似乎不起作用(我将在上面显示一个等效的python示例,但是它会导致一个大的异常堆栈转储)。



所以我的问题有两个方面:


  1. 我错过了什么?

  2. 有没有python调试器确实让我这样做?

显然,我可以把代码中的调用自己,但我喜欢交互工作,例如。我不想从头开始尝试用稍微不同的参数来调用。

解决方案

我已经回答我自己的问题!这是pydb中的debug命令:

 〜> cat -n /tmp/test_python.py 
1#!/ usr / local / bin / python
2
3 def foo():
4打印嗨
5 printbye
6
7退出(0)
8

〜> pydb /tmp/test_python.py
(/tmp/test_python.py:7):< module>
7退出(0)


(Pydb)调试foo()
输入RECURSIVE DEBUGGER
----------- -------------调用级别11
(/tmp/test_python.py:3):foo
3 def foo():

((Pydb))s
(/tmp/test_python.py:4):foo
4打印嗨

((Pydb))s
hi
(/tmp/test_python.py:5):foo
5打印bye


((Pydb))s
bye
------------------------从11级返回(< type'NoneType'>)
------ ----------------从10级返回(< type'NoneType'>)
LEAVING RECURSIVE DEBUGGER
(/tmp/test_python.py:7 ):< module>


Python is quite cool, but unfortunately, its debugger is not as good as perl -d.

One thing that I do very commonly when experimenting with code is to call a function from within the debugger, and step into that function, like so:

# NOTE THAT THIS PROGRAM EXITS IMMEDIATELY WITHOUT CALLING FOO()
~> cat -n /tmp/show_perl.pl
1  #!/usr/local/bin/perl
2
3  sub foo {
4      print "hi\n";
5      print "bye\n";
6  }
7
8  exit 0;

~> perl -d /tmp/show_perl.pl

Loading DB routines from perl5db.pl version 1.28
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(/tmp/show_perl.pl:8):    exit 0;

# MAGIC HAPPENS HERE -- I AM STEPPING INTO A FUNCTION THAT I AM CALLING INTERACTIVELY
  DB<1> s foo() 
main::((eval 6)[/usr/local/lib/perl5/5.8.6/perl5db.pl:628]:3):
3:      foo();


  DB<<2>> s
main::foo(/tmp/show_perl.pl:4):     print "hi\n";


  DB<<2>> n
hi
main::foo(/tmp/show_perl.pl:5):     print "bye\n";


  DB<<2>> n
bye


  DB<2> n
Debugged program terminated.  Use q to quit or R to restart,
  use O inhibit_exit to avoid stopping after program termination,
  h q, h R or h O to get additional info.  

  DB<2> q

This is incredibly useful when trying to step through a function's handling of various different inputs to figure out why it fails. However, it does not seem to work in either pdb or pydb (I'd show an equivalent python example to the one above but it results in a large exception stack dump).

So my question is twofold:

  1. Am I missing something?
  2. Is there a python debugger that would indeed let me do this?

Obviously I could put the calls in the code myself, but I love working interactively, eg. not having to start from scratch when I want to try calling with a slightly different set of arguments.

解决方案

And I've answered my own question! It's the "debug" command in pydb:

~> cat -n /tmp/test_python.py
     1  #!/usr/local/bin/python
     2
     3  def foo():
     4      print "hi"
     5      print "bye"
     6
     7  exit(0)
     8

~> pydb /tmp/test_python.py
(/tmp/test_python.py:7):  <module>
7 exit(0)


(Pydb) debug foo()
ENTERING RECURSIVE DEBUGGER
------------------------Call level 11
(/tmp/test_python.py:3):  foo
3 def foo():

((Pydb)) s
(/tmp/test_python.py:4):  foo
4     print "hi"

((Pydb)) s
hi
(/tmp/test_python.py:5):  foo
5     print "bye"


((Pydb)) s
bye
------------------------Return from level 11 (<type 'NoneType'>)
----------------------Return from level 10 (<type 'NoneType'>)
LEAVING RECURSIVE DEBUGGER
(/tmp/test_python.py:7):  <module>

这篇关于Python调试器:步入您以交互方式调用的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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