kution 发表于 2017-5-7 14:01:01

python中利用tracekback跟踪栈以及打印异常信息

  ​##sys.exc_info()

返回 (type, value, traceback). type为异常类型, value为异常的参数(通常为异常错误的信息), traceback为跟踪回溯的对象.


    exc_type, exc_value, exc_traceback = sys.exc_info()
print "*** print sys.exc_info:"
print 'exc_type is: %s, exc_value is: %s, exc_traceback is: %s' % (exc_type, exc_value, exc_traceback)
  输出:


*** print sys.exc_info:
exc_type is: <type 'exceptions.IndexError'>, exc_value is: tuple index out of range, exc_traceback is: <traceback object at 0x7fee3b00eb48>

traceback.print_tb(traceback[, limit[, file]])
  打印栈的跟踪信息. 如果省略limit, 将打印所有跟踪入口信息. file默认为std.err.


    print "*** print_tb:"
traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
  输出:


*** print_tb:
File "t.py", line 13, in <module>
mock()

traceback.print_exception(type, value, traceback[, limit[, file]])
  打印异常信息. (type, value, traceback)为sys.exc_info()返回的元组.和print_tb不同的是:

- 如果traceback不为空, 打印栈头信息(即最近被调用的信息).

- 在栈的信息后打印异常类型和常的参数.

- 如果是语法错误, 会打印对应的代码行数, 用”^”指明语法错误的位置.


    print "*** print_exception:"
traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
  输出:


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

traceback.print_exc(])
  print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file)的简写.


    print "*** print_exc:"
traceback.print_exc()
  输出:


*** print_exc:
Traceback (most recent call last):
File "t.py", line 13, in <module>
mock()
File "t.py", line 4, in mock
lumberjack()
File "t.py", line 7, in lumberjack
bright_side_of_death()
File "t.py", line 10, in bright_side_of_death
return tuple()
IndexError: tuple index out of range

traceback.format_exc()
  类似于print_exc(limit), 但是返回字符串而不是输出到file.


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


*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range

traceback.format_exception(type, value, tb[, limit])
  格式化栈信息和异常信息. 返回一个列表, 包括代码文件和代码行, 以及异常信息.


    print "*** format_exception:"
print repr(traceback.format_exception(exc_type, exc_value, exc_traceback))
  输出:


['Traceback (most recent call last):\n', 'File "t.py", line 13, in <module>\n    mock()\n', 'File "t.py", line 10, in mock\n    lumberjack()\n', 'File "t.py", line 4, in lumberjack\n    bright_side_of_death()\n', 'File "t.py", line 7, in bright_side_of_death\n    return tuple()\n', 'IndexError: tuple index out of range\n']

traceback.extract_tb(traceback[, limit])
  返回一个跟踪对象(traceback)的元组列表. 元组内容为(filename, line number, function name, text).


    print "*** extract_tb:"
print repr(traceback.extract_tb(exc_traceback))
  输出:


[('t.py', 13, '<module>', 'mock()'), ('t.py', 4, 'mock', 'lumberjack()'), ('t.py', 7, 'lumberjack', 'bright_side_of_death()'), ('t.py', 10, 'bright_side_of_death', 'return tuple()')]

traceback.extract_stack(])
  返回当前栈帧的原始跟踪(traceback)对象的信息, 格式和extract_tb一样, 元组内容为(filename, line number, function name, text).


    print "*** extract_stack:"
print traceback.extract_stack()
  输出:


*** extract_stack:
[('t.py', 47, '<module>', 'print traceback.extract_stack()')]

traceback.format_list(list)
  按照list对应的项, 返回一个元组列表, 形式为同extract_tb()或者extract_stack()返回的一样. 元组内容为(filename, line number, function name, text)

将extract_tb()或者extract_stack()返回的list进行格式化.


print traceback.format_list([('spam.py', 3, '<module>', 'spam.eggs()'), ('eggs.py', 42, 'eggs', 'return "bacon"')])
  输出:


['File "spam.py", line 3, in <module>\n    spam.eggs()\n', 'File "eggs.py", line 42, in eggs\n    return "bacon"\n']


traceback.format_tb(tb[, limit])
  format_list(extract_tb(tb, limit))的简写.
traceback.format_stack(])
  format_list(extract_stack(f, limit))的简写
traceback.tb_lineno(tb)
  返回traceback对象设置的行数
实例


import sys, traceback
def mock():
lumberjack()
def lumberjack():
bright_side_of_death()
def bright_side_of_death():
return tuple()
try:
mock()
except IndexError:
exc_type, exc_value, exc_traceback = sys.exc_info()
print "*** print sys.exc_info:"
print 'exc_type is: %s, exc_value is: %s, exc_traceback is: %s' % (exc_type, exc_value, exc_traceback)
print "-" *100
print "*** print_tb:"
traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
print "-" *100
print "*** print_exception:"
traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
print "-" *100
print "*** print_exc:"
traceback.print_exc()
print "-" *100
print "*** format_exc, first and last line:"
formatted_lines = traceback.format_exc().splitlines()
print formatted_lines
print formatted_lines[-1]
print "-" *100
print "*** format_exception:"
print repr(traceback.format_exception(exc_type, exc_value, exc_traceback))
print "-" *100
print "*** extract_tb:"
print repr(traceback.extract_tb(exc_traceback))
print "-" *100
print "*** extract_stack:"
print traceback.extract_stack()
print "-" *100
print "*** format_tb:"
print repr(traceback.format_tb(exc_traceback))
print "-" *100
print "*** tb_lineno:", exc_traceback.tb_lineno
print traceback.format_list([('spam.py', 3, '<module>', 'spam.eggs()'), ('eggs.py', 42, 'eggs', 'return "bacon"')])
  输出:


*** print sys.exc_info:
exc_type is: <type 'exceptions.IndexError'>, exc_value is: tuple index out of range, exc_traceback is: <traceback object at 0x7f7d659bab48>
----------------------------------------------------------------------------------------------------
*** print_tb:
File "t.py", line 13, in <module>
mock()
----------------------------------------------------------------------------------------------------
*** print_exception:
Traceback (most recent call last):
File "t.py", line 13, in <module>
mock()
File "t.py", line 4, in mock
lumberjack()
IndexError: tuple index out of range
----------------------------------------------------------------------------------------------------
*** print_exc:
Traceback (most recent call last):
File "t.py", line 13, in <module>
mock()
File "t.py", line 4, in mock
lumberjack()
File "t.py", line 7, in lumberjack
bright_side_of_death()
File "t.py", line 10, in bright_side_of_death
return tuple()
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 "t.py", line 13, in <module>\n    mock()\n', 'File "t.py", line 4, in mock\n    lumberjack()\n', 'File "t.py", line 7, in lumberjack\n    bright_side_of_death()\n', 'File "t.py", line 10, in bright_side_of_death\n    return tuple()\n', 'IndexError: tuple index out of range\n']
----------------------------------------------------------------------------------------------------
*** extract_tb:
[('t.py', 13, '<module>', 'mock()'), ('t.py', 4, 'mock', 'lumberjack()'), ('t.py', 7, 'lumberjack', 'bright_side_of_death()'), ('t.py', 10, 'bright_side_of_death', 'return tuple()')]
----------------------------------------------------------------------------------------------------
*** extract_stack:
[('t.py', 47, '<module>', 'print traceback.extract_stack()')]
----------------------------------------------------------------------------------------------------
*** format_tb:
['File "t.py", line 13, in <module>\n    mock()\n', 'File "t.py", line 4, in mock\n    lumberjack()\n', 'File "t.py", line 7, in lumberjack\n    bright_side_of_death()\n', 'File "t.py", line 10, in bright_side_of_death\n    return tuple()\n']
----------------------------------------------------------------------------------------------------
*** tb_lineno: 13
['File "spam.py", line 3, in <module>\n    spam.eggs()\n', 'File "eggs.py", line 42, in eggs\n    return "bacon"\n']


<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>         
版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: python中利用tracekback跟踪栈以及打印异常信息