]> granicus.if.org Git - python/commitdiff
Merged revisions 79060 via svnmerge from
authorCollin Winter <collinw@gmail.com>
Thu, 18 Mar 2010 22:46:40 +0000 (22:46 +0000)
committerCollin Winter <collinw@gmail.com>
Thu, 18 Mar 2010 22:46:40 +0000 (22:46 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r79060 | collin.winter | 2010-03-18 14:54:01 -0700 (Thu, 18 Mar 2010) | 4 lines

  Add support for weak references to code objects. This will be used by an optimization in the incoming Python 3 JIT.

  Patch by Reid Kleckner!
........

Doc/library/weakref.rst
Include/code.h
Lib/test/test_code.py
Lib/test/test_sys.py
Misc/NEWS
Objects/codeobject.c

index 53b13e5602137d2860c673c9d7a7b442fa08ef7f..7c6f6db56f96eabd11a1a87161bcbbfefd5cf7b5 100644 (file)
@@ -59,10 +59,10 @@ is exposed by the :mod:`weakref` module for the benefit of advanced uses.
 Not all objects can be weakly referenced; those objects which can include class
 instances, functions written in Python (but not in C), instance methods, sets,
 frozensets, file objects, :term:`generator`\s, type objects, sockets, arrays,
-deques, and regular expression pattern objects.
+deques, regular expression pattern objects, and code objects.
 
 .. versionchanged:: 3.2
-   Added support for thread.lock and threading.Lock.
+   Added support for thread.lock, threading.Lock, and code objects.
 
 Several built-in types such as :class:`list` and :class:`dict` do not directly
 support weak references but can add support through subclassing::
index c93d861cf006de9107c823b240eb6f510416bb5a..f0f88cd8f63f18fad23f0929ed86b470eb9d5f8f 100644 (file)
@@ -27,6 +27,7 @@ typedef struct {
     PyObject *co_lnotab;       /* string (encoding addr<->lineno mapping) See
                                   Objects/lnotab_notes.txt for details. */
     void *co_zombieframe;     /* for optimization only (see frameobject.c) */
+    PyObject *co_weakreflist;   /* to support weakrefs to code objects */
 } PyCodeObject;
 
 /* Masks for co_flags above */
index 53e787a408978ac00f97f6b34b85a14e4d1a4279..1a8af0db4280add4a77f226fd81da3bf819d7b61 100644 (file)
@@ -103,8 +103,10 @@ consts: ('None',)
 """
 
 import unittest
+import weakref
 import _testcapi
 
+
 def consts(t):
     """Yield a doctest-safe sequence of object reprs."""
     for elt in t:
@@ -131,12 +133,37 @@ class CodeTest(unittest.TestCase):
         self.assertEquals(co.co_firstlineno, 15)
 
 
+class CodeWeakRefTest(unittest.TestCase):
+
+    def test_basic(self):
+        # Create a code object in a clean environment so that we know we have
+        # the only reference to it left.
+        namespace = {}
+        exec("def f(): pass", globals(), namespace)
+        f = namespace["f"]
+        del namespace
+
+        self.called = False
+        def callback(code):
+            self.called = True
+
+        # f is now the last reference to the function, and through it, the code
+        # object.  While we hold it, check that we can create a weakref and
+        # deref it.  Then delete it, and check that the callback gets called and
+        # the reference dies.
+        coderef = weakref.ref(f.__code__, callback)
+        self.assertTrue(bool(coderef()))
+        del f
+        self.assertFalse(bool(coderef()))
+        self.assertTrue(self.called)
+
+
 def test_main(verbose=None):
     from test.support import run_doctest, run_unittest
     from test import test_code
     run_doctest(test_code, verbose)
-    run_unittest(CodeTest)
+    run_unittest(CodeTest, CodeWeakRefTest)
 
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     test_main()
index 516fa1544df59a134edc5fb57ec66def6852ed4a..eb7d1a42aa730764fe77006cf6f565584783cf14 100644 (file)
@@ -588,7 +588,7 @@ class SizeofTest(unittest.TestCase):
             return inner
         check(get_cell().__closure__[0], size(h + 'P'))
         # code
-        check(get_cell().__code__, size(h + '5i8Pi2P'))
+        check(get_cell().__code__, size(h + '5i8Pi3P'))
         # complex
         check(complex(0,1), size(h + '2d'))
         # method_descriptor (descriptor object)
index 5484d3941b7912ad1534261b7158a6306cc1365b..5f6920a2ea98f2a54c97e0320c44175e64abcbaf 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -233,6 +233,8 @@ Core and Builtins
   instances from being copied with copy.copy(), and bytes subclasses
   from being pickled properly.
 
+- Code objects now support weak references.
+
 C-API
 -----
 
index a772c56a5b737a87e97c6b8ab52cf8df8ac8ebe2..3e71e4cde3d7e4aa12c684391bb503bb019a8edb 100644 (file)
@@ -108,6 +108,7 @@ PyCode_New(int argcount, int kwonlyargcount,
                Py_INCREF(lnotab);
                co->co_lnotab = lnotab;
                 co->co_zombieframe = NULL;
+               co->co_weakreflist = NULL;
        }
        return co;
 }
@@ -331,6 +332,8 @@ code_dealloc(PyCodeObject *co)
        Py_XDECREF(co->co_lnotab);
         if (co->co_zombieframe != NULL)
                 PyObject_GC_Del(co->co_zombieframe);
+       if (co->co_weakreflist != NULL)
+               PyObject_ClearWeakRefs((PyObject*)co);
        PyObject_DEL(co);
 }
 
@@ -462,7 +465,7 @@ PyTypeObject PyCode_Type = {
        0,                              /* tp_traverse */
        0,                              /* tp_clear */
        code_richcompare,               /* tp_richcompare */
-       0,                              /* tp_weaklistoffset */
+       offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
        0,                              /* tp_iter */
        0,                              /* tp_iternext */
        0,                              /* tp_methods */