Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | import sys |
2 | ||
3 | class Empty: | |
4 | def __repr__(self): | |
5 | return '<Empty>' | |
6 | ||
7 | class Coerce: | |
8 | def __init__(self, arg): | |
9 | self.arg = arg | |
10 | ||
11 | def __repr__(self): | |
12 | return '<Coerce %s>' % self.arg | |
13 | ||
14 | def __coerce__(self, other): | |
15 | if isinstance(other, Coerce): | |
16 | return self.arg, other.arg | |
17 | else: | |
18 | return self.arg, other | |
19 | ||
20 | class Cmp: | |
21 | def __init__(self,arg): | |
22 | self.arg = arg | |
23 | ||
24 | def __repr__(self): | |
25 | return '<Cmp %s>' % self.arg | |
26 | ||
27 | def __cmp__(self, other): | |
28 | return cmp(self.arg, other) | |
29 | ||
30 | ||
31 | candidates = [2, 2.0, 2L, 2+0j, [1], (3,), None, Empty(), Coerce(2), Cmp(2.0)] | |
32 | ||
33 | def test(): | |
34 | for a in candidates: | |
35 | for b in candidates: | |
36 | try: | |
37 | x = a == b | |
38 | except: | |
39 | print 'cmp(%s, %s) => %s' % (a, b, sys.exc_info()[0]) | |
40 | else: | |
41 | if x: | |
42 | print "%s == %s" % (a, b) | |
43 | else: | |
44 | print "%s != %s" % (a, b) | |
45 | # Ensure default comparison compares id() of args | |
46 | L = [] | |
47 | for i in range(10): | |
48 | L.insert(len(L)//2, Empty()) | |
49 | for a in L: | |
50 | for b in L: | |
51 | if cmp(a, b) != cmp(id(a), id(b)): | |
52 | print "ERROR:", cmp(a, b), cmp(id(a), id(b)), id(a), id(b) | |
53 | ||
54 | test() |