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