This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Bogus SyntaxError in listcomp
Type: Stage:
Components: Interpreter Core Versions: Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: twouters Nosy List: georg.brandl, ncoghlan, tim.peters, twouters
Priority: high Keywords:

Created on 2006-04-07 22:51 by tim.peters, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
syn.py tim.peters, 2006-04-07 22:51 failing source code
Messages (7)
msg28146 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2006-04-07 22:51
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]
msg28147 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2006-04-09 03:32
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". . .
msg28148 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2006-04-09 04:07
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!).
msg28149 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-04-09 14:50
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.
msg28150 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-04-09 14:57
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.
msg28151 - (view) Author: Thomas Wouters (twouters) * (Python committer) Date: 2006-04-10 00:43
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.)
msg28152 - (view) Author: Thomas Wouters (twouters) * (Python committer) Date: 2006-04-12 00:08
Logged In: YES 
user_id=34209

Fixed in trunk, revision 45286.
History
Date User Action Args
2022-04-11 14:56:16adminsetgithub: 43177
2006-04-07 22:51:23tim.peterscreate