top | item 1653333

(no title)

huwigs | 15 years ago

> Since I cannot tell Python to produce succinct unixy error messages instead of rambling stack traces

`sys.excepthook` is how you can do that.

Without:

     x = {}
     def f():
         print(x['foo'])
     f()
     # ...

      Traceback (most recent call last):
	File "stack.py", line 6, in <module>
	  f()
	File "stack.py", line 4, in f
	  print(x['foo'])
      KeyError: 'foo'
With:

     import sys

     def short_err(exc_type, exc, tb):
         sys.stderr.write("error: tracebacks too long\n")

     sys.excepthook=short_err

     x = {}
     def f():
         print(x['foo'])
     f()

     #...

     error: tracebacks too long
So don't worry about catching exceptions if you're just printing errors.

discuss

order

gn|15 years ago

> `sys.excepthook` is how you can do that.

Awesome. Thank you kindly.

> error: tracebacks too long

I like your style.