Skip to content

Commit 8ec6175

Browse files
committed
15.21小节完成,15章完成
1 parent af083cc commit 8ec6175

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11
==============================
2-
15.21 诊断分析代码错误
2+
15.21 诊断分段错误
33
==============================
44

55
----------
66
问题
77
----------
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堆栈信息,从而找出在发生错误的时候你的程序运行点。
1110

1211
|
1312
1413
----------
1514
解决方案
1615
----------
17-
The faulthandler module can be used to help you solve this problem. Include the
18-
following code in your program:
16+
``faulthandler`` 模块能被用来帮你解决这个问题。
17+
在你的程序中引入下列代码:
1918

20-
import faulthandler
21-
faulthandler.enable()
19+
.. code-block:: python
2220
23-
Alternatively, run Python with the -Xfaulthandler option such as this:
21+
import faulthandler
22+
faulthandler.enable()
2423
25-
bash % python3 -Xfaulthandler program.py
24+
另外还可以像下面这样使用 ``-Xfaulthandler`` 来运行Python:
2625

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+
::
3034

3135
Fatal Python error: Segmentation fault
3236

@@ -37,23 +41,21 @@ back being printed on failures. For example:
3741
File "example.py", line 19 in <module>
3842
Segmentation fault
3943

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里面哪里有错。
4245

4346
|
4447
4548
----------
4649
讨论
4750
----------
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+
那么你也得不到任何输出(除了程序奔溃外)。
5961

0 commit comments

Comments
 (0)