[pypy-svn] rev 2683 - in pypy/trunk/src/pypy: appspace interpreter interpreter/test

hpk at codespeak.net hpk at codespeak.net
Tue Dec 23 17:35:27 CET 2003


Author: hpk
Date: Tue Dec 23 17:35:26 2003
New Revision: 2683

Modified:
   pypy/trunk/src/pypy/appspace/builtin_functions_test.py
   pypy/trunk/src/pypy/interpreter/nestedscope.py
   pypy/trunk/src/pypy/interpreter/test/test_nestedscope.py
Log:
fixed the nested scope issue Alex mentioned in his mail. 
I think we should make a complete analysis of the corresponding
CPython code regarding the scopes, though.  On first read
it seems that CPython should produce the wrong results 
for calling vars()/locals() in inner scopes so i must be missing
something. 

Whatever, the tests pass and also the appspace/builtin_functions_test.py 
pass that Alex had commented out ...



Modified: pypy/trunk/src/pypy/appspace/builtin_functions_test.py
==============================================================================
--- pypy/trunk/src/pypy/appspace/builtin_functions_test.py	(original)
+++ pypy/trunk/src/pypy/appspace/builtin_functions_test.py	Tue Dec 23 17:35:26 2003
@@ -1192,9 +1192,7 @@
             import sys
             self.assertEqual(Set(vars(sys)), Set(dir(sys)))
             self.assertEqual(get_vars_f0(), {})
-            ''' XXX TODO: known bug: free-var 'get_vars_0' ``leaks``
             self.assertEqual(get_vars_f2(), {'a': 1, 'b': 2})
-            '''
             self.assertRaises(TypeError, vars, 42, 42)
             self.assertRaises(TypeError, vars, 42)
 

Modified: pypy/trunk/src/pypy/interpreter/nestedscope.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/nestedscope.py	(original)
+++ pypy/trunk/src/pypy/interpreter/nestedscope.py	Tue Dec 23 17:35:26 2003
@@ -71,7 +71,11 @@
 
     def fast2locals(self):
         PyInterpFrame.fast2locals(self)
-        freevarnames = self.code.co_cellvars + self.code.co_freevars
+        # cellvars are values exported to inner scopes
+        # freevars are values coming from outer scopes 
+        # for locals we only want the ones exported to inner-scopes 
+        # XXX check more exactly how CPython does it 
+        freevarnames = self.code.co_cellvars # + self.code.co_freevars
         for name, cell in zip(freevarnames, self.cells):
             try:
                 w_value = cell.get()

Modified: pypy/trunk/src/pypy/interpreter/test/test_nestedscope.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/test/test_nestedscope.py	(original)
+++ pypy/trunk/src/pypy/interpreter/test/test_nestedscope.py	Tue Dec 23 17:35:26 2003
@@ -34,6 +34,28 @@
             return (a, b)
         self.assertEquals(f(), (3, 4))
 
+    def test_nested_scope_locals(self):
+        def f():
+            x = 3
+            def g():
+                i = x
+                return locals()
+            return g()
+        d = f()
+        self.assertEquals(d, {'i':3})
+
+    def test_deeply_nested_scope_locals(self):
+        def f():
+            x = 3
+            def g():
+                def h():
+                    i = x
+                    return locals()
+                return locals(), h()
+            return g()
+        outer_locals, inner_locals = f()
+        self.assertEquals(inner_locals, {'i':3})
+        self.assertEquals(len(outer_locals), 1, "len!=1 for %r" % outer_locals)
 
 if __name__ == '__main__':
     test.main()


More information about the Pypy-commit mailing list