python - PDB 中的自动完成和制表键

标签 python python-2.7 debugging autocomplete pdb

我一直在尝试让 TAB 在 (pdb) 提示符下执行除插入制表符之外的其他操作。

我的想法是触发自动完成,例如 herehere , 但 tab 键除了向 pdb 添加制表符外没有做任何其他事情。

因此:

(pdb)var + tabKeyPressed

我想得到:

(pdb)variable

代替:

(pdb)var[          ]

最佳答案

iPython 是针对此问题的第三方解决方案。有时你只能依赖普通的 Python。我找到了 2 个解决方案。

Per-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 上测试

  2. 操作系统 - Linux Mint

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

关于python - PDB 中的自动完成和制表键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15971201/

相关文章:

php - phpstorm与xdebug的连接

debugging - Eclipse Scala 解释器 (REPL) - 正确使用和调试

python - 更改 pandas DataFrame 中每个组的第一个元素

python - 在 Python 2.7 中将 ½ 解析为 0.5

python - 如何使线图出现在同一个图形上而不是不同的图形上?

python-2.7 - Python pdfminer 提取图片每页生成多张图片(应该是单张图片)

Python:更改导入字典中的值没有效果

c - list[idx] = strdup(tok[1] +tok[2]) 或初始 malloc 到 list -C 的大小

python - 如何在 python pandas 中标记循环数的值

python - numpy 对每一行进行排序并检索第 k 个元素