How can I make my program crash properly when using the python cmd module?

It happens that if your code throws an exception at runtime and that your completion does not work, you do not know why, because the trace is not printed. Try this very short code to understand what I mean: the program should crash on the c = 2+ "ddda" line, obviously you are adding a line and int that just don't work. But instead of crashing, the exception seems to be caught, and you have no idea what is going on. The program continues to work as if nothing was happening.

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 an error when it is? (when using the cmd module).

+5
source share
1 answer

, - readline. - :

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'

, readline .

 

, readline.

+5

All Articles