Neil Schemenauer | fd288c7 | 2001-01-02 16:30:31 +0000 | [diff] [blame] | 1 | import sys |
| 2 | |
| 3 | from test_support import * |
| 4 | |
| 5 | class Empty: |
| 6 | def __repr__(self): |
| 7 | return '<Empty>' |
| 8 | |
| 9 | class Coerce: |
| 10 | def __init__(self, arg): |
| 11 | self.arg = arg |
| 12 | |
| 13 | def __repr__(self): |
| 14 | return '<Coerce %s>' % self.arg |
| 15 | |
| 16 | def __coerce__(self, other): |
| 17 | if isinstance(other, Coerce): |
| 18 | return self.arg, other.arg |
| 19 | else: |
Neil Schemenauer | e7e694f | 2001-01-03 02:13:26 +0000 | [diff] [blame^] | 20 | return self.arg, other |
Neil Schemenauer | fd288c7 | 2001-01-02 16:30:31 +0000 | [diff] [blame] | 21 | |
| 22 | class Cmp: |
| 23 | def __init__(self,arg): |
| 24 | self.arg = arg |
| 25 | |
| 26 | def __repr__(self): |
| 27 | return '<Cmp %s>' % self.arg |
| 28 | |
| 29 | def __cmp__(self, other): |
| 30 | return cmp(self.arg, other) |
| 31 | |
| 32 | class RCmp: |
| 33 | def __init__(self,arg): |
| 34 | self.arg = arg |
| 35 | |
| 36 | def __repr__(self): |
| 37 | return '<RCmp %s>' % self.arg |
| 38 | |
| 39 | def __rcmp__(self, other): |
| 40 | return cmp(other, self.arg) |
| 41 | |
| 42 | |
Neil Schemenauer | e7e694f | 2001-01-03 02:13:26 +0000 | [diff] [blame^] | 43 | candidates = [2, 2.0, 2L, 2+0j, [1], (3,), None, Empty(), Coerce(2), |
| 44 | Cmp(2.0), RCmp(2L)] |
Neil Schemenauer | fd288c7 | 2001-01-02 16:30:31 +0000 | [diff] [blame] | 45 | |
| 46 | def test(): |
| 47 | for a in candidates: |
| 48 | for b in candidates: |
Neil Schemenauer | fd288c7 | 2001-01-02 16:30:31 +0000 | [diff] [blame] | 49 | try: |
Neil Schemenauer | e7e694f | 2001-01-03 02:13:26 +0000 | [diff] [blame^] | 50 | x = a == b |
Neil Schemenauer | fd288c7 | 2001-01-02 16:30:31 +0000 | [diff] [blame] | 51 | except: |
Neil Schemenauer | e7e694f | 2001-01-03 02:13:26 +0000 | [diff] [blame^] | 52 | print 'cmp(%s, %s) => %s' % (a, b, sys.exc_info()[0]) |
Neil Schemenauer | fd288c7 | 2001-01-02 16:30:31 +0000 | [diff] [blame] | 53 | else: |
Neil Schemenauer | e7e694f | 2001-01-03 02:13:26 +0000 | [diff] [blame^] | 54 | if x: |
| 55 | print "%s == %s" % (a, b) |
| 56 | else: |
| 57 | print "%s != %s" % (a, b) |
Neil Schemenauer | fd288c7 | 2001-01-02 16:30:31 +0000 | [diff] [blame] | 58 | |
| 59 | test() |