blob: a3b22dc45062c9628a5c713b5ac7472f493ed26b [file] [log] [blame]
Guido van Rossumc76a2502007-08-09 14:26:58 +00001
2doctests = """
3
4 >>> k = "old value"
5 >>> { k: None for k in range(10) }
6 {0: None, 1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None, 8: None, 9: None}
7 >>> k
8 'old value'
9
10 >>> { k: k+10 for k in range(10) }
11 {0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17, 8: 18, 9: 19}
12
13 >>> g = "Global variable"
14 >>> { k: g for k in range(10) }
15 {0: 'Global variable', 1: 'Global variable', 2: 'Global variable', 3: 'Global variable', 4: 'Global variable', 5: 'Global variable', 6: 'Global variable', 7: 'Global variable', 8: 'Global variable', 9: 'Global variable'}
16
17 >>> { k: v for k in range(10) for v in range(10) if k == v }
18 {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
19
20 >>> { k: v for v in range(10) for k in range(v*9, v*10) }
21 {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4, 38: 4, 39: 4, 45: 5, 46: 5, 47: 5, 48: 5, 49: 5, 54: 6, 55: 6, 56: 6, 57: 6, 58: 6, 59: 6, 63: 7, 64: 7, 65: 7, 66: 7, 67: 7, 68: 7, 69: 7, 72: 8, 73: 8, 74: 8, 75: 8, 76: 8, 77: 8, 78: 8, 79: 8, 81: 9, 82: 9, 83: 9, 84: 9, 85: 9, 86: 9, 87: 9, 88: 9, 89: 9}
22
23 >>> { x: y for y, x in ((1, 2), (3, 4)) } = 5 # doctest: +IGNORE_EXCEPTION_DETAIL
24 Traceback (most recent call last):
25 ...
26 SyntaxError: ...
27
28 >>> { x: y for y, x in ((1, 2), (3, 4)) } += 5 # doctest: +IGNORE_EXCEPTION_DETAIL
29 Traceback (most recent call last):
30 ...
31 SyntaxError: ...
32
33"""
34
35__test__ = {'doctests' : doctests}
36
37def test_main(verbose=None):
38 import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +000039 from test import support
Guido van Rossumc76a2502007-08-09 14:26:58 +000040 from test import test_dictcomps
Benjamin Petersonee8712c2008-05-20 21:35:26 +000041 support.run_doctest(test_dictcomps, verbose)
Guido van Rossumc76a2502007-08-09 14:26:58 +000042
43 # verify reference counting
44 if verbose and hasattr(sys, "gettotalrefcount"):
45 import gc
46 counts = [None] * 5
47 for i in range(len(counts)):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000048 support.run_doctest(test_dictcomps, verbose)
Guido van Rossumc76a2502007-08-09 14:26:58 +000049 gc.collect()
50 counts[i] = sys.gettotalrefcount()
51 print(counts)
52
53if __name__ == "__main__":
54 test_main(verbose=True)