| 
									
										
										
										
											2001-01-02 16:30:31 +00:00
										 |  |  | import sys | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Empty: | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							|  |  |  |         return '<Empty>' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Coerce: | 
					
						
							|  |  |  |     def __init__(self, arg): | 
					
						
							|  |  |  |         self.arg = arg | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							|  |  |  |         return '<Coerce %s>' % self.arg | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __coerce__(self, other): | 
					
						
							|  |  |  |         if isinstance(other, Coerce): | 
					
						
							|  |  |  |             return self.arg, other.arg | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2001-01-03 02:13:26 +00:00
										 |  |  |             return self.arg, other | 
					
						
							| 
									
										
										
										
											2001-01-02 16:30:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Cmp: | 
					
						
							|  |  |  |     def __init__(self,arg): | 
					
						
							|  |  |  |         self.arg = arg | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							|  |  |  |         return '<Cmp %s>' % self.arg | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __cmp__(self, other): | 
					
						
							|  |  |  |         return cmp(self.arg, other) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-04 01:34:52 +00:00
										 |  |  | candidates = [2, 2.0, 2L, 2+0j, [1], (3,), None, Empty(), Coerce(2), Cmp(2.0)] | 
					
						
							| 
									
										
										
										
											2001-01-02 16:30:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test(): | 
					
						
							|  |  |  |     for a in candidates: | 
					
						
							|  |  |  |         for b in candidates: | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2001-01-03 02:13:26 +00:00
										 |  |  |                 x = a == b | 
					
						
							| 
									
										
										
										
											2001-01-02 16:30:31 +00:00
										 |  |  |             except: | 
					
						
							| 
									
										
										
										
											2001-01-03 02:13:26 +00:00
										 |  |  |                 print 'cmp(%s, %s) => %s' % (a, b, sys.exc_info()[0]) | 
					
						
							| 
									
										
										
										
											2001-01-02 16:30:31 +00:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2001-01-03 02:13:26 +00:00
										 |  |  |                 if x: | 
					
						
							|  |  |  |                     print "%s == %s" % (a, b) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     print "%s != %s" % (a, b) | 
					
						
							| 
									
										
										
										
											2001-08-15 21:02:20 +00:00
										 |  |  |     # Ensure default comparison compares id() of args | 
					
						
							| 
									
										
										
										
											2001-08-16 16:56:16 +00:00
										 |  |  |     L = [] | 
					
						
							| 
									
										
										
										
											2001-08-15 21:02:20 +00:00
										 |  |  |     for i in range(10): | 
					
						
							| 
									
										
										
										
											2001-09-04 19:14:14 +00:00
										 |  |  |         L.insert(len(L)//2, Empty()) | 
					
						
							| 
									
										
										
										
											2001-08-15 21:02:20 +00:00
										 |  |  |     for a in L: | 
					
						
							|  |  |  |         for b in L: | 
					
						
							|  |  |  |             if cmp(a, b) != cmp(id(a), id(b)): | 
					
						
							|  |  |  |                 print "ERROR:", cmp(a, b), cmp(id(a), id(b)), id(a), id(b) | 
					
						
							| 
									
										
										
										
											2001-01-02 16:30:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | test() |