ยปCore Development>Code coverage>Lib/test/libregrtest/refleak.py

Python code coverage for Lib/test/libregrtest/refleak.py

#countcontent
1n/aimport errno
2n/aimport os
3n/aimport re
4n/aimport sys
5n/aimport warnings
6n/afrom inspect import isabstract
7n/afrom test import support
8n/a
9n/a
10n/atry:
11n/a MAXFD = os.sysconf("SC_OPEN_MAX")
12n/aexcept Exception:
13n/a MAXFD = 256
14n/a
15n/a
16n/adef fd_count():
17n/a """Count the number of open file descriptors"""
18n/a if sys.platform.startswith(('linux', 'freebsd')):
19n/a try:
20n/a names = os.listdir("/proc/self/fd")
21n/a return len(names)
22n/a except FileNotFoundError:
23n/a pass
24n/a
25n/a count = 0
26n/a for fd in range(MAXFD):
27n/a try:
28n/a # Prefer dup() over fstat(). fstat() can require input/output
29n/a # whereas dup() doesn't.
30n/a fd2 = os.dup(fd)
31n/a except OSError as e:
32n/a if e.errno != errno.EBADF:
33n/a raise
34n/a else:
35n/a os.close(fd2)
36n/a count += 1
37n/a return count
38n/a
39n/a
40n/adef dash_R(the_module, test, indirect_test, huntrleaks):
41n/a """Run a test multiple times, looking for reference leaks.
42n/a
43n/a Returns:
44n/a False if the test didn't leak references; True if we detected refleaks.
45n/a """
46n/a # This code is hackish and inelegant, but it seems to do the job.
47n/a import copyreg
48n/a import collections.abc
49n/a
50n/a if not hasattr(sys, 'gettotalrefcount'):
51n/a raise Exception("Tracking reference leaks requires a debug build "
52n/a "of Python")
53n/a
54n/a # Save current values for dash_R_cleanup() to restore.
55n/a fs = warnings.filters[:]
56n/a ps = copyreg.dispatch_table.copy()
57n/a pic = sys.path_importer_cache.copy()
58n/a try:
59n/a import zipimport
60n/a except ImportError:
61n/a zdc = None # Run unmodified on platforms without zipimport support
62n/a else:
63n/a zdc = zipimport._zip_directory_cache.copy()
64n/a abcs = {}
65n/a for abc in [getattr(collections.abc, a) for a in collections.abc.__all__]:
66n/a if not isabstract(abc):
67n/a continue
68n/a for obj in abc.__subclasses__() + [abc]:
69n/a abcs[obj] = obj._abc_registry.copy()
70n/a
71n/a nwarmup, ntracked, fname = huntrleaks
72n/a fname = os.path.join(support.SAVEDCWD, fname)
73n/a repcount = nwarmup + ntracked
74n/a rc_deltas = [0] * repcount
75n/a alloc_deltas = [0] * repcount
76n/a fd_deltas = [0] * repcount
77n/a
78n/a print("beginning", repcount, "repetitions", file=sys.stderr)
79n/a print(("1234567890"*(repcount//10 + 1))[:repcount], file=sys.stderr,
80n/a flush=True)
81n/a # initialize variables to make pyflakes quiet
82n/a rc_before = alloc_before = fd_before = 0
83n/a for i in range(repcount):
84n/a indirect_test()
85n/a alloc_after, rc_after, fd_after = dash_R_cleanup(fs, ps, pic, zdc,
86n/a abcs)
87n/a print('.', end='', file=sys.stderr, flush=True)
88n/a if i >= nwarmup:
89n/a rc_deltas[i] = rc_after - rc_before
90n/a alloc_deltas[i] = alloc_after - alloc_before
91n/a fd_deltas[i] = fd_after - fd_before
92n/a alloc_before = alloc_after
93n/a rc_before = rc_after
94n/a fd_before = fd_after
95n/a print(file=sys.stderr)
96n/a # These checkers return False on success, True on failure
97n/a def check_rc_deltas(deltas):
98n/a return any(deltas)
99n/a def check_alloc_deltas(deltas):
100n/a # At least 1/3rd of 0s
101n/a if 3 * deltas.count(0) < len(deltas):
102n/a return True
103n/a # Nothing else than 1s, 0s and -1s
104n/a if not set(deltas) <= {1,0,-1}:
105n/a return True
106n/a return False
107n/a failed = False
108n/a for deltas, item_name, checker in [
109n/a (rc_deltas, 'references', check_rc_deltas),
110n/a (alloc_deltas, 'memory blocks', check_alloc_deltas),
111n/a (fd_deltas, 'file descriptors', check_rc_deltas)]:
112n/a if checker(deltas):
113n/a msg = '%s leaked %s %s, sum=%s' % (
114n/a test, deltas[nwarmup:], item_name, sum(deltas))
115n/a print(msg, file=sys.stderr, flush=True)
116n/a with open(fname, "a") as refrep:
117n/a print(msg, file=refrep)
118n/a refrep.flush()
119n/a failed = True
120n/a return failed
121n/a
122n/a
123n/adef dash_R_cleanup(fs, ps, pic, zdc, abcs):
124n/a import gc, copyreg
125n/a import collections.abc
126n/a from weakref import WeakSet
127n/a
128n/a # Restore some original values.
129n/a warnings.filters[:] = fs
130n/a copyreg.dispatch_table.clear()
131n/a copyreg.dispatch_table.update(ps)
132n/a sys.path_importer_cache.clear()
133n/a sys.path_importer_cache.update(pic)
134n/a try:
135n/a import zipimport
136n/a except ImportError:
137n/a pass # Run unmodified on platforms without zipimport support
138n/a else:
139n/a zipimport._zip_directory_cache.clear()
140n/a zipimport._zip_directory_cache.update(zdc)
141n/a
142n/a # clear type cache
143n/a sys._clear_type_cache()
144n/a
145n/a # Clear ABC registries, restoring previously saved ABC registries.
146n/a for abc in [getattr(collections.abc, a) for a in collections.abc.__all__]:
147n/a if not isabstract(abc):
148n/a continue
149n/a for obj in abc.__subclasses__() + [abc]:
150n/a obj._abc_registry = abcs.get(obj, WeakSet()).copy()
151n/a obj._abc_cache.clear()
152n/a obj._abc_negative_cache.clear()
153n/a
154n/a clear_caches()
155n/a
156n/a # Collect cyclic trash and read memory statistics immediately after.
157n/a func1 = sys.getallocatedblocks
158n/a func2 = sys.gettotalrefcount
159n/a gc.collect()
160n/a return func1(), func2(), fd_count()
161n/a
162n/a
163n/adef clear_caches():
164n/a import gc
165n/a
166n/a # Clear the warnings registry, so they can be displayed again
167n/a for mod in sys.modules.values():
168n/a if hasattr(mod, '__warningregistry__'):
169n/a del mod.__warningregistry__
170n/a
171n/a # Flush standard output, so that buffered data is sent to the OS and
172n/a # associated Python objects are reclaimed.
173n/a for stream in (sys.stdout, sys.stderr, sys.__stdout__, sys.__stderr__):
174n/a if stream is not None:
175n/a stream.flush()
176n/a
177n/a # Clear assorted module caches.
178n/a # Don't worry about resetting the cache if the module is not loaded
179n/a try:
180n/a distutils_dir_util = sys.modules['distutils.dir_util']
181n/a except KeyError:
182n/a pass
183n/a else:
184n/a distutils_dir_util._path_created.clear()
185n/a re.purge()
186n/a
187n/a try:
188n/a _strptime = sys.modules['_strptime']
189n/a except KeyError:
190n/a pass
191n/a else:
192n/a _strptime._regex_cache.clear()
193n/a
194n/a try:
195n/a urllib_parse = sys.modules['urllib.parse']
196n/a except KeyError:
197n/a pass
198n/a else:
199n/a urllib_parse.clear_cache()
200n/a
201n/a try:
202n/a urllib_request = sys.modules['urllib.request']
203n/a except KeyError:
204n/a pass
205n/a else:
206n/a urllib_request.urlcleanup()
207n/a
208n/a try:
209n/a linecache = sys.modules['linecache']
210n/a except KeyError:
211n/a pass
212n/a else:
213n/a linecache.clearcache()
214n/a
215n/a try:
216n/a mimetypes = sys.modules['mimetypes']
217n/a except KeyError:
218n/a pass
219n/a else:
220n/a mimetypes._default_mime_types()
221n/a
222n/a try:
223n/a filecmp = sys.modules['filecmp']
224n/a except KeyError:
225n/a pass
226n/a else:
227n/a filecmp._cache.clear()
228n/a
229n/a try:
230n/a struct = sys.modules['struct']
231n/a except KeyError:
232n/a pass
233n/a else:
234n/a struct._clearcache()
235n/a
236n/a try:
237n/a doctest = sys.modules['doctest']
238n/a except KeyError:
239n/a pass
240n/a else:
241n/a doctest.master = None
242n/a
243n/a try:
244n/a ctypes = sys.modules['ctypes']
245n/a except KeyError:
246n/a pass
247n/a else:
248n/a ctypes._reset_cache()
249n/a
250n/a try:
251n/a typing = sys.modules['typing']
252n/a except KeyError:
253n/a pass
254n/a else:
255n/a for f in typing._cleanups:
256n/a f()
257n/a
258n/a gc.collect()
259n/a
260n/a
261n/adef warm_caches():
262n/a # char cache
263n/a s = bytes(range(256))
264n/a for i in range(256):
265n/a s[i:i+1]
266n/a # unicode cache
267n/a [chr(i) for i in range(256)]
268n/a # int cache
269n/a list(range(-5, 257))