开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

source-code-analysis/python3.8.1

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
python3.8.1
/
Doc
/
library
/
faulthandler.rst
python3.8.1
/
Doc
/
library
/
faulthandler.rst
faulthandler.rst 5.99 KB
一键复制 编辑 原始数据 按行查看 历史
zhangweibo 提交于 2021年11月16日 09:46 +08:00 . git init

:mod:`faulthandler` --- Dump the Python traceback

.. module:: faulthandler
 :synopsis: Dump the Python traceback.

.. versionadded:: 3.3


This module contains functions to dump Python tracebacks explicitly, on a fault, after a timeout, or on a user signal. Call :func:`faulthandler.enable` to install fault handlers for the :const:`SIGSEGV`, :const:`SIGFPE`, :const:`SIGABRT`, :const:`SIGBUS`, and :const:`SIGILL` signals. You can also enable them at startup by setting the :envvar:`PYTHONFAULTHANDLER` environment variable or by using the :option:`-X` faulthandler command line option.

The fault handler is compatible with system fault handlers like Apport or the Windows fault handler. The module uses an alternative stack for signal handlers if the :c:func:`sigaltstack` function is available. This allows it to dump the traceback even on a stack overflow.

The fault handler is called on catastrophic cases and therefore can only use signal-safe functions (e.g. it cannot allocate memory on the heap). Because of this limitation traceback dumping is minimal compared to normal Python tracebacks:

  • Only ASCII is supported. The backslashreplace error handler is used on encoding.
  • Each string is limited to 500 characters.
  • Only the filename, the function name and the line number are displayed. (no source code)
  • It is limited to 100 frames and 100 threads.
  • The order is reversed: the most recent call is shown first.

By default, the Python traceback is written to :data:`sys.stderr`. To see tracebacks, applications must be run in the terminal. A log file can alternatively be passed to :func:`faulthandler.enable`.

The module is implemented in C, so tracebacks can be dumped on a crash or when Python is deadlocked.

Dumping the traceback

.. function:: dump_traceback(file=sys.stderr, all_threads=True)

 Dump the tracebacks of all threads into *file*. If *all_threads* is
 ``False``, dump only the current thread.

 .. versionchanged:: 3.5
 Added support for passing file descriptor to this function.


Fault handler state

.. function:: enable(file=sys.stderr, all_threads=True)

 Enable the fault handler: install handlers for the :const:`SIGSEGV`,
 :const:`SIGFPE`, :const:`SIGABRT`, :const:`SIGBUS` and :const:`SIGILL`
 signals to dump the Python traceback. If *all_threads* is ``True``,
 produce tracebacks for every running thread. Otherwise, dump only the current
 thread.

 The *file* must be kept open until the fault handler is disabled: see
 :ref:`issue with file descriptors <faulthandler-fd>`.

 .. versionchanged:: 3.5
 Added support for passing file descriptor to this function.

 .. versionchanged:: 3.6
 On Windows, a handler for Windows exception is also installed.

.. function:: disable()

 Disable the fault handler: uninstall the signal handlers installed by
 :func:`enable`.

.. function:: is_enabled()

 Check if the fault handler is enabled.


Dumping the tracebacks after a timeout

.. function:: dump_traceback_later(timeout, repeat=False, file=sys.stderr, exit=False)

 Dump the tracebacks of all threads, after a timeout of *timeout* seconds, or
 every *timeout* seconds if *repeat* is ``True``. If *exit* is ``True``, call
 :c:func:`_exit` with status=1 after dumping the tracebacks. (Note
 :c:func:`_exit` exits the process immediately, which means it doesn't do any
 cleanup like flushing file buffers.) If the function is called twice, the new
 call replaces previous parameters and resets the timeout. The timer has a
 sub-second resolution.

 The *file* must be kept open until the traceback is dumped or
 :func:`cancel_dump_traceback_later` is called: see :ref:`issue with file
 descriptors <faulthandler-fd>`.

 This function is implemented using a watchdog thread.

 .. versionchanged:: 3.7
 This function is now always available.

 .. versionchanged:: 3.5
 Added support for passing file descriptor to this function.

.. function:: cancel_dump_traceback_later()

 Cancel the last call to :func:`dump_traceback_later`.


Dumping the traceback on a user signal

.. function:: register(signum, file=sys.stderr, all_threads=True, chain=False)

 Register a user signal: install a handler for the *signum* signal to dump
 the traceback of all threads, or of the current thread if *all_threads* is
 ``False``, into *file*. Call the previous handler if chain is ``True``.

 The *file* must be kept open until the signal is unregistered by
 :func:`unregister`: see :ref:`issue with file descriptors <faulthandler-fd>`.

 Not available on Windows.

 .. versionchanged:: 3.5
 Added support for passing file descriptor to this function.

.. function:: unregister(signum)

 Unregister a user signal: uninstall the handler of the *signum* signal
 installed by :func:`register`. Return ``True`` if the signal was registered,
 ``False`` otherwise.

 Not available on Windows.


Issue with file descriptors

:func:`enable`, :func:`dump_traceback_later` and :func:`register` keep the file descriptor of their file argument. If the file is closed and its file descriptor is reused by a new file, or if :func:`os.dup2` is used to replace the file descriptor, the traceback will be written into a different file. Call these functions again each time that the file is replaced.

Example

Example of a segmentation fault on Linux with and without enabling the fault handler:

$ python3 -c "import ctypes; ctypes.string_at(0)"
Segmentation fault

$ python3 -q -X faulthandler
>>> import ctypes
>>> ctypes.string_at(0)
Fatal Python error: Segmentation fault

Current thread 0x00007fb899f39700 (most recent call first):
 File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at
 File "<stdin>", line 1 in <module>
Segmentation fault
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python_sourcecode/python3.8.1.git
git@gitee.com:python_sourcecode/python3.8.1.git
python_sourcecode
python3.8.1
python3.8.1
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /