[pypy-svn] rev 961 - in pypy/trunk/src/pypy: interpreter interpreter/test objspace/std

hpk at codespeak.net hpk at codespeak.net
Sun Jun 22 19:08:32 CEST 2003


Author: hpk
Date: Sun Jun 22 19:08:32 2003
New Revision: 961

Modified:
   pypy/trunk/src/pypy/interpreter/opcode.py
   pypy/trunk/src/pypy/interpreter/opcode_app.py
   pypy/trunk/src/pypy/interpreter/pycode.py
   pypy/trunk/src/pypy/interpreter/pycode_app.py
   pypy/trunk/src/pypy/interpreter/test/test_interpreter.py
   pypy/trunk/src/pypy/objspace/std/dictobject.py
Log:
- implemented NestedScopes together with some tests

- added an _appendcell method to W_Dictobject-instances

- cleanups



Modified: pypy/trunk/src/pypy/interpreter/opcode.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/opcode.py	(original)
+++ pypy/trunk/src/pypy/interpreter/opcode.py	Sun Jun 22 19:08:32 2003
@@ -442,8 +442,7 @@
     #     syntactically nested frames?
     varname = f.getfreevarname(varindex)
     w_varname = f.space.wrap(varname)
-    w_value = f.space.gethelper(appfile).call("load_closure",
-                                              [f.w_locals, w_varname])
+    w_value = f.w_locals.cell(f.space, w_varname)
     f.valuestack.push(w_value)
 
 def LOAD_DEREF(f, varindex):

Modified: pypy/trunk/src/pypy/interpreter/opcode_app.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/opcode_app.py	(original)
+++ pypy/trunk/src/pypy/interpreter/opcode_app.py	Sun Jun 22 19:08:32 2003
@@ -125,11 +125,6 @@
             except KeyError:
                 raise NameError, "name '"+name+"' is not defined"
 
-def load_closure(locals, name):
-    # this assumes that 'locals' is an extended dictionary with a
-    # 'cell' method to explicitely access a cell
-    return locals.cell(name)
-
 def concatenate_arguments(args, extra_args):
     return args + tuple(extra_args)
 

Modified: pypy/trunk/src/pypy/interpreter/pycode.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/pycode.py	(original)
+++ pypy/trunk/src/pypy/interpreter/pycode.py	Sun Jun 22 19:08:32 2003
@@ -61,9 +61,13 @@
         w_bytecode = space.wrap(co)
         w_arguments = space.gethelper(appfile).call(
             "decode_code_arguments", [w_arguments, w_kwargs, w_defaults,
-                                      w_closure, w_bytecode])
+                                      w_bytecode])
         # we assume that decode_code_arguments() gives us a dictionary
         # of the correct length.
+        if space.is_true(w_closure):
+            l = zip(co.co_freevars, space.unpackiterable(w_closure))
+            for key,cell in l:
+                w_arguments._appendcell(space, space.wrap(key), cell)
         return w_arguments
         
 class PyByteCode(PyBaseCode):

Modified: pypy/trunk/src/pypy/interpreter/pycode_app.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/pycode_app.py	(original)
+++ pypy/trunk/src/pypy/interpreter/pycode_app.py	Sun Jun 22 19:08:32 2003
@@ -13,7 +13,7 @@
 # (I wonder if the pain of writing this at interpreter level might be
 # worth it...)
 
-def decode_code_arguments(args, kws, defs, closure, codeobject):
+def decode_code_arguments(args, kws, defs, codeobject):
     """
     Assumptions:
     args = sequence of the normal actual parameters

Modified: pypy/trunk/src/pypy/interpreter/test/test_interpreter.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/test/test_interpreter.py	(original)
+++ pypy/trunk/src/pypy/interpreter/test/test_interpreter.py	Sun Jun 22 19:08:32 2003
@@ -213,6 +213,25 @@
         def f(x): return x
         self.assertEquals(f(666), 666)
 
+    def test_nested_scope(self):
+        x = 42
+        def f(): return x
+        self.assertEquals(f(), 42)
+
+    def test_nested_scope2(self):
+        x = 42
+        y = 3
+        def f(): return x
+        self.assertEquals(f(), 42)
+
+    def test_nested_scope3(self):
+        x = 42
+        def f():
+            def g():
+                return x
+            return g
+        self.assertEquals(f()(), 42)
+            
 
 if __name__ == '__main__':
     test.main()

Modified: pypy/trunk/src/pypy/objspace/std/dictobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/dictobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/dictobject.py	Sun Jun 22 19:08:32 2003
@@ -63,6 +63,12 @@
     def cell(self,space,w_lookup):
         return space.wrap(self._cell(space,w_lookup))
 
+    def _appendcell(self, space, w_lookup, w_cell):
+        # there should be no w_lookup entry already!
+        data = self.data
+        lookup_hash = space.unwrap(space.hash(w_lookup))
+        cell = space.unwrap(w_cell)
+        data.append((w_lookup, lookup_hash, cell))
 
 registerimplementation(W_DictObject)
 


More information about the Pypy-commit mailing list