A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-checkins/2007-July/061010.html below:

[Python-checkins] r56201 - in sandbox/trunk/2to3: fixes/fix_dict.py fixes/fix_raise.py tests/test_fixers.py

[Python-checkins] r56201 - in sandbox/trunk/2to3: fixes/fix_dict.py fixes/fix_raise.py tests/test_fixers.pycollin.winter python-checkins at python.org
Sun Jul 8 23:21:49 CEST 2007
Author: collin.winter
Date: Sun Jul  8 23:21:49 2007
New Revision: 56201

Modified:
   sandbox/trunk/2to3/fixes/fix_dict.py
   sandbox/trunk/2to3/fixes/fix_raise.py
   sandbox/trunk/2to3/tests/test_fixers.py
Log:
Add a whole slew of prefix-related tests; fix two prefix-related bugs in the dict and raise fixers; minor test suite cleanup.

Modified: sandbox/trunk/2to3/fixes/fix_dict.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_dict.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_dict.py	Sun Jul  8 23:21:49 2007
@@ -27,14 +27,14 @@
 import patcomp
 from pgen2 import token
 from fixes import basefix
-from fixes.util import Name, Call, LParen, RParen
+from fixes.util import Name, Call, LParen, RParen, ArgList
 
 class FixDict(basefix.BaseFix):
     PATTERN = """
     power< head=any+
          trailer< '.' method=('keys'|'items'|'values'|
                               'iterkeys'|'iteritems'|'itervalues') >
-         trailer< '(' ')' >
+         parens=trailer< '(' ')' >
          tail=any*
     >
     """
@@ -42,20 +42,22 @@
     def transform(self, node):
         results = self.match(node)
         head = results["head"]
-        method = results["method"][0].value # Extract method name
+        method = results["method"][0] # Extract node for method name
         tail = results["tail"]
         syms = self.syms
-        isiter = method.startswith("iter")
+        method_name = method.value
+        isiter = method_name.startswith("iter")
         if isiter:
-            method = method[4:]
-        assert method in ("keys", "items", "values"), repr(method)
+            method_name = method_name[4:]
+        assert method_name in ("keys", "items", "values"), repr(method)
         head = [n.clone() for n in head]
         tail = [n.clone() for n in tail]
         special = not tail and self.in_special_context(node, isiter)
         args = head + [pytree.Node(syms.trailer,
                                    [pytree.Leaf(token.DOT, '.'),
-                                    Name(method)]),
-                       pytree.Node(syms.trailer, [LParen(), RParen()])]
+                                    Name(method_name,
+                                         prefix=method.get_prefix())]),
+                       results["parens"].clone()]
         new = pytree.Node(syms.power, args)
         if not special:
             new.set_prefix("")

Modified: sandbox/trunk/2to3/fixes/fix_raise.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_raise.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_raise.py	Sun Jul  8 23:21:49 2007
@@ -37,7 +37,7 @@
         syms = self.syms
         results = self.match(node)
         assert results
-        
+
         exc = results["exc"].clone()
         if exc.type is token.STRING:
             self.cannot_convert(node, "Python 3 does not support string exceptions")
@@ -49,18 +49,19 @@
         #  raise E1, V
         # Since Python 3 will not support this, we recurse down any tuple
         # literals, always taking the first element.
-        while is_tuple(exc):
-            # exc.children[1:-1] is the unparenthesized tuple
-            # exc.children[1].children[0] is the first element of the tuple
-            exc = exc.children[1].children[0].clone()
-        exc.set_prefix(" ")
+        if is_tuple(exc):
+          while is_tuple(exc):
+              # exc.children[1:-1] is the unparenthesized tuple
+              # exc.children[1].children[0] is the first element of the tuple
+              exc = exc.children[1].children[0].clone()
+          exc.set_prefix(" ")
 
         if "val" not in results:
             # One-argument raise
             new = pytree.Node(syms.raise_stmt, [Name("raise"), exc])
             new.set_prefix(node.get_prefix())
             return new
-        
+
         val = results["val"].clone()
         if is_tuple(val):
             args = [c.clone() for c in val.children[1:-1]]
@@ -71,7 +72,7 @@
         if "tb" in results:
             tb = results["tb"].clone()
             tb.set_prefix("")
-            
+
             e = Call(exc, args)
             with_tb = Attr(e, Name('with_traceback')) + [ArgList([tb])]
             new = pytree.Node(syms.simple_stmt, [Name("raise")] + with_tb)

Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py	(original)
+++ sandbox/trunk/2to3/tests/test_fixers.py	Sun Jul  8 23:21:49 2007
@@ -277,49 +277,60 @@
 class Test_intern(FixerTestCase):
     fixer = "intern"
 
-    def test_1(self):
-        b = """x = intern(a)"""
-        a = """x = sys.intern(a)"""
+    def test_prefix_preservation(self):
+        b = """x =   intern(  a  )"""
+        a = """x =   sys.intern(  a  )"""
         self.check(b, a)
 
-    def test_2(self):
         b = """y = intern("b" # test
               )"""
         a = """y = sys.intern("b" # test
               )"""
         self.check(b, a)
 
-    def test_3(self):
+        b = """z = intern(a+b+c.d,   )"""
+        a = """z = sys.intern(a+b+c.d,   )"""
+        self.check(b, a)
+
+    def test(self):
+        b = """x = intern(a)"""
+        a = """x = sys.intern(a)"""
+        self.check(b, a)
+
         b = """z = intern(a+b+c.d,)"""
         a = """z = sys.intern(a+b+c.d,)"""
         self.check(b, a)
 
-    def test_4(self):
         b = """intern("y%s" % 5).replace("y", "")"""
         a = """sys.intern("y%s" % 5).replace("y", "")"""
         self.check(b, a)
 
     # These should not be refactored
 
-    def test_unchanged_1(self):
+    def test_unchanged(self):
         s = """intern(a=1)"""
         self.check(s, s)
 
-    def test_unchanged_2(self):
         s = """intern(f, g)"""
         self.check(s, s)
 
-    def test_unchanged_3(self):
         s = """intern(*h)"""
         self.check(s, s)
 
-    def test_unchanged_4(self):
         s = """intern(**i)"""
         self.check(s, s)
 
+        s = """intern()"""
+        self.check(s, s)
+
 class Test_print(FixerTestCase):
     fixer = "print"
 
+    def test_prefix_preservation(self):
+        b = """print 1,   1+1,   1+1+1"""
+        a = """print(1,   1+1,   1+1+1)"""
+        self.check(b, a)
+
     def test_1(self):
         b = """print 1, 1+1, 1+1+1"""
         a = """print(1, 1+1, 1+1+1)"""
@@ -378,6 +389,11 @@
 class Test_exec(FixerTestCase):
     fixer = "exec"
 
+    def test_prefix_preservation(self):
+        b = """  exec code in ns1,   ns2"""
+        a = """  exec(code, ns1,   ns2)"""
+        self.check(b, a)
+
     def test_basic(self):
         b = """exec code"""
         a = """exec(code)"""
@@ -425,6 +441,11 @@
 class Test_repr(FixerTestCase):
     fixer = "repr"
 
+    def test_prefix_preservation(self):
+        b = """x =   `1 + 2`"""
+        a = """x =   repr(1 + 2)"""
+        self.check(b, a)
+
     def test_simple_1(self):
         b = """x = `1 + 2`"""
         a = """x = repr(1 + 2)"""
@@ -458,6 +479,19 @@
 class Test_except(FixerTestCase):
     fixer = "except"
 
+    def test_prefix_preservation(self):
+        b = """
+            try:
+                pass
+            except (RuntimeError, ImportError),    e:
+                pass"""
+        a = """
+            try:
+                pass
+            except (RuntimeError, ImportError) as    e:
+                pass"""
+        self.check(b, a)
+
     def test_tuple_unpack(self):
         b = """
             def foo():
@@ -606,11 +640,30 @@
         a = """raise Exception(5)"""
         self.check(b, a)
 
-    def test_prefix(self):
+    def test_prefix_preservation(self):
         b = """raise Exception,5"""
         a = """raise Exception(5)"""
         self.check(b, a)
 
+        b = """raise   Exception,    5"""
+        a = """raise   Exception(5)"""
+        self.check(b, a)
+
+    def test_with_comments(self):
+        b = """raise Exception, 5 # foo"""
+        a = """raise Exception(5) # foo"""
+        self.check(b, a)
+
+        b = """raise E, (5, 6) % (a, b) # foo"""
+        a = """raise E((5, 6) % (a, b)) # foo"""
+        self.check(b, a)
+
+        b = """def foo():
+                    raise Exception, 5, 6 # foo"""
+        a = """def foo():
+                    raise Exception(5).with_traceback(6) # foo"""
+        self.check(b, a)
+
     def test_tuple_value(self):
         b = """raise Exception, (5, 6, 7)"""
         a = """raise Exception(5, 6, 7)"""
@@ -859,36 +912,34 @@
         a = """b = 0x12"""
         self.check(b, a)
 
-    # These should not be touched
-
-    def test_6(self):
+    def test_unchanged_1(self):
         b = """a = 12"""
-        a = """a = 12"""
-        self.check(b, a)
+        self.check(b, b)
 
-    def test_7(self):
+    def test_unchanged_2(self):
         b = """b = 0x12"""
-        a = """b = 0x12"""
-        self.check(b, a)
+        self.check(b, b)
 
-    def test_8(self):
+    def test_unchanged_3(self):
         b = """c = 3.14"""
-        a = """c = 3.14"""
+        self.check(b, b)
+
+    def test_prefix_preservation(self):
+        b = """x =   long(  x  )"""
+        a = """x =   int(  x  )"""
         self.check(b, a)
 
 
 class Test_sysexcattrs(FixerTestCase):
     fixer = "sysexcattrs"
 
-    def test_1(self):
+    def test(self):
         s = """f = sys.exc_type"""
         self.warns(s, s, "This attribute is going away")
 
-    def test_2(self):
         s = """f = sys.exc_value"""
         self.warns(s, s, "This attribute is going away")
 
-    def test_3(self):
         s = """f = sys.exc_traceback"""
         self.warns(s, s, "This attribute is going away")
 
@@ -896,12 +947,47 @@
 class Test_dict(FixerTestCase):
     fixer = "dict"
 
+    def test_prefix_preservation(self):
+        b = "if   d. keys  (  )  : pass"
+        a = "if   list(d. keys  (  ))  : pass"
+        self.check(b, a)
+
+        b = "if   d. items  (  )  : pass"
+        a = "if   list(d. items  (  ))  : pass"
+        self.check(b, a)
+
+        b = "if   d. iterkeys  ( )  : pass"
+        a = "if   iter(d. keys  ( ))  : pass"
+        self.check(b, a)
+
+        b = "[i for i in    d.  iterkeys(  )  ]"
+        a = "[i for i in    d.  keys(  )  ]"
+        self.check(b, a)
+
+    def test_trailing_comment(self):
+        b = "d.keys() # foo"
+        a = "list(d.keys()) # foo"
+        self.check(b, a)
+
+        b = "d.items()  # foo"
+        a = "list(d.items())  # foo"
+        self.check(b, a)
+
+        b = "d.iterkeys()  # foo"
+        a = "iter(d.keys())  # foo"
+        self.check(b, a)
+
+        b = """[i for i in d.iterkeys() # foo
+               ]"""
+        a = """[i for i in d.keys() # foo
+               ]"""
+        self.check(b, a)
+
     def test_01(self):
         b = "d.keys()"
         a = "list(d.keys())"
         self.check(b, a)
 
-    def test_01a(self):
         b = "a[0].foo().keys()"
         a = "list(a[0].foo().keys())"
         self.check(b, a)
@@ -933,13 +1019,11 @@
 
     def test_07(self):
         b = "list(d.keys())"
-        a = b
-        self.check(b, a)
+        self.check(b, b)
 
     def test_08(self):
         b = "sorted(d.keys())"
-        a = b
-        self.check(b, a)
+        self.check(b, b)
 
     def test_09(self):
         b = "iter(d.keys())"
@@ -1024,6 +1108,19 @@
 class Test_xrange(FixerTestCase):
     fixer = "xrange"
 
+    def test_prefix_preservation(self):
+        b = """x =    xrange(  10  )"""
+        a = """x =    range(  10  )"""
+        self.check(b, a)
+
+        b = """x = xrange(  1  ,  10   )"""
+        a = """x = range(  1  ,  10   )"""
+        self.check(b, a)
+
+        b = """x = xrange(  0  ,  10 ,  2 )"""
+        a = """x = range(  0  ,  10 ,  2 )"""
+        self.check(b, a)
+
     def test_1(self):
         b = """x = xrange(10)"""
         a = """x = range(10)"""
@@ -1048,6 +1145,15 @@
 class Test_raw_input(FixerTestCase):
     fixer = "raw_input"
 
+    def test_prefix_preservation(self):
+        b = """x =    raw_input(   )"""
+        a = """x =    input(   )"""
+        self.check(b, a)
+
+        b = """x = raw_input(   ''   )"""
+        a = """x = input(   ''   )"""
+        self.check(b, a)
+
     def test_1(self):
         b = """x = raw_input()"""
         a = """x = input()"""
@@ -1067,6 +1173,20 @@
 class Test_input(FixerTestCase):
     fixer = "input"
 
+    def test_prefix_preservation(self):
+        b = """x =   input(   )"""
+        a = """x =   eval(input(   ))"""
+        self.check(b, a)
+
+        b = """x = input(   ''   )"""
+        a = """x = eval(input(   ''   ))"""
+        self.check(b, a)
+
+    def test_trailing_comment(self):
+        b = """x = input()  #  foo"""
+        a = """x = eval(input())  #  foo"""
+        self.check(b, a)
+
     def test_1(self):
         b = """x = input()"""
         a = """x = eval(input())"""
@@ -1837,6 +1957,15 @@
 class Test_callable(FixerTestCase):
     fixer = "callable"
 
+    def test_prefix_preservation(self):
+        b = """callable(    x)"""
+        a = """hasattr(    x, '__call__')"""
+        self.check(b, a)
+
+        b = """if     callable(x): pass"""
+        a = """if     hasattr(x, '__call__'): pass"""
+        self.check(b, a)
+
     def test_callable_call(self):
         b = """callable(x)"""
         a = """hasattr(x, '__call__')"""
@@ -1852,9 +1981,17 @@
         a = """callable(x, kw=y)"""
         self.check(a, a)
 
+        a = """callable()"""
+        self.check(a, a)
+
 class Test_filter(FixerTestCase):
     fixer = "filter"
 
+    def test_prefix_preservation(self):
+        b = """x =   filter(    None,     'abc'   )"""
+        a = """x =   list(filter(    None,     'abc'   ))"""
+        self.check(b, a)
+
     def test_filter_basic(self):
         b = """x = filter(None, 'abc')"""
         a = """x = list(filter(None, 'abc'))"""
@@ -1898,6 +2035,16 @@
 class Test_map(FixerTestCase):
     fixer = "map"
 
+    def test_prefix_preservation(self):
+        b = """x =    map(   f,    'abc'   )"""
+        a = """x =    list(map(   f,    'abc'   ))"""
+        self.check(b, a)
+
+    def test_trailing_comment(self):
+        b = """x = map(f, 'abc')   #   foo"""
+        a = """x = list(map(f, 'abc'))   #   foo"""
+        self.check(b, a)
+
     def test_map_basic(self):
         b = """x = map(f, 'abc')"""
         a = """x = list(map(f, 'abc'))"""
More information about the Python-checkins mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4