1
1
==============================
2
- 15.21 诊断分析代码错误
2
+ 15.21 诊断分段错误
3
3
==============================
4
4
5
5
----------
6
6
问题
7
7
----------
8
- The interpreter violently crashes with a segmentation fault, bus error, access violation,
9
- or other fatal error. You would like to get a Python traceback that shows you where your
10
- program was running at the point of failure.
8
+ 解释器因为某个分段错误、总线错误、访问越界或其他致命错误而突然间奔溃。
9
+ 你想获得Python堆栈信息,从而找出在发生错误的时候你的程序运行点。
11
10
12
11
|
13
12
14
13
----------
15
14
解决方案
16
15
----------
17
- The faulthandler module can be used to help you solve this problem. Include the
18
- following code in your program:
16
+ `` faulthandler `` 模块能被用来帮你解决这个问题。
17
+ 在你的程序中引入下列代码:
19
18
20
- import faulthandler
21
- faulthandler.enable()
19
+ .. code-block :: python
22
20
23
- Alternatively, run Python with the -Xfaulthandler option such as this:
21
+ import faulthandler
22
+ faulthandler.enable()
24
23
25
- bash % python3 -Xfaulthandler program.py
24
+ 另外还可以像下面这样使用 `` -Xfaulthandler `` 来运行Python:
26
25
27
- Last, but not least, you can set the PYTHONFAULTHANDLER environment variable.
28
- With faulthandler enabled, fatal errors in C extensions will result in a Python trace‐
29
- back being printed on failures. For example:
26
+ ::
27
+
28
+ bash % python3 -Xfaulthandler program.py
29
+
30
+ 最后,你可以设置 ``PYTHONFAULTHANDLER `` 环境变量。
31
+ 开启faulthandler后,在C扩展中的致命错误会导致一个Python错误堆栈被打印出来。例如:
32
+
33
+ ::
30
34
31
35
Fatal Python error: Segmentation fault
32
36
@@ -37,23 +41,21 @@ back being printed on failures. For example:
37
41
File "example.py", line 19 in <module>
38
42
Segmentation fault
39
43
40
- Although this won’t tell you where in the C code things went awry, at least it can tell you
41
- how it got there from Python.
44
+ 尽管这个并不能告诉你C代码中哪里出错了,但是至少能告诉你Python里面哪里有错。
42
45
43
46
|
44
47
45
48
----------
46
49
讨论
47
50
----------
48
- The faulthandler will show you the stack traceback of the Python code executing at
49
- the time of failure. At the very least, this will show you the top-level extension function
50
- that was invoked. With the aid of pdb or other Python debugger, you can investigate the
51
- flow of the Python code leading to the error.
52
- faulthandler will not tell you anything about the failure from C. For that, you will
53
- need to use a traditional C debugger, such as gdb. However, the information from the
54
- faulthandler traceback may give you a better idea of where to direct your attention.
55
- It should be noted that certain kinds of errors in C may not be easily recoverable. For
56
- example, if a C extension trashes the stack or program heap, it may render faulthan
57
- dler inoperable and you’ll simply get no output at all (other than a crash). Obviously,
58
- your mileage may vary.
51
+ faulthandler会在Python代码执行出错的时候向你展示跟踪信息。
52
+ 至少,它会告诉你出错时被调用的最顶级扩展函数是哪个。
53
+ 在pdb和其他Python调试器的帮助下,你就能追根溯源找到错误所在的位置了。
54
+
55
+ faulthandler不会告诉你任何C语言中的错误信息。
56
+ 因此,你需要使用传统的C调试器,比如gdb。
57
+ 不过,在faulthandler追踪信息可以让你去判断从哪里着手。
58
+ 还要注意的是在C中某些类型的错误可能不太容易恢复。
59
+ 例如,如果一个C扩展丢弃了程序堆栈信息,它会让faulthandler不可用,
60
+ 那么你也得不到任何输出(除了程序奔溃外)。
59
61
0 commit comments