[python-win32] sys.excepthook not working as expected

Jens B. Jorgensen jens.jorgensen@tallan.com
Fri, 21 Feb 2003 12:34:33 -0600


Well, you have been bit by a namespace issue. traceback the object is 
passed to Myexcepthook but you thereafter say "import traceback" which 
in effect means that you just lost that traceback object that you're 
passing to format_exception. Change your function to:

def Myexcepthook(type, value, tb):
    print "in Myexcepthook"
    import traceback
    lines=traceback.format_exception(type, value, tb)
    print "---------------------Traceback lines-----------------------"
    print "\n".join(lines)
    print "-----------------------------------------------------------"
    sys.exit(2)


Larry Bates wrote:

>I have tried unsuccessfully to set my own function to handle uncaught
>exceptions.
>Below is a short test program that doesn't work as expected:
>
>import sys
>def Myexcepthook(type, value, traceback):
>    print "in Myexcepthook"
>    import traceback
>    lines=traceback.format_exception(type, value, traceback) 
>    print "---------------------Traceback lines-----------------------"
>    print "\n".join(lines)
>    print "-----------------------------------------------------------"
>    sys.exit(2)
>
>
>#
># set sys.excepthook
>#
>sys.excepthook=Myexcepthook
>#
># create an uncaught divide by zero exception
>#
>a=1/0
>#---------------------End of program------------------------
>
>When I execute this program I get:
>
>in Myexcepthook
>Error in sys.excepthook:
>Traceback (most recent call last):
>File "excepthooktest.py", line 6, in Myexcepthook
>lines=traceback.format_exception(type, value, traceback)
>File "C:\Python22\Lib\traceback.py", line 140, in format_exception
>list = list + format_tb(tb, limit)
>File "C:\Python22\Lib\traceback.py", line 75, in format_tb
>return format_list(extract_tb(tb, limit))
>File "C:\Python22\Lib\traceback.py", line 94, in extract_tb
>f = tb.tb_frame
>AttributeError: 'module' object has no attribute 'tb_frame'
>
>Original exception was:
>Traceback (most recent call last):
>File "excepthooktest.py", line 20, in ?
>a=1/0
>ZeroDivisionError: integer division or modulo by zero
>
>If I change:
>
>lines=traceback.format_exception(type, value, traceback)
>
>to
>
>lines=traceback.format_exception_only(type, value)
>
>Myexcepthook function behaves properly and prints:
>
>---------------------Traceback lines-----------------------
>ZeroDivisionError: integer division or modulo by zero
>
>-----------------------------------------------------------
>
>Anyone have any ideas. I would like to capture the traceback information as
>well as the exception.
>
>Also, the sys.excepthook override doesn't seem to work at all when I run
>this interactively, only works if run from console.
>
>BTW - I'm using ActiveState PythonWin 2.2.1 (#34, Apr 15 2002, 09:51:39)
>
>Thanks in advance,
>Larry Bates
>
>lbates@syscononline.com
>
>
>
>_______________________________________________
>Python-win32 mailing list
>Python-win32@python.org
>http://mail.python.org/mailman/listinfo/python-win32
>  
>

-- 
Jens B. Jorgensen
jens.jorgensen@tallan.com

"With a focused commitment to our clients and our people, we deliver value through customized technology solutions"