Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Misc/SpecialBuilds.txt
12 views
1
This file describes some special Python build types enabled via compile-time
2
preprocessor directives.
3
4
IMPORTANT: if you want to build a debug-enabled Python, it is recommended that
5
you use ``./configure --with-pydebug``, rather than the options listed here.
6
7
However, if you wish to define some of these options individually, it is best
8
to define them in the EXTRA_CFLAGS make variable;
9
``make EXTRA_CFLAGS="-DPy_REF_DEBUG"``.
10
11
12
Py_REF_DEBUG
13
------------
14
15
Turn on aggregate reference counting. This arranges that extern _Py_RefTotal
16
hold a count of all references, the sum of ob_refcnt across all objects.
17
Passing ``-X showrefcount`` on the command line causes the interactive
18
interpreter to print the reference count total as well the number of memory
19
blocks allocated after each statement:
20
21
>>> 23
22
23
23
[8288 refs, 14332 blocks]
24
>>>
25
26
Note that if this count increases when you're not storing away new objects,
27
there's probably a leak. Remember, though, that in interactive mode the special
28
name "_" holds a reference to the last result displayed!
29
30
Py_REF_DEBUG also checks after every decref to verify that the refcount hasn't
31
gone negative, and causes an immediate fatal error if it has.
32
33
Py_DEBUG implies Py_REF_DEBUG.
34
35
Special gimmicks:
36
37
sys.gettotalrefcount()
38
Return current total of all refcounts.
39
40
41
Py_TRACE_REFS
42
-------------
43
44
Build option: ``./configure --with-trace-refs``.
45
46
Turn on heavy reference debugging. This is major surgery. Every PyObject grows
47
two more pointers, to maintain a doubly-linked list of all live heap-allocated
48
objects. Most built-in type objects are not in this list, as they're statically
49
allocated.
50
51
Note that because the fundamental PyObject layout changes, Python modules
52
compiled with Py_TRACE_REFS are incompatible with modules compiled without it.
53
54
Special gimmicks:
55
56
sys.getobjects(max[, type])
57
Return list of the (no more than) max most-recently allocated objects, most
58
recently allocated first in the list, least-recently allocated last in the
59
list. max=0 means no limit on list length. If an optional type object is
60
passed, the list is also restricted to objects of that type. The return
61
list itself, and some temp objects created just to call sys.getobjects(),
62
are excluded from the return list. Note that the list returned is just
63
another object, though, so may appear in the return list the next time you
64
call getobjects(); note that every object in the list is kept alive too,
65
simply by virtue of being in the list.
66
67
envvar PYTHONDUMPREFS
68
If this envvar exists, Py_FinalizeEx() arranges to print a list of all
69
still-live heap objects. This is printed twice, in different formats,
70
before and after Py_FinalizeEx has cleaned up everything it can clean up. The
71
first output block produces the repr() of each object so is more
72
informative; however, a lot of stuff destined to die is still alive then.
73
The second output block is much harder to work with (repr() can't be invoked
74
anymore -- the interpreter has been torn down too far), but doesn't list any
75
objects that will die. The tool script combinerefs.py can be run over this
76
to combine the info from both output blocks. The second output block, and
77
combinerefs.py, were new in Python 2.3b1.
78
79
80
Py_DEBUG
81
--------
82
83
This is what is generally meant by "a debug build" of Python.
84
85
Py_DEBUG implies LLTRACE and Py_REF_DEBUG. In addition, C assert()s are enabled
86
(via the C way: by not defining NDEBUG), and some routines do additional sanity
87
checks inside "#ifdef Py_DEBUG" blocks.
88
89
90
LLTRACE
91
-------
92
93
Compile in support for Low Level TRACE-ing of the main interpreter loop.
94
95
When this preprocessor symbol is defined, before PyEval_EvalFrame executes a
96
frame's code it checks the frame's global namespace for a variable
97
"__lltrace__". If such a variable is found, mounds of information about what
98
the interpreter is doing are sprayed to stdout, such as every opcode and opcode
99
argument and values pushed onto and popped off the value stack.
100
101
Not useful very often, but very useful when needed.
102
103
Py_DEBUG implies LLTRACE.
104
105