02a2cbfeecdcaeaba3683d106a3abd13f25932e1
[cython.git] / tests / run / dictcomp.pyx
1 __doc__ = u"""
2 >>> type(smoketest_dict()) is dict
3 True
4 >>> type(smoketest_list()) is dict
5 True
6
7 >>> sorted(smoketest_dict().items())
8 [(2, 0), (4, 4), (6, 8)]
9 >>> sorted(smoketest_list().items())
10 [(2, 0), (4, 4), (6, 8)]
11
12 >>> list(typed().items())
13 [(A, 1), (A, 1), (A, 1)]
14 >>> sorted(iterdict().items())
15 [(1, 'a'), (2, 'b'), (3, 'c')]
16 """
17
18 cimport cython
19
20 def smoketest_dict():
21     return { x+2:x*2
22              for x in range(5)
23              if x % 2 == 0 }
24
25 @cython.test_fail_if_path_exists(
26     "//ComprehensionNode//ComprehensionAppendNode",
27     "//SimpleCallNode//ComprehensionNode")
28 @cython.test_assert_path_exists(
29     "//ComprehensionNode",
30     "//ComprehensionNode//DictComprehensionAppendNode")
31 def smoketest_list():
32     return dict([ (x+2,x*2)
33                   for x in range(5)
34                   if x % 2 == 0 ])
35
36 cdef class A:
37     def __repr__(self): return u"A"
38     def __richcmp__(one, other, op): return one is other
39     def __hash__(self): return id(self) % 65536
40
41 def typed():
42     cdef A obj
43     return {obj:1 for obj in [A(), A(), A()]}
44
45 def iterdict():
46     cdef dict d = dict(a=1,b=2,c=3)
47     return {d[key]:key for key in d}
48
49 def sorted(it):
50     l = list(it)
51     l.sort()
52     return l