[ python-Bugs-1466641 ] Bogus SyntaxError in listcomp

SourceForge.net noreply at sourceforge.net
Mon Apr 10 02:43:54 CEST 2006


Bugs item #1466641, was opened at 2006-04-08 00:51
Message generated for change (Comment added) made by twouters
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: 7
Submitted By: Tim Peters (tim_one)
>Assigned to: Thomas Wouters (twouters)
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: Thomas Wouters (twouters)
Date: 2006-04-10 02:43

Message:
Logged In: YES 
user_id=34209

Yeah, this is definately caused by the PEP 308 patch, so I
guess the error is mine (but don't tell anyone.) Unless
someone beats me to it, I'll fix it and add a test tomorrow
(but please don't fix it without checking in some tests.)


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

Comment By: Georg Brandl (gbrandl)
Date: 2006-04-09 16: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 16: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 06: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 05: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