annotate Lib/test/test_abc.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 6710a43f193f
children b36cb4602e21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Copyright 2007 Google, Inc. All Rights Reserved.
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # Licensed to PSF under a Contributor Agreement.
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 """Unit tests for abc.py."""
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import unittest
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 from test import test_support
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 import abc
45634
6282dfd5dc49 Since abc._Abstract was replaces by a new type flags the regression test suite fails. I've added a new function inspect.isabstract(). Is the mmethod fine or should I check if object is a instance of type or subclass of object, too?
Christian Heimes <christian@cheimes.de>
parents: 45468
diff changeset
10 from inspect import isabstract
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 class TestABC(unittest.TestCase):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 def test_abstractmethod_basics(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 @abc.abstractmethod
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 def foo(self): pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 self.assertEqual(foo.__isabstractmethod__, True)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 def bar(self): pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 self.assertEqual(hasattr(bar, "__isabstractmethod__"), False)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 def test_abstractproperty_basics(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 @abc.abstractproperty
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 def foo(self): pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 self.assertEqual(foo.__isabstractmethod__, True)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 def bar(self): pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 self.assertEqual(hasattr(bar, "__isabstractmethod__"), False)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 class C:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 __metaclass__ = abc.ABCMeta
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 @abc.abstractproperty
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 def foo(self): return 3
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 class D(C):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 @property
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 def foo(self): return super(D, self).foo
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 self.assertEqual(D().foo, 3)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 def test_abstractmethod_integration(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 for abstractthing in [abc.abstractmethod, abc.abstractproperty]:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 class C:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 __metaclass__ = abc.ABCMeta
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 @abstractthing
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 def foo(self): pass # abstract
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 def bar(self): pass # concrete
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 self.assertEqual(C.__abstractmethods__, set(["foo"]))
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 self.assertRaises(TypeError, C) # because foo is abstract
45634
6282dfd5dc49 Since abc._Abstract was replaces by a new type flags the regression test suite fails. I've added a new function inspect.isabstract(). Is the mmethod fine or should I check if object is a instance of type or subclass of object, too?
Christian Heimes <christian@cheimes.de>
parents: 45468
diff changeset
47 self.assert_(isabstract(C))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 class D(C):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 def bar(self): pass # concrete override of concrete
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 self.assertEqual(D.__abstractmethods__, set(["foo"]))
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 self.assertRaises(TypeError, D) # because foo is still abstract
45634
6282dfd5dc49 Since abc._Abstract was replaces by a new type flags the regression test suite fails. I've added a new function inspect.isabstract(). Is the mmethod fine or should I check if object is a instance of type or subclass of object, too?
Christian Heimes <christian@cheimes.de>
parents: 45468
diff changeset
52 self.assert_(isabstract(D))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 class E(D):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 def foo(self): pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 self.assertEqual(E.__abstractmethods__, set())
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 E() # now foo is concrete, too
45634
6282dfd5dc49 Since abc._Abstract was replaces by a new type flags the regression test suite fails. I've added a new function inspect.isabstract(). Is the mmethod fine or should I check if object is a instance of type or subclass of object, too?
Christian Heimes <christian@cheimes.de>
parents: 45468
diff changeset
57 self.failIf(isabstract(E))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 class F(E):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 @abstractthing
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 def bar(self): pass # abstract override of concrete
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 self.assertEqual(F.__abstractmethods__, set(["bar"]))
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 self.assertRaises(TypeError, F) # because bar is abstract now
45634
6282dfd5dc49 Since abc._Abstract was replaces by a new type flags the regression test suite fails. I've added a new function inspect.isabstract(). Is the mmethod fine or should I check if object is a instance of type or subclass of object, too?
Christian Heimes <christian@cheimes.de>
parents: 45468
diff changeset
63 self.assert_(isabstract(F))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
44551
f9c950e8f2e0 Fix issue 1747: allow classic classes to be checked for being subclasses of
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43958
diff changeset
65 def test_subclass_oldstyle_class(self):
f9c950e8f2e0 Fix issue 1747: allow classic classes to be checked for being subclasses of
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43958
diff changeset
66 class A:
f9c950e8f2e0 Fix issue 1747: allow classic classes to be checked for being subclasses of
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43958
diff changeset
67 __metaclass__ = abc.ABCMeta
f9c950e8f2e0 Fix issue 1747: allow classic classes to be checked for being subclasses of
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43958
diff changeset
68 class OldstyleClass:
f9c950e8f2e0 Fix issue 1747: allow classic classes to be checked for being subclasses of
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43958
diff changeset
69 pass
f9c950e8f2e0 Fix issue 1747: allow classic classes to be checked for being subclasses of
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43958
diff changeset
70 self.assertFalse(issubclass(OldstyleClass, A))
f9c950e8f2e0 Fix issue 1747: allow classic classes to be checked for being subclasses of
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43958
diff changeset
71 self.assertFalse(issubclass(A, OldstyleClass))
f9c950e8f2e0 Fix issue 1747: allow classic classes to be checked for being subclasses of
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43958
diff changeset
72
45788
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45634
diff changeset
73 def test_isinstance_class(self):
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45634
diff changeset
74 class A:
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45634
diff changeset
75 __metaclass__ = abc.ABCMeta
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45634
diff changeset
76 class OldstyleClass:
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45634
diff changeset
77 pass
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45634
diff changeset
78 self.assertFalse(isinstance(OldstyleClass, A))
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45634
diff changeset
79 self.assertTrue(isinstance(OldstyleClass, type(OldstyleClass)))
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45634
diff changeset
80 self.assertFalse(isinstance(A, OldstyleClass))
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45634
diff changeset
81 # This raises a recursion depth error, but is low-priority:
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45634
diff changeset
82 # self.assertTrue(isinstance(A, abc.ABCMeta))
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45634
diff changeset
83
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 def test_registration_basics(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 class A:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 __metaclass__ = abc.ABCMeta
44551
f9c950e8f2e0 Fix issue 1747: allow classic classes to be checked for being subclasses of
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43958
diff changeset
87 class B(object):
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 b = B()
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 self.assertEqual(issubclass(B, A), False)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
91 self.assertEqual(issubclass(B, (A,)), False)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 self.assertEqual(isinstance(b, A), False)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
93 self.assertEqual(isinstance(b, (A,)), False)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 A.register(B)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 self.assertEqual(issubclass(B, A), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
96 self.assertEqual(issubclass(B, (A,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 self.assertEqual(isinstance(b, A), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
98 self.assertEqual(isinstance(b, (A,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 class C(B):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 c = C()
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 self.assertEqual(issubclass(C, A), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
103 self.assertEqual(issubclass(C, (A,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 self.assertEqual(isinstance(c, A), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
105 self.assertEqual(isinstance(c, (A,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
45310
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
107 def test_isinstance_invalidation(self):
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
108 class A:
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
109 __metaclass__ = abc.ABCMeta
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
110 class B(object):
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
111 pass
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
112 b = B()
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
113 self.assertEqual(isinstance(b, A), False)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
114 self.assertEqual(isinstance(b, (A,)), False)
45310
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
115 A.register(B)
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
116 self.assertEqual(isinstance(b, A), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
117 self.assertEqual(isinstance(b, (A,)), True)
45310
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
118
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 def test_registration_builtins(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 class A:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 __metaclass__ = abc.ABCMeta
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 A.register(int)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 self.assertEqual(isinstance(42, A), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
124 self.assertEqual(isinstance(42, (A,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 self.assertEqual(issubclass(int, A), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
126 self.assertEqual(issubclass(int, (A,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 class B(A):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 B.register(basestring)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 self.assertEqual(isinstance("", A), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
131 self.assertEqual(isinstance("", (A,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 self.assertEqual(issubclass(str, A), True)
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
133 self.assertEqual(issubclass(str, (A,)), True)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 def test_registration_edge_cases(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 class A:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 __metaclass__ = abc.ABCMeta
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 A.register(A) # should pass silently
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 class A1(A):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 self.assertRaises(RuntimeError, A1.register, A) # cycles not allowed
44551
f9c950e8f2e0 Fix issue 1747: allow classic classes to be checked for being subclasses of
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43958
diff changeset
142 class B(object):
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 A1.register(B) # ok
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 A1.register(B) # should pass silently
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 class C(A):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 A.register(C) # should pass silently
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 self.assertRaises(RuntimeError, C.register, A) # cycles not allowed
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 C.register(B) # ok
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 def test_registration_transitiveness(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 class A:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 __metaclass__ = abc.ABCMeta
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 self.failUnless(issubclass(A, A))
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
156 self.failUnless(issubclass(A, (A,)))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 class B:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 __metaclass__ = abc.ABCMeta
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 self.failIf(issubclass(A, B))
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
160 self.failIf(issubclass(A, (B,)))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 self.failIf(issubclass(B, A))
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
162 self.failIf(issubclass(B, (A,)))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 class C:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 __metaclass__ = abc.ABCMeta
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 A.register(B)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 class B1(B):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 self.failUnless(issubclass(B1, A))
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
169 self.failUnless(issubclass(B1, (A,)))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 class C1(C):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 B1.register(C1)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 self.failIf(issubclass(C, B))
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
174 self.failIf(issubclass(C, (B,)))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 self.failIf(issubclass(C, B1))
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
176 self.failIf(issubclass(C, (B1,)))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 self.failUnless(issubclass(C1, A))
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
178 self.failUnless(issubclass(C1, (A,)))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 self.failUnless(issubclass(C1, B))
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
180 self.failUnless(issubclass(C1, (B,)))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 self.failUnless(issubclass(C1, B1))
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
182 self.failUnless(issubclass(C1, (B1,)))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 C1.register(int)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 class MyInt(int):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 pass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 self.failUnless(issubclass(MyInt, A))
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
187 self.failUnless(issubclass(MyInt, (A,)))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 self.failUnless(isinstance(42, A))
48876
d01c1d768841 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 45788
diff changeset
189 self.failUnless(isinstance(42, (A,)))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43327
diff changeset
191 def test_all_new_methods_are_called(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43327
diff changeset
192 class A:
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43327
diff changeset
193 __metaclass__ = abc.ABCMeta
44551
f9c950e8f2e0 Fix issue 1747: allow classic classes to be checked for being subclasses of
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43958
diff changeset
194 class B(object):
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43327
diff changeset
195 counter = 0
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43327
diff changeset
196 def __new__(cls):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43327
diff changeset
197 B.counter += 1
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43327
diff changeset
198 return super(B, cls).__new__(cls)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43327
diff changeset
199 class C(A, B):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43327
diff changeset
200 pass
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43327
diff changeset
201 self.assertEqual(B.counter, 0)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43327
diff changeset
202 C()
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43327
diff changeset
203 self.assertEqual(B.counter, 1)
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 43327
diff changeset
204
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
205
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 def test_main():
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 test_support.run_unittest(TestABC)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 if __name__ == "__main__":
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 unittest.main()