调试构建

Python can be built with several macros to enable extra checks of the interpreter and extension modules. These checks tend to add a large amount of overhead to the runtime so they are not enabled by default.

A full list of the various types of debugging builds is in the file Misc/SpecialBuilds.txt in the Python source distribution. Builds are available that support tracing of reference counts, debugging the memory allocator, or low-level profiling of the main interpreter loop. Only the most frequently-used builds will be described in the remainder of this section.

Compiling the interpreter with the Py_DEBUG macro defined produces what is generally meant by “a debug build” of Python. Py_DEBUG is enabled in the Unix build by adding --with-pydebug to the ./configure command. It is also implied by the presence of the not-Python-specific _DEBUG macro. When Py_DEBUG is enabled in the Unix build, compiler optimization is disabled.

除了前面描述的引用计数调试之外,还执行以下额外检查:

  • 额外检查将添加到对象分配器。

  • 额外的检查将添加到解析器和编译器中。

  • Downcasts from wide types to narrow types are checked for loss of information.

  • 许多断言被添加到字典和集合实现中。另外,集合对象需要 test_c_api() 方法。

  • 输入参数的完整性检查被添加到框架创建中。

  • 使用已知的无效模式初始化整型的存储,以捕获对未初始化数字的引用。

  • 添加底层跟踪和额外的异常检查到虚拟机的运行时中。

  • Extra checks are added to the memory arena implementation.

  • 添加额外调试到线程模块。

这里可能没有提到的额外的检查。

Defining Py_TRACE_REFS enables reference tracing. When defined, a circular doubly linked list of active objects is maintained by adding two extra fields to every PyObject. Total allocations are tracked as well. Upon exit, all existing references are printed. (In interactive mode this happens after every statement run by the interpreter.) Implied by Py_DEBUG.

有关更多详细信息,请参阅Python源代码中的 Misc/SpecialBuilds.txt