cython
1# mode: run
2
3import cython
4compiled = cython.compiled
5
6
7@cython.cclass
8class X(object):
9x = cython.declare(cython.int)
10
11def __init__(self, x):
12self.x = x
13
14def __repr__(self):
15return "<%d>" % self.x
16
17
18@cython.cfunc
19@cython.locals(x=X)
20def x_of(x):
21return x.x
22
23
24@cython.cclass
25class ClassEq(X):
26"""
27>>> a = ClassEq(1)
28>>> b = ClassEq(2)
29>>> c = ClassEq(1)
30>>> a == a
31True
32>>> a != a
33False
34
35>>> a == b
36False
37>>> a != b
38True
39
40>>> a == c
41True
42>>> a != c
43False
44
45>>> b == c
46False
47>>> b != c
48True
49
50>>> c == a
51True
52>>> c != a
53False
54
55>>> b == a
56False
57>>> b != a
58True
59
60>>> a < b # doctest: +ELLIPSIS
61Traceback (most recent call last):
62TypeError...
63>>> a > b # doctest: +ELLIPSIS
64Traceback (most recent call last):
65TypeError...
66>>> a <= b # doctest: +ELLIPSIS
67Traceback (most recent call last):
68TypeError...
69>>> a >= b # doctest: +ELLIPSIS
70Traceback (most recent call last):
71TypeError...
72
73>>> print(a.__eq__.__doc__)
74EQ
75"""
76def __eq__(self, other):
77"""EQ"""
78assert 1 <= self.x <= 2
79assert isinstance(self, ClassEq), type(self)
80if isinstance(other, X):
81return self.x == x_of(other)
82elif isinstance(other, int):
83return self.x < other
84return NotImplemented
85
86
87@cython.cclass
88class ClassEqNe(ClassEq):
89"""
90>>> a = ClassEqNe(1)
91>>> b = ClassEqNe(2)
92>>> c = ClassEqNe(1)
93>>> a == a
94True
95>>> a != a
96False
97
98>>> a == b
99False
100>>> a != b
101True
102
103>>> a == c
104True
105>>> a != c
106False
107
108>>> b == c
109False
110>>> b != c
111True
112
113>>> c == a
114True
115>>> c != a
116False
117
118>>> b == a
119False
120>>> b != a
121True
122
123>>> a < b # doctest: +ELLIPSIS
124Traceback (most recent call last):
125TypeError...
126>>> a > b # doctest: +ELLIPSIS
127Traceback (most recent call last):
128TypeError...
129>>> a <= b # doctest: +ELLIPSIS
130Traceback (most recent call last):
131TypeError...
132>>> a >= b # doctest: +ELLIPSIS
133Traceback (most recent call last):
134TypeError...
135
136#>>> print(a.__eq__.__doc__)
137#EQ
138>>> print(a.__ne__.__doc__)
139NE
140"""
141def __ne__(self, other):
142"""NE"""
143assert 1 <= self.x <= 2
144assert isinstance(self, ClassEqNe), type(self)
145if isinstance(other, X):
146return self.x != x_of(other)
147elif isinstance(other, int):
148return self.x < other
149return NotImplemented
150
151
152@cython.cclass
153class ClassEqNeGe(ClassEqNe):
154"""
155>>> a = ClassEqNeGe(1)
156>>> b = ClassEqNeGe(2)
157>>> c = ClassEqNeGe(1)
158>>> a == a
159True
160>>> a != a
161False
162>>> a >= a
163True
164>>> a <= a
165True
166
167>>> a == b
168False
169>>> a != b
170True
171>>> a >= b
172False
173>>> b <= a
174False
175
176>>> a == c
177True
178>>> a != c
179False
180>>> a >= c
181True
182>>> c <= a
183True
184
185>>> b == c
186False
187>>> b != c
188True
189>>> b >= c
190True
191>>> c <= b
192True
193
194>>> c == a
195True
196>>> c != a
197False
198>>> c >= a
199True
200>>> a <= c
201True
202
203>>> b == a
204False
205>>> b != a
206True
207>>> b >= a
208True
209>>> a <= b
210True
211
212>>> a < b # doctest: +ELLIPSIS
213Traceback (most recent call last):
214TypeError...
215>>> a > b # doctest: +ELLIPSIS
216Traceback (most recent call last):
217TypeError...
218
219>>> 2 <= a
220False
221>>> a >= 2
222False
223>>> 1 <= a
224True
225>>> a >= 1
226True
227>>> a >= 2
228False
229
230>>> 'x' <= a # doctest: +ELLIPSIS
231Traceback (most recent call last):
232TypeError...
233>>> a >= 'x' # doctest: +ELLIPSIS
234Traceback (most recent call last):
235TypeError...
236
237#>>> print(a.__eq__.__doc__)
238#EQ
239#>>> print(a.__ne__.__doc__)
240#NE
241>>> print(a.__ge__.__doc__)
242GE
243"""
244def __ge__(self, other):
245"""GE"""
246assert 1 <= self.x <= 2
247assert isinstance(self, ClassEqNeGe), type(self)
248if isinstance(other, X):
249return self.x >= x_of(other)
250elif isinstance(other, int):
251return self.x >= other
252return NotImplemented
253
254
255@cython.cclass
256class ClassRichcmpOverride(ClassEqNeGe):
257"""
258>>> a = ClassRichcmpOverride(1)
259>>> b = ClassRichcmpOverride(1)
260
261>>> a == a
262True
263>>> a != a
264False
265
266>>> a != b if compiled else a == b # Python ignores __richcmp__()
267True
268>>> a == b if compiled else a != b # Python ignores __richcmp__()
269False
270
271>>> if not compiled: raise TypeError # doctest: +ELLIPSIS
272... else: a >= b # should no longer work when __richcmp__ is overwritten
273Traceback (most recent call last):
274TypeError...
275"""
276def __richcmp__(self, other, op):
277return NotImplemented
278
279
280@cython.cclass
281class ClassLe(X):
282"""
283>>> a = ClassLe(1)
284>>> b = ClassLe(2)
285>>> c = ClassLe(1)
286
287>>> a <= b
288True
289>>> b >= a
290True
291>>> b <= a
292False
293>>> a >= b
294False
295
296>>> a <= c
297True
298>>> c >= a
299True
300>>> c <= a
301True
302>>> a >= c
303True
304
305>>> b <= c
306False
307>>> c >= b
308False
309>>> c <= b
310True
311>>> b >= c
312True
313
314>>> 2 >= a
315True
316>>> a <= 2
317True
318>>> 1 >= a
319True
320>>> a <= 1
321True
322>>> a <= 0
323False
324
325>>> 'x' >= a # doctest: +ELLIPSIS
326Traceback (most recent call last):
327TypeError...
328>>> a <= 'x' # doctest: +ELLIPSIS
329Traceback (most recent call last):
330TypeError...
331"""
332def __le__(self, other):
333assert 1 <= self.x <= 2
334assert isinstance(self, ClassLe), type(self)
335if isinstance(other, X):
336return self.x <= x_of(other)
337elif isinstance(other, int):
338return self.x <= other
339return NotImplemented
340
341
342@cython.cclass
343class ClassLt(X):
344"""
345>>> a = ClassLt(1)
346>>> b = ClassLt(2)
347>>> c = ClassLt(1)
348
349>>> a < b
350True
351>>> b > a
352True
353>>> b < a
354False
355>>> a > b
356False
357
358>>> a < c
359False
360>>> c > a
361False
362>>> c < a
363False
364>>> a > c
365False
366
367>>> b < c
368False
369>>> c > b
370False
371>>> c < b
372True
373>>> b > c
374True
375
376>>> sorted([a, b, c])
377[<1>, <1>, <2>]
378>>> sorted([b, a, c])
379[<1>, <1>, <2>]
380
381>>> 2 > a
382True
383>>> a < 2
384True
385>>> 1 > a
386False
387>>> a < 1
388False
389
390>>> 1 < a # doctest: +ELLIPSIS
391Traceback (most recent call last):
392TypeError...
393
394>>> 'x' > a # doctest: +ELLIPSIS
395Traceback (most recent call last):
396TypeError...
397>>> a < 'x' # doctest: +ELLIPSIS
398Traceback (most recent call last):
399TypeError...
400"""
401def __lt__(self, other):
402assert 1 <= self.x <= 2
403assert isinstance(self, ClassLt), type(self)
404if isinstance(other, X):
405return self.x < x_of(other)
406elif isinstance(other, int):
407return self.x < other
408return NotImplemented
409
410
411@cython.cclass
412class ClassLtGtInherited(X):
413"""
414>>> a = ClassLtGtInherited(1)
415>>> b = ClassLtGtInherited(2)
416>>> c = ClassLtGtInherited(1)
417
418>>> a < b
419True
420>>> b > a
421True
422>>> b < a
423False
424>>> a > b
425False
426
427>>> a < c
428False
429>>> c > a
430False
431>>> c < a
432False
433>>> a > c
434False
435
436>>> b < c
437False
438>>> c > b
439False
440>>> c < b
441True
442>>> b > c
443True
444
445>>> sorted([a, b, c])
446[<1>, <1>, <2>]
447>>> sorted([b, a, c])
448[<1>, <1>, <2>]
449"""
450def __gt__(self, other):
451assert 1 <= self.x <= 2
452assert isinstance(self, ClassLtGtInherited), type(self)
453if isinstance(other, X):
454return self.x > x_of(other)
455elif isinstance(other, int):
456return self.x > other
457return NotImplemented
458
459
460@cython.cclass
461class ClassLtGt(X):
462"""
463>>> a = ClassLtGt(1)
464>>> b = ClassLtGt(2)
465>>> c = ClassLtGt(1)
466
467>>> a < b
468True
469>>> b > a
470True
471>>> b < a
472False
473>>> a > b
474False
475
476>>> a < c
477False
478>>> c > a
479False
480>>> c < a
481False
482>>> a > c
483False
484
485>>> b < c
486False
487>>> c > b
488False
489>>> c < b
490True
491>>> b > c
492True
493
494>>> sorted([a, b, c])
495[<1>, <1>, <2>]
496>>> sorted([b, a, c])
497[<1>, <1>, <2>]
498
499>>> 2 > a
500True
501>>> 2 < a
502False
503>>> a < 2
504True
505>>> a > 2
506False
507
508>>> 'x' > a # doctest: +ELLIPSIS
509Traceback (most recent call last):
510TypeError...
511>>> 'x' < a # doctest: +ELLIPSIS
512Traceback (most recent call last):
513TypeError...
514>>> a < 'x' # doctest: +ELLIPSIS
515Traceback (most recent call last):
516TypeError...
517>>> a > 'x' # doctest: +ELLIPSIS
518Traceback (most recent call last):
519TypeError...
520"""
521def __lt__(self, other):
522assert 1 <= self.x <= 2
523assert isinstance(self, ClassLtGt), type(self)
524if isinstance(other, X):
525return self.x < x_of(other)
526elif isinstance(other, int):
527return self.x < other
528return NotImplemented
529
530def __gt__(self, other):
531assert 1 <= self.x <= 2
532assert isinstance(self, ClassLtGt), type(self)
533if isinstance(other, X):
534return self.x > x_of(other)
535elif isinstance(other, int):
536return self.x > other
537return NotImplemented
538
539
540@cython.cclass
541class List(list):
542"""
543>>> l = [1, 2, 3, 4]
544>>> notl = List(l)
545>>> notl == l
546False
547>>> notl != l # implemented by base type
548False
549>>> notl == notl
550True
551>>> notl != notl # implemented by base type
552False
553"""
554def __eq__(self, other):
555return self is other or list(self) != list(other)
556