debugging PDB中的自动完成和Tab键

2w3rbyxf  于 2023-08-06  发布在  其他
关注(0)|答案(4)|浏览(87)

我一直在尝试让TAB做一些其他的事情,而不是在(pdb)提示符下插入一个制表符。
我想到的是触发自动完成,比如在herehere中,但是tab键除了向pdb添加制表符之外没有做任何其他事情。
因此:

(pdb)var + tabKeyPressed

字符串
我想要得到:

(pdb)variable


而不是:

(pdb)var[          ]

ttygqcqt

ttygqcqt1#

iPython是解决这个问题的第三方解决方案。有时候你只能依赖vanilla Python。我找到了两个解决方案。

每shell解决方案-使用模块'rlcompleter':

$ python3

Python 3.4.3 (default, Sep 14 2016, 12:36:27) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import pdb
>>> pdb.set_trace()
--Return--
> <stdin>(1)<module>()->None

# press tab - but nothing
(Pdb) str.
*** SyntaxError: invalid syntax
(Pdb) --KeyboardInterrupt--
(Pdb) c
>>> import rlcompleter
>>> pdb.Pdb.complete=rlcompleter.Completer(locals()).complete
>>> pdb.set_trace()
--Return--
> <stdin>(1)<module>()->None
(Pdb) str.
str.__add__(            str.__getattribute__(   str.__name__            str.__text_signature__  str.isdigit(            str.rfind(
str.__base__(           str.__getitem__(        str.__ne__(             str.__weakrefoffset__   str.isidentifier(       str.rindex(
str.__bases__           str.__getnewargs__(     str.__new__(            str.capitalize(         str.islower(            str.rjust(
str.__basicsize__       str.__gt__(             str.__prepare__(        str.casefold(           str.isnumeric(          str.rpartition(
str.__call__(           str.__hash__(           str.__qualname__        str.center(             str.isprintable(        str.rsplit(
str.__class__(          str.__init__(           str.__reduce__(         str.count(              str.isspace(            str.rstrip(
str.__contains__(       str.__instancecheck__(  str.__reduce_ex__(      str.encode(             str.istitle(            str.split(
str.__delattr__(        str.__itemsize__        str.__repr__(           str.endswith(           str.isupper(            str.splitlines(
str.__dict__            str.__iter__(           str.__rmod__(           str.expandtabs(         str.join(               str.startswith(
str.__dictoffset__      str.__le__(             str.__rmul__(           str.find(               str.ljust(              str.strip(
str.__dir__(            str.__len__(            str.__setattr__(        str.format(             str.lower(              str.swapcase(
str.__doc__             str.__lt__(             str.__sizeof__(         str.format_map(         str.lstrip(             str.title(
str.__eq__(             str.__mod__(            str.__str__(            str.index(              str.maketrans(          str.translate(
str.__flags__           str.__module__          str.__subclasscheck__(  str.isalnum(            str.mro(                str.upper(
str.__format__(         str.__mro__             str.__subclasses__(     str.isalpha(            str.partition(          str.zfill(
str.__ge__(             str.__mul__(            str.__subclasshook__(   str.isdecimal(          str.replace(            
(Pdb) c
>>>

字符串

系统级解决方案-使用文件~/.pdbrc

$ cat ~/.pdbrc
import rlcompleter
pdb.Pdb.complete=rlcompleter.Completer(locals()).complete
$ python3
Python 3.4.3 (default, Sep 14 2016, 12:36:27) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdb
>>> pdb.set_trace()
--Return--
> <stdin>(1)<module>()->None
(Pdb) str.
str.__add__(            str.__getattribute__(   str.__name__            str.__text_signature__  str.isdigit(            str.rfind(
str.__base__(           str.__getitem__(        str.__ne__(             str.__weakrefoffset__   str.isidentifier(       str.rindex(
str.__bases__           str.__getnewargs__(     str.__new__(            str.capitalize(         str.islower(            str.rjust(
str.__basicsize__       str.__gt__(             str.__prepare__(        str.casefold(           str.isnumeric(          str.rpartition(
str.__call__(           str.__hash__(           str.__qualname__        str.center(             str.isprintable(        str.rsplit(
str.__class__(          str.__init__(           str.__reduce__(         str.count(              str.isspace(            str.rstrip(
str.__contains__(       str.__instancecheck__(  str.__reduce_ex__(      str.encode(             str.istitle(            str.split(
str.__delattr__(        str.__itemsize__        str.__repr__(           str.endswith(           str.isupper(            str.splitlines(
str.__dict__            str.__iter__(           str.__rmod__(           str.expandtabs(         str.join(               str.startswith(
str.__dictoffset__      str.__le__(             str.__rmul__(           str.find(               str.ljust(              str.strip(
str.__dir__(            str.__len__(            str.__setattr__(        str.format(             str.lower(              str.swapcase(
str.__doc__             str.__lt__(             str.__sizeof__(         str.format_map(         str.lstrip(             str.title(
str.__eq__(             str.__mod__(            str.__str__(            str.index(              str.maketrans(          str.translate(
str.__flags__           str.__module__          str.__subclasscheck__(  str.isalnum(            str.mro(                str.upper(
str.__format__(         str.__mro__             str.__subclasses__(     str.isalpha(            str.partition(          str.zfill(
str.__ge__(             str.__mul__(            str.__subclasshook__(   str.isdecimal(          str.replace(            
(Pdb) c
>>>


备注:
1.仅在Python 3.4上测试

  1. Linux Mint
    1.基于https://reminiscential.wordpress.com/2009/06/12/python-enable-auto-complete-in-a-pdb-session/
omtl5h9j

omtl5h9j2#

ipdb来救援。
ipdb导出函数以访问IPython调试器,该调试器具有制表符补全、语法突出显示、更好的回溯、更好的内省功能,接口与pdb模块相同。

0ejtzxu1

0ejtzxu13#

官方文件称:
在版本3.3中更改:通过readline模块的制表符完成可用于命令和命令参数,例如当前全局和局部名称作为p命令的参数提供。
https://docs.python.org/3/library/pdb.html
所以你只需要使用p命令:

(Pdb) p var[TAB] # complete global and local names
var1 var2
(Pdb) [TAB] # complete commands
EOF        b          cl         cont       disable    exit       interact   list       next       quit       retval     source     unalias    up         
a          break      clear      continue   display    h          j          ll         p          r          run        step       undisplay  w          
alias      bt         commands   d          down       help       jump       longlist   pp         restart    rv         tbreak     unt        whatis     
args       c          condition  debug      enable     ignore     l          n          q          return     s          u          until      where

字符串

jutyujz0

jutyujz04#

如果你使用macOS和NetBSD libedit(默认),或者你的Python不是用GNU readline lib而是用Net BSD libedit编译的,请在~/.editrc中插入python:bind ^I rl_complete。在本例中,^I有两个字符(^I)。
此外,您必须删除您尝试的GNU readline解决方案。例如~/.pdbrc(或./.pdbrc)或rlcompleter中的某个部分。

相关问题