1
2cimport cython
3
4def f(a,b):
5    """
6    >>> f(1,[1,2,3])
7    False
8    >>> f(5,[1,2,3])
9    True
10    >>> f(2,(1,2,3))
11    False
12    """
13    result = a not in b
14    return result
15
16def g(a,b):
17    """
18    >>> g(1,[1,2,3])
19    0
20    >>> g(5,[1,2,3])
21    1
22    >>> g(2,(1,2,3))
23    0
24    """
25    cdef int result
26    result = a not in b
27    return result
28
29def h(b):
30    """
31    >>> h([1,2,3,4])
32    False
33    >>> h([1,3,4])
34    True
35    """
36    result = 2 not in b
37    return result
38
39def j(b):
40    """
41    >>> j([1,2,3,4])
42    0
43    >>> j([1,3,4])
44    1
45    """
46    cdef int result
47    result = 2 not in b
48    return result
49
50@cython.test_fail_if_path_exists("//SwitchStatNode")
51def k(a):
52    """
53    >>> k(1)
54    0
55    >>> k(5)
56    1
57    """
58    cdef int result = a not in [1,2,3,4]
59    return result
60
61@cython.test_assert_path_exists("//SwitchStatNode")
62@cython.test_fail_if_path_exists("//PrimaryCmpNode")
63def m_list(int a):
64    """
65    >>> m_list(2)
66    0
67    >>> m_list(5)
68    1
69    """
70    cdef int result = a not in [1,2,3,4]
71    return result
72
73@cython.test_assert_path_exists("//SwitchStatNode")
74@cython.test_fail_if_path_exists("//PrimaryCmpNode")
75def m_tuple(int a):
76    """
77    >>> m_tuple(2)
78    0
79    >>> m_tuple(5)
80    1
81    """
82    cdef int result = a not in (1,2,3,4)
83    return result
84
85@cython.test_assert_path_exists("//SwitchStatNode")
86@cython.test_fail_if_path_exists("//BoolBinopNode", "//BoolBinopNode", "//PrimaryCmpNode")
87def m_set(int a):
88    """
89    >>> m_set(2)
90    0
91    >>> m_set(5)
92    1
93    """
94    cdef int result = a not in {1,2,3,4}
95    return result
96
97cdef bytes bytes_string = b'abcdefg'
98
99@cython.test_assert_path_exists("//PrimaryCmpNode")
100@cython.test_fail_if_path_exists("//SwitchStatNode", "//BoolBinopNode", "//BoolBinopNode")
101def m_bytes(char a):
102    """
103    >>> m_bytes(ord('f'))
104    0
105    >>> m_bytes(ord('X'))
106    1
107    """
108    cdef int result = a not in bytes_string
109    return result
110
111@cython.test_assert_path_exists("//SwitchStatNode")
112@cython.test_fail_if_path_exists("//BoolBinopNode", "//BoolBinopNode", "//PrimaryCmpNode")
113def m_bytes_literal(char a):
114    """
115    >>> m_bytes_literal(ord('f'))
116    0
117    >>> m_bytes_literal(ord('X'))
118    1
119    """
120    cdef int result = a not in b'abcdefg'
121    return result
122
123cdef unicode unicode_string = u'abcdefg\u1234\uF8D2'
124py_unicode_string = unicode_string
125
126cdef unicode klingon_character = u'\uF8D2'
127py_klingon_character = klingon_character
128
129@cython.test_assert_path_exists("//PrimaryCmpNode")
130@cython.test_fail_if_path_exists("//SwitchStatNode", "//BoolBinopNode", "//BoolBinopNode")
131def m_unicode(Py_UNICODE a, unicode unicode_string):
132    """
133    >>> m_unicode(ord('f'), py_unicode_string)
134    0
135    >>> m_unicode(ord('X'), py_unicode_string)
136    1
137    >>> m_unicode(ord(py_klingon_character), py_unicode_string)
138    0
139    >>> 'f' in None    # doctest: +ELLIPSIS
140    Traceback (most recent call last):
141    TypeError: ...iterable...
142    >>> m_unicode(ord('f'), None)
143    Traceback (most recent call last):
144    TypeError: argument of type 'NoneType' is not iterable
145    """
146    cdef int result = a not in unicode_string
147    return result
148
149@cython.test_assert_path_exists("//SwitchStatNode")
150@cython.test_fail_if_path_exists("//BoolBinopNode", "//BoolBinopNode", "//PrimaryCmpNode")
151def m_unicode_literal(Py_UNICODE a):
152    """
153    >>> m_unicode_literal(ord('f'))
154    0
155    >>> m_unicode_literal(ord('X'))
156    1
157    >>> m_unicode_literal(ord(py_klingon_character))
158    0
159    """
160    cdef int result = a not in u'abcdefg\u1234\uF8D2'
161    return result
162
163@cython.test_assert_path_exists("//SwitchStatNode", "//BoolBinopNode")
164@cython.test_fail_if_path_exists("//PrimaryCmpNode")
165def m_tuple_in_or_notin(int a):
166    """
167    >>> m_tuple_in_or_notin(2)
168    0
169    >>> m_tuple_in_or_notin(3)
170    1
171    >>> m_tuple_in_or_notin(5)
172    1
173    """
174    cdef int result = a not in (1,2,3,4) or a in (3,4)
175    return result
176
177@cython.test_assert_path_exists("//SwitchStatNode", "//BoolBinopNode")
178@cython.test_fail_if_path_exists("//PrimaryCmpNode")
179def m_tuple_notin_or_notin(int a):
180    """
181    >>> m_tuple_notin_or_notin(2)
182    1
183    >>> m_tuple_notin_or_notin(6)
184    1
185    >>> m_tuple_notin_or_notin(4)
186    0
187    """
188    cdef int result = a not in (1,2,3,4) or a not in (4,5)
189    return result
190
191@cython.test_assert_path_exists("//SwitchStatNode")
192@cython.test_fail_if_path_exists("//BoolBinopNode", "//BoolBinopNode", "//PrimaryCmpNode")
193def m_tuple_notin_and_notin(int a):
194    """
195    >>> m_tuple_notin_and_notin(2)
196    0
197    >>> m_tuple_notin_and_notin(6)
198    0
199    >>> m_tuple_notin_and_notin(5)
200    1
201    """
202    cdef int result = a not in (1,2,3,4) and a not in (6,7)
203    return result
204
205@cython.test_assert_path_exists("//SwitchStatNode", "//BoolBinopNode")
206@cython.test_fail_if_path_exists("//PrimaryCmpNode")
207def m_tuple_notin_and_notin_overlap(int a):
208    """
209    >>> m_tuple_notin_and_notin_overlap(2)
210    0
211    >>> m_tuple_notin_and_notin_overlap(4)
212    0
213    >>> m_tuple_notin_and_notin_overlap(5)
214    1
215    """
216    cdef int result = a not in (1,2,3,4) and a not in (3,4)
217    return result
218
219@cython.test_assert_path_exists("//SwitchStatNode")
220@cython.test_fail_if_path_exists("//BoolBinopNode", "//BoolBinopNode", "//PrimaryCmpNode")
221def conditional_int(int a):
222    """
223    >>> conditional_int(1)
224    2
225    >>> conditional_int(0)
226    1
227    >>> conditional_int(5)
228    1
229    """
230    return 1 if a not in (1,2,3,4) else 2
231
232@cython.test_assert_path_exists("//SwitchStatNode")
233@cython.test_fail_if_path_exists("//BoolBinopNode", "//BoolBinopNode", "//PrimaryCmpNode")
234def conditional_object(int a):
235    """
236    >>> conditional_object(1)
237    '2'
238    >>> conditional_object(0)
239    1
240    >>> conditional_object(5)
241    1
242    """
243    return 1 if a not in (1,2,3,4) else '2'
244
245@cython.test_assert_path_exists("//SwitchStatNode")
246@cython.test_fail_if_path_exists("//BoolBinopNode", "//BoolBinopNode", "//PrimaryCmpNode")
247def conditional_bytes(char a):
248    """
249    >>> conditional_bytes(ord('a'))
250    '2'
251    >>> conditional_bytes(ord('X'))
252    1
253    >>> conditional_bytes(0)
254    1
255    """
256    return 1 if a not in b'abc' else '2'
257
258@cython.test_assert_path_exists("//SwitchStatNode")
259@cython.test_fail_if_path_exists("//BoolBinopNode", "//BoolBinopNode", "//PrimaryCmpNode")
260def conditional_unicode(Py_UNICODE a):
261    """
262    >>> conditional_unicode(ord('a'))
263    '2'
264    >>> conditional_unicode(ord('X'))
265    1
266    >>> conditional_unicode(0)
267    1
268    """
269    return 1 if a not in u'abc' else '2'
270
271@cython.test_assert_path_exists("//SwitchStatNode")
272@cython.test_fail_if_path_exists("//BoolBinopNode", "//BoolBinopNode", "//PrimaryCmpNode")
273def conditional_none(int a):
274    """
275    >>> conditional_none(1)
276    1
277    >>> conditional_none(0)
278    >>> conditional_none(5)
279    """
280    return None if a not in {1,2,3,4} else 1
281
282def n(a):
283    """
284    >>> n('d *')
285    0
286    >>> n('xxx')
287    1
288    """
289    cdef int result = a.lower() not in [u'a *',u'b *',u'c *',u'd *']
290    return result
291
292def p(a):
293    """
294    >>> p('a')
295    0
296    >>> p(1)
297    1
298    """
299    cdef dict d = {u'a': 1, u'b': 2}
300    cdef int result = a not in d
301    return result
302
303def q(a):
304    """
305    >>> q(1)
306    Traceback (most recent call last):
307    TypeError: 'NoneType' object is not iterable
308    """
309    cdef dict d = None
310    cdef int result = a not in d # should fail with a TypeError
311    return result
312