内容表

  • Python 开发模式
    • Python 开发模式的影响
    • ResourceWarning 范例
    • 坏的文件描述符错误范例

上一话题

pydoc — 文档编制生成器和在线帮助系统

下一话题
就业培训     下载中心     Wiki     联络
登录   注册

Log
  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 开发工具
  9. Python 开发模式

Python 开发模式 ¶

Added in version 3.7.

Python 开发模式引入默认情况下,启用非常昂贵的额外运行时校验。它不应该比默认更冗余,若代码正确;才发出新警告,当检测到问题时。

可以启用它使用 -X dev 命令行选项或通过设置 PYTHONDEVMODE 环境变量到 1 .

另请参阅 Python debug build .

Python 开发模式的影响 ¶

启用 Python 开发模式类似于以下命令,但有额外效果如下所述:

PYTHONMALLOC=debug PYTHONASYNCIODEBUG=1 python -W default -X faulthandler
										

Python 开发模式的影响:

  • 添加 default 警告过滤 。展示下列警告:

    • DeprecationWarning

    • ImportWarning

    • PendingDeprecationWarning

    • ResourceWarning

    通常,以上警告被过滤由默认 警告过滤 .

    It behaves as if the -W default command line option is used.

    使用 -W error command line option or set the PYTHONWARNINGS 环境变量到 error to treat warnings as errors.

  • Install debug hooks on memory allocators to check for:

    • 缓冲下溢

    • 缓冲溢出

    • Memory allocator API violation

    • Unsafe usage of the GIL

    见 PyMem_SetupDebugHooks() C 函数。

    It behaves as if the PYTHONMALLOC environment variable is set to debug .

    To enable the Python Development Mode without installing debug hooks on memory allocators, set the PYTHONMALLOC 环境变量到 default .

  • 调用 faulthandler.enable() at Python startup to install handlers for the SIGSEGV , SIGFPE , SIGABRT , SIGBUS and SIGILL signals to dump the Python traceback on a crash.

    It behaves as if the -X faulthandler command line option is used or if the PYTHONFAULTHANDLER environment variable is set to 1 .

  • 启用 asyncio debug mode 。例如, asyncio checks for coroutines that were not awaited and logs them.

    It behaves as if the PYTHONASYNCIODEBUG environment variable is set to 1 .

  • 检查 encoding and errors arguments for string encoding and decoding operations. Examples: open() , str.encode() and bytes.decode() .

    By default, for best performance, the errors argument is only checked at the first encoding/decoding error and the encoding argument is sometimes ignored for empty strings.

  • The io.IOBase destructor logs close() 异常。

  • 设置 dev_mode attribute of sys.flags to True .

The Python Development Mode does not enable the tracemalloc module by default, because the overhead cost (to performance and memory) would be too large. Enabling the tracemalloc module provides additional information on the origin of some errors. For example, ResourceWarning logs the traceback where the resource was allocated, and a buffer overflow error logs the traceback where the memory block was allocated.

The Python Development Mode does not prevent the -O command line option from removing assert statements nor from setting __debug__ to False .

The Python Development Mode can only be enabled at the Python startup. Its value can be read from sys.flags.dev_mode .

3.8 版改变: The io.IOBase destructor now logs close() 异常。

3.9 版改变: The encoding and errors arguments are now checked for string encoding and decoding operations.

ResourceWarning 范例 ¶

Example of a script counting the number of lines of the text file specified in the command line:

import sys
def main():
    fp = open(sys.argv[1])
    nlines = len(fp.readlines())
    print(nlines)
    # The file is closed implicitly
if __name__ == "__main__":
    main()
									

The script does not close the file explicitly. By default, Python does not emit any warning. Example using README.txt, which has 269 lines:

$ python script.py README.txt
269
									

Enabling the Python Development Mode displays a ResourceWarning 警告:

$ python -X dev script.py README.txt
269
script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='README.rst' mode='r' encoding='UTF-8'>
  main()
ResourceWarning: Enable tracemalloc to get the object allocation traceback
									

In addition, enabling tracemalloc shows the line where the file was opened:

$ python -X dev -X tracemalloc=5 script.py README.rst
269
script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='README.rst' mode='r' encoding='UTF-8'>
  main()
Object allocated at (most recent call last):
  File "script.py", lineno 10
    main()
  File "script.py", lineno 4
    fp = open(sys.argv[1])
									

The fix is to close explicitly the file. Example using a context manager:

def main():
    # Close the file explicitly when exiting the with block
    with open(sys.argv[1]) as fp:
        nlines = len(fp.readlines())
    print(nlines)
									

Not closing a resource explicitly can leave a resource open for way longer than expected; it can cause severe issues upon exiting Python. It is bad in CPython, but it is even worse in PyPy. Closing resources explicitly makes an application more deterministic and more reliable.

坏的文件描述符错误范例 ¶

Script displaying the first line of itself:

import os
def main():
    fp = open(__file__)
    firstline = fp.readline()
    print(firstline.rstrip())
    os.close(fp.fileno())
    # The file is closed implicitly
main()
								

By default, Python does not emit any warning:

$ python script.py
import os
								

The Python Development Mode shows a ResourceWarning and logs a “Bad file descriptor” error when finalizing the file object:

$ python -X dev script.py
import os
script.py:10: ResourceWarning: unclosed file <_io.TextIOWrapper name='script.py' mode='r' encoding='UTF-8'>
  main()
ResourceWarning: Enable tracemalloc to get the object allocation traceback
Exception ignored in: <_io.TextIOWrapper name='script.py' mode='r' encoding='UTF-8'>
Traceback (most recent call last):
  File "script.py", line 10, in <module>
    main()
OSError: [Errno 9] Bad file descriptor
								

os.close(fp.fileno()) closes the file descriptor. When the file object finalizer tries to close the file descriptor again, it fails with the Bad file descriptor error. A file descriptor must be closed only once. In the worst case scenario, closing it twice can lead to a crash (see bpo-18748 了解范例)。

The fix is to remove the os.close(fp.fileno()) line, or open the file with closefd=False .

内容表

  • Python 开发模式
    • Python 开发模式的影响
    • ResourceWarning 范例
    • 坏的文件描述符错误范例

上一话题

pydoc — 文档编制生成器和在线帮助系统

下一话题

doctest — 测试交互 Python 范例

本页

  • 报告 Bug
  • 展示源

快速搜索

键入搜索术语或模块、类、函数名称。

  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 开发工具
  9. Python 开发模式

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1