[英]Is there a way to search the code while in the Python debugger?
Simple question but I can't find the answer.简单的问题,但我找不到答案。
In the Perl debugger, one can search the code by using "/string" as in typing / followed with the string one is searching for.在 Perl 调试器中,可以使用“/string”来搜索代码,就像在键入 / 后跟正在搜索的字符串一样。
Here's an example of a simple Perl debugging session where I'm looking for the word "init" to locate the subroutine initFromCfgFile.这是一个简单的 Perl 调试会话的示例,我在其中查找单词“init”来定位子例程 initFromCfgFile。 I find it at line 93 after using "/" twice:使用“/”两次后,我在第 93 行找到它:
% perl -d padmin.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::(padmin.pl:18): my $ACTION_STOP = 'stop';
DB<1> /init
59: initFromCfgFile();
DB<2> /
93: sub initFromCfgFile {
Is there a way to do the same with the Python debugger?有没有办法对 Python 调试器做同样的事情?
You can subclass pdb.Pdb
, which is a subclass of cmd.Cmd
, with a do_find
method to implement a find
command (and a do_f
attribute to implement a f
command alias):你也可以继承pdb.Pdb
,这是一个子类cmd.Cmd
,具有do_find
方法来实现find
命令(和do_f
属性来实现f
命令别名):
import pdb
import linecache
class fdb(pdb.Pdb):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.find = None
def do_find(self, arg):
if arg:
self.find = arg
self.last_lineno = 0
elif not self.find:
self.error('A search string must be specified.')
return
filename = self.curframe.f_code.co_filename
for lineno, line in enumerate(linecache.getlines(filename, self.curframe.f_globals), 1):
if lineno > self.last_lineno and self.find in line:
self._print_lines([line], lineno)
self.last_lineno = lineno
break
else:
self.error(f'No {"more " if self.last_lineno else ""}matching line found.')
do_f = do_find
fdb().set_trace()
Sample input and output:示例输入和输出:
--Return--
> test.py(26)<module>()->None
-> fdb().set_trace()
(Pdb) find
*** A search string must be specified.
(Pdb) find foobar
*** No matching line found.
(Pdb) find error
14 self.error('A search string must be specified.')
(Pdb) f
23 self.error(f'No {"more " if self.last_lineno else ""}matching line found.')
(Pdb) f
*** No more matching line found.
(Pdb)
Demo: https://repl.it/@blhsing/PureHarmfulApplicationframework演示: https : //repl.it/@blhsing/PureHarmfulApplicationframework
Reading through the pdb docs , it doesn't look like it.通读 pdb 文档,它看起来不像。 You can use l
to show code, and it takes a line number argument so you can browse around, and you can also try source <expr>
to try to find the source associated with a particular object, but for full text searching i think your best bet is to just open the file in an editor separately, or use grep.您可以使用l
来显示代码,它需要一个行号参数,以便您可以浏览,您也可以尝试使用source <expr>
来尝试查找与特定对象关联的源,但是对于全文搜索,我认为您的最好的办法是单独在编辑器中打开文件,或使用 grep。
I use a full-featured IDE for Python development rather than directly using command line tools.我使用功能齐全的 IDE 进行 Python 开发,而不是直接使用命令行工具。 In IntelliJ IDEA/PyCharm, you can navigate code easily while in the middle of a debugging session.在 IntelliJ IDEA/PyCharm 中,您可以在调试会话期间轻松导航代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.