conditional sys.excepthook (outside of the interactive interpreter)?

Vlastimil Brom vlastimil.brom at gmail.com
Mon Mar 9 05:46:34 EDT 2015


Hi all,
I'd like to as for advise about posibilities of redirecting error
traceback via sys.excepthook in a gui (wxpython in my case) with a
requirement, that the errors from the code typed in the interactive
interpreter (as part of my app) are printed there directly and are not
catched from the excepthook.
This worked in python 2.7, but was apparently changed for py 3 (3.4 in
my case).
Now, in the underlying library module the excepthook function is
explicitly called:

... \Python34\Lib\code.py
    def showtraceback(self):
[...]
        finally:
            tblist = tb = None
        if sys.excepthook is sys.__excepthook__:
            self.write(''.join(lines))
        else:
            # If someone has set sys.excepthook, we let that take precedence
            # over self.write
            sys.excepthook(type, value, tb)

I asume, this was an intentional change, but
I could only find a rather hackish solution in specialcasing the
intepreter calls in my excepthook function:

def redirect_error(err_type, err_value, err_trace):
    if "wx\\py\\interpreter.py" in "".join(traceback.format_stack()):
        sys.__excepthook__(err_type, err_value, err_trace)
        return False
    ...

Are there probably any drawbacks in using this,
(apart from the additional item in the traceback, code.py ... )?
>>> 1/0 # pyshell using  redirect_error excethook
Traceback (most recent call last):
  File "C:\Python34\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
ZeroDivisionError: division by zero
>>>

Some more info including the wxpython specific context is included in
my original post in the wxpython maillist:
https://groups.google.com/forum/#!topic/wxpython-users/E-kqf-TeQJ0

Any hints or opinions on some more standard approaches are much appreciated.

Thanks and regards,
    vbr



More information about the Python-list mailing list