1 2cimport cython 3 4def dictcomp(): 5 """ 6 >>> sorted(dictcomp().items()) 7 [(2, 0), (4, 4), (6, 8)] 8 >>> sorted(dictcomp().items()) 9 [(2, 0), (4, 4), (6, 8)] 10 """ 11 x = 'abc' 12 result = { x+2:x*2 13 for x in range(5) 14 if x % 2 == 0 } 15 assert x == 'abc' # do not leak! 16 return result 17 18@cython.test_assert_path_exists( 19 "//InlinedGeneratorExpressionNode", 20 "//DictComprehensionAppendNode") 21def genexpr(): 22 """ 23 >>> type(genexpr()) is dict 24 True 25 >>> type(genexpr()) is dict 26 True 27 """ 28 x = 'abc' 29 result = dict( (x+2,x*2) 30 for x in range(5) 31 if x % 2 == 0 ) 32 assert x == 'abc' 33 return result 34 35cdef class A: 36 def __repr__(self): return u"A" 37 def __richcmp__(one, other, int op): return one is other 38 def __hash__(self): return id(self) % 65536 39 40def typed_dictcomp(): 41 """ 42 >>> list(typed_dictcomp().items()) 43 [(A, 1), (A, 1), (A, 1)] 44 """ 45 cdef A obj 46 return {obj:1 for obj in [A(), A(), A()]} 47 48def iterdict_dictcomp(): 49 """ 50 >>> sorted(iterdict_dictcomp().items()) 51 [(1, 'a'), (2, 'b'), (3, 'c')] 52 """ 53 cdef dict d = dict(a=1,b=2,c=3) 54 return {d[key]:key for key in d} 55 56def sorted(it): 57 l = list(it) 58 l.sort() 59 return l 60 61 62# Copied from sre_compile.py in CPython 3.7. Previously failed to detect variable initialisation. 63_equivalences = ( 64 # LATIN SMALL LETTER I, LATIN SMALL LETTER DOTLESS I 65 (0x69, 0x131), # iı 66 # LATIN SMALL LETTER S, LATIN SMALL LETTER LONG S 67 (0x73, 0x17f), # sſ 68 # MICRO SIGN, GREEK SMALL LETTER MU 69 (0xb5, 0x3bc), # µμ 70 # COMBINING GREEK YPOGEGRAMMENI, GREEK SMALL LETTER IOTA, GREEK PROSGEGRAMMENI 71 (0x345, 0x3b9, 0x1fbe), # \u0345ιι 72 # ... 73) 74 75_ignorecase_fixes = { 76 i: tuple(j for j in t if i != j) 77 for t in _equivalences for i in t 78} 79 80def nested_tuple(): 81 """ 82 >>> modlevel, funclevel = nested_tuple() 83 >>> modlevel == funclevel or (modlevel, funclevel) 84 True 85 """ 86 inner_ignorecase_fixes = { 87 i: tuple(j for j in t if i != j) 88 for t in _equivalences for i in t 89 } 90 91 return sorted(_ignorecase_fixes.items()), sorted(inner_ignorecase_fixes.items()) 92