[1.5, 2.0] parser.tuple2ast error on **kw arguments

Jeff Epler jepler at inetnebr.com
Mon Dec 11 20:41:06 CET 2000


This looks like a bug in the tuple2ast function of the parser module
in 1.5.2 and 2.0.  I discovered this bug while extending the Python
2.0 test_parser module with more test cases.  Unfortunately, I haven't
yet investigated the bug further, nor produced a patch.

Python 1.5.2 (#1, Sep 17 1999, 20:15:36)  [GCC egcs-2.91.66 19990314/Linux (egcs- on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import parser
>>> parser.tuple2ast(parser.expr("lambda x, *y, **z: 0").totuple())
Traceback (innermost last):
  File "<stdin>", line 1, in ?
parser.ParserError: Expected node type 16, got 36.
>>> lambda x, *y, **z: 0
<function <lambda> at 80c6858>

Python 2.0 (#1, Dec 10 2000, 12:10:38)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import parser
>>> parser.tuple2ast(parser.expr("lambda x, *y, **z: 0").totuple())
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
parser.ParserError: Expected node type 262, got 16.
>>> lambda x, *y, **z: 0
<function <lambda> at 0x81b7004>

PS I enclose my extended list of tests.  Some are commented out
because I am currently testing my (spark-derived) parser against
the 1.5.2 subset.  I hope that a larger parser test suite might be
included in a future release of Python, perhaps drawing on these
test cases.
PPS My revised roundtrip() function, not included, compares the output
of my parser to the totuple() form of the AST produced by Python itself,
thus ensuring a high degree of confidence in the compatibility of the
two.


print "Expressions:"

test_expr('r"hi"')
test_expr("'hi'")
test_expr("'hi' 'there'")
test_expr("''' 'hi' 'there' '''")
test_expr('"hi"')
test_expr('"hi" "there"')
test_expr('""" "hi" "there" """')
test_expr("foo(1)")
test_expr("foo(1,)")
test_expr("foo(1, 2)")
test_expr("foo(1, 2,)")
test_expr("foo(1, a=2)")
test_expr("foo(1, a=2,)")
test_expr("foo(1, a=2, b=2,)")

test_expr("(1, 2, 3)")
test_expr("[1, 2, 3]")
#test_expr("[x**3 for x in range(20)]")
#test_expr("[x**3 for x in range(20) if x % 3]")
#test_expr("foo(*args)")
#test_expr("foo(*args, **kw)")
#test_expr("foo(**kw)")
test_expr("foo(key=value)")
#test_expr("foo(key=value, *args)")
#test_expr("foo(key=value, *args, **kw)")
#test_expr("foo(key=value, **kw)")
#test_expr("foo(a, b, c, *args)")
#test_expr("foo(a, b, c, *args, **kw)")
#test_expr("foo(a, b, c, **kw)")
test_expr("foo + bar")
test_expr("foo - bar - baz")
test_expr("foo * bar")
test_expr("foo * bar / baz")
test_expr("foo ^ bar")
test_expr("foo ^ bar ^ baz")
test_expr("foo | bar")
test_expr("foo | bar | baz")
test_expr("foo & bar")
test_expr("foo & bar & baz")
test_expr("foo > bar > baz")
test_expr("foo < bar")
test_expr("+foo")
test_expr("-foo")
test_expr("not foo")
test_expr("foo not in bar")
test_expr("foo in bar")
test_expr("foo is bar")
test_expr("foo is not bar")
test_expr("x[0]")
test_expr("x[0:]")
test_expr("x[:0]")
test_expr("x[:]")
test_expr("x[0:0]")
test_expr("x[0:0:0]")
test_expr("x[:0:0]")
test_expr("x[0::0]")
test_expr("x[0:0:]")
test_expr("x[::0]")
test_expr("x[0::]")
test_expr("x[:0:]")
test_expr("x[::]")
test_expr("lambda: 1")
test_expr("lambda x: 1")
test_expr("lambda x,: 1")
test_expr("lambda x,y: 1")
test_expr("lambda x,y,: 1")
test_expr("lambda x=1: 1")
test_expr("lambda x=1,: 1")
test_expr("lambda x=1,y=2: 1")
test_expr("lambda x=1,y=2,: 1")
test_expr("lambda x,y=2: 1")
test_expr("lambda x,y=2,: 1")
test_expr("lambda x,y,: 1")
test_expr("lambda x,*y: 1")
test_expr("lambda x,**y: 1")
#test_expr("lambda x,*y,* *z: 1")  # Fails in 1.5.2 due to bug in ast2tuple
#test_expr("lambda x,*y,**z: 1")  # Fails in 1.5.2 due to bug in ast2tuple

print
print "Statements:"
test_suite("return")
test_suite("return 1")
test_suite("return 1,2")
test_suite("raise")
test_suite("raise a")
test_suite("raise a, b")
test_suite("raise a, b, c")
test_suite("del x")
test_suite("del x[0]")
test_suite("del x[:]")
test_suite("x=0")
test_suite("x[0]=0")
test_suite("x[:]=0")
test_suite("import x")
test_suite("import x.y")
test_suite("import z, x")
test_suite("import x.y, x.z")
test_suite("from x import z")
test_suite("from x import z, a")
test_suite("from x.y import z")
test_suite("global a")
test_suite("global a")
test_suite("global a,b")
test_suite("global a,b")
test_suite("pass")
test_suite("print")
test_suite("print 1")
test_suite("print 1,")
test_suite("print 1, 2")
test_suite("if 1: pass")
test_suite("if 1:\n\tpass\n")
test_suite("if 1:\n\tpass\n\tpass\n")
test_suite("for i in (): pass")
test_suite("for i in ():\n pass\nelse:\n pass\n")
test_suite("while 1:\n pass\nelse:\n pass\n")
test_suite("while 1:\n continue\nelse:\n pass\n")
test_suite("while 1:\n break\nelse:\n pass\n")
test_suite("try: pass\nexcept: pass\n")
test_suite("try: pass\nfinally: pass\n")
test_suite("try: pass\nexcept a, b: pass\n")
test_suite("try: pass\nexcept a: pass\n")
test_suite("def foo(): pass")
test_suite("def foo(a): pass")
test_suite("def foo(a,): pass")
test_suite("def foo(a, b): pass")
test_suite("def foo(a, b,): pass")
test_suite("def foo(a, b=2): pass")
test_suite("def foo(a, b=2,): pass")
test_suite("def foo(a, *c): pass")
test_suite("def foo(a, **d): pass")
#test_suite("def foo(a, *c, **d): pass") # Fails in 1.5.2 due to parser error
#test_suite("def foo(a, *c, * *d): pass") # Fails in 1.5.2 due to parser error
test_suite("class c: pass")
test_suite("class c(a): pass")
test_suite("class c(a,): pass")
test_suite("class c(a,b): pass")
test_suite("class c(a,b,): pass")
#test_suite("print >>fp")
#test_suite("print >>fp, 1")
#test_suite("print >>fp, 1,")
test_suite("assert 1")
test_suite("assert 1, 1")
# expr_stmt
test_suite("a")
test_suite("a = b")
test_suite("a = b = c = d = e")
test_suite("a, b = b, a")
#test_suite("a += b")
#test_suite("a -= b")
#test_suite("a *= b")
#test_suite("a /= b")
#test_suite("a %= b")
#test_suite("a &= b")
#test_suite("a |= b")
#test_suite("a ^= b")
#test_suite("a <<= b")
#test_suite("a >>= b")
#test_suite("a **= b")





More information about the Python-list mailing list