发布于 2015-08-30 07:57:26 | 129 次阅读 | 评论: 0 | 来源: 网络整理

问题

The interpreter violently crashes with a segmentation fault, bus error, access violation, or other fatal error. You would like to get a Python traceback that shows you where your program was running at the point of failure.


解决方案

The faulthandler module can be used to help you solve this problem. Include the following code in your program:

import faulthandler faulthandler.enable()

Alternatively, run Python with the -Xfaulthandler option such as this:

bash % python3 -Xfaulthandler program.py

Last, but not least, you can set the PYTHONFAULTHANDLER environment variable. With faulthandler enabled, fatal errors in C extensions will result in a Python trace‐ back being printed on failures. For example:

Fatal Python error: Segmentation fault

Current thread 0x00007fff71106cc0:
File “example.py”, line 6 in foo File “example.py”, line 10 in bar File “example.py”, line 14 in spam File “example.py”, line 19 in <module>

Segmentation fault

Although this won’t tell you where in the C code things went awry, at least it can tell you how it got there from Python.


讨论

The faulthandler will show you the stack traceback of the Python code executing at the time of failure. At the very least, this will show you the top-level extension function that was invoked. With the aid of pdb or other Python debugger, you can investigate the flow of the Python code leading to the error. faulthandler will not tell you anything about the failure from C. For that, you will need to use a traditional C debugger, such as gdb. However, the information from the faulthandler traceback may give you a better idea of where to direct your attention. It should be noted that certain kinds of errors in C may not be easily recoverable. For example, if a C extension trashes the stack or program heap, it may render faulthan dler inoperable and you’ll simply get no output at all (other than a crash). Obviously, your mileage may vary.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务