如何在Python中获取完整的异常堆栈跟踪 [英] How to get a complete exception stack trace in Python

查看:1054
本文介绍了如何在Python中获取完整的异常堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码段:

  import traceback 

def a():
b ()

def b():
try:
c()
除了:
traceback.print_exc()

def c():
assert False

a()

生成此输出:

 追溯(最近的最后一次调用):
文件test.py,第8行,在b
c()
文件test.py,第13行,c
assert False
AssertionError

如果我想要完整的堆栈跟踪(包括调用),应该怎么使用?



如果重要有Python 2.6.6



编辑:我想要得到的是我离开尝试除外的信息,让异常传播到顶级。此片段例如:

  def a():
b()

def b ():
c()

def c():
assert False

a()
/ pre>

生成此输出:

 追溯(最近的通话最后):
文件test.py,第10行,< module>
a()
文件test.py,第2行,
b()
文件test.py,第5行,b
c()
文件test.py,第8行,c
assert False
AssertionError


解决方案

我不知道是否有更好的方法,但这是我所做的:

  import traceback 
import sys

def format_exception(e):
exception_list = traceback.format_stack()
exception_list = exception_list [ -2]
exception_list.extend(traceback.format_tb(sys.exc_info()[2]))
exception_list.extend(traceback.format_exception_only(sys.exc_info()[0],sys.exc_info )[$]

exception_str =追溯(最近的最后一次调用):\\\

exception_str + =.join(exception_list)
#最后\\\

exception_str = exception_str [: - 1]

return exception_str

def main1():
main2()

def main2():
尝试:
main3()
除了异常作为e:
打印仅打印当前堆栈框架上方的追溯
print.join(traceback.format_exception(sys。 exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]))
打印
打印打印完整的追溯,就好像我们没有抓住它。
print format_exception(e)

def main3():
raise异常()

如果__name__ =='__main__':
main1()

这里是我得到的输出:

 仅打印当前堆栈框架上方的追溯
追溯(最近的最后一次调用):
文件exc.py,第22行,in main2
main3()
文件exc.py,第31行,main3
raise异常()
异常


打印完整的追溯,就好像我们没有在这里抓到...
追溯(最近的最后一次调用):
文件exc.py,第34行,在< module& GT;
main1()
文件exc.py,第18行,main1
main2()
文件exc.py,第22行,main2
main3()
文件exc.py,第31行,main3
raise异常()
异常


The following snippet:

import traceback

def a():
    b()

def b():
    try:
        c()
    except:
        traceback.print_exc()

def c():
    assert False

a()

Produces this output:

Traceback (most recent call last):
  File "test.py", line 8, in b
    c()
  File "test.py", line 13, in c
    assert False
AssertionError

What should I use if I want the complete stack trace including the call to a?

If it matters I have Python 2.6.6

edit: What I'd like to get is the same information I'd get if I left the try except out and let the exception propagate to the top level. This snippet for example:

def a():
    b()

def b():
    c()

def c():
    assert False

a()

Produces this output:

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    a()
  File "test.py", line 2, in a
    b()
  File "test.py", line 5, in b
    c()
  File "test.py", line 8, in c
    assert False
AssertionError

解决方案

I don't know if there is a better way, but here's what I did:

import traceback
import sys

def format_exception(e):
    exception_list = traceback.format_stack()
    exception_list = exception_list[:-2]
    exception_list.extend(traceback.format_tb(sys.exc_info()[2]))
    exception_list.extend(traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1]))

    exception_str = "Traceback (most recent call last):\n"
    exception_str += "".join(exception_list)
    # Removing the last \n
    exception_str = exception_str[:-1]

    return exception_str

def main1():
    main2()

def main2():
    try:
        main3()
    except Exception as e:
        print "Printing only the traceback above the current stack frame"
        print "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
        print
        print "Printing the full traceback as if we had not caught it here..."
        print format_exception(e)

def main3():
    raise Exception()

if __name__ == '__main__':
    main1()

And here's the output I get:

Printing only the traceback above the current stack frame
Traceback (most recent call last):
  File "exc.py", line 22, in main2
    main3()
  File "exc.py", line 31, in main3
    raise Exception()
Exception


Printing the full traceback as if we had not caught it here...
Traceback (most recent call last):
  File "exc.py", line 34, in <module>
    main1()
  File "exc.py", line 18, in main1
    main2()
  File "exc.py", line 22, in main2
    main3()
  File "exc.py", line 31, in main3
    raise Exception()
Exception

这篇关于如何在Python中获取完整的异常堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆