Issue1205

classification
Title: list comprehension difference to python
Type: behaviour Severity: normal
Components: Core Versions: 2.5b0
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: fwierzbicki Nosy List: fwierzbicki, pb, pjenvey
Priority: Keywords:

Created on 2008-12-16.05:48:14 by pb, last changed 2008-12-17.03:44:13 by fwierzbicki.

Files
File name Uploaded Description Edit Remove
listcomprehension.py pb, 2008-12-16.05:48:13 sample
ramanujan.py pb, 2008-12-16.22:15:56 hardy ramanujan number
Messages
msg3931 (view) Author: pb (pb) Date: 2008-12-16.05:48:12
List comprehension seems to work differently on python 2.5.2 versus
jython2.5b0.
With python2.5 the attached program gives
---
1
[(1, (0, 1), (0, 1))]
---
whereas with jython2.5b0 it gives
---
6
[(1, (0, 1), (0, 1)), (8, (0, 2), (0, 1)), (27, (0, 3), (0, 1)), (9, (1,
2), (0, 1)), (28, (1, 3), (0, 1)), (35, (2, 3), (0, 1))]
---
obviously getting some if of computation wrong (works with jython2.2
though).
msg3932 (view) Author: Philip Jenvey (pjenvey) Date: 2008-12-16.06:09:05
An AST bug. Here's the difference between py/jy ASTs:

--- py25	2008-12-15 22:00:48.000000000 -0800
+++ jy25	2008-12-15 22:01:01.000000000 -0800
@@ -89,39 +89,7 @@
          ('left', ('Name (2,125)', ('id', 'y1'), ('ctx', ('Load',)))),
          ('ops', ('Lt',)),
          ('comparators',
-          ('Name (2,130)', ('id', 'y2'), ('ctx', ('Load',))))),
-        ('Compare (2,136)',
-         ('left',
-          ('BinOp (2,136)',
-           ('left',
-            ('BinOp (2,136)',
-             ('left',
-              ('Name (2,136)', ('id', 'x1'), ('ctx', ('Load',)))),
-             ('op', ('Pow',)),
-             ('right', ('Num (2,140)', ('n', 3))))),
-           ('op', ('Add',)),
-           ('right',
-            ('BinOp (2,142)',
-             ('left',
-              ('Name (2,142)', ('id', 'x2'), ('ctx', ('Load',)))),
-             ('op', ('Pow',)),
-             ('right', ('Num (2,146)', ('n', 3))))))),
-         ('ops', ('Eq',)),
-         ('comparators',
-          ('BinOp (2,151)',
-           ('left',
-            ('BinOp (2,151)',
-             ('left',
-              ('Name (2,151)', ('id', 'y1'), ('ctx', ('Load',)))),
-             ('op', ('Pow',)),
-             ('right', ('Num (2,155)', ('n', 3))))),
-           ('op', ('Add',)),
-           ('right',
-            ('BinOp (2,157)',
-             ('left',
-              ('Name (2,157)', ('id', 'y2'), ('ctx', ('Load',)))),
-             ('op', ('Pow',)),
-             ('right', ('Num (2,161)', ('n', 3)))))))))))))),
+          ('Name (2,130)', ('id', 'y2'), ('ctx', ('Load',))))))))))),
   ('Print (3,0)',
    ('dest', None),
    ('values',

Which means we didn't recognize the last if statement. i.e. we're 
thinking it's this (CPython gives the result of len 6 with this):

l = [(x1**3+x2**3,(x1,x2),(y1,y2)) for x1 in range(4) for x2 in range(4) 
if x1 < x2 for y1 in range(g) for y2 in range(g) if y1 < y2]
msg3934 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2008-12-16.17:18:07
I have the example down to:

[a for b in c if d < e if f > g ]

Looks like multiple compares are not being handled correctly in Jython.
msg3935 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2008-12-16.17:31:14
It's even worse than I thought -- only one if clause ever makes it into
the AST.  Yikes how did that make it through the test suite?  I'll
definitely add the original reported list comprehension to our test suite.
msg3937 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2008-12-16.19:25:36
Fixed along with the symmetrical problem in Generator Expressions in r5770.
msg3939 (view) Author: pb (pb) Date: 2008-12-16.22:15:56
wow - that was fast!
In case it should make it to the test cases you might add the following
instead, which is the complete expression that should compute 1729, the
Hardy-Ramanujan number, which is "the smallest number expressible as the
sum of two cubes in two different ways". 
http://en.wikipedia.org/wiki/1729_(number)
Nicer in a test case...
msg3940 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2008-12-17.03:44:12
pb: cool -- the Hardy-Ramanujan number angle does make the test more
fun.  I'll modify the tests accordingly :).  As for the turn-around
being fast, you have Philip Jenvey to thank for that -- He converted the
problem to an AST mismatch with CPython, and AST mismatches with CPython
are a sort of weird obsession of mine.
History
Date User Action Args
2008-12-17 03:44:13fwierzbickisetmessages: + msg3940
2008-12-16 22:15:56pbsetfiles: + ramanujan.py
messages: + msg3939
2008-12-16 19:25:36fwierzbickisetstatus: open -> closed
resolution: accepted -> fixed
messages: + msg3937
2008-12-16 17:31:14fwierzbickisetmessages: + msg3935
2008-12-16 17:18:08fwierzbickisetresolution: accepted
messages: + msg3934
2008-12-16 06:09:05pjenveysetassignee: fwierzbicki
messages: + msg3932
nosy: + pjenvey, fwierzbicki
2008-12-16 05:48:14pbcreate