diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2008-08-26 22:40:48 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2008-08-26 22:40:48 (GMT) |
commit | ec569b794737be248671d0dfac11b664fc930eef (patch) | |
tree | 76d3b98a51063f9f922a15cbe36ca58a7f0892da /Lib | |
parent | e2dffc0aeb6e03d8e6512a13cb3fe05562f7c655 (diff) | |
download | cpython-ec569b794737be248671d0dfac11b664fc930eef.zip cpython-ec569b794737be248671d0dfac11b664fc930eef.tar.gz cpython-ec569b794737be248671d0dfac11b664fc930eef.tar.bz2 |
Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
match Python 2.5 speed despite the __instancecheck__ / __subclasscheck__
mechanism. In the process, fix a bug where isinstance() and issubclass(),
when given a tuple of classes as second argument, were looking up
__instancecheck__ / __subclasscheck__ on the tuple rather than on each
type object.
Reviewed by Benjamin Peterson and Raymond Hettinger.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_abc.py | 24 | ||||
-rw-r--r-- | Lib/test/test_exceptions.py | 16 | ||||
-rw-r--r-- | Lib/test/test_typechecks.py | 13 |
3 files changed, 48 insertions, 5 deletions
diff --git a/Lib/test/test_abc.py b/Lib/test/test_abc.py index a4cefc5..4965c42 100644 --- a/Lib/test/test_abc.py +++ b/Lib/test/test_abc.py @@ -75,15 +75,21 @@ class TestABC(unittest.TestCase): pass b = B() self.assertEqual(issubclass(B, A), False) + self.assertEqual(issubclass(B, (A,)), False) self.assertEqual(isinstance(b, A), False) + self.assertEqual(isinstance(b, (A,)), False) A.register(B) self.assertEqual(issubclass(B, A), True) + self.assertEqual(issubclass(B, (A,)), True) self.assertEqual(isinstance(b, A), True) + self.assertEqual(isinstance(b, (A,)), True) class C(B): pass c = C() self.assertEqual(issubclass(C, A), True) + self.assertEqual(issubclass(C, (A,)), True) self.assertEqual(isinstance(c, A), True) + self.assertEqual(isinstance(c, (A,)), True) def test_isinstance_invalidation(self): class A(metaclass=abc.ABCMeta): @@ -92,22 +98,29 @@ class TestABC(unittest.TestCase): pass b = B() self.assertEqual(isinstance(b, A), False) + self.assertEqual(isinstance(b, (A,)), False) A.register(B) self.assertEqual(isinstance(b, A), True) + self.assertEqual(isinstance(b, (A,)), True) def test_registration_builtins(self): class A(metaclass=abc.ABCMeta): pass A.register(int) self.assertEqual(isinstance(42, A), True) + self.assertEqual(isinstance(42, (A,)), True) self.assertEqual(issubclass(int, A), True) + self.assertEqual(issubclass(int, (A,)), True) class B(A): pass B.register(str) class C(str): pass self.assertEqual(isinstance("", A), True) + self.assertEqual(isinstance("", (A,)), True) self.assertEqual(issubclass(str, A), True) + self.assertEqual(issubclass(str, (A,)), True) self.assertEqual(issubclass(C, A), True) + self.assertEqual(issubclass(C, (A,)), True) def test_registration_edge_cases(self): class A(metaclass=abc.ABCMeta): @@ -130,29 +143,40 @@ class TestABC(unittest.TestCase): class A(metaclass=abc.ABCMeta): pass self.failUnless(issubclass(A, A)) + self.failUnless(issubclass(A, (A,))) class B(metaclass=abc.ABCMeta): pass self.failIf(issubclass(A, B)) + self.failIf(issubclass(A, (B,))) self.failIf(issubclass(B, A)) + self.failIf(issubclass(B, (A,))) class C(metaclass=abc.ABCMeta): pass A.register(B) class B1(B): pass self.failUnless(issubclass(B1, A)) + self.failUnless(issubclass(B1, (A,))) class C1(C): pass B1.register(C1) self.failIf(issubclass(C, B)) + self.failIf(issubclass(C, (B,))) self.failIf(issubclass(C, B1)) + self.failIf(issubclass(C, (B1,))) self.failUnless(issubclass(C1, A)) + self.failUnless(issubclass(C1, (A,))) self.failUnless(issubclass(C1, B)) + self.failUnless(issubclass(C1, (B,))) self.failUnless(issubclass(C1, B1)) + self.failUnless(issubclass(C1, (B1,))) C1.register(int) class MyInt(int): pass self.failUnless(issubclass(MyInt, A)) + self.failUnless(issubclass(MyInt, (A,))) self.failUnless(isinstance(42, A)) + self.failUnless(isinstance(42, (A,))) def test_all_new_methods_are_called(self): class A(metaclass=abc.ABCMeta): diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index c7de97c..b671cbc 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -582,12 +582,18 @@ class ExceptionTests(unittest.TestCase): except KeyError: pass except: - self.fail("Should have raised TypeError") + self.fail("Should have raised KeyError") else: - self.fail("Should have raised TypeError") - self.assertEqual(stderr.getvalue(), - "Exception ValueError: ValueError() " - "in <class 'KeyError'> ignored\n") + self.fail("Should have raised KeyError") + + def g(): + try: + return g() + except RuntimeError: + return sys.exc_info() + e, v, tb = g() + self.assert_(isinstance(v, RuntimeError), type(v)) + self.assert_("maximum recursion depth exceeded" in str(v), str(v)) def test_MemoryError(self): diff --git a/Lib/test/test_typechecks.py b/Lib/test/test_typechecks.py index 901c326..17cd5d3 100644 --- a/Lib/test/test_typechecks.py +++ b/Lib/test/test_typechecks.py @@ -33,26 +33,39 @@ class TypeChecksTest(unittest.TestCase): def testIsSubclassBuiltin(self): self.assertEqual(issubclass(int, Integer), True) + self.assertEqual(issubclass(int, (Integer,)), True) self.assertEqual(issubclass(float, Integer), False) + self.assertEqual(issubclass(float, (Integer,)), False) def testIsInstanceBuiltin(self): self.assertEqual(isinstance(42, Integer), True) + self.assertEqual(isinstance(42, (Integer,)), True) self.assertEqual(isinstance(3.14, Integer), False) + self.assertEqual(isinstance(3.14, (Integer,)), False) def testIsInstanceActual(self): self.assertEqual(isinstance(Integer(), Integer), True) + self.assertEqual(isinstance(Integer(), (Integer,)), True) def testIsSubclassActual(self): self.assertEqual(issubclass(Integer, Integer), True) + self.assertEqual(issubclass(Integer, (Integer,)), True) def testSubclassBehavior(self): self.assertEqual(issubclass(SubInt, Integer), True) + self.assertEqual(issubclass(SubInt, (Integer,)), True) self.assertEqual(issubclass(SubInt, SubInt), True) + self.assertEqual(issubclass(SubInt, (SubInt,)), True) self.assertEqual(issubclass(Integer, SubInt), False) + self.assertEqual(issubclass(Integer, (SubInt,)), False) self.assertEqual(issubclass(int, SubInt), False) + self.assertEqual(issubclass(int, (SubInt,)), False) self.assertEqual(isinstance(SubInt(), Integer), True) + self.assertEqual(isinstance(SubInt(), (Integer,)), True) self.assertEqual(isinstance(SubInt(), SubInt), True) + self.assertEqual(isinstance(SubInt(), (SubInt,)), True) self.assertEqual(isinstance(42, SubInt), False) + self.assertEqual(isinstance(42, (SubInt,)), False) def test_main(): |