ยปCore Development>Code coverage>Tools/scripts/combinerefs.py

Python code coverage for Tools/scripts/combinerefs.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/a"""
4n/acombinerefs path
5n/a
6n/aA helper for analyzing PYTHONDUMPREFS output.
7n/a
8n/aWhen the PYTHONDUMPREFS envar is set in a debug build, at Python shutdown
9n/atime Py_FinalizeEx() prints the list of all live objects twice: first it
10n/aprints the repr() of each object while the interpreter is still fully intact.
11n/aAfter cleaning up everything it can, it prints all remaining live objects
12n/aagain, but the second time just prints their addresses, refcounts, and type
13n/anames (because the interpreter has been torn down, calling repr methods at
14n/athis point can get into infinite loops or blow up).
15n/a
16n/aSave all this output into a file, then run this script passing the path to
17n/athat file. The script finds both output chunks, combines them, then prints
18n/aa line of output for each object still alive at the end:
19n/a
20n/a address refcnt typename repr
21n/a
22n/aaddress is the address of the object, in whatever format the platform C
23n/aproduces for a %p format code.
24n/a
25n/arefcnt is of the form
26n/a
27n/a "[" ref "]"
28n/a
29n/awhen the object's refcount is the same in both PYTHONDUMPREFS output blocks,
30n/aor
31n/a
32n/a "[" ref_before "->" ref_after "]"
33n/a
34n/aif the refcount changed.
35n/a
36n/atypename is object->ob_type->tp_name, extracted from the second PYTHONDUMPREFS
37n/aoutput block.
38n/a
39n/arepr is repr(object), extracted from the first PYTHONDUMPREFS output block.
40n/aCAUTION: If object is a container type, it may not actually contain all the
41n/aobjects shown in the repr: the repr was captured from the first output block,
42n/aand some of the containees may have been released since then. For example,
43n/ait's common for the line showing the dict of interned strings to display
44n/astrings that no longer exist at the end of Py_FinalizeEx; this can be recognized
45n/a(albeit painfully) because such containees don't have a line of their own.
46n/a
47n/aThe objects are listed in allocation order, with most-recently allocated
48n/aprinted first, and the first object allocated printed last.
49n/a
50n/a
51n/aSimple examples:
52n/a
53n/a 00857060 [14] str '__len__'
54n/a
55n/aThe str object '__len__' is alive at shutdown time, and both PYTHONDUMPREFS
56n/aoutput blocks said there were 14 references to it. This is probably due to
57n/aC modules that intern the string "__len__" and keep a reference to it in a
58n/afile static.
59n/a
60n/a 00857038 [46->5] tuple ()
61n/a
62n/a46-5 = 41 references to the empty tuple were removed by the cleanup actions
63n/abetween the times PYTHONDUMPREFS produced output.
64n/a
65n/a 00858028 [1025->1456] str '<dummy key>'
66n/a
67n/aThe string '<dummy key>', which is used in dictobject.c to overwrite a real
68n/akey that gets deleted, grew several hundred references during cleanup. It
69n/asuggests that stuff did get removed from dicts by cleanup, but that the dicts
70n/athemselves are staying alive for some reason. """
71n/a
72n/aimport re
73n/aimport sys
74n/a
75n/a# Generate lines from fileiter. If whilematch is true, continue reading
76n/a# while the regexp object pat matches line. If whilematch is false, lines
77n/a# are read so long as pat doesn't match them. In any case, the first line
78n/a# that doesn't match pat (when whilematch is true), or that does match pat
79n/a# (when whilematch is false), is lost, and fileiter will resume at the line
80n/a# following it.
81n/adef read(fileiter, pat, whilematch):
82n/a for line in fileiter:
83n/a if bool(pat.match(line)) == whilematch:
84n/a yield line
85n/a else:
86n/a break
87n/a
88n/adef combine(fname):
89n/a f = open(fname)
90n/a
91n/a fi = iter(f)
92n/a
93n/a for line in read(fi, re.compile(r'^Remaining objects:$'), False):
94n/a pass
95n/a
96n/a crack = re.compile(r'([a-zA-Z\d]+) \[(\d+)\] (.*)')
97n/a addr2rc = {}
98n/a addr2guts = {}
99n/a before = 0
100n/a for line in read(fi, re.compile(r'^Remaining object addresses:$'), False):
101n/a m = crack.match(line)
102n/a if m:
103n/a addr, addr2rc[addr], addr2guts[addr] = m.groups()
104n/a before += 1
105n/a else:
106n/a print('??? skipped:', line)
107n/a
108n/a after = 0
109n/a for line in read(fi, crack, True):
110n/a after += 1
111n/a m = crack.match(line)
112n/a assert m
113n/a addr, rc, guts = m.groups() # guts is type name here
114n/a if addr not in addr2rc:
115n/a print('??? new object created while tearing down:', line.rstrip())
116n/a continue
117n/a print(addr, end=' ')
118n/a if rc == addr2rc[addr]:
119n/a print('[%s]' % rc, end=' ')
120n/a else:
121n/a print('[%s->%s]' % (addr2rc[addr], rc), end=' ')
122n/a print(guts, addr2guts[addr])
123n/a
124n/a f.close()
125n/a print("%d objects before, %d after" % (before, after))
126n/a
127n/aif __name__ == '__main__':
128n/a combine(sys.argv[1])