我一直在尝试让 TAB 在 (pdb) 提示符下执行除插入选项卡之外的其他操作。
我想到的是触发自动完成,例如在here或here中,但是tab键除了向pdb添加选项卡之外没有任何其他作用。
因此:
(pdb)var + tabKeyPressed
我想要得到:
(pdb)变量
而不是:
(pdb)var[ ]
iPython 是针对此问题的第三方解决方案。有时你只能依赖普通的 Python。我找到了 2 个解决方案。
每 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
>>>
备注:
仅在 Python 3.4 上测试
操作系统 - Linux Mint
基于https://reminiscential.wordpress.com/2009/06/12/python-enable-auto-complete-in-a-pdb-session/
ipdb 来救援。
ipdb 导出函数来访问 IPython 调试器,其特点 制表符补全,语法高亮,更好的回溯,更好 使用与 pdb 模块相同的接口进行内省。
官方文件说:
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
如果您使用带有 NetBSD libedit(默认)的 macOS,或者您的 Python 不是使用 GNU readline lib 而是 Net BSD libedit 编译的,请在
python:bind ^I rl_complete
中插入 ~/.editrc
。
在这种情况下,^I
有两个字符。(^
和I
)
此外,您还必须删除您尝试过的 GNU readline 解决方案。例如
答案的
~/.pdbrc
或rlcompleter
中的某些部分。