| 
									
										
										
										
											2001-01-18 15:48:05 +00:00
										 |  |  | # Tests for rich comparisons | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  | from test import support | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import operator | 
					
						
							| 
									
										
										
										
											2001-01-18 15:48:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Number: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, x): | 
					
						
							|  |  |  |         self.x = x | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __lt__(self, other): | 
					
						
							|  |  |  |         return self.x < other | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __le__(self, other): | 
					
						
							|  |  |  |         return self.x <= other | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __eq__(self, other): | 
					
						
							|  |  |  |         return self.x == other | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __ne__(self, other): | 
					
						
							|  |  |  |         return self.x != other | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __gt__(self, other): | 
					
						
							|  |  |  |         return self.x > other | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __ge__(self, other): | 
					
						
							|  |  |  |         return self.x >= other | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __cmp__(self, other): | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  |         raise support.TestFailed("Number.__cmp__() should not be called") | 
					
						
							| 
									
										
										
										
											2001-01-18 15:48:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |         return "Number(%r)" % (self.x, ) | 
					
						
							| 
									
										
										
										
											2001-01-18 15:48:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Vector: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, data): | 
					
						
							|  |  |  |         self.data = data | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __len__(self): | 
					
						
							|  |  |  |         return len(self.data) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __getitem__(self, i): | 
					
						
							|  |  |  |         return self.data[i] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __setitem__(self, i, v): | 
					
						
							|  |  |  |         self.data[i] = v | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-15 15:46:38 +00:00
										 |  |  |     __hash__ = None # Vectors cannot be hashed | 
					
						
							| 
									
										
										
										
											2001-01-18 15:48:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-11-28 19:15:13 +00:00
										 |  |  |     def __bool__(self): | 
					
						
							| 
									
										
										
										
											2007-08-29 23:37:32 +00:00
										 |  |  |         raise TypeError("Vectors cannot be used in Boolean contexts") | 
					
						
							| 
									
										
										
										
											2001-01-18 15:48:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __cmp__(self, other): | 
					
						
							| 
									
										
										
										
											2008-05-20 21:35:26 +00:00
										 |  |  |         raise support.TestFailed("Vector.__cmp__() should not be called") | 
					
						
							| 
									
										
										
										
											2001-01-18 15:48:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |         return "Vector(%r)" % (self.data, ) | 
					
						
							| 
									
										
										
										
											2001-01-18 15:48:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __lt__(self, other): | 
					
						
							|  |  |  |         return Vector([a < b for a, b in zip(self.data, self.__cast(other))]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __le__(self, other): | 
					
						
							|  |  |  |         return Vector([a <= b for a, b in zip(self.data, self.__cast(other))]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __eq__(self, other): | 
					
						
							|  |  |  |         return Vector([a == b for a, b in zip(self.data, self.__cast(other))]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __ne__(self, other): | 
					
						
							|  |  |  |         return Vector([a != b for a, b in zip(self.data, self.__cast(other))]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __gt__(self, other): | 
					
						
							|  |  |  |         return Vector([a > b for a, b in zip(self.data, self.__cast(other))]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __ge__(self, other): | 
					
						
							|  |  |  |         return Vector([a >= b for a, b in zip(self.data, self.__cast(other))]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __cast(self, other): | 
					
						
							|  |  |  |         if isinstance(other, Vector): | 
					
						
							|  |  |  |             other = other.data | 
					
						
							|  |  |  |         if len(self.data) != len(other): | 
					
						
							| 
									
										
										
										
											2007-08-29 23:37:32 +00:00
										 |  |  |             raise ValueError("Cannot compare vectors of different length") | 
					
						
							| 
									
										
										
										
											2001-01-18 15:48:05 +00:00
										 |  |  |         return other | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  | opmap = { | 
					
						
							|  |  |  |     "lt": (lambda a,b: a< b, operator.lt, operator.__lt__), | 
					
						
							|  |  |  |     "le": (lambda a,b: a<=b, operator.le, operator.__le__), | 
					
						
							|  |  |  |     "eq": (lambda a,b: a==b, operator.eq, operator.__eq__), | 
					
						
							|  |  |  |     "ne": (lambda a,b: a!=b, operator.ne, operator.__ne__), | 
					
						
							|  |  |  |     "gt": (lambda a,b: a> b, operator.gt, operator.__gt__), | 
					
						
							|  |  |  |     "ge": (lambda a,b: a>=b, operator.ge, operator.__ge__) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class VectorTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def checkfail(self, error, opname, *args): | 
					
						
							|  |  |  |         for op in opmap[opname]: | 
					
						
							|  |  |  |             self.assertRaises(error, op, *args) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def checkequal(self, opname, a, b, expres): | 
					
						
							|  |  |  |         for op in opmap[opname]: | 
					
						
							|  |  |  |             realres = op(a, b) | 
					
						
							|  |  |  |             # can't use assertEqual(realres, expres) here | 
					
						
							|  |  |  |             self.assertEqual(len(realres), len(expres)) | 
					
						
							| 
									
										
										
										
											2007-05-07 22:24:25 +00:00
										 |  |  |             for i in range(len(realres)): | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |                 # results are bool, so we can use "is" here | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |                 self.assertTrue(realres[i] is expres[i]) | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_mixed(self): | 
					
						
							|  |  |  |         # check that comparisons involving Vector objects | 
					
						
							|  |  |  |         # which return rich results (i.e. Vectors with itemwise | 
					
						
							|  |  |  |         # comparison results) work | 
					
						
							|  |  |  |         a = Vector(range(2)) | 
					
						
							|  |  |  |         b = Vector(range(3)) | 
					
						
							|  |  |  |         # all comparisons should fail for different length | 
					
						
							|  |  |  |         for opname in opmap: | 
					
						
							|  |  |  |             self.checkfail(ValueError, opname, a, b) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-07 22:24:25 +00:00
										 |  |  |         a = list(range(5)) | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |         b = 5 * [2] | 
					
						
							|  |  |  |         # try mixed arguments (but not (a, b) as that won't return a bool vector) | 
					
						
							|  |  |  |         args = [(a, Vector(b)), (Vector(a), b), (Vector(a), Vector(b))] | 
					
						
							|  |  |  |         for (a, b) in args: | 
					
						
							|  |  |  |             self.checkequal("lt", a, b, [True,  True,  False, False, False]) | 
					
						
							|  |  |  |             self.checkequal("le", a, b, [True,  True,  True,  False, False]) | 
					
						
							|  |  |  |             self.checkequal("eq", a, b, [False, False, True,  False, False]) | 
					
						
							|  |  |  |             self.checkequal("ne", a, b, [True,  True,  False, True,  True ]) | 
					
						
							|  |  |  |             self.checkequal("gt", a, b, [False, False, False, True,  True ]) | 
					
						
							|  |  |  |             self.checkequal("ge", a, b, [False, False, True,  True,  True ]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-02-11 06:12:03 +00:00
										 |  |  |             for ops in opmap.values(): | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |                 for op in ops: | 
					
						
							| 
									
										
										
										
											2006-11-28 19:15:13 +00:00
										 |  |  |                     # calls __bool__, which should fail | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |                     self.assertRaises(TypeError, bool, op(a, b)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class NumberTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_basic(self): | 
					
						
							|  |  |  |         # Check that comparisons involving Number objects | 
					
						
							|  |  |  |         # give the same results give as comparing the | 
					
						
							|  |  |  |         # corresponding ints | 
					
						
							| 
									
										
										
										
											2007-05-07 22:24:25 +00:00
										 |  |  |         for a in range(3): | 
					
						
							|  |  |  |             for b in range(3): | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |                 for typea in (int, Number): | 
					
						
							|  |  |  |                     for typeb in (int, Number): | 
					
						
							|  |  |  |                         if typea==typeb==int: | 
					
						
							|  |  |  |                             continue # the combination int, int is useless | 
					
						
							|  |  |  |                         ta = typea(a) | 
					
						
							|  |  |  |                         tb = typeb(b) | 
					
						
							| 
									
										
										
										
											2007-02-11 06:12:03 +00:00
										 |  |  |                         for ops in opmap.values(): | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |                             for op in ops: | 
					
						
							|  |  |  |                                 realoutcome = op(a, b) | 
					
						
							|  |  |  |                                 testoutcome = op(ta, tb) | 
					
						
							|  |  |  |                                 self.assertEqual(realoutcome, testoutcome) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def checkvalue(self, opname, a, b, expres): | 
					
						
							|  |  |  |         for typea in (int, Number): | 
					
						
							|  |  |  |             for typeb in (int, Number): | 
					
						
							|  |  |  |                 ta = typea(a) | 
					
						
							|  |  |  |                 tb = typeb(b) | 
					
						
							|  |  |  |                 for op in opmap[opname]: | 
					
						
							|  |  |  |                     realres = op(ta, tb) | 
					
						
							|  |  |  |                     realres = getattr(realres, "x", realres) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |                     self.assertTrue(realres is expres) | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_values(self): | 
					
						
							|  |  |  |         # check all operators and all comparison results | 
					
						
							|  |  |  |         self.checkvalue("lt", 0, 0, False) | 
					
						
							|  |  |  |         self.checkvalue("le", 0, 0, True ) | 
					
						
							|  |  |  |         self.checkvalue("eq", 0, 0, True ) | 
					
						
							|  |  |  |         self.checkvalue("ne", 0, 0, False) | 
					
						
							|  |  |  |         self.checkvalue("gt", 0, 0, False) | 
					
						
							|  |  |  |         self.checkvalue("ge", 0, 0, True ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.checkvalue("lt", 0, 1, True ) | 
					
						
							|  |  |  |         self.checkvalue("le", 0, 1, True ) | 
					
						
							|  |  |  |         self.checkvalue("eq", 0, 1, False) | 
					
						
							|  |  |  |         self.checkvalue("ne", 0, 1, True ) | 
					
						
							|  |  |  |         self.checkvalue("gt", 0, 1, False) | 
					
						
							|  |  |  |         self.checkvalue("ge", 0, 1, False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.checkvalue("lt", 1, 0, False) | 
					
						
							|  |  |  |         self.checkvalue("le", 1, 0, False) | 
					
						
							|  |  |  |         self.checkvalue("eq", 1, 0, False) | 
					
						
							|  |  |  |         self.checkvalue("ne", 1, 0, True ) | 
					
						
							|  |  |  |         self.checkvalue("gt", 1, 0, True ) | 
					
						
							|  |  |  |         self.checkvalue("ge", 1, 0, True ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MiscTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_misbehavin(self): | 
					
						
							|  |  |  |         class Misb: | 
					
						
							| 
									
										
											  
											
												Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r78018 | georg.brandl | 2010-02-06 11:08:21 +0100 (Sa, 06 Feb 2010) | 1 line
  #7864: make deprecation notices a bit clearer.
........
  r78035 | georg.brandl | 2010-02-06 23:44:17 +0100 (Sa, 06 Feb 2010) | 1 line
  Fix duplicate import.
........
  r78036 | georg.brandl | 2010-02-06 23:49:47 +0100 (Sa, 06 Feb 2010) | 1 line
  Remove unused import.
........
  r78037 | georg.brandl | 2010-02-06 23:59:15 +0100 (Sa, 06 Feb 2010) | 1 line
  No need to assign the results of expressions used only for side effects.
........
  r78038 | georg.brandl | 2010-02-07 00:02:29 +0100 (So, 07 Feb 2010) | 1 line
  Add a missing import.
........
  r78039 | georg.brandl | 2010-02-07 00:06:24 +0100 (So, 07 Feb 2010) | 1 line
  Add missing imports.
........
  r78040 | georg.brandl | 2010-02-07 00:08:00 +0100 (So, 07 Feb 2010) | 1 line
  Fix a few UnboundLocalErrors in test_long.
........
  r78042 | georg.brandl | 2010-02-07 00:12:12 +0100 (So, 07 Feb 2010) | 1 line
  Add missing import.
........
  r78043 | georg.brandl | 2010-02-07 00:12:19 +0100 (So, 07 Feb 2010) | 1 line
  Remove duplicate test method.
........
  r78046 | georg.brandl | 2010-02-07 00:18:00 +0100 (So, 07 Feb 2010) | 1 line
  Fix various missing import/unbound name errors.
........
  r78048 | georg.brandl | 2010-02-07 00:23:45 +0100 (So, 07 Feb 2010) | 1 line
  We heard you like test failures so we put unbound locals in your test so that you can fail while you fail.
........
  r78049 | georg.brandl | 2010-02-07 00:33:33 +0100 (So, 07 Feb 2010) | 1 line
  Fix import/access for some identifiers.  _TestSharedCTypes does not seem to be executed?
........
  r78050 | georg.brandl | 2010-02-07 00:34:10 +0100 (So, 07 Feb 2010) | 1 line
  Fix more unbound locals in code paths that do not seem to be used.
........
  r78051 | georg.brandl | 2010-02-07 00:53:52 +0100 (So, 07 Feb 2010) | 1 line
  Add missing import when running these tests standalone.
........
  r78052 | georg.brandl | 2010-02-07 00:54:04 +0100 (So, 07 Feb 2010) | 1 line
  Add missing import when running these tests standalone.
........
  r78054 | georg.brandl | 2010-02-07 00:58:25 +0100 (So, 07 Feb 2010) | 1 line
  Add missing import.
........
  r78059 | georg.brandl | 2010-02-07 12:34:15 +0100 (So, 07 Feb 2010) | 1 line
  Use "regexp" consistently.
........
  r78075 | georg.brandl | 2010-02-07 13:16:12 +0100 (So, 07 Feb 2010) | 1 line
  Fix another duplicated test method.
........
  r78076 | georg.brandl | 2010-02-07 13:19:43 +0100 (So, 07 Feb 2010) | 1 line
  Fix wrong usage of "except X, Y:".
........
  r78077 | georg.brandl | 2010-02-07 13:25:50 +0100 (So, 07 Feb 2010) | 1 line
  Fix two redefined test methods.
........
  r78078 | georg.brandl | 2010-02-07 13:27:06 +0100 (So, 07 Feb 2010) | 1 line
  Fix a redefined test method.
........
  r78079 | georg.brandl | 2010-02-07 13:34:26 +0100 (So, 07 Feb 2010) | 1 line
  Add a minimal test for fnmatchcase().
........
  r78080 | georg.brandl | 2010-02-07 13:55:12 +0100 (So, 07 Feb 2010) | 1 line
  Remove duplicate test method.
........
											
										 
											2010-03-14 10:23:39 +00:00
										 |  |  |             def __lt__(self_, other): return 0 | 
					
						
							|  |  |  |             def __gt__(self_, other): return 0 | 
					
						
							|  |  |  |             def __eq__(self_, other): return 0 | 
					
						
							|  |  |  |             def __le__(self_, other): self.fail("This shouldn't happen") | 
					
						
							|  |  |  |             def __ge__(self_, other): self.fail("This shouldn't happen") | 
					
						
							|  |  |  |             def __ne__(self_, other): self.fail("This shouldn't happen") | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |         a = Misb() | 
					
						
							|  |  |  |         b = Misb() | 
					
						
							|  |  |  |         self.assertEqual(a<b, 0) | 
					
						
							|  |  |  |         self.assertEqual(a==b, 0) | 
					
						
							|  |  |  |         self.assertEqual(a>b, 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_not(self): | 
					
						
							| 
									
										
										
										
											2006-11-28 19:15:13 +00:00
										 |  |  |         # Check that exceptions in __bool__ are properly | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |         # propagated by the not operator | 
					
						
							|  |  |  |         import operator | 
					
						
							| 
									
										
										
										
											2006-03-24 07:02:16 +00:00
										 |  |  |         class Exc(Exception): | 
					
						
							| 
									
										
										
										
											2001-01-18 15:48:05 +00:00
										 |  |  |             pass | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |         class Bad: | 
					
						
							| 
									
										
										
										
											2006-11-28 19:15:13 +00:00
										 |  |  |             def __bool__(self): | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |                 raise Exc | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def do(bad): | 
					
						
							|  |  |  |             not bad | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for func in (do, operator.not_): | 
					
						
							|  |  |  |             self.assertRaises(Exc, func, Bad()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-21 19:29:56 +00:00
										 |  |  |     @support.no_tracing | 
					
						
							| 
									
										
										
										
											2023-11-17 15:52:11 -08:00
										 |  |  |     @support.infinite_recursion() | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |     def test_recursion(self): | 
					
						
							| 
									
										
										
										
											2003-10-28 12:05:48 +00:00
										 |  |  |         # Check that comparison for recursive objects fails gracefully | 
					
						
							| 
									
										
										
										
											2008-02-12 20:03:09 +00:00
										 |  |  |         from collections import UserList | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |         a = UserList() | 
					
						
							|  |  |  |         b = UserList() | 
					
						
							|  |  |  |         a.append(b) | 
					
						
							|  |  |  |         b.append(a) | 
					
						
							| 
									
										
										
										
											2015-07-03 01:04:23 -04:00
										 |  |  |         self.assertRaises(RecursionError, operator.eq, a, b) | 
					
						
							|  |  |  |         self.assertRaises(RecursionError, operator.ne, a, b) | 
					
						
							|  |  |  |         self.assertRaises(RecursionError, operator.lt, a, b) | 
					
						
							|  |  |  |         self.assertRaises(RecursionError, operator.le, a, b) | 
					
						
							|  |  |  |         self.assertRaises(RecursionError, operator.gt, a, b) | 
					
						
							|  |  |  |         self.assertRaises(RecursionError, operator.ge, a, b) | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         b.append(17) | 
					
						
							| 
									
										
										
										
											2003-10-28 12:05:48 +00:00
										 |  |  |         # Even recursive lists of different lengths are different, | 
					
						
							|  |  |  |         # but they cannot be ordered | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |         self.assertTrue(not (a == b)) | 
					
						
							|  |  |  |         self.assertTrue(a != b) | 
					
						
							| 
									
										
										
										
											2015-07-03 01:04:23 -04:00
										 |  |  |         self.assertRaises(RecursionError, operator.lt, a, b) | 
					
						
							|  |  |  |         self.assertRaises(RecursionError, operator.le, a, b) | 
					
						
							|  |  |  |         self.assertRaises(RecursionError, operator.gt, a, b) | 
					
						
							|  |  |  |         self.assertRaises(RecursionError, operator.ge, a, b) | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |         a.append(17) | 
					
						
							| 
									
										
										
										
											2015-07-03 01:04:23 -04:00
										 |  |  |         self.assertRaises(RecursionError, operator.eq, a, b) | 
					
						
							|  |  |  |         self.assertRaises(RecursionError, operator.ne, a, b) | 
					
						
							| 
									
										
										
										
											2003-10-28 12:05:48 +00:00
										 |  |  |         a.insert(0, 11) | 
					
						
							|  |  |  |         b.insert(0, 12) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |         self.assertTrue(not (a == b)) | 
					
						
							|  |  |  |         self.assertTrue(a != b) | 
					
						
							|  |  |  |         self.assertTrue(a < b) | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-22 07:49:36 +03:00
										 |  |  |     def test_exception_message(self): | 
					
						
							|  |  |  |         class Spam: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         tests = [ | 
					
						
							|  |  |  |             (lambda: 42 < None, r"'<' .* of 'int' and 'NoneType'"), | 
					
						
							|  |  |  |             (lambda: None < 42, r"'<' .* of 'NoneType' and 'int'"), | 
					
						
							|  |  |  |             (lambda: 42 > None, r"'>' .* of 'int' and 'NoneType'"), | 
					
						
							|  |  |  |             (lambda: "foo" < None, r"'<' .* of 'str' and 'NoneType'"), | 
					
						
							|  |  |  |             (lambda: "foo" >= 666, r"'>=' .* of 'str' and 'int'"), | 
					
						
							|  |  |  |             (lambda: 42 <= None, r"'<=' .* of 'int' and 'NoneType'"), | 
					
						
							|  |  |  |             (lambda: 42 >= None, r"'>=' .* of 'int' and 'NoneType'"), | 
					
						
							|  |  |  |             (lambda: 42 < [], r"'<' .* of 'int' and 'list'"), | 
					
						
							|  |  |  |             (lambda: () > [], r"'>' .* of 'tuple' and 'list'"), | 
					
						
							|  |  |  |             (lambda: None >= None, r"'>=' .* of 'NoneType' and 'NoneType'"), | 
					
						
							|  |  |  |             (lambda: Spam() < 42, r"'<' .* of 'Spam' and 'int'"), | 
					
						
							|  |  |  |             (lambda: 42 < Spam(), r"'<' .* of 'int' and 'Spam'"), | 
					
						
							|  |  |  |             (lambda: Spam() <= Spam(), r"'<=' .* of 'Spam' and 'Spam'"), | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |         for i, test in enumerate(tests): | 
					
						
							|  |  |  |             with self.subTest(test=i): | 
					
						
							|  |  |  |                 with self.assertRaisesRegex(TypeError, test[1]): | 
					
						
							|  |  |  |                     test[0]() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  | class DictTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_dicts(self): | 
					
						
							|  |  |  |         # Verify that __eq__ and __ne__ work for dicts even if the keys and | 
					
						
							| 
									
										
										
										
											2005-08-24 09:08:57 +00:00
										 |  |  |         # values don't support anything other than __eq__ and __ne__ (and | 
					
						
							|  |  |  |         # __hash__).  Complex numbers are a fine example of that. | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |         import random | 
					
						
							|  |  |  |         imag1a = {} | 
					
						
							|  |  |  |         for i in range(50): | 
					
						
							|  |  |  |             imag1a[random.randrange(100)*1j] = random.randrange(100)*1j | 
					
						
							| 
									
										
										
										
											2007-02-15 04:01:01 +00:00
										 |  |  |         items = list(imag1a.items()) | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |         random.shuffle(items) | 
					
						
							|  |  |  |         imag1b = {} | 
					
						
							|  |  |  |         for k, v in items: | 
					
						
							|  |  |  |             imag1b[k] = v | 
					
						
							|  |  |  |         imag2 = imag1b.copy() | 
					
						
							|  |  |  |         imag2[k] = v + 1.0 | 
					
						
							| 
									
										
										
										
											2007-07-11 12:20:59 +00:00
										 |  |  |         self.assertEqual(imag1a, imag1a) | 
					
						
							|  |  |  |         self.assertEqual(imag1a, imag1b) | 
					
						
							|  |  |  |         self.assertEqual(imag2, imag2) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |         self.assertTrue(imag1a != imag2) | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |         for opname in ("lt", "le", "gt", "ge"): | 
					
						
							|  |  |  |             for op in opmap[opname]: | 
					
						
							|  |  |  |                 self.assertRaises(TypeError, op, imag1a, imag2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ListTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_coverage(self): | 
					
						
							|  |  |  |         # exercise all comparisons for lists | 
					
						
							|  |  |  |         x = [42] | 
					
						
							|  |  |  |         self.assertIs(x<x, False) | 
					
						
							|  |  |  |         self.assertIs(x<=x, True) | 
					
						
							|  |  |  |         self.assertIs(x==x, True) | 
					
						
							|  |  |  |         self.assertIs(x!=x, False) | 
					
						
							|  |  |  |         self.assertIs(x>x, False) | 
					
						
							|  |  |  |         self.assertIs(x>=x, True) | 
					
						
							|  |  |  |         y = [42, 42] | 
					
						
							|  |  |  |         self.assertIs(x<y, True) | 
					
						
							|  |  |  |         self.assertIs(x<=y, True) | 
					
						
							|  |  |  |         self.assertIs(x==y, False) | 
					
						
							|  |  |  |         self.assertIs(x!=y, True) | 
					
						
							|  |  |  |         self.assertIs(x>y, False) | 
					
						
							|  |  |  |         self.assertIs(x>=y, False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_badentry(self): | 
					
						
							|  |  |  |         # make sure that exceptions for item comparison are properly | 
					
						
							|  |  |  |         # propagated in list comparisons | 
					
						
							| 
									
										
										
										
											2006-03-24 07:02:16 +00:00
										 |  |  |         class Exc(Exception): | 
					
						
							| 
									
										
										
										
											2001-01-18 15:48:05 +00:00
										 |  |  |             pass | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |         class Bad: | 
					
						
							|  |  |  |             def __eq__(self, other): | 
					
						
							|  |  |  |                 raise Exc | 
					
						
							| 
									
										
										
										
											2001-01-18 15:48:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  |         x = [Bad()] | 
					
						
							|  |  |  |         y = [Bad()] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for op in opmap["eq"]: | 
					
						
							|  |  |  |             self.assertRaises(Exc, op, x, y) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_goodentry(self): | 
					
						
							|  |  |  |         # This test exercises the final call to PyObject_RichCompare() | 
					
						
							|  |  |  |         # in Objects/listobject.c::list_richcompare() | 
					
						
							|  |  |  |         class Good: | 
					
						
							|  |  |  |             def __lt__(self, other): | 
					
						
							|  |  |  |                 return True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         x = [Good()] | 
					
						
							|  |  |  |         y = [Good()] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for op in opmap["lt"]: | 
					
						
							|  |  |  |             self.assertIs(op(x, y), True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Merged revisions 59565-59594 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r59568 | facundo.batista | 2007-12-19 13:53:01 +0100 (Wed, 19 Dec 2007) | 3 lines
  Some minor cleanups.  Thanks Mark Dickinson.
........
  r59573 | raymond.hettinger | 2007-12-19 19:13:31 +0100 (Wed, 19 Dec 2007) | 1 line
  Fix issue 1661: Flags argument silently ignored in re functions with compiled regexes.
........
  r59574 | guido.van.rossum | 2007-12-19 20:41:06 +0100 (Wed, 19 Dec 2007) | 7 lines
  Patch #1583 by Adam Olsen.
  This adds signal.set_wakeup_fd(fd) which sets a file descriptor to
  which a zero byte will be written whenever a C exception handler runs.
  I added a simple C API as well, PySignal_SetWakeupFd(fd).
........
  r59575 | raymond.hettinger | 2007-12-19 23:14:34 +0100 (Wed, 19 Dec 2007) | 1 line
  Bigger range for non-extended opargs.
........
  r59576 | guido.van.rossum | 2007-12-19 23:51:13 +0100 (Wed, 19 Dec 2007) | 5 lines
  Patch #1549 by Thomas Herve.
  This changes the rules for when __hash__ is inherited slightly,
  by allowing it to be inherited when one or more of __lt__, __le__,
  __gt__, __ge__ are overridden, as long as __eq__ and __ne__ aren't.
........
  r59577 | raymond.hettinger | 2007-12-20 02:25:05 +0100 (Thu, 20 Dec 2007) | 1 line
  Add comments
........
  r59578 | brett.cannon | 2007-12-20 11:09:52 +0100 (Thu, 20 Dec 2007) | 3 lines
  Add tests for the warnings module; specifically formatwarning and showwarning.
  Still need tests for warn_explicit and simplefilter.
........
  r59582 | guido.van.rossum | 2007-12-20 18:28:10 +0100 (Thu, 20 Dec 2007) | 2 lines
  Patch #1672 by Joseph Armbruster.  Use tempdir() to get a temporary directory.
........
  r59584 | georg.brandl | 2007-12-20 22:03:02 +0100 (Thu, 20 Dec 2007) | 2 lines
  Fix refleak introduced in r59576.
........
  r59586 | guido.van.rossum | 2007-12-21 00:48:28 +0100 (Fri, 21 Dec 2007) | 4 lines
  Improve performance of built-in any()/all() by avoiding PyIter_Next() --
  using a trick found in ifilter().
  Feel free to backport to 2.5.
........
  r59591 | andrew.kuchling | 2007-12-22 18:27:02 +0100 (Sat, 22 Dec 2007) | 1 line
  Add item
........
											
										 
											2007-12-24 08:52:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-29 21:31:19 +00:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2015-04-13 15:00:43 -05:00
										 |  |  |     unittest.main() |