summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_abc.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2008-08-26 22:40:48 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2008-08-26 22:40:48 (GMT)
commitec569b794737be248671d0dfac11b664fc930eef (patch)
tree76d3b98a51063f9f922a15cbe36ca58a7f0892da /Lib/test/test_abc.py
parente2dffc0aeb6e03d8e6512a13cb3fe05562f7c655 (diff)
downloadcpython-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/test/test_abc.py')
-rw-r--r--Lib/test/test_abc.py24
1 files changed, 24 insertions, 0 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):