mirror of
https://github.com/python/cpython.git
synced 2025-10-22 22:53:06 +00:00

svn+ssh://pythondev@svn.python.org/python/trunk ........ r63208 | georg.brandl | 2008-05-13 15:04:54 -0400 (Tue, 13 May 2008) | 2 lines #2831: add start argument to enumerate(). Patch by Scott Dial and me. ........ r63209 | marc-andre.lemburg | 2008-05-13 15:10:45 -0400 (Tue, 13 May 2008) | 3 lines Remove leftovers from reverted setuptools checkin (they were added in r45525). ........ r63211 | georg.brandl | 2008-05-13 17:32:03 -0400 (Tue, 13 May 2008) | 2 lines Fix a refleak in the _warnings module. ........ r63212 | andrew.kuchling | 2008-05-13 20:46:41 -0400 (Tue, 13 May 2008) | 1 line List all the removes and renamed modules ........ r63214 | brett.cannon | 2008-05-13 21:09:40 -0400 (Tue, 13 May 2008) | 2 lines Rewrap some lines in test_py3kwarn. ........ r63219 | georg.brandl | 2008-05-14 02:34:15 -0400 (Wed, 14 May 2008) | 2 lines Add NEWS entry for #2831. ........ r63220 | neal.norwitz | 2008-05-14 02:47:56 -0400 (Wed, 14 May 2008) | 3 lines Fix "refleak" by restoring the tearDown method removed by accident (AFAICT) in r62788. ........ r63221 | georg.brandl | 2008-05-14 03:18:22 -0400 (Wed, 14 May 2008) | 2 lines Fix another "refleak" by clearing the filters after test. ........ r63222 | neal.norwitz | 2008-05-14 03:21:42 -0400 (Wed, 14 May 2008) | 5 lines Install the json package and tests as well as the lib2to3 tests so the tests work when run from an install directory. They are currently skipped on the daily runs (not from the buildbots) for checking refleaks, etc. ........ r63256 | andrew.kuchling | 2008-05-14 21:10:24 -0400 (Wed, 14 May 2008) | 1 line Note some removals and a rename ........ r63311 | brett.cannon | 2008-05-15 00:36:53 -0400 (Thu, 15 May 2008) | 2 lines Add a snippet for the deprecation directive for docs. ........ r63313 | gregory.p.smith | 2008-05-15 00:56:18 -0400 (Thu, 15 May 2008) | 5 lines disable the crashing test. I will also file a bug. This crash does not appear to be a new bug, its just that the test coverage went up recently exposing it. (I verified that by testing this test code on an older Modules/_bsddb.c) ........ r63320 | georg.brandl | 2008-05-15 11:08:32 -0400 (Thu, 15 May 2008) | 2 lines #2863: add gen.__name__ and add this name to generator repr(). ........ r63324 | andrew.kuchling | 2008-05-15 16:07:39 -0400 (Thu, 15 May 2008) | 1 line Import class from distutils.cmd, not .core, to avoid circular import ........ r63327 | alexandre.vassalotti | 2008-05-15 16:31:42 -0400 (Thu, 15 May 2008) | 2 lines Fixed typo in a doctest of test_genexps. ........ r63332 | benjamin.peterson | 2008-05-15 18:34:33 -0400 (Thu, 15 May 2008) | 2 lines add Mac modules to the list of deprecated ones ........ r63333 | benjamin.peterson | 2008-05-15 18:41:16 -0400 (Thu, 15 May 2008) | 2 lines fix typos in whatsnew ........ r63348 | benjamin.peterson | 2008-05-15 22:24:49 -0400 (Thu, 15 May 2008) | 2 lines make test_platform a bit more assertive (We'll see what the buildbots say.) ........
280 lines
6.9 KiB
Python
280 lines
6.9 KiB
Python
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)
|
|
<class 'generator'>
|
|
>>> list(g)
|
|
[0, 1, 4, 9]
|
|
|
|
Test direct calls to next()
|
|
|
|
>>> g = (i*i for i in range(3))
|
|
>>> next(g)
|
|
0
|
|
>>> next(g)
|
|
1
|
|
>>> next(g)
|
|
4
|
|
>>> next(g)
|
|
Traceback (most recent call last):
|
|
File "<pyshell#21>", line 1, in -toplevel-
|
|
next(g)
|
|
StopIteration
|
|
|
|
Does it stay stopped?
|
|
|
|
>>> next(g)
|
|
Traceback (most recent call last):
|
|
File "<pyshell#21>", line 1, in -toplevel-
|
|
next(g)
|
|
StopIteration
|
|
>>> list(g)
|
|
[]
|
|
|
|
Test running gen when defining function is out of scope
|
|
|
|
>>> def f(n):
|
|
... return (i*i for i in range(n))
|
|
>>> list(f(10))
|
|
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
|
|
|
|
>>> def f(n):
|
|
... return ((i,j) for i in range(3) for j in range(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 range(3) for j in range(4) if j in range(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 range(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 range(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 range(10))) #doctest: +ELLIPSIS
|
|
{'a': <<genexpr> generator object at ...>}
|
|
|
|
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 "<pyshell#4>", 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 = list(map(id, ((i,i) for i in range(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
|
|
|
|
>>> (y for y in (1,2)) += 10
|
|
Traceback (most recent call last):
|
|
...
|
|
SyntaxError: augmented assignment to generator expression not possible
|
|
|
|
|
|
########### Tests borrowed from or inspired by test_generators.py ############
|
|
|
|
Make a generator that acts like range()
|
|
|
|
>>> yrange = lambda n: (i for i in range(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", next(r))
|
|
... 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 = (next(me) for i in range(10))
|
|
>>> me = g
|
|
>>> next(me)
|
|
Traceback (most recent call last):
|
|
File "<pyshell#30>", line 1, in -toplevel-
|
|
next(me)
|
|
File "<pyshell#28>", line 1, in <generator expression>
|
|
g = (next(me) for i in range(10))
|
|
ValueError: generator already executing
|
|
|
|
Verify exception propagation
|
|
|
|
>>> g = (10 // i for i in (5, 0, 2))
|
|
>>> next(g)
|
|
2
|
|
>>> next(g)
|
|
Traceback (most recent call last):
|
|
File "<pyshell#37>", line 1, in -toplevel-
|
|
next(g)
|
|
File "<pyshell#35>", line 1, in <generator expression>
|
|
g = (10 // i for i in (5, 0, 2))
|
|
ZeroDivisionError: integer division or modulo by zero
|
|
>>> next(g)
|
|
Traceback (most recent call last):
|
|
File "<pyshell#38>", line 1, in -toplevel-
|
|
next(g)
|
|
StopIteration
|
|
|
|
Make sure that None is a valid return value
|
|
|
|
>>> list(None for i in range(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'])
|
|
>>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected
|
|
True
|
|
|
|
>>> print(g.__next__.__doc__)
|
|
x.__next__() <==> next(x)
|
|
>>> 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
|
|
>>> next(me)
|
|
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 range(len(counts)):
|
|
test_support.run_doctest(test_genexps, verbose)
|
|
gc.collect()
|
|
counts[i] = sys.gettotalrefcount()
|
|
print(counts)
|
|
|
|
if __name__ == "__main__":
|
|
test_main(verbose=True)
|