【发布时间】:2020-04-22 13:56:50
【问题描述】:
考虑这段代码:
#!/usr/bin/env python3
from cmd import Cmd
import readline
class mycmd(Cmd):
def match_display_hook(self, substitution, matches, longest_match_length):
someNonexistentMethod()
print()
for match in matches:
print(match)
print(self.prompt, readline.get_line_buffer(), sep='', end='', flush=True)
def do_crash(self, s):
someNonexistentMethod()
def do_quit(self, s):
return True
if __name__ == '__main__':
obj = mycmd()
readline.set_completion_display_matches_hook(obj.match_display_hook)
obj.cmdloop()
当我运行它并点击 TabTab 时,我希望看到 NameError: name 'someNonexistentMethod' is not defined。但是,实际上似乎根本没有发生任何事情(确实发生了错误,因此打印完成的其他函数不会运行;我只是没有看到错误)。当我运行crash 时,我确实看到了预期的错误,所以我知道错误处理在整个程序中运行良好,但只是在set_completion_display_matches_hook 回调中被破坏了。为什么会这样,我可以做些什么吗?
【问题讨论】:
标签: python error-handling readline error-suppression python-cmd