PageRenderTime 387ms CodeModel.GetById 181ms app.highlight 7ms RepoModel.GetById 195ms app.codeStats 0ms

/Lib/test/crashers/gc_inspection.py

http://unladen-swallow.googlecode.com/
Python | 32 lines | 3 code | 0 blank | 29 comment | 0 complexity | fb68a4f8c7554e6e21d832bf16f5b066 MD5 | raw file
 1"""
 2gc.get_referrers() can be used to see objects before they are fully built.
 3
 4Note that this is only an example.  There are many ways to crash Python
 5by using gc.get_referrers(), as well as many extension modules (even
 6when they are using perfectly documented patterns to build objects).
 7
 8Identifying and removing all places that expose to the GC a
 9partially-built object is a long-term project.  A patch was proposed on
10SF specifically for this example but I consider fixing just this single
11example a bit pointless (#1517042).
12
13A fix would include a whole-scale code review, possibly with an API
14change to decouple object creation and GC registration, and according
15fixes to the documentation for extension module writers.  It's unlikely
16to happen, though.  So this is currently classified as
17"gc.get_referrers() is dangerous, use only for debugging".
18"""
19
20import gc
21
22
23def g():
24    marker = object()
25    yield marker
26    # now the marker is in the tuple being constructed
27    [tup] = [x for x in gc.get_referrers(marker) if type(x) is tuple]
28    print tup
29    print tup[1]
30
31
32tuple(g())