blob: 4d554a397b4a51b307d60aee4baf5aa4a24ff979 [file] [log] [blame]
Thomas Wouters1d75a792000-08-17 22:37:32 +00001"Test the functionality of Python classes implementing operators."
2
Thomas Woutersed03b412007-08-28 21:37:11 +00003import unittest
Thomas Woutersed03b412007-08-28 21:37:11 +00004
Thomas Wouters1d75a792000-08-17 22:37:32 +00005
6testmeths = [
7
8# Binary operations
9 "add",
10 "radd",
11 "sub",
12 "rsub",
13 "mul",
14 "rmul",
Serhiy Storchakac2ccce72015-03-12 22:01:30 +020015 "matmul",
16 "rmatmul",
Neal Norwitzbcc0db82006-03-24 08:14:36 +000017 "truediv",
18 "rtruediv",
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +020019 "floordiv",
20 "rfloordiv",
Thomas Wouters1d75a792000-08-17 22:37:32 +000021 "mod",
22 "rmod",
23 "divmod",
24 "rdivmod",
25 "pow",
26 "rpow",
27 "rshift",
28 "rrshift",
29 "lshift",
30 "rlshift",
31 "and",
32 "rand",
33 "or",
34 "ror",
35 "xor",
36 "rxor",
37
38# List/dict operations
39 "contains",
40 "getitem",
Thomas Wouters1d75a792000-08-17 22:37:32 +000041 "setitem",
Thomas Wouters1d75a792000-08-17 22:37:32 +000042 "delitem",
Thomas Wouters1d75a792000-08-17 22:37:32 +000043
44# Unary operations
45 "neg",
46 "pos",
47 "abs",
Thomas Wouters1d75a792000-08-17 22:37:32 +000048
49# generic operations
50 "init",
Thomas Wouters1d75a792000-08-17 22:37:32 +000051 ]
52
53# These need to return something other than None
Thomas Wouters1d75a792000-08-17 22:37:32 +000054# "hash",
55# "str",
56# "repr",
Neil Schemenauer3a313e32004-07-19 16:29:17 +000057# "int",
Neil Schemenauer3a313e32004-07-19 16:29:17 +000058# "float",
Thomas Wouters1d75a792000-08-17 22:37:32 +000059
60# These are separate because they can influence the test of other methods.
61# "getattr",
62# "setattr",
63# "delattr",
64
Thomas Woutersed03b412007-08-28 21:37:11 +000065callLst = []
66def trackCall(f):
67 def track(*args, **kwargs):
68 callLst.append((f.__name__, args))
69 return f(*args, **kwargs)
70 return track
Thomas Wouters1d75a792000-08-17 22:37:32 +000071
Thomas Woutersed03b412007-08-28 21:37:11 +000072statictests = """
73@trackCall
74def __hash__(self, *args):
75 return hash(id(self))
Thomas Wouters1d75a792000-08-17 22:37:32 +000076
Thomas Woutersed03b412007-08-28 21:37:11 +000077@trackCall
78def __str__(self, *args):
79 return "AllTests"
Thomas Wouters1d75a792000-08-17 22:37:32 +000080
Thomas Woutersed03b412007-08-28 21:37:11 +000081@trackCall
82def __repr__(self, *args):
83 return "AllTests"
Neil Schemenauer3a313e32004-07-19 16:29:17 +000084
Thomas Woutersed03b412007-08-28 21:37:11 +000085@trackCall
86def __int__(self, *args):
87 return 1
Guido van Rossumcd16bf62007-06-13 18:07:49 +000088
Thomas Woutersed03b412007-08-28 21:37:11 +000089@trackCall
90def __index__(self, *args):
91 return 1
Neil Schemenauer3a313e32004-07-19 16:29:17 +000092
Thomas Woutersed03b412007-08-28 21:37:11 +000093@trackCall
94def __float__(self, *args):
95 return 1.0
Thomas Wouters1d75a792000-08-17 22:37:32 +000096
Thomas Woutersed03b412007-08-28 21:37:11 +000097@trackCall
Thomas Woutersed03b412007-08-28 21:37:11 +000098def __eq__(self, *args):
99 return True
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000100
Thomas Woutersed03b412007-08-28 21:37:11 +0000101@trackCall
102def __ne__(self, *args):
103 return False
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000104
Thomas Woutersed03b412007-08-28 21:37:11 +0000105@trackCall
106def __lt__(self, *args):
107 return False
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000108
Thomas Woutersed03b412007-08-28 21:37:11 +0000109@trackCall
110def __le__(self, *args):
111 return True
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000112
Thomas Woutersed03b412007-08-28 21:37:11 +0000113@trackCall
114def __gt__(self, *args):
115 return False
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000116
Thomas Woutersed03b412007-08-28 21:37:11 +0000117@trackCall
118def __ge__(self, *args):
119 return True
120"""
Barry Warsaw07d8d642001-08-20 20:29:07 +0000121
Thomas Woutersed03b412007-08-28 21:37:11 +0000122# Synthesize all the other AllTests methods from the names in testmeths.
Tim Peters01705212001-12-11 19:28:47 +0000123
124method_template = """\
Thomas Woutersed03b412007-08-28 21:37:11 +0000125@trackCall
126def __%s__(self, *args):
127 pass
Tim Peters01705212001-12-11 19:28:47 +0000128"""
129
Thomas Wouters4cdada92006-04-15 09:19:16 +0000130d = {}
Thomas Woutersed03b412007-08-28 21:37:11 +0000131exec(statictests, globals(), d)
Thomas Wouters1d75a792000-08-17 22:37:32 +0000132for method in testmeths:
Thomas Woutersed03b412007-08-28 21:37:11 +0000133 exec(method_template % method, globals(), d)
134AllTests = type("AllTests", (object,), d)
135del d, statictests, method, method_template
Thomas Wouters1d75a792000-08-17 22:37:32 +0000136
Thomas Woutersed03b412007-08-28 21:37:11 +0000137class ClassTests(unittest.TestCase):
138 def setUp(self):
139 callLst[:] = []
Thomas Wouters1d75a792000-08-17 22:37:32 +0000140
Thomas Woutersed03b412007-08-28 21:37:11 +0000141 def assertCallStack(self, expected_calls):
142 actualCallList = callLst[:] # need to copy because the comparison below will add
143 # additional calls to callLst
144 if expected_calls != actualCallList:
145 self.fail("Expected call list:\n %s\ndoes not match actual call list\n %s" %
146 (expected_calls, actualCallList))
Thomas Wouters1d75a792000-08-17 22:37:32 +0000147
Thomas Woutersed03b412007-08-28 21:37:11 +0000148 def testInit(self):
149 foo = AllTests()
150 self.assertCallStack([("__init__", (foo,))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000151
Thomas Woutersed03b412007-08-28 21:37:11 +0000152 def testBinaryOps(self):
153 testme = AllTests()
154 # Binary operations
Thomas Wouters1d75a792000-08-17 22:37:32 +0000155
Thomas Woutersed03b412007-08-28 21:37:11 +0000156 callLst[:] = []
157 testme + 1
158 self.assertCallStack([("__add__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000159
Thomas Woutersed03b412007-08-28 21:37:11 +0000160 callLst[:] = []
161 1 + testme
162 self.assertCallStack([("__radd__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000163
Thomas Woutersed03b412007-08-28 21:37:11 +0000164 callLst[:] = []
165 testme - 1
166 self.assertCallStack([("__sub__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000167
Thomas Woutersed03b412007-08-28 21:37:11 +0000168 callLst[:] = []
169 1 - testme
170 self.assertCallStack([("__rsub__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000171
Thomas Woutersed03b412007-08-28 21:37:11 +0000172 callLst[:] = []
173 testme * 1
174 self.assertCallStack([("__mul__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000175
Thomas Woutersed03b412007-08-28 21:37:11 +0000176 callLst[:] = []
177 1 * testme
178 self.assertCallStack([("__rmul__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000179
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200180 callLst[:] = []
Serhiy Storchakac2ccce72015-03-12 22:01:30 +0200181 testme @ 1
182 self.assertCallStack([("__matmul__", (testme, 1))])
183
184 callLst[:] = []
185 1 @ testme
186 self.assertCallStack([("__rmatmul__", (testme, 1))])
187
188 callLst[:] = []
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200189 testme / 1
190 self.assertCallStack([("__truediv__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000191
192
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200193 callLst[:] = []
194 1 / testme
195 self.assertCallStack([("__rtruediv__", (testme, 1))])
196
197 callLst[:] = []
198 testme // 1
199 self.assertCallStack([("__floordiv__", (testme, 1))])
200
201
202 callLst[:] = []
203 1 // testme
204 self.assertCallStack([("__rfloordiv__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000205
Thomas Woutersed03b412007-08-28 21:37:11 +0000206 callLst[:] = []
207 testme % 1
208 self.assertCallStack([("__mod__", (testme, 1))])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000209
Thomas Woutersed03b412007-08-28 21:37:11 +0000210 callLst[:] = []
211 1 % testme
212 self.assertCallStack([("__rmod__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000213
214
Thomas Woutersed03b412007-08-28 21:37:11 +0000215 callLst[:] = []
216 divmod(testme,1)
217 self.assertCallStack([("__divmod__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000218
Thomas Woutersed03b412007-08-28 21:37:11 +0000219 callLst[:] = []
220 divmod(1, testme)
221 self.assertCallStack([("__rdivmod__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000222
Thomas Woutersed03b412007-08-28 21:37:11 +0000223 callLst[:] = []
224 testme ** 1
225 self.assertCallStack([("__pow__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000226
Thomas Woutersed03b412007-08-28 21:37:11 +0000227 callLst[:] = []
228 1 ** testme
229 self.assertCallStack([("__rpow__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000230
Thomas Woutersed03b412007-08-28 21:37:11 +0000231 callLst[:] = []
232 testme >> 1
233 self.assertCallStack([("__rshift__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000234
Thomas Woutersed03b412007-08-28 21:37:11 +0000235 callLst[:] = []
236 1 >> testme
237 self.assertCallStack([("__rrshift__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000238
Thomas Woutersed03b412007-08-28 21:37:11 +0000239 callLst[:] = []
240 testme << 1
241 self.assertCallStack([("__lshift__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000242
Thomas Woutersed03b412007-08-28 21:37:11 +0000243 callLst[:] = []
244 1 << testme
245 self.assertCallStack([("__rlshift__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000246
Thomas Woutersed03b412007-08-28 21:37:11 +0000247 callLst[:] = []
248 testme & 1
249 self.assertCallStack([("__and__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000250
Thomas Woutersed03b412007-08-28 21:37:11 +0000251 callLst[:] = []
252 1 & testme
253 self.assertCallStack([("__rand__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000254
Thomas Woutersed03b412007-08-28 21:37:11 +0000255 callLst[:] = []
256 testme | 1
257 self.assertCallStack([("__or__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000258
Thomas Woutersed03b412007-08-28 21:37:11 +0000259 callLst[:] = []
260 1 | testme
261 self.assertCallStack([("__ror__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000262
Thomas Woutersed03b412007-08-28 21:37:11 +0000263 callLst[:] = []
264 testme ^ 1
265 self.assertCallStack([("__xor__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000266
Thomas Woutersed03b412007-08-28 21:37:11 +0000267 callLst[:] = []
268 1 ^ testme
269 self.assertCallStack([("__rxor__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000270
Thomas Woutersed03b412007-08-28 21:37:11 +0000271 def testListAndDictOps(self):
272 testme = AllTests()
273
274 # List/dict operations
275
276 class Empty: pass
277
278 try:
279 1 in Empty()
280 self.fail('failed, should have raised TypeError')
281 except TypeError:
282 pass
283
284 callLst[:] = []
285 1 in testme
286 self.assertCallStack([('__contains__', (testme, 1))])
287
288 callLst[:] = []
289 testme[1]
290 self.assertCallStack([('__getitem__', (testme, 1))])
291
292 callLst[:] = []
293 testme[1] = 1
294 self.assertCallStack([('__setitem__', (testme, 1, 1))])
295
296 callLst[:] = []
297 del testme[1]
298 self.assertCallStack([('__delitem__', (testme, 1))])
299
300 callLst[:] = []
301 testme[:42]
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000302 self.assertCallStack([('__getitem__', (testme, slice(None, 42)))])
Thomas Woutersed03b412007-08-28 21:37:11 +0000303
304 callLst[:] = []
305 testme[:42] = "The Answer"
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000306 self.assertCallStack([('__setitem__', (testme, slice(None, 42),
307 "The Answer"))])
Thomas Woutersed03b412007-08-28 21:37:11 +0000308
309 callLst[:] = []
310 del testme[:42]
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000311 self.assertCallStack([('__delitem__', (testme, slice(None, 42)))])
Thomas Woutersed03b412007-08-28 21:37:11 +0000312
313 callLst[:] = []
314 testme[2:1024:10]
315 self.assertCallStack([('__getitem__', (testme, slice(2, 1024, 10)))])
316
317 callLst[:] = []
318 testme[2:1024:10] = "A lot"
319 self.assertCallStack([('__setitem__', (testme, slice(2, 1024, 10),
320 "A lot"))])
321 callLst[:] = []
322 del testme[2:1024:10]
323 self.assertCallStack([('__delitem__', (testme, slice(2, 1024, 10)))])
324
325 callLst[:] = []
326 testme[:42, ..., :24:, 24, 100]
327 self.assertCallStack([('__getitem__', (testme, (slice(None, 42, None),
328 Ellipsis,
329 slice(None, 24, None),
330 24, 100)))])
331 callLst[:] = []
332 testme[:42, ..., :24:, 24, 100] = "Strange"
333 self.assertCallStack([('__setitem__', (testme, (slice(None, 42, None),
334 Ellipsis,
335 slice(None, 24, None),
336 24, 100), "Strange"))])
337 callLst[:] = []
338 del testme[:42, ..., :24:, 24, 100]
339 self.assertCallStack([('__delitem__', (testme, (slice(None, 42, None),
340 Ellipsis,
341 slice(None, 24, None),
342 24, 100)))])
343
Thomas Woutersed03b412007-08-28 21:37:11 +0000344 def testUnaryOps(self):
345 testme = AllTests()
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000346
Thomas Woutersed03b412007-08-28 21:37:11 +0000347 callLst[:] = []
348 -testme
349 self.assertCallStack([('__neg__', (testme,))])
350 callLst[:] = []
351 +testme
352 self.assertCallStack([('__pos__', (testme,))])
353 callLst[:] = []
354 abs(testme)
355 self.assertCallStack([('__abs__', (testme,))])
356 callLst[:] = []
357 int(testme)
358 self.assertCallStack([('__int__', (testme,))])
359 callLst[:] = []
360 float(testme)
361 self.assertCallStack([('__float__', (testme,))])
362 callLst[:] = []
363 oct(testme)
364 self.assertCallStack([('__index__', (testme,))])
365 callLst[:] = []
366 hex(testme)
367 self.assertCallStack([('__index__', (testme,))])
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000368
369
Thomas Woutersed03b412007-08-28 21:37:11 +0000370 def testMisc(self):
371 testme = AllTests()
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000372
Thomas Woutersed03b412007-08-28 21:37:11 +0000373 callLst[:] = []
374 hash(testme)
375 self.assertCallStack([('__hash__', (testme,))])
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000376
Thomas Woutersed03b412007-08-28 21:37:11 +0000377 callLst[:] = []
378 repr(testme)
379 self.assertCallStack([('__repr__', (testme,))])
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000380
Thomas Woutersed03b412007-08-28 21:37:11 +0000381 callLst[:] = []
382 str(testme)
383 self.assertCallStack([('__str__', (testme,))])
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000384
Thomas Woutersed03b412007-08-28 21:37:11 +0000385 callLst[:] = []
386 testme == 1
387 self.assertCallStack([('__eq__', (testme, 1))])
388
389 callLst[:] = []
390 testme < 1
391 self.assertCallStack([('__lt__', (testme, 1))])
392
393 callLst[:] = []
394 testme > 1
395 self.assertCallStack([('__gt__', (testme, 1))])
396
397 callLst[:] = []
398 testme != 1
399 self.assertCallStack([('__ne__', (testme, 1))])
400
401 callLst[:] = []
402 1 == testme
403 self.assertCallStack([('__eq__', (1, testme))])
404
405 callLst[:] = []
406 1 < testme
407 self.assertCallStack([('__gt__', (1, testme))])
408
409 callLst[:] = []
410 1 > testme
411 self.assertCallStack([('__lt__', (1, testme))])
412
413 callLst[:] = []
414 1 != testme
415 self.assertCallStack([('__ne__', (1, testme))])
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000416
417
Thomas Woutersed03b412007-08-28 21:37:11 +0000418 def testGetSetAndDel(self):
419 # Interfering tests
420 class ExtraTests(AllTests):
421 @trackCall
422 def __getattr__(self, *args):
423 return "SomeVal"
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000424
Thomas Woutersed03b412007-08-28 21:37:11 +0000425 @trackCall
426 def __setattr__(self, *args):
427 pass
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000428
Thomas Woutersed03b412007-08-28 21:37:11 +0000429 @trackCall
430 def __delattr__(self, *args):
431 pass
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000432
Thomas Woutersed03b412007-08-28 21:37:11 +0000433 testme = ExtraTests()
434
435 callLst[:] = []
436 testme.spam
437 self.assertCallStack([('__getattr__', (testme, "spam"))])
438
439 callLst[:] = []
440 testme.eggs = "spam, spam, spam and ham"
441 self.assertCallStack([('__setattr__', (testme, "eggs",
442 "spam, spam, spam and ham"))])
443
444 callLst[:] = []
445 del testme.cardinal
446 self.assertCallStack([('__delattr__', (testme, "cardinal"))])
447
448 def testDel(self):
449 x = []
450
451 class DelTest:
452 def __del__(self):
453 x.append("crab people, crab people")
454 testme = DelTest()
455 del testme
456 import gc
457 gc.collect()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000458 self.assertEqual(["crab people, crab people"], x)
Thomas Woutersed03b412007-08-28 21:37:11 +0000459
460 def testBadTypeReturned(self):
461 # return values of some method are type-checked
462 class BadTypeClass:
463 def __int__(self):
464 return None
465 __float__ = __int__
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200466 __complex__ = __int__
Thomas Woutersed03b412007-08-28 21:37:11 +0000467 __str__ = __int__
468 __repr__ = __int__
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200469 __bytes__ = __int__
470 __bool__ = __int__
471 __index__ = __int__
472 def index(x):
473 return [][x]
Thomas Woutersed03b412007-08-28 21:37:11 +0000474
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200475 for f in [float, complex, str, repr, bytes, bin, oct, hex, bool, index]:
Thomas Woutersed03b412007-08-28 21:37:11 +0000476 self.assertRaises(TypeError, f, BadTypeClass())
477
478 def testHashStuff(self):
479 # Test correct errors from hash() on objects with comparisons but
480 # no __hash__
481
482 class C0:
483 pass
484
485 hash(C0()) # This should work; the next two should raise TypeError
486
Thomas Woutersed03b412007-08-28 21:37:11 +0000487 class C2:
488 def __eq__(self, other): return 1
489
490 self.assertRaises(TypeError, hash, C2())
491
492
493 def testSFBug532646(self):
494 # Test for SF bug 532646
495
496 class A:
497 pass
498 A.__call__ = A()
499 a = A()
500
501 try:
502 a() # This should not segfault
Yury Selivanovf488fb42015-07-03 01:04:23 -0400503 except RecursionError:
Thomas Woutersed03b412007-08-28 21:37:11 +0000504 pass
505 else:
Yury Selivanovf488fb42015-07-03 01:04:23 -0400506 self.fail("Failed to raise RecursionError")
Thomas Woutersed03b412007-08-28 21:37:11 +0000507
508 def testForExceptionsRaisedInInstanceGetattr2(self):
509 # Tests for exceptions raised in instance_getattr2().
510
511 def booh(self):
512 raise AttributeError("booh")
513
514 class A:
515 a = property(booh)
516 try:
517 A().a # Raised AttributeError: A instance has no attribute 'a'
518 except AttributeError as x:
519 if str(x) != "booh":
520 self.fail("attribute error for A().a got masked: %s" % x)
521
522 class E:
523 __eq__ = property(booh)
524 E() == E() # In debug mode, caused a C-level assert() to fail
525
526 class I:
527 __init__ = property(booh)
528 try:
529 # In debug mode, printed XXX undetected error and
530 # raises AttributeError
531 I()
532 except AttributeError as x:
533 pass
534 else:
535 self.fail("attribute error for I.__init__ got masked")
536
537 def testHashComparisonOfMethods(self):
538 # Test comparison and hash of methods
539 class A:
540 def __init__(self, x):
541 self.x = x
542 def f(self):
543 pass
544 def g(self):
545 pass
546 def __eq__(self, other):
547 return self.x == other.x
548 def __hash__(self):
549 return self.x
550 class B(A):
551 pass
552
553 a1 = A(1)
554 a2 = A(2)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000555 self.assertEqual(a1.f, a1.f)
556 self.assertNotEqual(a1.f, a2.f)
557 self.assertNotEqual(a1.f, a1.g)
558 self.assertEqual(a1.f, A(1).f)
559 self.assertEqual(hash(a1.f), hash(a1.f))
560 self.assertEqual(hash(a1.f), hash(A(1).f))
Thomas Woutersed03b412007-08-28 21:37:11 +0000561
Ezio Melottib3aedd42010-11-20 19:04:17 +0000562 self.assertNotEqual(A.f, a1.f)
563 self.assertNotEqual(A.f, A.g)
564 self.assertEqual(B.f, A.f)
565 self.assertEqual(hash(B.f), hash(A.f))
Thomas Woutersed03b412007-08-28 21:37:11 +0000566
567 # the following triggers a SystemError in 2.4
Christian Heimes4a22b5d2007-11-25 09:39:14 +0000568 a = A(hash(A.f)^(-1))
Thomas Woutersed03b412007-08-28 21:37:11 +0000569 hash(a.f)
570
Zachary Ware38c707e2015-04-13 15:00:43 -0500571if __name__ == '__main__':
572 unittest.main()