[pypy-svn] r66300 - in pypy/branch/parser-compiler/pypy/interpreter/astcompiler: . test

benjamin at codespeak.net benjamin at codespeak.net
Fri Jul 17 05:13:10 CEST 2009


Author: benjamin
Date: Fri Jul 17 05:13:09 2009
New Revision: 66300

Modified:
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/symtable.py
   pypy/branch/parser-compiler/pypy/interpreter/astcompiler/test/test_symtable.py
Log:
refactor scope optimization slightly

Modified: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/codegen.py	Fri Jul 17 05:13:09 2009
@@ -149,7 +149,7 @@
         kind = name_ops_default
         container = self.names
         if scope == symtable.SCOPE_LOCAL:
-            if self.scope.optimized:
+            if self.scope.can_be_optimized:
                 container = self.var_names
                 kind = name_ops_fast
         elif scope == symtable.SCOPE_FREE:
@@ -159,7 +159,7 @@
             kind = name_ops_deref
             container = self.cell_vars
         elif scope == symtable.SCOPE_GLOBAL_IMPLICIT:
-            if self.scope.optimized:
+            if self.scope.locals_fully_known:
                 kind = name_ops_global
         elif scope == symtable.SCOPE_GLOBAL_EXPLICIT:
             kind = name_ops_global
@@ -1115,7 +1115,7 @@
         scope = self.scope
         assert isinstance(scope, symtable.FunctionScope)
         flags = 0
-        if scope.optimized:
+        if scope.locals_fully_known:
             flags |= consts.CO_OPTIMIZED
         if scope.nested:
             flags |= consts.CO_NESTED

Modified: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/symtable.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/astcompiler/symtable.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/symtable.py	Fri Jul 17 05:13:09 2009
@@ -24,11 +24,13 @@
 
 class Scope(object):
 
-    def __init__(self, node, name, optimized):
+    can_be_optimized = False
+
+    def __init__(self, node, name):
         self.node = node
         self.parent = None
         self.name = name
-        self.optimized = optimized
+        self.locals_fully_known = False
         self.symbols = None
         self.roles = {}
         self.varnames = []
@@ -172,16 +174,19 @@
 class ModuleScope(Scope):
 
     def __init__(self, module):
-        Scope.__init__(self, module, "top", False)
+        Scope.__init__(self, module, "top")
 
 
 class FunctionScope(Scope):
 
+    can_be_optimized = True
+
     def __init__(self, func, name):
-        Scope.__init__(self, func, name, True)
+        Scope.__init__(self, func, name)
         self.has_variable_arg = False
         self.has_keywords_arg = False
         self.is_generator = False
+        self.optimized = True
         self.return_with_value = False
         self.import_star = None
         self.bare_exec = None
@@ -251,7 +256,7 @@
                 trailer = "is a nested function"
             raise SyntaxError(err % (self.name, trailer), node.lineno,
                               node.col_offset)
-        self.optimized = self.optimized and not self.has_exec
+        self.locals_fully_known = self.optimized and not self.has_exec
 
 
 class ClassScope(Scope):
@@ -259,7 +264,7 @@
     _hide_bound_from_nested_scopes = True
 
     def __init__(self, clsdef):
-        Scope.__init__(self, clsdef, clsdef.name, False)
+        Scope.__init__(self, clsdef, clsdef.name)
 
     def mangle(self, name):
         return misc.mangle(name, self.name)

Modified: pypy/branch/parser-compiler/pypy/interpreter/astcompiler/test/test_symtable.py
==============================================================================
--- pypy/branch/parser-compiler/pypy/interpreter/astcompiler/test/test_symtable.py	(original)
+++ pypy/branch/parser-compiler/pypy/interpreter/astcompiler/test/test_symtable.py	Fri Jul 17 05:13:09 2009
@@ -55,12 +55,12 @@
     def test_toplevel(self):
         scp = self.mod_scope("x = 4")
         assert scp.lookup("x") == symtable.SCOPE_LOCAL
-        assert not scp.optimized
+        assert not scp.locals_fully_known
         scp = self.mod_scope("x = 4", "single")
-        assert not scp.optimized
+        assert not scp.locals_fully_known
         assert scp.lookup("x") == symtable.SCOPE_LOCAL
         scp = self.mod_scope("x*4*6", "eval")
-        assert not scp.optimized
+        assert not scp.locals_fully_known
         assert scp.lookup("x") == symtable.SCOPE_GLOBAL_IMPLICIT
 
     def test_duplicate_argument(self):
@@ -253,9 +253,9 @@
         assert exc.msg == "name 'x' is both local and global"
 
     def test_optimization(self):
-        assert not self.mod_scope("").optimized
-        assert not self.class_scope("class x: pass").optimized
-        assert self.func_scope("def f(): pass").optimized
+        assert not self.mod_scope("").can_be_optimized
+        assert not self.class_scope("class x: pass").can_be_optimized
+        assert self.func_scope("def f(): pass").can_be_optimized
 
     def test_unoptimization_with_nested_scopes(self):
         table = (
@@ -294,11 +294,13 @@
         self.mod_scope("exec 'hi'")
         scp = self.func_scope("def f(): exec 'hi'")
         assert not scp.optimized
+        assert not scp.locals_fully_known
         assert isinstance(scp.bare_exec, ast.Exec)
         assert scp.has_exec
         for line in ("exec 'hi' in g", "exec 'hi' in g, h"):
             scp = self.func_scope("def f(): " + line)
-            assert not scp.optimized
+            assert scp.optimized
+            assert not scp.locals_fully_known
             assert scp.bare_exec is None
             assert scp.has_exec
 



More information about the Pypy-commit mailing list