回溯對象被送入excepthook方法不具有 文檔中列出的屬性或屬性的任意,所以 它顯然不同的形式「回溯對象」的。
我沒有看到的是:
import sys
import traceback
def myhandler(e_class, e_instance, tb_obj):
print "hello**************\n"
print e_instance
traceback.print_tb(tb_obj)
print
while tb_obj:
frame = tb_obj.tb_frame
print 'locals --->', frame.f_locals
print 'globals -->', frame.f_globals, "\n"
tb_obj = tb_obj.tb_next
print "goodbye************"
sys.excepthook = myhandler
x = 10
def do_stuff():
y = 20
def inner():
z = 30
1/0
inner()
do_stuff()
--output:--
hello**************
integer division or modulo by zero
File "1.py", line 32, in <module>
do_stuff()
File "1.py", line 30, in do_stuff
inner()
File "1.py", line 28, in inner
1/0
locals ---> {'__builtins__': <module '__builtin__' (built-in)>, '__file__': '1.py', 'traceback': <module 'traceback' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.pyc'>, '__package__': None, 'sys': <module 'sys' (built-in)>, 'x': 10, '__name__': '__main__', 'do_stuff': <function do_stuff at 0x2b33f0>, '__doc__': None, 'myhandler': <function myhandler at 0x2b3430>}
globals --> {'__builtins__': <module '__builtin__' (built-in)>, '__file__': '1.py', 'traceback': <module 'traceback' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.pyc'>, '__package__': None, 'sys': <module 'sys' (built-in)>, 'x': 10, '__name__': '__main__', 'do_stuff': <function do_stuff at 0x2b33f0>, '__doc__': None, 'myhandler': <function myhandler at 0x2b3430>}
locals ---> {'y': 20, 'inner': <function inner at 0x2b33b0>}
globals --> {'__builtins__': <module '__builtin__' (built-in)>, '__file__': '1.py', 'traceback': <module 'traceback' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.pyc'>, '__package__': None, 'sys': <module 'sys' (built-in)>, 'x': 10, '__name__': '__main__', 'do_stuff': <function do_stuff at 0x2b33f0>, '__doc__': None, 'myhandler': <function myhandler at 0x2b3430>}
locals ---> {'z': 30}
globals --> {'__builtins__': <module '__builtin__' (built-in)>, '__file__': '1.py', 'traceback': <module 'traceback' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/traceback.pyc'>, '__package__': None, 'sys': <module 'sys' (built-in)>, 'x': 10, '__name__': '__main__', 'do_stuff': <function do_stuff at 0x2b33f0>, '__doc__': None, 'myhandler': <function myhandler at 0x2b3430>}
goodbye************
我讀過一些關於追蹤模塊,但它不是老老實實做 多大意義,我
回溯模塊與追溯對象不同。兩人恰好有相同的名字。在sys.excepthook()函數中,如果命名參數變量traceback
,不要期望它具有跟蹤模塊中列出的任何方法。這裏有一個回溯對象的屬性:
http://docs.python.org/2/reference/datamodel.html
+1大格格,很好的問題,表現爲思想和人性化。與我看到的一些問題形成鮮明對比。 –
同意Alex G.和我對答案非常感興趣 –