| 
									
										
										
										
											2007-04-15 12:05:43 +00:00
										 |  |  | doctests = """
 | 
					
						
							|  |  |  | ########### Tests mostly copied from test_listcomps.py ############ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Test simple loop with conditional | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> sum({i*i for i in range(100) if i&1 == 1}) | 
					
						
							|  |  |  |     166650 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Test simple case | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> {2*y + x + 1 for x in (0,) for y in (1,)} | 
					
						
							|  |  |  |     {3} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Test simple nesting | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> list(sorted({(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(sorted({(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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Verify that syntax error's are raised for setcomps used as lvalues | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> {y for y in (1,2)} = 10          # doctest: +IGNORE_EXCEPTION_DETAIL | 
					
						
							|  |  |  |     Traceback (most recent call last): | 
					
						
							|  |  |  |        ... | 
					
						
							|  |  |  |     SyntaxError: ... | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> {y for y in (1,2)} += 10         # doctest: +IGNORE_EXCEPTION_DETAIL | 
					
						
							|  |  |  |     Traceback (most recent call last): | 
					
						
							|  |  |  |        ... | 
					
						
							|  |  |  |     SyntaxError: ... | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-07 22:24:25 +00:00
										 |  |  | Make a nested set comprehension that acts like set(range()) | 
					
						
							| 
									
										
										
										
											2007-04-15 12:05:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     >>> def srange(n): | 
					
						
							| 
									
										
										
										
											2007-05-07 22:24:25 +00:00
										 |  |  |     ...     return {i for i in range(n)} | 
					
						
							| 
									
										
										
										
											2007-04-15 12:05:43 +00:00
										 |  |  |     >>> list(sorted(srange(10))) | 
					
						
							|  |  |  |     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Same again, only as a lambda expression instead of a function definition | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-07 22:24:25 +00:00
										 |  |  |     >>> lrange = lambda n:  {i for i in range(n)} | 
					
						
							| 
									
										
										
										
											2007-04-15 12:05:43 +00:00
										 |  |  |     >>> list(sorted(lrange(10))) | 
					
						
							|  |  |  |     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Generators can call other generators: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> def grange(n): | 
					
						
							| 
									
										
										
										
											2007-05-07 22:24:25 +00:00
										 |  |  |     ...     for x in {i for i in range(n)}: | 
					
						
							| 
									
										
										
										
											2007-04-15 12:05:43 +00:00
										 |  |  |     ...         yield x | 
					
						
							|  |  |  |     >>> list(sorted(grange(5))) | 
					
						
							|  |  |  |     [0, 1, 2, 3, 4] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Make sure that None is a valid return value | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-07 22:24:25 +00:00
										 |  |  |     >>> {None for i in range(10)} | 
					
						
							| 
									
										
										
										
											2007-04-15 12:05:43 +00:00
										 |  |  |     {None} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ########### Tests for various scoping corner cases ############ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Return lambdas that use the iteration variable as a default argument | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> items = {(lambda i=i: i) for i in range(5)} | 
					
						
							|  |  |  |     >>> {x() for x in items} == set(range(5)) | 
					
						
							|  |  |  |     True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Same again, only this time as a closure variable | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> items = {(lambda: i) for i in range(5)} | 
					
						
							|  |  |  |     >>> {x() for x in items} | 
					
						
							|  |  |  |     {4} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Another way to test that the iteration variable is local to the list comp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> items = {(lambda: i) for i in range(5)} | 
					
						
							|  |  |  |     >>> i = 20 | 
					
						
							|  |  |  |     >>> {x() for x in items} | 
					
						
							|  |  |  |     {4} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | And confirm that a closure can jump over the list comp scope | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> items = {(lambda: y) for i in range(5)} | 
					
						
							|  |  |  |     >>> y = 2 | 
					
						
							|  |  |  |     >>> {x() for x in items} | 
					
						
							|  |  |  |     {2} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | We also repeat each of the above scoping tests inside a function | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> def test_func(): | 
					
						
							|  |  |  |     ...     items = {(lambda i=i: i) for i in range(5)} | 
					
						
							|  |  |  |     ...     return {x() for x in items} | 
					
						
							|  |  |  |     >>> test_func() == set(range(5)) | 
					
						
							|  |  |  |     True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> def test_func(): | 
					
						
							|  |  |  |     ...     items = {(lambda: i) for i in range(5)} | 
					
						
							|  |  |  |     ...     return {x() for x in items} | 
					
						
							|  |  |  |     >>> test_func() | 
					
						
							|  |  |  |     {4} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> def test_func(): | 
					
						
							|  |  |  |     ...     items = {(lambda: i) for i in range(5)} | 
					
						
							|  |  |  |     ...     i = 20 | 
					
						
							|  |  |  |     ...     return {x() for x in items} | 
					
						
							|  |  |  |     >>> test_func() | 
					
						
							|  |  |  |     {4} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     >>> def test_func(): | 
					
						
							|  |  |  |     ...     items = {(lambda: y) for i in range(5)} | 
					
						
							|  |  |  |     ...     y = 2 | 
					
						
							|  |  |  |     ...     return {x() for x in items} | 
					
						
							|  |  |  |     >>> test_func() | 
					
						
							|  |  |  |     {2} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | __test__ = {'doctests' : doctests} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_main(verbose=None): | 
					
						
							|  |  |  |     import sys | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  |     from test import support | 
					
						
							| 
									
										
											  
											
												Merged revisions 55225-55227,55229-55269 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
  r55238 | guido.van.rossum | 2007-05-10 16:46:05 -0700 (Thu, 10 May 2007) | 9 lines
  Merged revisions 55227 via svnmerge from
  svn+ssh://pythondev@svn.python.org/python/trunk
  ........
    r55227 | guido.van.rossum | 2007-05-10 10:20:15 -0700 (Thu, 10 May 2007) | 2 lines
    Fix a bug in test_c_api() that caused a negative refcount.
  ........
................
  r55246 | neal.norwitz | 2007-05-11 00:01:52 -0700 (Fri, 11 May 2007) | 1 line
  Remove commands.getstatus() it is obsolete.
................
  r55248 | neal.norwitz | 2007-05-11 00:29:05 -0700 (Fri, 11 May 2007) | 2 lines
  Remove bsddb185 support.
................
  r55249 | neal.norwitz | 2007-05-11 00:29:50 -0700 (Fri, 11 May 2007) | 1 line
  Remove bsddb185 module too
................
  r55250 | neal.norwitz | 2007-05-11 00:32:13 -0700 (Fri, 11 May 2007) | 1 line
  bsddb185: Gotta remove from the file checked in, not Setup
................
  r55251 | neal.norwitz | 2007-05-11 00:53:26 -0700 (Fri, 11 May 2007) | 1 line
  Remove obsolete IRIX modules (as much as I could find, there is probably more)
................
  r55252 | neal.norwitz | 2007-05-11 00:55:35 -0700 (Fri, 11 May 2007) | 1 line
  Remove SGI turd.
................
  r55254 | georg.brandl | 2007-05-11 03:11:01 -0700 (Fri, 11 May 2007) | 2 lines
  Add a case for set comprehensions to the "cannot assign to" switch.
................
  r55255 | georg.brandl | 2007-05-11 03:11:25 -0700 (Fri, 11 May 2007) | 2 lines
  Fix wrong imports.
................
  r55261 | georg.brandl | 2007-05-11 07:37:48 -0700 (Fri, 11 May 2007) | 2 lines
  Remove removed tex files.
................
  r55262 | georg.brandl | 2007-05-11 08:28:41 -0700 (Fri, 11 May 2007) | 2 lines
  Commit PEP 3132 implementation.
................
  r55264 | georg.brandl | 2007-05-11 08:50:19 -0700 (Fri, 11 May 2007) | 2 lines
  Check in the inevitable AST version number and format Py_ssize_t with %zd.
................
  r55265 | neal.norwitz | 2007-05-11 09:12:22 -0700 (Fri, 11 May 2007) | 1 line
  Remove mention of os.popen* and popen2.* since these will be removed.
................
  r55266 | neal.norwitz | 2007-05-11 09:19:57 -0700 (Fri, 11 May 2007) | 1 line
  Get doc to build again (almost, the doc is fine)
................
  r55267 | neal.norwitz | 2007-05-11 09:21:02 -0700 (Fri, 11 May 2007) | 1 line
  Really get doc to build (remove use of string module)
................
  r55269 | neal.norwitz | 2007-05-11 09:29:43 -0700 (Fri, 11 May 2007) | 1 line
  Add some notes to cleanup later
................
											
										 
											2007-05-11 16:50:42 +00:00
										 |  |  |     from test import test_setcomps | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  |     support.run_doctest(test_setcomps, verbose) | 
					
						
							| 
									
										
										
										
											2007-04-15 12:05:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # verify reference counting | 
					
						
							|  |  |  |     if verbose and hasattr(sys, "gettotalrefcount"): | 
					
						
							|  |  |  |         import gc | 
					
						
							|  |  |  |         counts = [None] * 5 | 
					
						
							| 
									
										
										
										
											2007-05-07 22:24:25 +00:00
										 |  |  |         for i in range(len(counts)): | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  |             support.run_doctest(test_setcomps, verbose) | 
					
						
							| 
									
										
										
										
											2007-04-15 12:05:43 +00:00
										 |  |  |             gc.collect() | 
					
						
							|  |  |  |             counts[i] = sys.gettotalrefcount() | 
					
						
							|  |  |  |         print(counts) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     test_main(verbose=True) |