1 "Test the functionality of Python classes implementing operators."
5 from test
import support
35 # List/dict operations
50 # These need to return something other than None
57 # These are separate because they can influence the test of other methods.
64 def track(*args
, **kwargs
):
65 callLst
.append((f
.__name
__, args
))
66 return f(*args
, **kwargs
)
71 def __hash__(self, *args):
75 def __str__(self, *args):
79 def __repr__(self, *args):
83 def __int__(self, *args):
87 def __index__(self, *args):
91 def __float__(self, *args):
95 def __eq__(self, *args):
99 def __ne__(self, *args):
103 def __lt__(self, *args):
107 def __le__(self, *args):
111 def __gt__(self, *args):
115 def __ge__(self, *args):
119 # Synthesize all the other AllTests methods from the names in testmeths.
121 method_template
= """\
123 def __%s__(self, *args):
128 exec(statictests
, globals(), d
)
129 for method
in testmeths
:
130 exec(method_template
% method
, globals(), d
)
131 AllTests
= type("AllTests", (object,), d
)
132 del d
, statictests
, method
, method_template
134 class ClassTests(unittest
.TestCase
):
138 def assertCallStack(self
, expected_calls
):
139 actualCallList
= callLst
[:] # need to copy because the comparison below will add
140 # additional calls to callLst
141 if expected_calls
!= actualCallList
:
142 self
.fail("Expected call list:\n %s\ndoes not match actual call list\n %s" %
143 (expected_calls
, actualCallList
))
147 self
.assertCallStack([("__init__", (foo
,))])
149 def testBinaryOps(self
):
155 self
.assertCallStack([("__add__", (testme
, 1))])
159 self
.assertCallStack([("__radd__", (testme
, 1))])
163 self
.assertCallStack([("__sub__", (testme
, 1))])
167 self
.assertCallStack([("__rsub__", (testme
, 1))])
171 self
.assertCallStack([("__mul__", (testme
, 1))])
175 self
.assertCallStack([("__rmul__", (testme
, 1))])
180 self
.assertCallStack([("__div__", (testme
, 1))])
185 self
.assertCallStack([("__rdiv__", (testme
, 1))])
189 self
.assertCallStack([("__mod__", (testme
, 1))])
193 self
.assertCallStack([("__rmod__", (testme
, 1))])
198 self
.assertCallStack([("__divmod__", (testme
, 1))])
202 self
.assertCallStack([("__rdivmod__", (testme
, 1))])
206 self
.assertCallStack([("__pow__", (testme
, 1))])
210 self
.assertCallStack([("__rpow__", (testme
, 1))])
214 self
.assertCallStack([("__rshift__", (testme
, 1))])
218 self
.assertCallStack([("__rrshift__", (testme
, 1))])
222 self
.assertCallStack([("__lshift__", (testme
, 1))])
226 self
.assertCallStack([("__rlshift__", (testme
, 1))])
230 self
.assertCallStack([("__and__", (testme
, 1))])
234 self
.assertCallStack([("__rand__", (testme
, 1))])
238 self
.assertCallStack([("__or__", (testme
, 1))])
242 self
.assertCallStack([("__ror__", (testme
, 1))])
246 self
.assertCallStack([("__xor__", (testme
, 1))])
250 self
.assertCallStack([("__rxor__", (testme
, 1))])
252 def testListAndDictOps(self
):
255 # List/dict operations
261 self
.fail('failed, should have raised TypeError')
267 self
.assertCallStack([('__contains__', (testme
, 1))])
271 self
.assertCallStack([('__getitem__', (testme
, 1))])
275 self
.assertCallStack([('__setitem__', (testme
, 1, 1))])
279 self
.assertCallStack([('__delitem__', (testme
, 1))])
283 self
.assertCallStack([('__getitem__', (testme
, slice(None, 42)))])
286 testme
[:42] = "The Answer"
287 self
.assertCallStack([('__setitem__', (testme
, slice(None, 42),
292 self
.assertCallStack([('__delitem__', (testme
, slice(None, 42)))])
296 self
.assertCallStack([('__getitem__', (testme
, slice(2, 1024, 10)))])
299 testme
[2:1024:10] = "A lot"
300 self
.assertCallStack([('__setitem__', (testme
, slice(2, 1024, 10),
303 del testme
[2:1024:10]
304 self
.assertCallStack([('__delitem__', (testme
, slice(2, 1024, 10)))])
307 testme
[:42, ..., :24:, 24, 100]
308 self
.assertCallStack([('__getitem__', (testme
, (slice(None, 42, None),
310 slice(None, 24, None),
313 testme
[:42, ..., :24:, 24, 100] = "Strange"
314 self
.assertCallStack([('__setitem__', (testme
, (slice(None, 42, None),
316 slice(None, 24, None),
317 24, 100), "Strange"))])
319 del testme
[:42, ..., :24:, 24, 100]
320 self
.assertCallStack([('__delitem__', (testme
, (slice(None, 42, None),
322 slice(None, 24, None),
325 def testUnaryOps(self
):
330 self
.assertCallStack([('__neg__', (testme
,))])
333 self
.assertCallStack([('__pos__', (testme
,))])
336 self
.assertCallStack([('__abs__', (testme
,))])
339 self
.assertCallStack([('__int__', (testme
,))])
342 self
.assertCallStack([('__float__', (testme
,))])
345 self
.assertCallStack([('__index__', (testme
,))])
348 self
.assertCallStack([('__index__', (testme
,))])
356 self
.assertCallStack([('__hash__', (testme
,))])
360 self
.assertCallStack([('__repr__', (testme
,))])
364 self
.assertCallStack([('__str__', (testme
,))])
368 self
.assertCallStack([('__eq__', (testme
, 1))])
372 self
.assertCallStack([('__lt__', (testme
, 1))])
376 self
.assertCallStack([('__gt__', (testme
, 1))])
380 self
.assertCallStack([('__ne__', (testme
, 1))])
384 self
.assertCallStack([('__eq__', (1, testme
))])
388 self
.assertCallStack([('__gt__', (1, testme
))])
392 self
.assertCallStack([('__lt__', (1, testme
))])
396 self
.assertCallStack([('__ne__', (1, testme
))])
399 def testGetSetAndDel(self
):
401 class ExtraTests(AllTests
):
403 def __getattr__(self
, *args
):
407 def __setattr__(self
, *args
):
411 def __delattr__(self
, *args
):
414 testme
= ExtraTests()
418 self
.assertCallStack([('__getattr__', (testme
, "spam"))])
421 testme
.eggs
= "spam, spam, spam and ham"
422 self
.assertCallStack([('__setattr__', (testme
, "eggs",
423 "spam, spam, spam and ham"))])
427 self
.assertCallStack([('__delattr__', (testme
, "cardinal"))])
434 x
.append("crab people, crab people")
439 self
.assertEquals(["crab people, crab people"], x
)
441 def testBadTypeReturned(self
):
442 # return values of some method are type-checked
452 for f
in [int, float, str, repr, oct, hex]:
453 self
.assertRaises(TypeError, f
, BadTypeClass())
455 def testHashStuff(self
):
456 # Test correct errors from hash() on objects with comparisons but
462 hash(C0()) # This should work; the next two should raise TypeError
465 def __eq__(self
, other
): return 1
467 self
.assertRaises(TypeError, hash, C2())
470 def testSFBug532646(self
):
471 # Test for SF bug 532646
479 a() # This should not segfault
483 self
.fail("Failed to raise RuntimeError")
485 def testForExceptionsRaisedInInstanceGetattr2(self
):
486 # Tests for exceptions raised in instance_getattr2().
489 raise AttributeError("booh")
494 A().a
# Raised AttributeError: A instance has no attribute 'a'
495 except AttributeError as x
:
497 self
.fail("attribute error for A().a got masked: %s" % x
)
500 __eq__
= property(booh
)
501 E() == E() # In debug mode, caused a C-level assert() to fail
504 __init__
= property(booh
)
506 # In debug mode, printed XXX undetected error and
507 # raises AttributeError
509 except AttributeError as x
:
512 self
.fail("attribute error for I.__init__ got masked")
514 def testHashComparisonOfMethods(self
):
515 # Test comparison and hash of methods
517 def __init__(self
, x
):
523 def __eq__(self
, other
):
524 return self
.x
== other
.x
532 self
.assertEquals(a1
.f
, a1
.f
)
533 self
.assertNotEquals(a1
.f
, a2
.f
)
534 self
.assertNotEquals(a1
.f
, a1
.g
)
535 self
.assertEquals(a1
.f
, A(1).f
)
536 self
.assertEquals(hash(a1
.f
), hash(a1
.f
))
537 self
.assertEquals(hash(a1
.f
), hash(A(1).f
))
539 self
.assertNotEquals(A
.f
, a1
.f
)
540 self
.assertNotEquals(A
.f
, A
.g
)
541 self
.assertEquals(B
.f
, A
.f
)
542 self
.assertEquals(hash(B
.f
), hash(A
.f
))
544 # the following triggers a SystemError in 2.4
545 a
= A(hash(A
.f
)^
(-1))
549 support
.run_unittest(ClassTests
)
551 if __name__
=='__main__':