Python-traceback:打印或检索堆栈回溯

官方文档

        该模块提供了一个标准接口来提取、格式化和打印 Python 程序的堆栈跟踪结果。它完全模仿Python 解释器在打印堆栈跟踪结果时的行为。当您想要在程序控制下打印堆栈跟踪结果时,例如在“封装”解释器时,这是非常有用的。

        这个模块使用 traceback 对象 —— 这是存储在 sys.last_traceback 中的对象类型变量,并作为 sys.exc_info() 的第三项被返回。

        因功能比较单一,这里只记录下使用示例,便于查找,参考下方代码:

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:")
    # exc_type below is ignored on 3.5 and later
    traceback.print_exception(exc_type, exc_value, exc_traceback,
                              limit=2, file=sys.stdout)

    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)

    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])

    print("*** format_exception:")
    # exc_type below is ignored on 3.5 and later
    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 "traceback_demo.py", line 10, in <module>
    lumberjack()


*** print_exception:
Traceback (most recent call last):
  File "traceback_demo.py", line 10, in <module>
    lumberjack()
  File "traceback_demo.py", line 4, in lumberjack
    bright_side_of_death()
IndexError: tuple index out of range


*** print_exc:
Traceback (most recent call last):
  File "traceback_demo.py", line 10, in <module>
    lumberjack()
  File "traceback_demo.py", 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 "traceback_demo.py", line 10, in <module>\n    lumberjack()\n', '  File "traceback_demo.py", line 4, in lumberjack\n    bright_side_of_death()\n', '  File "traceback_demo.py", line 7, in bright_side_of_death\n    return tuple()[0]\n', 'IndexError: tuple index out of range\n']


*** extract_tb:
[<FrameSummary file traceback_demo.py, line 10 in <module>>, <FrameSummary file traceback_demo.py, line 4 in lumberjack>, <FrameSummary file traceback_demo.py, line 7 in bright_side_of_death>]


*** format_tb:
['  File "traceback_demo.py", line 10, in <module>\n    lumberjack()\n', '  File "traceback_demo.py", line 4, in lumberjack\n    bright_side_of_death()\n', '  File "traceback_demo.py", line 7, in bright_side_of_death\n    return tuple()[0]\n']


*** tb_lineno: 10
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值