]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.2/Lib/test/crashers/gc_inspection.py
AppPkg/Applications/Python: Add Python 2.7.2 sources since the release of Python...
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / test / crashers / gc_inspection.py
diff --git a/AppPkg/Applications/Python/Python-2.7.2/Lib/test/crashers/gc_inspection.py b/AppPkg/Applications/Python/Python-2.7.2/Lib/test/crashers/gc_inspection.py
new file mode 100644 (file)
index 0000000..96be977
--- /dev/null
@@ -0,0 +1,32 @@
+"""\r
+gc.get_referrers() can be used to see objects before they are fully built.\r
+\r
+Note that this is only an example.  There are many ways to crash Python\r
+by using gc.get_referrers(), as well as many extension modules (even\r
+when they are using perfectly documented patterns to build objects).\r
+\r
+Identifying and removing all places that expose to the GC a\r
+partially-built object is a long-term project.  A patch was proposed on\r
+SF specifically for this example but I consider fixing just this single\r
+example a bit pointless (#1517042).\r
+\r
+A fix would include a whole-scale code review, possibly with an API\r
+change to decouple object creation and GC registration, and according\r
+fixes to the documentation for extension module writers.  It's unlikely\r
+to happen, though.  So this is currently classified as\r
+"gc.get_referrers() is dangerous, use only for debugging".\r
+"""\r
+\r
+import gc\r
+\r
+\r
+def g():\r
+    marker = object()\r
+    yield marker\r
+    # now the marker is in the tuple being constructed\r
+    [tup] = [x for x in gc.get_referrers(marker) if type(x) is tuple]\r
+    print tup\r
+    print tup[1]\r
+\r
+\r
+tuple(g())\r