[Python-Dev] problem with genexp

Nick Coghlan ncoghlan at gmail.com
Tue Oct 11 15:44:59 CEST 2005

Nick Coghlan wrote:
> Neal Norwitz wrote:
>>There's a problem with genexp's that I think really needs to get
>>fixed.  See http://python.org/sf/1167751 the details are below.  This
>>I agree with the bug report that the code should either raise a
>>SyntaxError or do the right thing.
> I agree it should be a SyntaxError - I believe the AST compiler actually 
> raises one in this situation.

I was half right. Both the normal compiler and the AST compiler give a 
SyntaxError if you write:

   foo((a=i for i in range(10)))

The problem is definitely on the parser end though:

Py> compiler.parse("foo(x=i for i in range(10))")
Module(None, Stmt([Discard(CallFunc(Name('foo'), [Keyword('x', Name('i'))], 
None, None))]))

It's getting to what looks like a valid keyword argument in "x=i" and throwing 
the rest of it away, when it should be flagging a syntax error (the parser's 
limited lookahead should still be enough to spot the erroneous 'for' keyword 
and bail out). The error will be even more obscure if there is an "i" visible 
from the location of the function call.

Whereas when it's parenthesised correctly, the parse tree looks more like this:
Py> compiler.parse("foo(x=(i for i in range(10)))")
Module(None, Stmt([Discard(CallFunc(Name('foo'), [Keyword('x', 
GenExpr(GenExprInner(Name('i'), [GenExprFor(AssName('i', 'OP_ASSIGN'), 
CallFunc(Name('range'), [Const(10)], None, None), [])])))], None, None))]))


P.S. I added a comment showing the parser output to the SF bug report.

Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list