[Python-checkins] python/dist/src/Lib/test test_genexps.py, NONE, 1.1 test_deque.py, 1.10, 1.11 test_grammar.py, 1.49, 1.50 test_parser.py, 1.17, 1.18 (original) (raw)
rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Wed May 19 04:20:43 EDT 2004
- Previous message: [Python-checkins] python/dist/src/Lib/compiler ast.py, 1.22, 1.23 pycodegen.py, 1.66, 1.67 symbols.py, 1.13, 1.14 transformer.py, 1.38, 1.39
- Next message: [Python-checkins] python/dist/src/Misc ACKS, 1.261, 1.262 NEWS, 1.972, 1.973
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Update of /cvsroot/python/python/dist/src/Lib/test In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9770/Lib/test
Modified Files: test_deque.py test_grammar.py test_parser.py Added Files: test_genexps.py Log Message: SF patch #872326: Generator expression implementation (Code contributed by Jiwon Seo.)
The documentation portion of the patch is being re-worked and will be checked-in soon. Likewise, PEP 289 will be updated to reflect Guido's rationale for the design decisions on binding behavior (as described in in his patch comments and in discussions on python-dev).
The test file, test_genexps.py, is written in doctest format and is meant to exercise all aspects of the the patch. Further additions are welcome from everyone. Please stress test this new feature as much as possible before the alpha release.
--- NEW FILE: test_genexps.py --- 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)
<type 'generator'>
>>> 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 "<pyshell#21>", line 1, in -toplevel-
g.next()
StopIteration
Does it stay stopped?
>>> g.next()
Traceback (most recent call last):
File "<pyshell#21>", 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) #... #SyntaxError: invalid syntax
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 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)))
>>> max(tupleids) - min(tupleids)
0
########### 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 "<pyshell#30>", line 1, in -toplevel-
me.next()
File "<pyshell#28>", line 1, in <generator expression>
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 "<pyshell#37>", line 1, in -toplevel-
g.next()
File "<pyshell#35>", line 1, in <generator expression>
g = (10 // i for i in (5, 0, 2))
ZeroDivisionError: integer division or modulo by zero
>>> g.next()
Traceback (most recent call last):
File "<pyshell#38>", 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)
Index: test_deque.py
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_deque.py,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** test_deque.py 12 May 2004 20:55:56 -0000 1.10 --- test_deque.py 19 May 2004 08:20:08 -0000 1.11
*** 567,571 **** # doctests from test import test_deque ! # test_support.run_doctest(test_deque, verbose)
if name == "main": --- 567,571 ---- # doctests from test import test_deque ! test_support.run_doctest(test_deque, verbose)
if name == "main":
Index: test_grammar.py
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_grammar.py,v retrieving revision 1.49 retrieving revision 1.50 diff -C2 -d -r1.49 -r1.50 *** test_grammar.py 12 Feb 2004 17:35:11 -0000 1.49 --- test_grammar.py 19 May 2004 08:20:09 -0000 1.50
*** 740,741 **** --- 740,784 ---- if sno == sp_sno and pno == sp_pno ]
+
generator expression tests
- g = ([x for x in range(10)] for x in range(1))
- verify(g.next() == [x for x in range(10)])
- try:
g.next()
raise TestFailed, 'should produce StopIteration exception'
- except StopIteration:
pass
- a = 1
- try:
g = (a for d in a)
g.next()
raise TestFailed, 'should produce TypeError'
- except TypeError:
pass
- verify(list((x, y) for x in 'abcd' for y in 'abcd') == [(x, y) for x in 'abcd' for y in 'abcd'])
- verify(list((x, y) for x in 'ab' for y in 'xy') == [(x, y) for x in 'ab' for y in 'xy'])
- a = [x for x in range(10)]
- b = (x for x in (y for y in a))
- verify(sum(b) == sum([x for x in range(10)]))
- verify(sum(x2 for x in range(10)) == sum([x2 for x in range(10)]))
- verify(sum(xx for x in range(10) if x%2) == sum([xx for x in range(10) if x%2]))
- verify(sum(x for x in (y for y in range(10))) == sum([x for x in range(10)]))
- verify(sum(x for x in (y for y in (z for z in range(10)))) == sum([x for x in range(10)]))
- verify(sum(x for x in [y for y in (z for z in range(10))]) == sum([x for x in range(10)]))
- verify(sum(x for x in (y for y in (z for z in range(10) if True)) if True) == sum([x for x in range(10)]))
- verify(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True) == 0)
- check_syntax("foo(x for x in range(10), 100)")
- check_syntax("foo(100, x for x in range(10))")
test for outmost iterable precomputation
- x = 10; g = (i for i in range(x)); x = 5
- verify(len(list(g)) == 10)
This should hold, since we're only precomputing outmost iterable.
- x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
- x = 5; t = True;
- verify([(i,j) for i in range(10) for j in range(5)] == list(g))
Index: test_parser.py
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_parser.py,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** test_parser.py 1 May 2003 17:45:44 -0000 1.17 --- test_parser.py 19 May 2004 08:20:09 -0000 1.18
*** 68,71 **** --- 68,73 ---- self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0") self.check_expr("lambda x, *y, **z: 0")
self.check_expr("(x for x in range(10))")
self.check_expr("foo(x for x in range(10))") def test_print(self):
- Previous message: [Python-checkins] python/dist/src/Lib/compiler ast.py, 1.22, 1.23 pycodegen.py, 1.66, 1.67 symbols.py, 1.13, 1.14 transformer.py, 1.38, 1.39
- Next message: [Python-checkins] python/dist/src/Misc ACKS, 1.261, 1.262 NEWS, 1.972, 1.973
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]