一:traceback說明
該模塊提供了一個標准接口來提取,格式化和打印Python程序的堆棧跟蹤。它完全模仿Python解釋器在打印堆棧跟蹤時的行為。當您想要在程序控制下打印堆棧跟蹤時,這很有用。
二:模塊定義了以下功能:
traceback.print_tb(tb [,limit [,file ] ] ) 打印以限制回溯對象tb的堆棧跟蹤條目。如果 省略限制或者None打印所有條目。如果文件被省略或者None輸出到了sys.stderr; 否則它應該是一個打開的文件或文件類對象來接收輸出。 traceback.print_exception(etype,value,tb [,limit [,file ] ] ) 打印異常信息,並將traceback tb中的堆棧跟蹤條目限制為文件。這與以下方面有所不同:(1)如果tb不是,則打印一個標題; (2)在堆棧跟蹤之后打印異常etype和值 ; (3)如果etype的值和值具有適當的格式,則會打印語法錯誤發生的行,並在其中指出錯誤的大概位置。print_tb()NoneTraceback (most recent call last):SyntaxError traceback.print_exc([ limit [,file ] ] ) 這是一個簡寫。(事實上,它用於以線程安全的方式檢索相同的信息,而不是使用已棄用的變量。)print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file)sys.exc_info() traceback.format_exc([ 限制] ) 這就像是print_exc(limit)返回一個字符串,而不是打印到一個文件。 2.4版本中的新功能。 traceback.print_last([ limit [,file ] ] ) 這是一個簡寫。一般而言,只有在例外達到交互式提示后才能使用(請參閱)。print_exception(sys.last_type, sys.last_value, sys.last_traceback, limit, file)sys.last_type traceback.print_stack([ f [,limit [,file ] ] ] ) 該函數從其調用點打印堆棧跟蹤。可選的 f參數可用於指定要啟動的備用堆棧幀。可選限制和文件參數與for具有相同的含義 print_exception()。 traceback.extract_tb(tb [,limit ] ) 返回一個列表,最多可以限制從回溯對象tb中提取的“預處理”堆棧跟蹤條目。這對堆棧跟蹤的替代格式非常有用。如果限制被忽略或者None所有條目被提取。“預處理”堆棧跟蹤條目是一個4元組(文件名,行號,函數名稱*,文本),表示通常為堆棧跟蹤打印的信息。該文本是一個帶有前導和尾隨空白字符的字符串; 如果源不可用,它是None。 traceback.extract_stack([ f [,limit ] ] ) 從當前堆棧幀中提取原始回溯。返回值與格式相同extract_tb()。可選的f和限制 參數與for具有相同的含義print_stack()。 traceback.format_list(extracted_list ) 給出extract_tb()or extract_stack()返回的元組列表,返回一個准備打印的字符串列表。結果列表中的每個字符串對應於參數列表中具有相同索引的項目。每個字符串以換行符結束; 這些字符串也可以包含內部換行符,對於那些源文本行不是的項目 None。 traceback.format_exception_only(etype,value ) 格式化追溯的異常部分。的參數是異常類型,VLAN時和值,如由下式給出sys.last_type和 sys.last_value。返回值是一個字符串列表,每個字符串都以換行符結尾。通常,該列表包含一個字符串; 但是,對於 SyntaxError例外情況,它包含幾行(打印時)顯示有關語法錯誤發生位置的詳細信息。指示發生異常的消息是列表中總是最后一個字符串。 traceback.format_exception(etype,value,tb [,limit ] ) 格式化堆棧跟蹤和異常信息。參數與相應的參數具有相同的含義print_exception()。返回值是一串字符串,每個字符串以換行符結尾,一些字符串包含內部換行符。當這些行連接並打印時,打印的文本與打印的文本完全相同print_exception()。 traceback.format_tb(tb [,limit ] ) 速記。format_list(extract_tb(tb, limit)) traceback.format_stack([ f [,limit ] ] ) 速記。format_list(extract_stack(f, limit)) traceback.tb_lineno(tb ) 該函數返回在回溯對象中設置的當前行號。這個函數是必須的,因為在2.3之前的Python版本中,當-O標志被傳遞給Python時,tb.tb_lineno它沒有被正確更新。這個功能在2.3版以后沒有用。
三:示例
import sys, traceback def lumberjack(): bright_side_of_death() def bright_side_of_death(): return tuple()[0] try: lumberjack() except IndexError: exc_type, exc_value, exc_traceback = sys.exc_info() print "*** print_tb:" traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) print "*** print_exception:" traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout) print "*** print_exc:" traceback.print_exc() print "*** format_exc, first and last line:" formatted_lines = traceback.format_exc().splitlines() print formatted_lines[0] print formatted_lines[-1] print "*** format_exception:" print repr(traceback.format_exception(exc_type, exc_value, exc_traceback)) print "*** extract_tb:" print repr(traceback.extract_tb(exc_traceback)) print "*** format_tb:" print repr(traceback.format_tb(exc_traceback)) print "*** tb_lineno:", exc_traceback.tb_lineno =示例的輸出結果如下所示: *** print_tb: File "<doctest...>", line 10, in <module> lumberjack() *** print_exception: Traceback (most recent call last): File "<doctest...>", line 10, in <module> lumberjack() File "<doctest...>", line 4, in lumberjack bright_side_of_death() IndexError: tuple index out of range *** print_exc: Traceback (most recent call last): File "<doctest...>", line 10, in <module> lumberjack() File "<doctest...>", line 4, in lumberjack bright_side_of_death() IndexError: tuple index out of range *** format_exc, first and last line: Traceback (most recent call last): IndexError: tuple index out of range *** format_exception: ['Traceback (most recent call last):\n', ' File "<doctest...>", line 10, in <module>\n lumberjack()\n', ' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n', ' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n', 'IndexError: tuple index out of range\n'] *** extract_tb: [('<doctest...>', 10, '<module>', 'lumberjack()'), ('<doctest...>', 4, 'lumberjack', 'bright_side_of_death()'), ('<doctest...>', 7, 'bright_side_of_death', 'return tuple()[0]')] *** format_tb: [' File "<doctest...>", line 10, in <module>\n lumberjack()\n', ' File "<doctest...>", line 4, in lumberjack\n bright_side_of_death()\n', ' File "<doctest...>", line 7, in bright_side_of_death\n return tuple()[0]\n']======
