[pypy-svn] r16452 - in pypy/dist/pypy/interpreter: . stablecompiler test

hpk at codespeak.net hpk at codespeak.net
Thu Aug 25 11:55:59 CEST 2005


Author: hpk
Date: Thu Aug 25 11:55:58 2005
New Revision: 16452

Modified:
   pypy/dist/pypy/interpreter/pycode.py
   pypy/dist/pypy/interpreter/pyframe.py
   pypy/dist/pypy/interpreter/stablecompiler/pyassem.py
   pypy/dist/pypy/interpreter/stablecompiler/pycodegen.py
   pypy/dist/pypy/interpreter/test/test_exec.py
Log:
(hpk,arigo,samuele)

more closely mirror the code object's flags from CPython
regarding the locals dict on frames.  Makes another
(added) scope-related test pass that previously failed. 



Modified: pypy/dist/pypy/interpreter/pycode.py
==============================================================================
--- pypy/dist/pypy/interpreter/pycode.py	(original)
+++ pypy/dist/pypy/interpreter/pycode.py	Thu Aug 25 11:55:58 2005
@@ -248,11 +248,20 @@
         else:
             return None
 
-    def dictscope_needed(self):
+    def initialize_frame_scopes(self, frame): 
         # regular functions always have CO_OPTIMIZED and CO_NEWLOCALS.
         # class bodies only have CO_NEWLOCALS.
-        return not (self.co_flags & CO_OPTIMIZED)
-
+        # CO_NEWLOCALS: make a locals dict unless optimized is also set
+        # CO_OPTIMIZED: no locals dict needed at all 
+        flags = self.co_flags
+        if flags & CO_OPTIMIZED: 
+            return 
+        if flags & CO_NEWLOCALS:
+            frame.w_locals = frame.space.newdict([])
+        else:
+            assert frame.w_globals is not None
+            frame.w_locals = frame.w_globals 
+        
     def getjoinpoints(self):
         """Compute the bytecode positions that are potential join points
         (for FlowObjSpace)"""

Modified: pypy/dist/pypy/interpreter/pyframe.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyframe.py	(original)
+++ pypy/dist/pypy/interpreter/pyframe.py	Thu Aug 25 11:55:58 2005
@@ -46,9 +46,7 @@
         self.builtin = space.builtin.pick_builtin(w_globals)
         # regular functions always have CO_OPTIMIZED and CO_NEWLOCALS.
         # class bodies only have CO_NEWLOCALS.
-        if code.dictscope_needed():
-            self.w_locals = space.newdict([])  # set to None by Frame.__init__
-
+        code.initialize_frame_scopes(self)
         self.fastlocals_w = [None]*self.numlocals
         self.w_f_trace = None
         self.last_instr = -1

Modified: pypy/dist/pypy/interpreter/stablecompiler/pyassem.py
==============================================================================
--- pypy/dist/pypy/interpreter/stablecompiler/pyassem.py	(original)
+++ pypy/dist/pypy/interpreter/stablecompiler/pyassem.py	Thu Aug 25 11:55:58 2005
@@ -317,7 +317,8 @@
 class PyFlowGraph(FlowGraph):
     super_init = FlowGraph.__init__
 
-    def __init__(self, name, filename, args=(), optimized=0, klass=None):
+    def __init__(self, name, filename, args=(), optimized=0, 
+                 klass=None, newlocals=0):
         self.super_init()
         self.name = name
         self.filename = filename
@@ -325,10 +326,11 @@
         self.args = args # XXX
         self.argcount = getArgCount(args)
         self.klass = klass
+        self.flags = 0
         if optimized:
-            self.flags = CO_OPTIMIZED | CO_NEWLOCALS
-        else:
-            self.flags = 0
+            self.flags |= CO_OPTIMIZED
+        if newlocals:
+            self.flags |= CO_NEWLOCALS
         self.consts = []
         self.names = []
         # Free variables found by the symbol table scan, including

Modified: pypy/dist/pypy/interpreter/stablecompiler/pycodegen.py
==============================================================================
--- pypy/dist/pypy/interpreter/stablecompiler/pycodegen.py	(original)
+++ pypy/dist/pypy/interpreter/stablecompiler/pycodegen.py	Thu Aug 25 11:55:58 2005
@@ -1290,7 +1290,8 @@
 
         args, hasTupleArg = generateArgList(func.argnames)
         self.graph = pyassem.PyFlowGraph(name, func.filename, args,
-                                         optimized=1)
+                                         optimized=self.localsfullyknown,
+                                         newlocals=1)
         self.isLambda = isLambda
         self.super_init()
 

Modified: pypy/dist/pypy/interpreter/test/test_exec.py
==============================================================================
--- pypy/dist/pypy/interpreter/test/test_exec.py	(original)
+++ pypy/dist/pypy/interpreter/test/test_exec.py	Thu Aug 25 11:55:58 2005
@@ -159,3 +159,13 @@
         import sys
         assert d['platform'] == 3
 
+    def test_exec_load_name(self):
+        d = {'x': 2}
+        exec """if 1:
+            def f():
+                save = x 
+                exec "x=3"
+                return x,save
+        """ in d
+        res = d['f']()
+        assert res == (3, 2)



More information about the Pypy-commit mailing list