Skip to content

Commit abfd638

Browse files
authored
bpo-43774: Enhance debug build documentation (GH-25712)
* Add also references to --with-trace-refs option. * Move _ob_next and _ob_prev at the end, since they don't exist by default and are related to debug.
1 parent 8ec2f0d commit abfd638

File tree

4 files changed

+53
-69
lines changed

4 files changed

+53
-69
lines changed

‎Doc/c-api/intro.rst‎

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -749,34 +749,12 @@ It is also implied by the presence of the
749749
not-Python-specific :c:macro:`_DEBUG` macro. When :c:macro:`Py_DEBUG` is enabled
750750
in the Unix build, compiler optimization is disabled.
751751

752-
In addition to the reference count debugging described below, the following
753-
extra checks are performed:
752+
In addition to the reference count debugging described below, extra checks are
753+
performed, see :ref:`Python Debug Build <debug-build>`.
754754

755-
* Extra checks are added to the object allocator.
756-
757-
* Extra checks are added to the parser and compiler.
758-
759-
* Downcasts from wide types to narrow types are checked for loss of information.
760-
761-
* A number of assertions are added to the dictionary and set implementations.
762-
In addition, the set object acquires a :meth:`test_c_api` method.
763-
764-
* Sanity checks of the input arguments are added to frame creation.
765-
766-
* The storage for ints is initialized with a known invalid pattern to catch
767-
reference to uninitialized digits.
768-
769-
* Low-level tracing and extra exception checking are added to the runtime
770-
virtual machine.
771-
772-
* Extra checks are added to the memory arena implementation.
773-
774-
* Extra debugging is added to the thread module.
775-
776-
There may be additional checks not mentioned here.
777-
778-
Defining :c:macro:`Py_TRACE_REFS` enables reference tracing. When defined, a
779-
circular doubly linked list of active objects is maintained by adding two extra
755+
Defining :c:macro:`Py_TRACE_REFS` enables reference tracing
756+
(see the :option:`configure --with-trace-refs option <--with-trace-refs>`).
757+
When defined, a circular doubly linked list of active objects is maintained by adding two extra
780758
fields to every :c:type:`PyObject`. Total allocations are tracked as well. Upon
781759
exit, all existing references are printed. (In interactive mode this happens
782760
after every statement run by the interpreter.)

‎Doc/c-api/typeobj.rst‎

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -482,27 +482,6 @@ metatype) initializes :c:member:`~PyTypeObject.tp_itemsize`, which means that it
482482
type objects) *must* have the :attr:`ob_size` field.
483483

484484

485-
.. c:member:: PyObject* PyObject._ob_next
486-
PyObject* PyObject._ob_prev
487-
488-
These fields are only present when the macro ``Py_TRACE_REFS`` is defined.
489-
490-
Their initialization to ``NULL`` is taken care of by the
491-
``PyObject_HEAD_INIT`` macro. For :ref:`statically allocated objects
492-
<static-types>`, these fields always remain ``NULL``. For :ref:`dynamically
493-
allocated objects <heap-types>`, these two fields are used to link the
494-
object into a doubly-linked list of *all* live objects on the heap.
495-
496-
This could be used for various debugging purposes; currently the only uses
497-
are the :func:`sys.getobjects` function and to print the objects that are
498-
still alive at the end of a run when the environment variable
499-
:envvar:`PYTHONDUMPREFS` is set.
500-
501-
**Inheritance:**
502-
503-
These fields are not inherited by subtypes.
504-
505-
506485
.. c:member:: Py_ssize_t PyObject.ob_refcnt
507486
508487
This is the type object's reference count, initialized to ``1`` by the
@@ -540,6 +519,28 @@ type objects) *must* have the :attr:`ob_size` field.
540519
This field is inherited by subtypes.
541520

542521

522+
.. c:member:: PyObject* PyObject._ob_next
523+
PyObject* PyObject._ob_prev
524+
525+
These fields are only present when the macro ``Py_TRACE_REFS`` is defined
526+
(see the :option:`configure --with-trace-refs option <--with-trace-refs>`).
527+
528+
Their initialization to ``NULL`` is taken care of by the
529+
``PyObject_HEAD_INIT`` macro. For :ref:`statically allocated objects
530+
<static-types>`, these fields always remain ``NULL``. For :ref:`dynamically
531+
allocated objects <heap-types>`, these two fields are used to link the
532+
object into a doubly-linked list of *all* live objects on the heap.
533+
534+
This could be used for various debugging purposes; currently the only uses
535+
are the :func:`sys.getobjects` function and to print the objects that are
536+
still alive at the end of a run when the environment variable
537+
:envvar:`PYTHONDUMPREFS` is set.
538+
539+
**Inheritance:**
540+
541+
These fields are not inherited by subtypes.
542+
543+
543544
PyVarObject Slots
544545
-----------------
545546

‎Doc/using/configure.rst‎

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -208,43 +208,48 @@ recommended for best performance.
208208

209209
.. _debug-build:
210210

211-
Debug build
212-
-----------
211+
Python Debug Build
212+
------------------
213213

214214
A debug build is Python built with the :option:`--with-pydebug` configure
215215
option.
216216

217217
Effects of a debug build:
218218

219-
* Define ``Py_DEBUG`` and ``Py_REF_DEBUG`` macros.
219+
* Display all warnings by default: the list of default warning filters is empty
220+
in the :mod:`warnings` module.
220221
* Add ``d`` to :data:`sys.abiflags`.
221222
* Add :func:`sys.gettotalrefcount` function.
222223
* Add :option:`-X showrefcount <-X>` command line option.
223224
* Add :envvar:`PYTHONTHREADDEBUG` environment variable.
224225
* Add support for the ``__ltrace__`` variable: enable low-level tracing in the
225226
bytecode evaluation loop if the variable is defined.
226-
* The list of default warning filters is empty in the :mod:`warnings` module.
227-
* Install debug hooks on memory allocators to detect buffer overflow and other
228-
memory errors: see :c:func:`PyMem_SetupDebugHooks`.
229-
* Build Python with assertions (don't set ``NDEBUG`` macro):
230-
``assert(...);`` and ``_PyObject_ASSERT(...);``.
231-
See also the :option:`--with-assertions` configure option.
232-
* Unicode and int objects are created with their memory filled with a pattern
233-
to help detecting uninitialized bytes.
234-
* Many functions ensure that are not called with an exception raised, since
235-
they can clear or replace the current exception.
236-
* The garbage collector (:func:`gc.collect` function) runs some basic checks on
237-
objects consistency.
238-
* More generally, add runtime checks, code surroundeded by ``#ifdef Py_DEBUG``
239-
and ``#endif``.
227+
* Install :ref:`debug hooks on memory allocators <default-memory-allocators>`
228+
to detect buffer overflow and other memory errors.
229+
* Define ``Py_DEBUG`` and ``Py_REF_DEBUG`` macros.
230+
* Add runtime checks: code surroundeded by ``#ifdef Py_DEBUG`` and ``#endif``.
231+
Enable ``assert(...)`` and ``_PyObject_ASSERT(...)`` assertions: don't set
232+
the ``NDEBUG`` macro (see also the :option:`--with-assertions` configure
233+
option). Main runtime checks:
234+
235+
* Add sanity checks on the function arguments.
236+
* Unicode and int objects are created with their memory filled with a pattern
237+
to detect usage of uninitialized objects.
238+
* Ensure that functions which can clear or replace the current exception are
239+
not called with an exception raised.
240+
* The garbage collector (:func:`gc.collect` function) runs some basic checks
241+
on objects consistency.
242+
* The :c:macro:`Py_SAFE_DOWNCAST()` macro checks for integer underflow and
243+
overflow when downcasting from wide types to narrow types.
240244

241245
See also the :ref:`Python Development Mode <devmode>` and the
242246
:option:`--with-trace-refs` configure option.
243247

244248
.. versionchanged:: 3.8
245249
Release builds and debug builds are now ABI compatible: defining the
246-
``Py_DEBUG`` macro no longer implies the ``Py_TRACE_REFS`` macro, which
247-
introduces the only ABI incompatibility.
250+
``Py_DEBUG`` macro no longer implies the ``Py_TRACE_REFS`` macro (see the
251+
:option:`--with-trace-refs` option), which introduces the only ABI
252+
incompatibility.
248253

249254

250255
Debug options

‎Doc/whatsnew/3.8.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ Release builds and :ref:`debug builds <debug-build>` are now ABI compatible: def
215215
``Py_DEBUG`` macro no longer implies the ``Py_TRACE_REFS`` macro, which
216216
introduces the only ABI incompatibility. The ``Py_TRACE_REFS`` macro, which
217217
adds the :func:`sys.getobjects` function and the :envvar:`PYTHONDUMPREFS`
218-
environment variable, can be set using the new ``./configure --with-trace-refs``
219-
build option.
218+
environment variable, can be set using the new :option:`./configure
219+
--with-trace-refs <--with-trace-refs>` build option.
220220
(Contributed by Victor Stinner in :issue:`36465`.)
221221

222222
On Unix, C extensions are no longer linked to libpython except on Android

0 commit comments

Comments
 (0)