summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2008-08-26 22:42:08 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2008-08-26 22:42:08 (GMT)
commit0668c62677e76b2acf7e4d11d5e9c1f4420a54f1 (patch)
tree0d6001ed47c5e6f715996eb2fc7e512d8c7a2de7 /Lib
parent14cb6bcf2ba953735ec1ef622f8ff8e23db1f326 (diff)
downloadcpython-0668c62677e76b2acf7e4d11d5e9c1f4420a54f1.zip
cpython-0668c62677e76b2acf7e4d11d5e9c1f4420a54f1.tar.gz
cpython-0668c62677e76b2acf7e4d11d5e9c1f4420a54f1.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.py23
-rw-r--r--Lib/test/test_exceptions.py32
-rw-r--r--Lib/test/test_typechecks.py13
3 files changed, 62 insertions, 6 deletions
diff --git a/Lib/test/test_abc.py b/Lib/test/test_abc.py
index 8fed220..3e0955f 100644
--- a/Lib/test/test_abc.py
+++ b/Lib/test/test_abc.py
@@ -88,15 +88,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:
@@ -105,20 +111,26 @@ 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
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(basestring)
self.assertEqual(isinstance("", A), True)
+ self.assertEqual(isinstance("", (A,)), True)
self.assertEqual(issubclass(str, A), True)
+ self.assertEqual(issubclass(str, (A,)), True)
def test_registration_edge_cases(self):
class A:
@@ -141,29 +153,40 @@ class TestABC(unittest.TestCase):
class A:
__metaclass__ = abc.ABCMeta
self.failUnless(issubclass(A, A))
+ self.failUnless(issubclass(A, (A,)))
class B:
__metaclass__ = abc.ABCMeta
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
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:
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index c3a9308..0576b62 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -333,7 +333,19 @@ class ExceptionTests(unittest.TestCase):
return g()
except ValueError:
return -1
- self.assertRaises(RuntimeError, g)
+
+ # The test prints an unraisable recursion error when
+ # doing "except ValueError", this is because subclass
+ # checking has recursion checking too.
+ with captured_output("stderr"):
+ try:
+ g()
+ except RuntimeError:
+ pass
+ except:
+ self.fail("Should have raised KeyError")
+ else:
+ self.fail("Should have raised KeyError")
def testUnicodeStrUsage(self):
# Make sure both instances and classes have a str and unicode
@@ -363,12 +375,20 @@ 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 "
- "<type 'exceptions.KeyError'> ignored\n")
+ self.fail("Should have raised KeyError")
+
+ with captured_output("stderr") as stderr:
+ def g():
+ try:
+ return g()
+ except RuntimeError:
+ return sys.exc_info()
+ e, v, tb = g()
+ self.assert_(e is RuntimeError, e)
+ self.assert_("maximum recursion depth exceeded" in str(v), v)
+
def test_main():
run_unittest(ExceptionTests)
diff --git a/Lib/test/test_typechecks.py b/Lib/test/test_typechecks.py
index 21be62d..32aa660 100644
--- a/Lib/test/test_typechecks.py
+++ b/Lib/test/test_typechecks.py
@@ -41,26 +41,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 testInfiniteRecursionCaughtProperly(self):
e = Evil()