summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorCollin Winter <collinw@gmail.com>2010-03-18 22:46:40 (GMT)
committerCollin Winter <collinw@gmail.com>2010-03-18 22:46:40 (GMT)
commit4222e9c07cc9a223ac756f8cc1c1cd2f9212765e (patch)
treed9d477df9e477ed208bee9b3748f7ad1492e8f86 /Lib
parentb3a482962d12871db122dbc843476eea054b2782 (diff)
downloadcpython-4222e9c07cc9a223ac756f8cc1c1cd2f9212765e.zip
cpython-4222e9c07cc9a223ac756f8cc1c1cd2f9212765e.tar.gz
cpython-4222e9c07cc9a223ac756f8cc1c1cd2f9212765e.tar.bz2
Merged revisions 79060 via svnmerge from
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! ........
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_code.py31
-rw-r--r--Lib/test/test_sys.py2
2 files changed, 30 insertions, 3 deletions
diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py
index 53e787a..1a8af0d 100644
--- a/Lib/test/test_code.py
+++ b/Lib/test/test_code.py
@@ -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()
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 516fa15..eb7d1a4 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -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)