[Python-checkins] r54577 - in sandbox/trunk/2to3: fixes/fix_next.py fixes/util.py tests/test_util.py

collin.winter python-checkins at python.org
Mon Mar 26 17:20:22 CEST 2007


Author: collin.winter
Date: Mon Mar 26 17:20:19 2007
New Revision: 54577

Modified:
   sandbox/trunk/2to3/fixes/fix_next.py
   sandbox/trunk/2to3/fixes/util.py
   sandbox/trunk/2to3/tests/test_util.py
Log:
Move find_binding and support code out of fix_next and into fixes.util; add tests for find_binding.

Modified: sandbox/trunk/2to3/fixes/fix_next.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_next.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_next.py	Mon Mar 26 17:20:19 2007
@@ -10,7 +10,7 @@
 from pgen2 import token
 from pygram import python_symbols as syms
 from fixes import basefix
-from fixes.util import Name, Call
+from fixes.util import Name, Call, find_binding, any
 
 bind_warning = "Calls to builtin next() possibly shadowed by global binding"
 
@@ -69,7 +69,7 @@
             n = Name("__next__", prefix=name.get_prefix())
             name.replace(n)
         elif attr:
-            # We don't do this transformation if we're assignment to "x.next".
+            # We don't do this transformation if we're assigning to "x.next".
             # Unfortunately, it doesn't seem possible to do this in PATTERN,
             #  so it's being done here.
             if is_assign_target(node):
@@ -94,69 +94,9 @@
                 node.shadowed_next = True
 
 
-### The following functions are to find module-level bindings
-
-def find_binding(name, file_input):
-    for child in file_input.children:
-        if child.type == syms.for_stmt:
-            if find(name, child.children[1]):
-                return child
-        elif child.type == syms.funcdef and child.children[1].value == name:
-            return child
-        elif is_import_binding(child, name):
-            return child
-        elif child.type == syms.simple_stmt:
-            if child.children[0].type == syms.expr_stmt:
-                n = find(name, child.children[0].children[0])
-                if n:
-                    return n
-
-def find(name, node):
-    nodes = [node]
-    while nodes:
-        node = nodes.pop()
-        if isinstance(node, pytree.Node):
-            nodes.extend(node.children)
-        elif node.type == token.NAME and node.value == name:
-            return node
-    return None
-
-def is_import_binding(node, name):
-    if node.type == syms.simple_stmt:
-        i = node.children[0]
-        if i.type == syms.import_name:
-            imp = i.children[1]
-            if imp.type == syms.dotted_as_names:
-                for child in imp.children:
-                    if child.type == syms.dotted_as_name:
-                        if child.children[2].value == name:
-                            return i
-            elif imp.type == syms.dotted_as_name:
-                last = imp.children[-1]
-                if last.type == token.NAME and last.value == name:
-                    return i
-        elif i.type == syms.import_from:
-            n = i.children[3]
-            if n.type == syms.import_as_names:
-                if find(name, n):
-                    return i
-            elif n.type == token.NAME and n.value == name:
-                return i
-    return None
-
-
 ### The following functions help test if node is part of an assignment
 ###  target.
 
-try:
-    any
-except NameError:
-    def any(l):
-        for o in l:
-            if o:
-                return True
-        return False
-
 def is_assign_target(node):
     assign = find_assign(node)    
     if assign is None:

Modified: sandbox/trunk/2to3/fixes/util.py
==============================================================================
--- sandbox/trunk/2to3/fixes/util.py	(original)
+++ sandbox/trunk/2to3/fixes/util.py	Mon Mar 26 17:20:19 2007
@@ -112,3 +112,66 @@
     set = set
 except NameError:
     from sets import Set as set
+
+###########################################################
+### The following functions are to find bindings in a suite
+###########################################################
+
+_def_syms = set([syms.classdef, syms.funcdef])
+def find_binding(name, node):
+    for child in node.children:
+        if child.type == syms.for_stmt:
+            if _find(name, child.children[1]):
+                return child
+            elif _find(name, child.children[-1]):
+                return child
+        elif child.type in _def_syms and child.children[1].value == name:
+            return child
+        elif _is_import_binding(child, name):
+            return child
+        elif child.type == syms.simple_stmt:
+            if child.children[0].type == syms.expr_stmt:
+                n = _find(name, child.children[0].children[0])
+                if n:
+                    return n
+
+_block_syms = set([syms.funcdef, syms.classdef, syms.trailer])
+def _find(name, node):
+    nodes = [node]
+    while nodes:
+        node = nodes.pop()
+        if node.type > 256 and node.type not in _block_syms:
+            nodes.extend(node.children)
+        elif node.type == token.NAME and node.value == name:
+            return node
+    return None
+
+def _is_import_binding(node, name):
+    if node.type == syms.simple_stmt:
+        i = node.children[0]
+        if i.type == syms.import_name:
+            imp = i.children[1]
+            if imp.type == syms.dotted_as_names:
+                for child in imp.children:
+                    if child.type == syms.dotted_as_name:
+                        if child.children[2].value == name:
+                            return i
+                    elif child.type == token.NAME and child.value == name:
+                        return i
+            elif imp.type == syms.dotted_as_name:
+                last = imp.children[-1]
+                if last.type == token.NAME and last.value == name:
+                    return i
+            elif imp.type == token.NAME and imp.value == name:
+                return i
+        elif i.type == syms.import_from:
+            n = i.children[3]
+            if n.type == syms.import_as_names and _find(name, n):
+                return i
+            elif n.type == syms.import_as_name:
+                child = n.children[2]
+                if child.type == token.NAME and child.value == name:
+                    return i
+            elif n.type == token.NAME and n.value == name:
+                return i
+    return None

Modified: sandbox/trunk/2to3/tests/test_util.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_util.py	(original)
+++ sandbox/trunk/2to3/tests/test_util.py	Mon Mar 26 17:20:19 2007
@@ -12,13 +12,15 @@
 import pytree
 from fixes import util
 
-def parse(code):
+
+def parse(code, strip_levels=0):
     # The topmost node is file_input, which we don't care about.
     # The next-topmost node is a *_stmt node, which we also don't care about
     tree = support.parse_string(code)
-    node = tree.children[0].children[0]
-    node.parent = None
-    return node
+    for i in range(strip_levels):
+        tree = tree.children[0]
+    tree.parent = None
+    return tree
     
 class MacroTestCase(support.TestCase):
     def assertStr(self, node, string):
@@ -29,7 +31,7 @@
 
 class Test_is_tuple(support.TestCase):
     def is_tuple(self, string):
-        return util.is_tuple(parse(string))
+        return util.is_tuple(parse(string, strip_levels=2))
 
     def test_valid(self):
         self.failUnless(self.is_tuple("(a, b)"))
@@ -44,7 +46,7 @@
 
 class Test_is_list(support.TestCase):
     def is_list(self, string):
-        return util.is_list(parse(string))
+        return util.is_list(parse(string, strip_levels=2))
 
     def test_valid(self):
         self.failUnless(self.is_list("[]"))
@@ -60,7 +62,7 @@
 class Test_Attr(MacroTestCase):
     def test(self):
         from fixes.util import Attr, Name
-        call = parse("foo()")
+        call = parse("foo()", strip_levels=2)
     
         self.assertStr(Attr(Name("a"), Name("b")), "a.b")
         self.assertStr(Attr(call, Name("b")), "foo().b")
@@ -79,7 +81,102 @@
         self.assertStr(Name("a"), "a")
         self.assertStr(Name("foo.foo().bar"), "foo.foo().bar")
         self.assertStr(Name("a", prefix="b"), "ba")
+        
 
+class Test_find_binding(support.TestCase):
+    def find_binding(self, name, string):
+        return util.find_binding(name, parse(string))
+
+    def test_simple_assignment(self):
+        self.failUnless(self.find_binding("a", "a = b"))
+        self.failUnless(self.find_binding("a", "a = [b, c, d]"))
+        self.failUnless(self.find_binding("a", "a = foo()"))
+        self.failUnless(self.find_binding("a", "a = foo().foo.foo[6][foo]"))
+        self.failIf(self.find_binding("a", "foo = a"))
+        self.failIf(self.find_binding("a", "foo = (a, b, c)"))
+
+    def test_tuple_assignment(self):
+        self.failUnless(self.find_binding("a", "(a,) = b"))
+        self.failUnless(self.find_binding("a", "(a, b, c) = [b, c, d]"))
+        self.failUnless(self.find_binding("a", "(c, (d, a), b) = foo()"))
+        self.failUnless(self.find_binding("a", "(a, b) = foo().foo.foo[6][foo]"))
+        self.failIf(self.find_binding("a", "(foo, b) = (b, a)"))
+        self.failIf(self.find_binding("a", "(foo, b, c) = (a, b, c)"))
+
+    def test_list_assignment(self):
+        self.failUnless(self.find_binding("a", "[a] = b"))
+        self.failUnless(self.find_binding("a", "[a, b, c] = [b, c, d]"))
+        self.failUnless(self.find_binding("a", "[c, [d, a], b] = foo()"))
+        self.failUnless(self.find_binding("a", "[a, b] = foo().foo.foo[a][foo]"))
+        self.failIf(self.find_binding("a", "[foo, b] = (b, a)"))
+        self.failIf(self.find_binding("a", "[foo, b, c] = (a, b, c)"))
+        
+    def test_invalid_assignments(self):
+        self.failIf(self.find_binding("a", "foo.a = 5"))
+        self.failIf(self.find_binding("a", "foo[a] = 5"))
+        self.failIf(self.find_binding("a", "foo(a) = 5"))
+        self.failIf(self.find_binding("a", "foo(a, b) = 5"))
+        
+    def test_simple_import(self):
+        self.failUnless(self.find_binding("a", "import a"))
+        self.failUnless(self.find_binding("a", "import b, c, a, d"))
+        self.failIf(self.find_binding("a", "import b"))
+        self.failIf(self.find_binding("a", "import b, c, d"))
+        
+    def test_from_import(self):
+        self.failUnless(self.find_binding("a", "from x import a"))
+        self.failUnless(self.find_binding("a", "from a import a"))
+        self.failUnless(self.find_binding("a", "from x import b, c, a, d"))
+        self.failUnless(self.find_binding("a", "from x.b import a"))
+        self.failUnless(self.find_binding("a", "from x.b import b, c, a, d"))
+        self.failIf(self.find_binding("a", "from a import b"))
+        self.failIf(self.find_binding("a", "from a.d import b"))
+        self.failIf(self.find_binding("a", "from d.a import b"))
+        
+    def test_import_as(self):
+        self.failUnless(self.find_binding("a", "import b as a"))
+        self.failUnless(self.find_binding("a", "import b as a, c, a as f, d"))
+        self.failIf(self.find_binding("a", "import a as f"))
+        self.failIf(self.find_binding("a", "import b, c as f, d as e"))
+        
+    def test_from_import_as(self):
+        self.failUnless(self.find_binding("a", "from x import b as a"))
+        self.failUnless(self.find_binding("a", "from x import g as a, d as b"))
+        self.failUnless(self.find_binding("a", "from x.b import t as a"))
+        self.failUnless(self.find_binding("a", "from x.b import g as a, d"))
+        self.failIf(self.find_binding("a", "from a import b as t"))
+        self.failIf(self.find_binding("a", "from a.d import b as t"))
+        self.failIf(self.find_binding("a", "from d.a import b as t"))
+        
+    def test_function_def(self):
+        self.failUnless(self.find_binding("a", "def a(): pass"))
+        self.failUnless(self.find_binding("a", "def a(b, c, d): pass"))
+        self.failUnless(self.find_binding("a", "def a(): b = 7"))
+        self.failIf(self.find_binding("a", "def d(b, (c, a), e): pass"))
+        self.failIf(self.find_binding("a", "def d(a=7): pass"))
+        self.failIf(self.find_binding("a", "def d(a): pass"))
+        self.failIf(self.find_binding("a", "def d(): a = 7"))
+        
+    def test_class_def(self):
+        self.failUnless(self.find_binding("a", "class a: pass"))
+        self.failUnless(self.find_binding("a", "class a(): pass"))
+        self.failUnless(self.find_binding("a", "class a(b): pass"))
+        self.failUnless(self.find_binding("a", "class a(b, c=8): pass"))
+        self.failIf(self.find_binding("a", "class d: pass"))
+        self.failIf(self.find_binding("a", "class d(a): pass"))
+        self.failIf(self.find_binding("a", "class d(b, a=7): pass"))
+        self.failIf(self.find_binding("a", "class d(b, *a): pass"))
+        self.failIf(self.find_binding("a", "class d(b, **a): pass"))
+        self.failIf(self.find_binding("a", "class d: a = 7"))
+        
+    def test_for(self):
+        self.failUnless(self.find_binding("a", "for a in r: pass"))
+        self.failUnless(self.find_binding("a", "for a, b in r: pass"))
+        self.failUnless(self.find_binding("a", "for (a, b) in r: pass"))
+        self.failUnless(self.find_binding("a", "for c, (a,) in r: pass"))
+        self.failUnless(self.find_binding("a", "for c, (a, b) in r: pass"))
+        self.failUnless(self.find_binding("a", "for c in r: a = c"))
+        
 
 if __name__ == "__main__":
     import __main__


More information about the Python-checkins mailing list