[Python-checkins] r56386 - in sandbox/trunk/2to3: Grammar.txt fixes/fix_dict.py fixes/fix_filter.py fixes/fix_map.py fixes/util.py tests/data/py3_test_grammar.py

collin.winter python-checkins at python.org
Sun Jul 15 00:33:47 CEST 2007


Author: collin.winter
Date: Sun Jul 15 00:33:47 2007
New Revision: 56386

Modified:
   sandbox/trunk/2to3/   (props changed)
   sandbox/trunk/2to3/Grammar.txt
   sandbox/trunk/2to3/fixes/fix_dict.py
   sandbox/trunk/2to3/fixes/fix_filter.py
   sandbox/trunk/2to3/fixes/fix_map.py
   sandbox/trunk/2to3/fixes/util.py
   sandbox/trunk/2to3/tests/data/py3_test_grammar.py
Log:
Sync grammar with p3yk branch, update some fixers accordingly.


Modified: sandbox/trunk/2to3/Grammar.txt
==============================================================================
--- sandbox/trunk/2to3/Grammar.txt	(original)
+++ sandbox/trunk/2to3/Grammar.txt	Sun Jul 15 00:33:47 2007
@@ -124,8 +124,8 @@
        '{' [dictsetmaker] '}' |
        '`' testlist1 '`' |
        NAME | NUMBER | STRING+ | '.' '.' '.')
-listmaker: test ( list_for | (',' test)* [','] )
-testlist_gexp: test ( gen_for | (',' test)* [','] )
+listmaker: test ( comp_for | (',' test)* [','] )
+testlist_gexp: test ( comp_for | (',' test)* [','] )
 lambdef: 'lambda' [varargslist] ':' test
 trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
 subscriptlist: subscript (',' subscript)* [',']
@@ -133,20 +133,17 @@
 sliceop: ':' [test]
 exprlist: expr (',' expr)* [',']
 testlist: test (',' test)* [',']
-dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
+dictsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
+                (test (comp_for | (',' test)* [','])) )
 
 classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
 
 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | '**' test)
-argument: test [gen_for] | test '=' test  # Really [keyword '='] test
+argument: test [comp_for] | test '=' test  # Really [keyword '='] test
 
-list_iter: list_for | list_if
-list_for: 'for' exprlist 'in' testlist_safe [list_iter]
-list_if: 'if' old_test [list_iter]
-
-gen_iter: gen_for | gen_if
-gen_for: 'for' exprlist 'in' or_test [gen_iter]
-gen_if: 'if' old_test [gen_iter]
+comp_iter: comp_for | comp_if
+comp_for: 'for' exprlist 'in' testlist_safe [comp_iter]
+comp_if: 'if' old_test [comp_iter]
 
 testlist1: test (',' test)*
 

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 15 00:33:47 2007
@@ -70,8 +70,7 @@
     p1 = patcomp.compile_pattern(P1)
 
     P2 = """for_stmt< 'for' any 'in' node=any ':' any* >
-            | list_for< 'for' any 'in' node=any any* >
-            | gen_for< 'for' any 'in' node=any any* >
+            | comp_for< 'for' any 'in' node=any any* >
          """
     p2 = patcomp.compile_pattern(P2)
 

Modified: sandbox/trunk/2to3/fixes/fix_filter.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_filter.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_filter.py	Sun Jul 15 00:33:47 2007
@@ -58,8 +58,7 @@
         return new
 
     P0 = """for_stmt< 'for' any 'in' node=any ':' any* >
-            | list_for< 'for' any 'in' node=any any* >
-            | gen_for< 'for' any 'in' node=any any* >
+            | comp_for< 'for' any 'in' node=any any* >
          """
     p0 = patcomp.compile_pattern(P0)
 

Modified: sandbox/trunk/2to3/fixes/fix_map.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_map.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_map.py	Sun Jul 15 00:33:47 2007
@@ -70,8 +70,7 @@
         return new
 
     P0 = """for_stmt< 'for' any 'in' node=any ':' any* >
-            | list_for< 'for' any 'in' node=any any* >
-            | gen_for< 'for' any 'in' node=any any* >
+            | comp_for< 'for' any 'in' node=any any* >
          """
     p0 = patcomp.compile_pattern(P0)
 

Modified: sandbox/trunk/2to3/fixes/util.py
==============================================================================
--- sandbox/trunk/2to3/fixes/util.py	(original)
+++ sandbox/trunk/2to3/fixes/util.py	Sun Jul 15 00:33:47 2007
@@ -96,8 +96,8 @@
         test.set_prefix(" ")
         if_leaf = Leaf(token.NAME, "if")
         if_leaf.set_prefix(" ")
-        inner_args.append(Node(syms.list_if, [if_leaf, test]))
-    inner = Node(syms.listmaker, [xp, Node(syms.list_for, inner_args)])
+        inner_args.append(Node(syms.comp_if, [if_leaf, test]))
+    inner = Node(syms.listmaker, [xp, Node(syms.comp_for, inner_args)])
     return Node(syms.atom,
                        [Leaf(token.LBRACE, "["),
                         inner,

Modified: sandbox/trunk/2to3/tests/data/py3_test_grammar.py
==============================================================================
--- sandbox/trunk/2to3/tests/data/py3_test_grammar.py	(original)
+++ sandbox/trunk/2to3/tests/data/py3_test_grammar.py	Sun Jul 15 00:33:47 2007
@@ -1,5 +1,3 @@
-# Python 3's Lib/test/test_grammar.py (r53781)
-
 # Python test set -- part 1, grammar.
 # This just tests whether the parser accepts them all.
 
@@ -16,7 +14,6 @@
 # testing import *
 from sys import *
 
- at class_decorator
 class TokenTests(unittest.TestCase):
 
     def testBackslash(self):
@@ -30,26 +27,32 @@
         self.assertEquals(x, 0, 'backslash ending comment')
 
     def testPlainIntegers(self):
+        self.assertEquals(type(000), type(0))
         self.assertEquals(0xff, 255)
-        self.assertEquals(0377, 255)
-        self.assertEquals(2147483647, 017777777777)
+        self.assertEquals(0o377, 255)
+        self.assertEquals(2147483647, 0o17777777777)
+        self.assertEquals(0b1001, 9)
         from sys import maxint
         if maxint == 2147483647:
-            self.assertEquals(-2147483647-1, -020000000000)
+            self.assertEquals(-2147483647-1, -0o20000000000)
             # XXX -2147483648
-            self.assert_(037777777777 > 0)
+            self.assert_(0o37777777777 > 0)
             self.assert_(0xffffffff > 0)
-            for s in '2147483648', '040000000000', '0x100000000':
+            self.assert_(0b1111111111111111111111111111111 > 0)
+            for s in ('2147483648', '0o40000000000', '0x100000000',
+                      '0b10000000000000000000000000000000'):
                 try:
                     x = eval(s)
                 except OverflowError:
                     self.fail("OverflowError on huge integer literal %r" % s)
         elif maxint == 9223372036854775807:
-            self.assertEquals(-9223372036854775807-1, -01000000000000000000000)
-            self.assert_(01777777777777777777777 > 0)
+            self.assertEquals(-9223372036854775807-1, -0o1000000000000000000000)
+            self.assert_(0o1777777777777777777777 > 0)
             self.assert_(0xffffffffffffffff > 0)
-            for s in '9223372036854775808', '02000000000000000000000', \
-                     '0x10000000000000000':
+            self.assert_(0b11111111111111111111111111111111111111111111111111111111111111 > 0)
+            for s in '9223372036854775808', '0o2000000000000000000000', \
+                     '0x10000000000000000', \
+                     '0b100000000000000000000000000000000000000000000000000000000000000':
                 try:
                     x = eval(s)
                 except OverflowError:
@@ -59,13 +62,13 @@
 
     def testLongIntegers(self):
         x = 0
-        x = 0
-        x = 0xffffffffffffffff
         x = 0xffffffffffffffff
-        x = 077777777777777777
-        x = 077777777777777777
-        x = 123456789012345678901234567890
+        x = 0Xffffffffffffffff
+        x = 0o77777777777777777
+        x = 0O77777777777777777
         x = 123456789012345678901234567890
+        x = 0b100000000000000000000000000000000000000000000000000000000000000000000
+        x = 0B111111111111111111111111111111111111111111111111111111111111111111111
 
     def testFloats(self):
         x = 3.14
@@ -124,6 +127,7 @@
     def testEllipsis(self):
         x = ...
         self.assert_(x is Ellipsis)
+        self.assertRaises(SyntaxError, eval, ".. .")
 
 class GrammarTests(unittest.TestCase):
 
@@ -146,51 +150,32 @@
         ### decorators: decorator+
         ### parameters: '(' [typedargslist] ')'
         ### typedargslist: ((tfpdef ['=' test] ',')*
-        ###                ('*' [tname] (',' tname ['=' test])* [',' '**' tname] | '**' tname)
+        ###                ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
         ###                | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
-        ### tname: NAME [':' test]
-        ### tfpdef: tname | '(' tfplist ')'
-        ### tfplist: tfpdef (',' tfpdef)* [',']
+        ### tfpdef: NAME [':' test]
         ### varargslist: ((vfpdef ['=' test] ',')*
-        ###              ('*' [vname] (',' vname ['=' test])*  [',' '**' vname] | '**' vname)
+        ###              ('*' [vfpdef] (',' vfpdef ['=' test])*  [',' '**' vfpdef] | '**' vfpdef)
         ###              | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
-        ### vname: NAME
-        ### vfpdef: vname | '(' vfplist ')'
-        ### vfplist: vfpdef (',' vfpdef)* [',']
+        ### vfpdef: NAME
         def f1(): pass
         f1()
         f1(*())
         f1(*(), **{})
         def f2(one_argument): pass
         def f3(two, arguments): pass
-        def f4(two, (compound, (argument, list))): pass
-        def f5((compound, first), two): pass
-        self.assertEquals(f2.func_code.co_varnames, ('one_argument',))
-        self.assertEquals(f3.func_code.co_varnames, ('two', 'arguments'))
-        if sys.platform.startswith('java'):
-            self.assertEquals(f4.func_code.co_varnames,
-                   ('two', '(compound, (argument, list))', 'compound', 'argument',
-                                'list',))
-            self.assertEquals(f5.func_code.co_varnames,
-                   ('(compound, first)', 'two', 'compound', 'first'))
-        else:
-            self.assertEquals(f4.func_code.co_varnames,
-                  ('two', '.1', 'compound', 'argument',  'list'))
-            self.assertEquals(f5.func_code.co_varnames,
-                  ('.0', 'two', 'compound', 'first'))
+        self.assertEquals(f2.__code__.co_varnames, ('one_argument',))
+        self.assertEquals(f3.__code__.co_varnames, ('two', 'arguments'))
         def a1(one_arg,): pass
         def a2(two, args,): pass
         def v0(*rest): pass
         def v1(a, *rest): pass
         def v2(a, b, *rest): pass
-        def v3(a, (b, c), *rest): return a, b, c, rest
 
         f1()
         f2(1)
         f2(1,)
         f3(1, 2)
         f3(1, 2,)
-        f4(1, (2, (3, 4)))
         v0()
         v0(1)
         v0(1,)
@@ -205,17 +190,7 @@
         v2(1,2,3)
         v2(1,2,3,4)
         v2(1,2,3,4,5,6,7,8,9,0)
-        v3(1,(2,3))
-        v3(1,(2,3),4)
-        v3(1,(2,3),4,5,6,7,8,9,0)
-
-        # ceval unpacks the formal arguments into the first argcount names;
-        # thus, the names nested inside tuples must appear after these names.
-        if sys.platform.startswith('java'):
-            self.assertEquals(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c'))
-        else:
-            self.assertEquals(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))
-        self.assertEquals(v3(1, (2, 3), 4), (1, 2, 3, (4,)))
+
         def d01(a=1): pass
         d01()
         d01(1)
@@ -288,10 +263,6 @@
         d22v(*(1, 2, 3, 4))
         d22v(1, 2, *(3, 4, 5))
         d22v(1, *(2, 3), **{'d': 4})
-        def d31v((x)): pass
-        d31v(1)
-        def d32v((x,)): pass
-        d32v((1,))
         # keyword only argument tests
         def pos0key1(*, key): return key
         pos0key1(key=100)
@@ -305,25 +276,37 @@
 
         # argument annotation tests
         def f(x) -> list: pass
-        self.assertEquals(f.func_annotations, {'return': list})
+        self.assertEquals(f.__annotations__, {'return': list})
         def f(x:int): pass
-        self.assertEquals(f.func_annotations, {'x': int})
+        self.assertEquals(f.__annotations__, {'x': int})
         def f(*x:str): pass
-        self.assertEquals(f.func_annotations, {'x': str})
+        self.assertEquals(f.__annotations__, {'x': str})
         def f(**x:float): pass
-        self.assertEquals(f.func_annotations, {'x': float})
+        self.assertEquals(f.__annotations__, {'x': float})
         def f(x, y:1+2): pass
-        self.assertEquals(f.func_annotations, {'y': 3})
-        def f(a, (b:1, c:2, d)): pass
-        self.assertEquals(f.func_annotations, {'b': 1, 'c': 2})
-        def f(a, (b:1, c:2, d), e:3=4, f=5, *g:6): pass
-        self.assertEquals(f.func_annotations,
+        self.assertEquals(f.__annotations__, {'y': 3})
+        def f(a, b:1, c:2, d): pass
+        self.assertEquals(f.__annotations__, {'b': 1, 'c': 2})
+        def f(a, b:1, c:2, d, e:3=4, f=5, *g:6): pass
+        self.assertEquals(f.__annotations__,
                           {'b': 1, 'c': 2, 'e': 3, 'g': 6})
-        def f(a, (b:1, c:2, d), e:3=4, f=5, *g:6, h:7, i=8, j:9=10,
+        def f(a, b:1, c:2, d, e:3=4, f=5, *g:6, h:7, i=8, j:9=10,
               **k:11) -> 12: pass
-        self.assertEquals(f.func_annotations,
+        self.assertEquals(f.__annotations__,
                           {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
                            'k': 11, 'return': 12})
+        # Check for SF Bug #1697248 - mixing decorators and a return annotation
+        def null(x): return x
+        @null
+        def f(x) -> list: pass
+        self.assertEquals(f.__annotations__, {'return': list})
+
+        # test MAKE_CLOSURE with a variety of oparg's
+        closure = 1
+        def f(): return closure
+        def f(x=1): return closure
+        def f(*, k=1): return closure
+        def f() -> int: return closure
 
     def testLambdef(self):
         ### lambdef: 'lambda' [varargslist] ':' test
@@ -354,7 +337,7 @@
             x = 1; pass; del x;
         foo()
 
-    ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt 
+    ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt
     # Tested below
 
     def testExprStmt(self):
@@ -714,6 +697,20 @@
             def meth2(self, arg): pass
             def meth3(self, a1, a2): pass
 
+        # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
+        # decorators: decorator+
+        # decorated: decorators (classdef | funcdef)
+        def class_decorator(x): return x
+        @class_decorator
+        class G: pass
+
+    def testDictcomps(self):
+        # dictorsetmaker: ( (test ':' test (comp_for |
+        #                                   (',' test ':' test)* [','])) |
+        #                   (test (comp_for | (',' test)* [','])) )
+        nums = [1, 2, 3]
+        self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
+
     def testListcomps(self):
         # list comprehension tests
         nums = [1, 2, 3, 4, 5]
@@ -780,9 +777,9 @@
     def testGenexps(self):
         # generator expression tests
         g = ([x for x in range(10)] for x in range(1))
-        self.assertEqual(g.next(), [x for x in range(10)])
+        self.assertEqual(next(g), [x for x in range(10)])
         try:
-            g.next()
+            next(g)
             self.fail('should produce StopIteration exception')
         except StopIteration:
             pass
@@ -790,7 +787,7 @@
         a = 1
         try:
             g = (a for d in a)
-            g.next()
+            next(g)
             self.fail('should produce TypeError')
         except TypeError:
             pass
@@ -838,7 +835,8 @@
             print(x)
             return ret
 
-        self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
+        # the next line is not allowed anymore
+        #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
         self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
         self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True])
         self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)


More information about the Python-checkins mailing list