## jython-checkins

 [Jython-checkins] SF.net SVN: jython:[5121] branches/asm/Lib/test/test_genexps.py From: - 2008-08-09 14:25:28 ```Revision: 5121 http://jython.svn.sourceforge.net/jython/?rev=5121&view=rev Author: fwierzbicki Date: 2008-08-09 14:25:25 +0000 (Sat, 09 Aug 2008) Log Message: ----------- from: http://svn.python.org/projects/python/branches/release25-maint/Lib/test_genexps.py@... Added Paths: ----------- branches/asm/Lib/test/test_genexps.py Added: branches/asm/Lib/test/test_genexps.py =================================================================== --- branches/asm/Lib/test/test_genexps.py (rev 0) +++ branches/asm/Lib/test/test_genexps.py 2008-08-09 14:25:25 UTC (rev 5121) @@ -0,0 +1,280 @@ +doctests = """ + +Test simple loop with conditional + + >>> sum(i*i for i in range(100) if i&1 == 1) + 166650 + +Test simple nesting + + >>> list((i,j) for i in range(3) for j in range(4) ) + [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] + +Test nesting with the inner expression dependent on the outer + + >>> list((i,j) for i in range(4) for j in range(i) ) + [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)] + +Make sure the induction variable is not exposed + + >>> i = 20 + >>> sum(i*i for i in range(100)) + 328350 + >>> i + 20 + +Test first class + + >>> g = (i*i for i in range(4)) + >>> type(g) + + >>> list(g) + [0, 1, 4, 9] + +Test direct calls to next() + + >>> g = (i*i for i in range(3)) + >>> g.next() + 0 + >>> g.next() + 1 + >>> g.next() + 4 + >>> g.next() + Traceback (most recent call last): + File "", line 1, in -toplevel- + g.next() + StopIteration + +Does it stay stopped? + + >>> g.next() + Traceback (most recent call last): + File "", line 1, in -toplevel- + g.next() + StopIteration + >>> list(g) + [] + +Test running gen when defining function is out of scope + + >>> def f(n): + ... return (i*i for i in xrange(n)) + >>> list(f(10)) + [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] + + >>> def f(n): + ... return ((i,j) for i in xrange(3) for j in xrange(n)) + >>> list(f(4)) + [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] + >>> def f(n): + ... return ((i,j) for i in xrange(3) for j in xrange(4) if j in xrange(n)) + >>> list(f(4)) + [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] + >>> list(f(2)) + [(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1)] + +Verify that parenthesis are required in a statement + + >>> def f(n): + ... return i*i for i in xrange(n) + Traceback (most recent call last): + ... + SyntaxError: invalid syntax + +Verify that parenthesis are required when used as a keyword argument value + + >>> dict(a = i for i in xrange(10)) + Traceback (most recent call last): + ... + SyntaxError: invalid syntax + +Verify that parenthesis are required when used as a keyword argument value + + >>> dict(a = (i for i in xrange(10))) #doctest: +ELLIPSIS + {'a': } + +Verify early binding for the outermost for-expression + + >>> x=10 + >>> g = (i*i for i in range(x)) + >>> x = 5 + >>> list(g) + [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] + +Verify that the outermost for-expression makes an immediate check +for iterability + + >>> (i for i in 6) + Traceback (most recent call last): + File "", line 1, in -toplevel- + (i for i in 6) + TypeError: 'int' object is not iterable + +Verify late binding for the outermost if-expression + + >>> include = (2,4,6,8) + >>> g = (i*i for i in range(10) if i in include) + >>> include = (1,3,5,7,9) + >>> list(g) + [1, 9, 25, 49, 81] + +Verify late binding for the innermost for-expression + + >>> g = ((i,j) for i in range(3) for j in range(x)) + >>> x = 4 + >>> list(g) + [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] + +Verify re-use of tuples (a side benefit of using genexps over listcomps) + + >>> tupleids = map(id, ((i,i) for i in xrange(10))) + >>> int(max(tupleids) - min(tupleids)) + 0 + +Verify that syntax error's are raised for genexps used as lvalues + + >>> (y for y in (1,2)) = 10 + Traceback (most recent call last): + ... + SyntaxError: can't assign to generator expression (, line 1) + + >>> (y for y in (1,2)) += 10 + Traceback (most recent call last): + ... + SyntaxError: augmented assignment to generator expression not possible (, line 1) + + +########### Tests borrowed from or inspired by test_generators.py ############ + +Make a generator that acts like range() + + >>> yrange = lambda n: (i for i in xrange(n)) + >>> list(yrange(10)) + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + +Generators always return to the most recent caller: + + >>> def creator(): + ... r = yrange(5) + ... print "creator", r.next() + ... return r + >>> def caller(): + ... r = creator() + ... for i in r: + ... print "caller", i + >>> caller() + creator 0 + caller 1 + caller 2 + caller 3 + caller 4 + +Generators can call other generators: + + >>> def zrange(n): + ... for i in yrange(n): + ... yield i + >>> list(zrange(5)) + [0, 1, 2, 3, 4] + + +Verify that a gen exp cannot be resumed while it is actively running: + + >>> g = (me.next() for i in xrange(10)) + >>> me = g + >>> me.next() + Traceback (most recent call last): + File "", line 1, in -toplevel- + me.next() + File "", line 1, in + g = (me.next() for i in xrange(10)) + ValueError: generator already executing + +Verify exception propagation + + >>> g = (10 // i for i in (5, 0, 2)) + >>> g.next() + 2 + >>> g.next() + Traceback (most recent call last): + File "", line 1, in -toplevel- + g.next() + File "", line 1, in + g = (10 // i for i in (5, 0, 2)) + ZeroDivisionError: integer division or modulo by zero + >>> g.next() + Traceback (most recent call last): + File "", line 1, in -toplevel- + g.next() + StopIteration + +Make sure that None is a valid return value + + >>> list(None for i in xrange(10)) + [None, None, None, None, None, None, None, None, None, None] + +Check that generator attributes are present + + >>> g = (i*i for i in range(3)) + >>> expected = set(['gi_frame', 'gi_running', 'next']) + >>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected + True + + >>> print g.next.__doc__ + x.next() -> the next value, or raise StopIteration + >>> import types + >>> isinstance(g, types.GeneratorType) + True + +Check the __iter__ slot is defined to return self + + >>> iter(g) is g + True + +Verify that the running flag is set properly + + >>> g = (me.gi_running for i in (0,1)) + >>> me = g + >>> me.gi_running + 0 + >>> me.next() + 1 + >>> me.gi_running + 0 + +Verify that genexps are weakly referencable + + >>> import weakref + >>> g = (i*i for i in range(4)) + >>> wr = weakref.ref(g) + >>> wr() is g + True + >>> p = weakref.proxy(g) + >>> list(p) + [0, 1, 4, 9] + + +""" + + +__test__ = {'doctests' : doctests} + +def test_main(verbose=None): + import sys + from test import test_support + from test import test_genexps + test_support.run_doctest(test_genexps, verbose) + + # verify reference counting + if verbose and hasattr(sys, "gettotalrefcount"): + import gc + counts = [None] * 5 + for i in xrange(len(counts)): + test_support.run_doctest(test_genexps, verbose) + gc.collect() + counts[i] = sys.gettotalrefcount() + print counts + +if __name__ == "__main__": + test_main(verbose=True) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ```
 [Jython-checkins] SF.net SVN: jython:[5122] branches/asm/Lib/test/test_genexps.py From: - 2008-08-09 16:27:46 ```Revision: 5122 http://jython.svn.sourceforge.net/jython/?rev=5122&view=rev Author: fwierzbicki Date: 2008-08-09 16:27:42 +0000 (Sat, 09 Aug 2008) Log Message: ----------- added #doctest: +IGNORE_EXCEPTION_DETAIL for SyntaxErrors that just have "invalid syntax" as the detail, since this is the default message and Antlr actually gives more information. Modified Paths: -------------- branches/asm/Lib/test/test_genexps.py Modified: branches/asm/Lib/test/test_genexps.py =================================================================== --- branches/asm/Lib/test/test_genexps.py 2008-08-09 14:25:25 UTC (rev 5121) +++ branches/asm/Lib/test/test_genexps.py 2008-08-09 16:27:42 UTC (rev 5122) @@ -76,7 +76,7 @@ Verify that parenthesis are required in a statement - >>> def f(n): + >>> def f(n): #doctest: +IGNORE_EXCEPTION_DETAIL ... return i*i for i in xrange(n) Traceback (most recent call last): ... @@ -84,7 +84,7 @@ Verify that parenthesis are required when used as a keyword argument value - >>> dict(a = i for i in xrange(10)) + >>> dict(a = i for i in xrange(10)) #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... SyntaxError: invalid syntax This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ```
 [Jython-checkins] SF.net SVN: jython:[5130] branches/asm/Lib/test/test_genexps.py From: - 2008-08-10 15:03:24 ```Revision: 5130 http://jython.svn.sourceforge.net/jython/?rev=5130&view=rev Author: fwierzbicki Date: 2008-08-10 15:03:21 +0000 (Sun, 10 Aug 2008) Log Message: ----------- Ignoring other SyntaxError details for now -- though I probably want to re-enable these two later. Modified Paths: -------------- branches/asm/Lib/test/test_genexps.py Modified: branches/asm/Lib/test/test_genexps.py =================================================================== --- branches/asm/Lib/test/test_genexps.py 2008-08-09 23:40:43 UTC (rev 5129) +++ branches/asm/Lib/test/test_genexps.py 2008-08-10 15:03:21 UTC (rev 5130) @@ -1,3 +1,10 @@ +#From http://svn.python.org/projects/python/branches/release25-maint/Lib/test_genexps.py@... + +#Ignoring details of SyntaxError for now -- justified I think for "invalid +# syntax" since that #message has very little info, where Antlr provides more +# detail. The others really are #differences that should be addressed, but I +# want to concentrate on the other errors for now. + doctests = """ Test simple loop with conditional @@ -134,12 +141,12 @@ Verify that syntax error's are raised for genexps used as lvalues - >>> (y for y in (1,2)) = 10 + >>> (y for y in (1,2)) = 10 #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... SyntaxError: can't assign to generator expression (, line 1) - >>> (y for y in (1,2)) += 10 + >>> (y for y in (1,2)) += 10 #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... SyntaxError: augmented assignment to generator expression not possible (, line 1) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ```
 [Jython-checkins] SF.net SVN: jython:[5142] branches/asm/Lib/test/test_genexps.py From: - 2008-08-11 03:17:47 ```Revision: 5142 http://jython.svn.sourceforge.net/jython/?rev=5142&view=rev Author: zyasoft Date: 2008-08-11 03:17:44 +0000 (Mon, 11 Aug 2008) Log Message: ----------- Commented out inappropriate doctest in test_genexps that tested for the reuse of memory objects. This obviously doesn't apply to Jython and other nonrefcounted implementations. Fixes test_genexps Modified Paths: -------------- branches/asm/Lib/test/test_genexps.py Modified: branches/asm/Lib/test/test_genexps.py =================================================================== --- branches/asm/Lib/test/test_genexps.py 2008-08-11 02:56:53 UTC (rev 5141) +++ branches/asm/Lib/test/test_genexps.py 2008-08-11 03:17:44 UTC (rev 5142) @@ -133,12 +133,13 @@ >>> list(g) [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] -Verify re-use of tuples (a side benefit of using genexps over listcomps) +This doesn't make sense for Jython or other non refcounted implementations +# Verify re-use of tuples (a side benefit of using genexps over listcomps) +# +# >>> tupleids = map(id, ((i,i) for i in xrange(10))) +# >>> int(max(tupleids) - min(tupleids)) +# 0 - >>> tupleids = map(id, ((i,i) for i in xrange(10))) - >>> int(max(tupleids) - min(tupleids)) - 0 - Verify that syntax error's are raised for genexps used as lvalues >>> (y for y in (1,2)) = 10 #doctest: +IGNORE_EXCEPTION_DETAIL This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ```