首页 > 编程 > Python > 正文

Python traceback模块

2020-01-04 17:37:18
字体:
来源:转载
供稿:网友
traceback模块被用来跟踪异常返回信息. 如下例所示:
  1. import traceback
  2. try:
  3.     raise SyntaxError, "traceback test"
  4. except:
  5.     traceback.print_exc()
将会在控制台输出类似结果:
  1. Traceback (most recent call last):
  2.  
  3. File "H:/PythonWorkSpace/Test/src/TracebackTest.py", line 3, in <module>
  4.  
  5. raise SyntaxError, "traceback test"
  6.  
  7. SyntaxError: traceback test

类似在你没有捕获异常时候, 解释器所返回的结果.
你也可以传入一个文件, 把返回信息写到文件中去, 如下:
  1. import traceback
  2. import StringIO
  3. try:
  4.     raise SyntaxError, "traceback test"
  5. except:
  6.     fp = StringIO.StringIO() #创建内存文件对象
  7.     traceback.print_exc(file=fp)
  8.     message = fp.getvalue()
  9.     print message
这样在控制台输出的结果和上面例子一样
traceback模块还提供了extract_tb函数来格式化跟踪返回信息, 得到包含错误信息的列表, 如下:
  1. import traceback
  2. import sys
  3.  
  4. def tracebacktest():
  5.     raise SyntaxError, "traceback test"
  6. try:
  7.     tracebacktest()
  8. except:
  9.     info = sys.exc_info()
  10.     for file, lineno, function, text in traceback.extract_tb(info[2]):
  11.         print file, "line:", lineno, "in", function
  12.         print text
  13.     print "** %s: %s" % info[:2]
控制台输出结果如下:
  1. H:/PythonWorkSpace/Test/src/TracebackTest.py line: 7 in <module>
  2.  
  3. tracebacktest()
  4.  
  5. H:/PythonWorkSpace/Test/src/TracebackTest.py line: 5 in tracebacktest
  6.  
  7. raise SyntaxError, "traceback test"
  8.  
  9. ** <type 'exceptions.SyntaxError'>: traceback test

Example 2-18 展示了 traceback 模块允许你在程序里打印异常的跟踪返回 (Traceback)信息, 类似未捕获异常时解释器所做的. 如 Example 2-18 所示. 2.11.0.1. Example 2-18. 使用 traceback 模块打印跟踪返回信息 File: traceback-example-1.py # note! import
Example 2-18 展示了 traceback 模块允许你在程序里打印异常的跟踪返回 (Traceback)信息, 类似未捕获异常时解释器所做的. 如 Example 2-18 所示.

2.11.0.1. Example 2-18. 使用 traceback 模块打印跟踪返回信息

  1. File: traceback-example-1.py
  2.  
  3. # importing the traceback module messes up the
  4. # exception state, so you better do that here and not
  5. # in the exception handler
  6. # 导入 traceback 会清理掉异常状态, 所以
  7. # 最好别在异常处理代码中导入该模块
  8. import traceback
  9.  
  10. try:
  11.     raise SyntaxError, "example"
  12. except:
  13.     traceback.print_exc()
  1. Traceback (innermost last):
  2.  
  3. File "traceback-example-1.py", line 7, in ?
  4.  
  5. SyntaxError: example

Example 2-19 使用 StringIO 模块将跟踪返回信息放在字符串中.

2.11.0.2. Example 2-19. 使用 traceback 模块将跟踪返回信息复制到字符串
File: traceback-example-2.py

  1. import traceback
  2. import StringIO
  3.  
  4. try:
  5.     raise IOError, "an i/o error occurred"
  6. except:
  7.     fp = StringIO.StringIO()
  8.     traceback.print_exc(file=fp)
  9.     message = fp.getvalue()
  10.  
  11.     print "failure! the error was:", repr(message)
  1. failure! the error was: 'Traceback (innermost last):/012 File
  2.  
  3. "traceback-example-2.py", line 5, in ?/012IOError: an i/o error
  4.  
  5. occurred/012'

你可以使用 extract_tb 函数格式化跟踪返回信息, 得到包含错误信息的列表, 如 Example 2-20 所示.

2.11.0.3. Example 2-20. 使用 traceback Module 模块编码 Traceback 对象
File: traceback-example-3.py

  1. import traceback
  2. import sys
  3.  
  4. def function():
  5.     raise IOError, "an i/o error occurred"
  6.  
  7. try:
  8.     function()
  9. except:
  10.     info = sys.exc_info()
  11.     for file, lineno, function, text in traceback.extract_tb(info[2]):
  12.         print file, "line", lineno, "in", function
  13.         print "=>", repr(text)
  14.     print "** %s: %s" % info[:2]
  1. traceback-example-3.py line 8 in ?
  2.  
  3. => 'function()'
  4.  
  5. traceback-example-3.py line 5 in function
  6.  
  7. => 'raise IOError, "an i/o error occurred"'
  8.  
  9. ** exceptions.IOError: an i/o error occurred

文章来自 http://docs.python.org/dev/library/traceback.html

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表