1 2cimport cython 3 4# Py2.3 doesn't have the set type, but Cython does :) 5_set = set 6 7def setcomp(): 8 """ 9 >>> type(setcomp()) is not list 10 True 11 >>> type(setcomp()) is _set 12 True 13 >>> sorted(setcomp()) 14 [0, 4, 8] 15 """ 16 x = 'abc' 17 result = { x*2 18 for x in range(5) 19 if x % 2 == 0 } 20 assert x == 'abc' # do not leak 21 return result 22 23@cython.test_assert_path_exists( 24 "//InlinedGeneratorExpressionNode", 25 "//ComprehensionAppendNode") 26def genexp_set(): 27 """ 28 >>> type(genexp_set()) is _set 29 True 30 >>> sorted(genexp_set()) 31 [0, 4, 8] 32 """ 33 x = 'abc' 34 result = set( x*2 35 for x in range(5) 36 if x % 2 == 0 ) 37 assert x == 'abc' # do not leak 38 return result 39 40cdef class A: 41 def __repr__(self): return u"A" 42 def __richcmp__(one, other, int op): return one is other 43 def __hash__(self): return id(self) % 65536 44 45def typed(): 46 """ 47 >>> list(typed()) 48 [A, A, A] 49 """ 50 cdef A obj 51 return {obj for obj in {A(), A(), A()}} 52 53def iterdict(): 54 """ 55 >>> sorted(iterdict()) 56 [1, 2, 3] 57 """ 58 cdef dict d = dict(a=1,b=2,c=3) 59 return {d[key] for key in d} 60 61def sorted(it): 62 l = list(it) 63 l.sort() 64 return l 65