How can I make my program properly crash when using the cmd python module?
cmd, python
Solution
Unfortunately, exceptions in completers are caught somewhere inside the dark depths of `readline`. You can try something like that:
import cmd
import traceback
def log_exceptions(fun):
def wrapped(*a, **kw):
try:
return fun(*a, **kw)
except Exception:
print traceback.format_exc()
raise
return wrapped
class App(cmd.Cmd):
@log_exceptions
def complete_foo(self,*arg):
# Uncommenting this line will silently crash the progrm
# making it hard to debug.
# Is there a way to force the program to crash ?
c = 2 + "ddda"
return "d dzpo idz dza dpaoi".split(" ")
$ python c.py
(Cmd) foo Traceback (most recent call last):
File "c.py", line 7, in wrapped
return fun(*a, **kw)
File "c.py", line 20, in complete_foo
c = 2 + "ddda"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Remove the decorator after debugging your completers, because printing tracebacks from inside the readline can mess up your terminal.
No, you can't crash readline easily.
Problem
What happens is that if your code raises a runtime exception and that your completion doesn't work, you have no idea why because traceback isn't printed. Try this very short code to see what I mean : the program should crash on line c = 2+"ddda", obviously you're adding a string and an int, which simply doesn't work. But instead of crashing, the exception is kind of caught and you have no idea what's happening. The program continues to run as if nothing happend. ``` import cmd class App(cmd.Cmd): def complete_foo(self,*arg): # Uncommenting this line will silently crash the progrm # making it hard to debug. # Is there a way to force the program to crash ? c = 2 + "ddda" return "d dzpo idz dza dpaoi".split(" ") def do_foo(self,*args): print "foo" App().cmdloop() ``` My question is : how to show the error when there is one ? (when using the cmd module).