ยปCore Development>Code coverage>Lib/test/crashers/gc_inspection.py

Python code coverage for Lib/test/crashers/gc_inspection.py

#countcontent
1n/a"""
2n/agc.get_referrers() can be used to see objects before they are fully built.
3n/a
4n/aNote that this is only an example. There are many ways to crash Python
5n/aby using gc.get_referrers(), as well as many extension modules (even
6n/awhen they are using perfectly documented patterns to build objects).
7n/a
8n/aIdentifying and removing all places that expose to the GC a
9n/apartially-built object is a long-term project. A patch was proposed on
10n/aSF specifically for this example but I consider fixing just this single
11n/aexample a bit pointless (#1517042).
12n/a
13n/aA fix would include a whole-scale code review, possibly with an API
14n/achange to decouple object creation and GC registration, and according
15n/afixes to the documentation for extension module writers. It's unlikely
16n/ato happen, though. So this is currently classified as
17n/a"gc.get_referrers() is dangerous, use only for debugging".
18n/a"""
19n/a
20n/aimport gc
21n/a
22n/a
23n/adef g():
24n/a marker = object()
25n/a yield marker
26n/a # now the marker is in the tuple being constructed
27n/a [tup] = [x for x in gc.get_referrers(marker) if type(x) is tuple]
28n/a print(tup)
29n/a print(tup[1])
30n/a
31n/a
32n/atuple(g())