[ python-Bugs-1466641 ] Bogus SyntaxError in listcomp

SourceForge.net noreply at sourceforge.net
Sun Apr 9 16:57:54 CEST 2006


Bugs item #1466641, was opened at 2006-04-07 22:51
Message generated for change (Comment added) made by gbrandl
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1466641&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Parser/Compiler
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Submitted By: Tim Peters (tim_one)
Assigned to: Nobody/Anonymous (nobody)
Summary: Bogus SyntaxError in listcomp

Initial Comment:
The attached syn.py gives a SyntaxError in 2.5a1 and
trunk.  Works fine in earlier Pythons.  Whittled down
from real-life Zope3 source.

def d(dir):
    return [fn
            for fn in os.listdir(dir)
            if fn
            if fn]


----------------------------------------------------------------------

>Comment By: Georg Brandl (gbrandl)
Date: 2006-04-09 14:57

Message:
Logged In: YES 
user_id=849994

Changing

list_if: 'if' test [list_iter]

to

list_if: 'if' old_test [list_iter]

solves the problem. Conditionals must then be enclosed in
parens.

Generator expressions are also affected.

----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2006-04-09 14:50

Message:
Logged In: YES 
user_id=849994

Haha. The second "if" is seen as the beginning of a
conditional expression, so

return [fn for fn in os.listdir(dir) if fn if fn else fn]

works.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2006-04-09 04:07

Message:
Logged In: YES 
user_id=31435

The whittled-down version looks ridiculous, but the original
wasn't quite such an affront to beauty :-)  It's really no
stranger than allowing pure "if" statements to nest, and it
would be more painful to contort the grammar to disallow it
(I haven't looked at the 2.5 parser, but it was very
surprising to me that it didn't allow it!).

----------------------------------------------------------------------

Comment By: Nick Coghlan (ncoghlan)
Date: 2006-04-09 03:32

Message:
Logged In: YES 
user_id=1038590

Is including two if clauses with a single for clause really
meant to be legal?

*goes and looks at language reference*

Wow. What a strange way to write "and". . .

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1466641&group_id=5470


More information about the Python-bugs-list mailing list