From: <fwi...@us...> - 2008-12-16 19:24:45
|
Revision: 5770 http://jython.svn.sourceforge.net/jython/?rev=5770&view=rev Author: fwierzbicki Date: 2008-12-16 19:24:41 +0000 (Tue, 16 Dec 2008) Log Message: ----------- Fix for http://bugs.jython.org/issue1205, thanks to pb for the report and Philip Jenvey for the AST analysis. List Comprehensions where only including one "list_if" in a series of "list_if". Generator Expressions had the same issue. Added a test to test_genexprs_jy.py and created test_listcomp_jy.py to test for the issue. Modified Paths: -------------- trunk/jython/Lib/test/test_genexps_jy.py trunk/jython/grammar/Python.g Added Paths: ----------- trunk/jython/Lib/test/test_listcomp_jy.py Modified: trunk/jython/Lib/test/test_genexps_jy.py =================================================================== --- trunk/jython/Lib/test/test_genexps_jy.py 2008-12-16 16:58:32 UTC (rev 5769) +++ trunk/jython/Lib/test/test_genexps_jy.py 2008-12-16 19:24:41 UTC (rev 5770) @@ -15,6 +15,13 @@ # this far we've already passed self.assert_(sorted(locals_test) == ['test_support', 'unittest']) + #http://bugs.jython.org/issue1205 applied to genexps. + def test_long_genexp(self): + r = 2 + g = ((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(r) for y2 in range(r) if y1 < y2 + if x1**3+x2**3 == y1**3+y2**3 ) + self.assertEquals(g.next(), (1, (0, 1), (0, 1))) def test_main(): test_support.run_unittest(GeneratorExpressionsTestCase) Added: trunk/jython/Lib/test/test_listcomp_jy.py =================================================================== --- trunk/jython/Lib/test/test_listcomp_jy.py (rev 0) +++ trunk/jython/Lib/test/test_listcomp_jy.py 2008-12-16 19:24:41 UTC (rev 5770) @@ -0,0 +1,19 @@ +import unittest +from test import test_support + +class ListCompTestCase(unittest.TestCase): + + #http://bugs.jython.org/issue1205 + def test_long_listcomp(self): + r = 2 + lc = [(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(r) for y2 in range(r) if y1 < y2 + if x1**3+x2**3 == y1**3+y2**3 ] + self.assertEquals(len(lc), 1) + self.assertEquals(lc, [(1, (0, 1), (0, 1))]) + +def test_main(): + test_support.run_unittest(ListCompTestCase) + +if __name__ == '__main__': + test_main() Modified: trunk/jython/grammar/Python.g =================================================================== --- trunk/jython/grammar/Python.g 2008-12-16 16:58:32 UTC (rev 5769) +++ trunk/jython/grammar/Python.g 2008-12-16 19:24:41 UTC (rev 5770) @@ -1516,60 +1516,55 @@ ; //list_iter: list_for | list_if -list_iter [List gens] returns [expr etype] +list_iter [List gens, List ifs] : list_for[gens] - | list_if[gens] { - $etype = $list_if.etype; - } + | list_if[gens, ifs] ; //list_for: 'for' exprlist 'in' testlist_safe [list_iter] list_for [List gens] - : FOR exprlist[expr_contextType.Store] IN testlist[expr_contextType.Load] (list_iter[gens])? +@init { + List ifs = new ArrayList(); +} + : FOR exprlist[expr_contextType.Store] IN testlist[expr_contextType.Load] (list_iter[gens, ifs])? { - List<expr> e = new ArrayList<expr>(); - if ($list_iter.etype != null) { - e.add($list_iter.etype); - } - gens.add(new comprehension($FOR, $exprlist.etype, actions.castExpr($testlist.tree), e)); + Collections.reverse(ifs); + gens.add(new comprehension($FOR, $exprlist.etype, actions.castExpr($testlist.tree), ifs)); } ; //list_if: 'if' test [list_iter] -list_if[List gens] returns [expr etype] - : IF test[expr_contextType.Load] (list_iter[gens])? +list_if[List gens, List ifs] + : IF test[expr_contextType.Load] (list_iter[gens, ifs])? { - $etype = actions.castExpr($test.tree); + ifs.add(actions.castExpr($test.tree)); } ; //gen_iter: gen_for | gen_if -gen_iter [List gens] returns [expr etype] +gen_iter [List gens, List ifs] : gen_for[gens] - | gen_if[gens] - { - $etype = $gen_if.etype; - } + | gen_if[gens, ifs] ; //gen_for: 'for' exprlist 'in' or_test [gen_iter] gen_for [List gens] - : FOR exprlist[expr_contextType.Store] IN or_test[expr_contextType.Load] gen_iter[gens]? +@init { + List ifs = new ArrayList(); +} + : FOR exprlist[expr_contextType.Store] IN or_test[expr_contextType.Load] gen_iter[gens, ifs]? { - List<expr> e = new ArrayList<expr>(); - if ($gen_iter.etype != null) { - e.add($gen_iter.etype); - } - gens.add(new comprehension($FOR, $exprlist.etype, actions.castExpr($or_test.tree), e)); + Collections.reverse(ifs); + gens.add(new comprehension($FOR, $exprlist.etype, actions.castExpr($or_test.tree), ifs)); } ; //gen_if: 'if' old_test [gen_iter] -gen_if[List gens] returns [expr etype] - : IF test[expr_contextType.Load] gen_iter[gens]? - { - $etype = actions.castExpr($test.tree); - } +gen_if[List gens, List ifs] + : IF test[expr_contextType.Load] gen_iter[gens, ifs]? + { + ifs.add(actions.castExpr($test.tree)); + } ; //yield_expr: 'yield' [testlist] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |