diff options
Diffstat (limited to 'Lib/test/test_class.py')
-rw-r--r-- | Lib/test/test_class.py | 276 |
1 files changed, 91 insertions, 185 deletions
diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index e5cdf088f42..c7003fbe608 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -2,7 +2,7 @@ import unittest -from test import test_support +from test import support testmeths = [ @@ -13,8 +13,8 @@ testmeths = [ "rsub", "mul", "rmul", - "div", - "rdiv", + "truediv", + "rtruediv", "mod", "rmod", "divmod", @@ -35,11 +35,8 @@ testmeths = [ # List/dict operations "contains", "getitem", - "getslice", "setitem", - "setslice", "delitem", - "delslice", # Unary operations "neg", @@ -51,15 +48,11 @@ testmeths = [ ] # These need to return something other than None -# "coerce", # "hash", # "str", # "repr", # "int", -# "long", # "float", -# "oct", -# "hex", # These are separate because they can influence the test of other methods. # "getattr", @@ -73,61 +66,70 @@ def trackCall(f): return f(*args, **kwargs) return track -class AllTests: - trackCall = trackCall +statictests = """ +@trackCall +def __hash__(self, *args): + return hash(id(self)) - @trackCall - def __coerce__(self, *args): - return (self,) + args +@trackCall +def __str__(self, *args): + return "AllTests" - @trackCall - def __hash__(self, *args): - return hash(id(self)) +@trackCall +def __repr__(self, *args): + return "AllTests" - @trackCall - def __str__(self, *args): - return "AllTests" +@trackCall +def __int__(self, *args): + return 1 - @trackCall - def __repr__(self, *args): - return "AllTests" +@trackCall +def __index__(self, *args): + return 1 - @trackCall - def __int__(self, *args): - return 1 +@trackCall +def __float__(self, *args): + return 1.0 - @trackCall - def __float__(self, *args): - return 1.0 +@trackCall +def __eq__(self, *args): + return True - @trackCall - def __long__(self, *args): - return 1L +@trackCall +def __ne__(self, *args): + return False - @trackCall - def __oct__(self, *args): - return '01' +@trackCall +def __lt__(self, *args): + return False - @trackCall - def __hex__(self, *args): - return '0x1' +@trackCall +def __le__(self, *args): + return True - @trackCall - def __cmp__(self, *args): - return 0 +@trackCall +def __gt__(self, *args): + return False + +@trackCall +def __ge__(self, *args): + return True +""" # Synthesize all the other AllTests methods from the names in testmeths. method_template = """\ @trackCall -def __%(method)s__(self, *args): +def __%s__(self, *args): pass """ +d = {} +exec(statictests, globals(), d) for method in testmeths: - exec method_template % locals() in AllTests.__dict__ - -del method, method_template + exec(method_template % method, globals(), d) +AllTests = type("AllTests", (object,), d) +del d, statictests, method, method_template class ClassTests(unittest.TestCase): def setUp(self): @@ -150,102 +152,102 @@ class ClassTests(unittest.TestCase): callLst[:] = [] testme + 1 - self.assertCallStack([("__coerce__", (testme, 1)), ("__add__", (testme, 1))]) + self.assertCallStack([("__add__", (testme, 1))]) callLst[:] = [] 1 + testme - self.assertCallStack([("__coerce__", (testme, 1)), ("__radd__", (testme, 1))]) + self.assertCallStack([("__radd__", (testme, 1))]) callLst[:] = [] testme - 1 - self.assertCallStack([("__coerce__", (testme, 1)), ("__sub__", (testme, 1))]) + self.assertCallStack([("__sub__", (testme, 1))]) callLst[:] = [] 1 - testme - self.assertCallStack([("__coerce__", (testme, 1)), ("__rsub__", (testme, 1))]) + self.assertCallStack([("__rsub__", (testme, 1))]) callLst[:] = [] testme * 1 - self.assertCallStack([("__coerce__", (testme, 1)), ("__mul__", (testme, 1))]) + self.assertCallStack([("__mul__", (testme, 1))]) callLst[:] = [] 1 * testme - self.assertCallStack([("__coerce__", (testme, 1)), ("__rmul__", (testme, 1))]) + self.assertCallStack([("__rmul__", (testme, 1))]) if 1/2 == 0: callLst[:] = [] testme / 1 - self.assertCallStack([("__coerce__", (testme, 1)), ("__div__", (testme, 1))]) + self.assertCallStack([("__div__", (testme, 1))]) callLst[:] = [] 1 / testme - self.assertCallStack([("__coerce__", (testme, 1)), ("__rdiv__", (testme, 1))]) + self.assertCallStack([("__rdiv__", (testme, 1))]) callLst[:] = [] testme % 1 - self.assertCallStack([("__coerce__", (testme, 1)), ("__mod__", (testme, 1))]) + self.assertCallStack([("__mod__", (testme, 1))]) callLst[:] = [] 1 % testme - self.assertCallStack([("__coerce__", (testme, 1)), ("__rmod__", (testme, 1))]) + self.assertCallStack([("__rmod__", (testme, 1))]) callLst[:] = [] divmod(testme,1) - self.assertCallStack([("__coerce__", (testme, 1)), ("__divmod__", (testme, 1))]) + self.assertCallStack([("__divmod__", (testme, 1))]) callLst[:] = [] divmod(1, testme) - self.assertCallStack([("__coerce__", (testme, 1)), ("__rdivmod__", (testme, 1))]) + self.assertCallStack([("__rdivmod__", (testme, 1))]) callLst[:] = [] testme ** 1 - self.assertCallStack([("__coerce__", (testme, 1)), ("__pow__", (testme, 1))]) + self.assertCallStack([("__pow__", (testme, 1))]) callLst[:] = [] 1 ** testme - self.assertCallStack([("__coerce__", (testme, 1)), ("__rpow__", (testme, 1))]) + self.assertCallStack([("__rpow__", (testme, 1))]) callLst[:] = [] testme >> 1 - self.assertCallStack([("__coerce__", (testme, 1)), ("__rshift__", (testme, 1))]) + self.assertCallStack([("__rshift__", (testme, 1))]) callLst[:] = [] 1 >> testme - self.assertCallStack([("__coerce__", (testme, 1)), ("__rrshift__", (testme, 1))]) + self.assertCallStack([("__rrshift__", (testme, 1))]) callLst[:] = [] testme << 1 - self.assertCallStack([("__coerce__", (testme, 1)), ("__lshift__", (testme, 1))]) + self.assertCallStack([("__lshift__", (testme, 1))]) callLst[:] = [] 1 << testme - self.assertCallStack([("__coerce__", (testme, 1)), ("__rlshift__", (testme, 1))]) + self.assertCallStack([("__rlshift__", (testme, 1))]) callLst[:] = [] testme & 1 - self.assertCallStack([("__coerce__", (testme, 1)), ("__and__", (testme, 1))]) + self.assertCallStack([("__and__", (testme, 1))]) callLst[:] = [] 1 & testme - self.assertCallStack([("__coerce__", (testme, 1)), ("__rand__", (testme, 1))]) + self.assertCallStack([("__rand__", (testme, 1))]) callLst[:] = [] testme | 1 - self.assertCallStack([("__coerce__", (testme, 1)), ("__or__", (testme, 1))]) + self.assertCallStack([("__or__", (testme, 1))]) callLst[:] = [] 1 | testme - self.assertCallStack([("__coerce__", (testme, 1)), ("__ror__", (testme, 1))]) + self.assertCallStack([("__ror__", (testme, 1))]) callLst[:] = [] testme ^ 1 - self.assertCallStack([("__coerce__", (testme, 1)), ("__xor__", (testme, 1))]) + self.assertCallStack([("__xor__", (testme, 1))]) callLst[:] = [] 1 ^ testme - self.assertCallStack([("__coerce__", (testme, 1)), ("__rxor__", (testme, 1))]) + self.assertCallStack([("__rxor__", (testme, 1))]) def testListAndDictOps(self): testme = AllTests() @@ -278,15 +280,16 @@ class ClassTests(unittest.TestCase): callLst[:] = [] testme[:42] - self.assertCallStack([('__getslice__', (testme, 0, 42))]) + self.assertCallStack([('__getitem__', (testme, slice(None, 42)))]) callLst[:] = [] testme[:42] = "The Answer" - self.assertCallStack([('__setslice__', (testme, 0, 42, "The Answer"))]) + self.assertCallStack([('__setitem__', (testme, slice(None, 42), + "The Answer"))]) callLst[:] = [] del testme[:42] - self.assertCallStack([('__delslice__', (testme, 0, 42))]) + self.assertCallStack([('__delitem__', (testme, slice(None, 42)))]) callLst[:] = [] testme[2:1024:10] @@ -319,50 +322,6 @@ class ClassTests(unittest.TestCase): slice(None, 24, None), 24, 100)))]) - # Now remove the slice hooks to see if converting normal slices to - # slice object works. - - getslice = AllTests.__getslice__ - del AllTests.__getslice__ - setslice = AllTests.__setslice__ - del AllTests.__setslice__ - delslice = AllTests.__delslice__ - del AllTests.__delslice__ - - # XXX when using new-style classes the slice testme[:42] produces - # slice(None, 42, None) instead of slice(0, 42, None). py3k will have - # to change this test. - callLst[:] = [] - testme[:42] - self.assertCallStack([('__getitem__', (testme, slice(0, 42, None)))]) - - callLst[:] = [] - testme[:42] = "The Answer" - self.assertCallStack([('__setitem__', (testme, slice(0, 42, None), - "The Answer"))]) - callLst[:] = [] - del testme[:42] - self.assertCallStack([('__delitem__', (testme, slice(0, 42, None)))]) - - # Restore the slice methods, or the tests will fail with regrtest -R. - AllTests.__getslice__ = getslice - AllTests.__setslice__ = setslice - AllTests.__delslice__ = delslice - - - @test_support.cpython_only - def testDelItem(self): - class A: - ok = False - def __delitem__(self, key): - self.ok = True - a = A() - # Subtle: we need to call PySequence_SetItem, not PyMapping_SetItem. - from _testcapi import sequence_delitem - sequence_delitem(a, 2) - self.assertTrue(a.ok) - - def testUnaryOps(self): testme = AllTests() @@ -379,17 +338,14 @@ class ClassTests(unittest.TestCase): int(testme) self.assertCallStack([('__int__', (testme,))]) callLst[:] = [] - long(testme) - self.assertCallStack([('__long__', (testme,))]) - callLst[:] = [] float(testme) self.assertCallStack([('__float__', (testme,))]) callLst[:] = [] oct(testme) - self.assertCallStack([('__oct__', (testme,))]) + self.assertCallStack([('__index__', (testme,))]) callLst[:] = [] hex(testme) - self.assertCallStack([('__hex__', (testme,))]) + self.assertCallStack([('__index__', (testme,))]) def testMisc(self): @@ -409,43 +365,35 @@ class ClassTests(unittest.TestCase): callLst[:] = [] testme == 1 - self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (testme, 1))]) + self.assertCallStack([('__eq__', (testme, 1))]) callLst[:] = [] testme < 1 - self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (testme, 1))]) + self.assertCallStack([('__lt__', (testme, 1))]) callLst[:] = [] testme > 1 - self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (testme, 1))]) - - callLst[:] = [] - eval('testme <> 1') # XXX kill this in py3k - self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (testme, 1))]) + self.assertCallStack([('__gt__', (testme, 1))]) callLst[:] = [] testme != 1 - self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (testme, 1))]) + self.assertCallStack([('__ne__', (testme, 1))]) callLst[:] = [] 1 == testme - self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (1, testme))]) + self.assertCallStack([('__eq__', (1, testme))]) callLst[:] = [] 1 < testme - self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (1, testme))]) + self.assertCallStack([('__gt__', (1, testme))]) callLst[:] = [] 1 > testme - self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (1, testme))]) - - callLst[:] = [] - eval('1 <> testme') - self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (1, testme))]) + self.assertCallStack([('__lt__', (1, testme))]) callLst[:] = [] 1 != testme - self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (1, testme))]) + self.assertCallStack([('__ne__', (1, testme))]) def testGetSetAndDel(self): @@ -496,40 +444,14 @@ class ClassTests(unittest.TestCase): def __int__(self): return None __float__ = __int__ - __long__ = __int__ __str__ = __int__ __repr__ = __int__ __oct__ = __int__ __hex__ = __int__ - for f in [int, float, long, str, repr, oct, hex]: + for f in [int, float, str, repr, oct, hex]: self.assertRaises(TypeError, f, BadTypeClass()) - def testMixIntsAndLongs(self): - # mixing up ints and longs is okay - class IntLongMixClass: - @trackCall - def __int__(self): - return 42L - - @trackCall - def __long__(self): - return 64 - - mixIntAndLong = IntLongMixClass() - - callLst[:] = [] - as_int = int(mixIntAndLong) - self.assertEqual(type(as_int), long) - self.assertEqual(as_int, 42L) - self.assertCallStack([('__int__', (mixIntAndLong,))]) - - callLst[:] = [] - as_long = long(mixIntAndLong) - self.assertEqual(type(as_long), long) - self.assertEqual(as_long, 64) - self.assertCallStack([('__long__', (mixIntAndLong,))]) - def testHashStuff(self): # Test correct errors from hash() on objects with comparisons but # no __hash__ @@ -539,11 +461,6 @@ class ClassTests(unittest.TestCase): hash(C0()) # This should work; the next two should raise TypeError - class C1: - def __cmp__(self, other): return 0 - - self.assertRaises(TypeError, hash, C1()) - class C2: def __eq__(self, other): return 1 @@ -575,7 +492,7 @@ class ClassTests(unittest.TestCase): a = property(booh) try: A().a # Raised AttributeError: A instance has no attribute 'a' - except AttributeError, x: + except AttributeError as x: if str(x) != "booh": self.fail("attribute error for A().a got masked: %s" % x) @@ -589,7 +506,7 @@ class ClassTests(unittest.TestCase): # In debug mode, printed XXX undetected error and # raises AttributeError I() - except AttributeError, x: + except AttributeError as x: pass else: self.fail("attribute error for I.__init__ got masked") @@ -625,22 +542,11 @@ class ClassTests(unittest.TestCase): self.assertEqual(hash(B.f), hash(A.f)) # the following triggers a SystemError in 2.4 - a = A(hash(A.f.im_func)^(-1)) + a = A(hash(A.f)^(-1)) hash(a.f) - def testAttrSlots(self): - class C: - pass - for c in C, C(): - self.assertRaises(TypeError, type(c).__getattribute__, c, []) - self.assertRaises(TypeError, type(c).__setattr__, c, [], []) - def test_main(): - with test_support.check_py3k_warnings( - (".+__(get|set|del)slice__ has been removed", DeprecationWarning), - ("classic int division", DeprecationWarning), - ("<> not supported", DeprecationWarning)): - test_support.run_unittest(ClassTests) + support.run_unittest(ClassTests) if __name__=='__main__': test_main() |