[pypy-svn] r59507 - in pypy/trunk/pypy/interpreter: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Tue Oct 28 19:20:15 CET 2008


Author: cfbolz
Date: Tue Oct 28 19:20:15 2008
New Revision: 59507

Modified:
   pypy/trunk/pypy/interpreter/nestedscope.py
   pypy/trunk/pypy/interpreter/test/test_nestedscope.py
   pypy/trunk/pypy/interpreter/typedef.py
Log:
implement cell_contents attribute on Cells.


Modified: pypy/trunk/pypy/interpreter/nestedscope.py
==============================================================================
--- pypy/trunk/pypy/interpreter/nestedscope.py	(original)
+++ pypy/trunk/pypy/interpreter/nestedscope.py	Tue Oct 28 19:20:15 2008
@@ -57,6 +57,13 @@
         return "<%s(%s) at 0x%x>" % (self.__class__.__name__,
                                      content, uid(self))
 
+    def descr__cell_contents(space, self):
+        try:
+            return self.get()
+        except ValueError:
+            raise OperationError(space.w_ValueError, space.wrap("Cell is empty"))
+
+
 
 super_initialize_frame_scopes = pyframe.PyFrame.initialize_frame_scopes
 super_fast2locals             = pyframe.PyFrame.fast2locals

Modified: pypy/trunk/pypy/interpreter/test/test_nestedscope.py
==============================================================================
--- pypy/trunk/pypy/interpreter/test/test_nestedscope.py	(original)
+++ pypy/trunk/pypy/interpreter/test/test_nestedscope.py	Tue Oct 28 19:20:15 2008
@@ -59,3 +59,23 @@
 
     def test_lambda_in_genexpr(self):
         assert eval('map(apply, (lambda: t for t in range(10)))') == range(10)
+
+    def test_cell_contents(self):
+        def f(x):
+            def f(y):
+                return x + y
+            return f
+
+        g = f(10)
+        assert g.func_closure[0].cell_contents == 10
+
+    def test_empty_cell_contents(self):
+
+        def f():
+            def f(y):
+                  return x + y
+            return f
+            x = 1
+
+        g = f()
+        raises(ValueError, "g.func_closure[0].cell_contents")

Modified: pypy/trunk/pypy/interpreter/typedef.py
==============================================================================
--- pypy/trunk/pypy/interpreter/typedef.py	(original)
+++ pypy/trunk/pypy/interpreter/typedef.py	Tue Oct 28 19:20:15 2008
@@ -877,6 +877,7 @@
                               unwrap_spec=['self', ObjSpace]),
     __setstate__ = interp2app(Cell.descr__setstate__,
                               unwrap_spec=['self', ObjSpace, W_Root]),
+    cell_contents= GetSetProperty(Cell.descr__cell_contents, cls=Cell),
 )
 Cell.acceptable_as_base_class = False
 



More information about the Pypy-commit mailing list