blob: 10caa797825728036c72b1ff203a683a9f6d7607 [file] [log] [blame]
Armin Rigod77ef8f2006-06-28 10:49:51 +00001"""
2gc.get_referrers() can be used to see objects before they are fully built.
Armin Rigo4df7c0a2006-07-25 18:09:57 +00003
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".
Armin Rigod77ef8f2006-06-28 10:49:51 +000018"""
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())