annotate Lib/test/test_isinstance.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 5fe9ceeca111
children 1c26505c1de6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23262
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
1 # Tests some corner cases with isinstance() and issubclass(). While these
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
2 # tests use new style classes and properties, they actually do whitebox
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
3 # testing of error conditions uncovered when using extension types.
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
4
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
5 import unittest
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 23521
diff changeset
6 from test import test_support
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
7 import sys
23262
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
8
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
9
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
10
23265
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
11 class TestIsInstanceExceptions(unittest.TestCase):
23262
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
12 # Test to make sure that an AttributeError when accessing the instance's
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
13 # class's bases is masked. This was actually a bug in Python 2.2 and
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
14 # 2.2.1 where the exception wasn't caught but it also wasn't being cleared
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
15 # (leading to an "undetected error" in the debug build). Set up is,
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
16 # isinstance(inst, cls) where:
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
17 #
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
18 # - inst isn't an InstanceType
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
19 # - cls isn't a ClassType, a TypeType, or a TupleType
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
20 # - cls has a __bases__ attribute
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
21 # - inst has a __class__ attribute
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
22 # - inst.__class__ as no __bases__ attribute
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
23 #
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
24 # Sounds complicated, I know, but this mimics a situation where an
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
25 # extension type raises an AttributeError when its __bases__ attribute is
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
26 # gotten. In that case, isinstance() should return False.
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
27 def test_class_has_no_bases(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
28 class I(object):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
29 def getclass(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
30 # This must return an object that has no __bases__ attribute
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
31 return None
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
32 __class__ = property(getclass)
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
33
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
34 class C(object):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
35 def getbases(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
36 return ()
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
37 __bases__ = property(getbases)
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
38
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
39 self.assertEqual(False, isinstance(I(), C()))
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
40
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
41 # Like above except that inst.__class__.__bases__ raises an exception
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
42 # other than AttributeError
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
43 def test_bases_raises_other_than_attribute_error(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
44 class E(object):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
45 def getbases(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
46 raise RuntimeError
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
47 __bases__ = property(getbases)
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
48
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
49 class I(object):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
50 def getclass(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
51 return E()
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
52 __class__ = property(getclass)
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
53
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
54 class C(object):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
55 def getbases(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
56 return ()
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
57 __bases__ = property(getbases)
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
58
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
59 self.assertRaises(RuntimeError, isinstance, I(), C())
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
60
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
61 # Here's a situation where getattr(cls, '__bases__') raises an exception.
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
62 # If that exception is not AttributeError, it should not get masked
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
63 def test_dont_mask_non_attribute_error(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
64 class I: pass
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
65
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
66 class C(object):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
67 def getbases(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
68 raise RuntimeError
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
69 __bases__ = property(getbases)
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
70
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
71 self.assertRaises(RuntimeError, isinstance, I(), C())
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
72
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
73 # Like above, except that getattr(cls, '__bases__') raises an
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
74 # AttributeError, which /should/ get masked as a TypeError
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
75 def test_mask_attribute_error(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
76 class I: pass
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
77
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
78 class C(object):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
79 def getbases(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
80 raise AttributeError
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
81 __bases__ = property(getbases)
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
82
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
83 self.assertRaises(TypeError, isinstance, I(), C())
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
84
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
85
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
86
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
87 # These tests are similar to above, but tickle certain code paths in
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
88 # issubclass() instead of isinstance() -- really PyObject_IsSubclass()
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
89 # vs. PyObject_IsInstance().
23265
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
90 class TestIsSubclassExceptions(unittest.TestCase):
23262
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
91 def test_dont_mask_non_attribute_error(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
92 class C(object):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
93 def getbases(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
94 raise RuntimeError
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
95 __bases__ = property(getbases)
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
96
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
97 class S(C): pass
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
98
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
99 self.assertRaises(RuntimeError, issubclass, C(), S())
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
100
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
101 def test_mask_attribute_error(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
102 class C(object):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
103 def getbases(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
104 raise AttributeError
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
105 __bases__ = property(getbases)
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
106
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
107 class S(C): pass
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23265
diff changeset
108
23262
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
109 self.assertRaises(TypeError, issubclass, C(), S())
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
110
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
111 # Like above, but test the second branch, where the __bases__ of the
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
112 # second arg (the cls arg) is tested. This means the first arg must
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
113 # return a valid __bases__, and it's okay for it to be a normal --
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
114 # unrelated by inheritance -- class.
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
115 def test_dont_mask_non_attribute_error_in_cls_arg(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
116 class B: pass
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
117
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
118 class C(object):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
119 def getbases(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
120 raise RuntimeError
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
121 __bases__ = property(getbases)
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
122
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
123 self.assertRaises(RuntimeError, issubclass, B, C())
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
124
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
125 def test_mask_attribute_error_in_cls_arg(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
126 class B: pass
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
127
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
128 class C(object):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
129 def getbases(self):
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
130 raise AttributeError
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
131 __bases__ = property(getbases)
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
132
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
133 self.assertRaises(TypeError, issubclass, B, C())
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
134
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
135
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
136
23265
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
137 # meta classes for creating abstract classes and instances
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
138 class AbstractClass(object):
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
139 def __init__(self, bases):
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
140 self.bases = bases
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
141
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
142 def getbases(self):
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
143 return self.bases
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
144 __bases__ = property(getbases)
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
145
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
146 def __call__(self):
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
147 return AbstractInstance(self)
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
148
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
149 class AbstractInstance(object):
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
150 def __init__(self, klass):
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
151 self.klass = klass
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
152
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
153 def getclass(self):
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
154 return self.klass
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
155 __class__ = property(getclass)
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
156
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
157 # abstract classes
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
158 AbstractSuper = AbstractClass(bases=())
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
159
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
160 AbstractChild = AbstractClass(bases=(AbstractSuper,))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
161
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
162 # normal classes
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
163 class Super:
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
164 pass
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
165
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
166 class Child(Super):
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
167 pass
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
168
26622
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
169 # new-style classes
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
170 class NewSuper(object):
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
171 pass
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
172
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
173 class NewChild(NewSuper):
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
174 pass
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
175
23265
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
176
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
177
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
178 class TestIsInstanceIsSubclass(unittest.TestCase):
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
179 # Tests to ensure that isinstance and issubclass work on abstract
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
180 # classes and instances. Before the 2.2 release, TypeErrors were
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
181 # raised when boolean values should have been returned. The bug was
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
182 # triggered by mixing 'normal' classes and instances were with
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
183 # 'abstract' classes and instances. This case tries to test all
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
184 # combinations.
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
185
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
186 def test_isinstance_normal(self):
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23265
diff changeset
187 # normal instances
23265
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
188 self.assertEqual(True, isinstance(Super(), Super))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
189 self.assertEqual(False, isinstance(Super(), Child))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
190 self.assertEqual(False, isinstance(Super(), AbstractSuper))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
191 self.assertEqual(False, isinstance(Super(), AbstractChild))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
192
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
193 self.assertEqual(True, isinstance(Child(), Super))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
194 self.assertEqual(False, isinstance(Child(), AbstractSuper))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
195
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
196 def test_isinstance_abstract(self):
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23265
diff changeset
197 # abstract instances
23265
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
198 self.assertEqual(True, isinstance(AbstractSuper(), AbstractSuper))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
199 self.assertEqual(False, isinstance(AbstractSuper(), AbstractChild))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
200 self.assertEqual(False, isinstance(AbstractSuper(), Super))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
201 self.assertEqual(False, isinstance(AbstractSuper(), Child))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
202
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
203 self.assertEqual(True, isinstance(AbstractChild(), AbstractChild))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
204 self.assertEqual(True, isinstance(AbstractChild(), AbstractSuper))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
205 self.assertEqual(False, isinstance(AbstractChild(), Super))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
206 self.assertEqual(False, isinstance(AbstractChild(), Child))
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23265
diff changeset
207
23265
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
208 def test_subclass_normal(self):
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
209 # normal classes
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
210 self.assertEqual(True, issubclass(Super, Super))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
211 self.assertEqual(False, issubclass(Super, AbstractSuper))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
212 self.assertEqual(False, issubclass(Super, Child))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
213
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
214 self.assertEqual(True, issubclass(Child, Child))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
215 self.assertEqual(True, issubclass(Child, Super))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
216 self.assertEqual(False, issubclass(Child, AbstractSuper))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
217
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
218 def test_subclass_abstract(self):
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
219 # abstract classes
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
220 self.assertEqual(True, issubclass(AbstractSuper, AbstractSuper))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
221 self.assertEqual(False, issubclass(AbstractSuper, AbstractChild))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
222 self.assertEqual(False, issubclass(AbstractSuper, Child))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
223
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
224 self.assertEqual(True, issubclass(AbstractChild, AbstractChild))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
225 self.assertEqual(True, issubclass(AbstractChild, AbstractSuper))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
226 self.assertEqual(False, issubclass(AbstractChild, Super))
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
227 self.assertEqual(False, issubclass(AbstractChild, Child))
23521
a90faa5f9417 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23265
diff changeset
228
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
229 def test_subclass_tuple(self):
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
230 # test with a tuple as the second argument classes
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
231 self.assertEqual(True, issubclass(Child, (Child,)))
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
232 self.assertEqual(True, issubclass(Child, (Super,)))
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
233 self.assertEqual(False, issubclass(Super, (Child,)))
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
234 self.assertEqual(True, issubclass(Super, (Child, Super)))
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
235 self.assertEqual(False, issubclass(Child, ()))
26622
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
236 self.assertEqual(True, issubclass(Super, (Child, (Super,))))
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
237
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
238 self.assertEqual(True, issubclass(NewChild, (NewChild,)))
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
239 self.assertEqual(True, issubclass(NewChild, (NewSuper,)))
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
240 self.assertEqual(False, issubclass(NewSuper, (NewChild,)))
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
241 self.assertEqual(True, issubclass(NewSuper, (NewChild, NewSuper)))
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
242 self.assertEqual(False, issubclass(NewChild, ()))
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
243 self.assertEqual(True, issubclass(NewSuper, (NewChild, (NewSuper,))))
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
244
19507a1d0254 Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents: 26612
diff changeset
245 self.assertEqual(True, issubclass(int, (long, (float, int))))
35309
5fe9ceeca111 Disable a few other tests, that can't work if Python is compiled without
Walter Dörwald <walter@livinglogic.de>
parents: 32538
diff changeset
246 if test_support.have_unicode:
5fe9ceeca111 Disable a few other tests, that can't work if Python is compiled without
Walter Dörwald <walter@livinglogic.de>
parents: 32538
diff changeset
247 self.assertEqual(True, issubclass(str, (unicode, (Child, NewChild, basestring))))
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 24364
diff changeset
248
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
249 def test_subclass_recursion_limit(self):
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
250 # make sure that issubclass raises RuntimeError before the C stack is
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
251 # blown
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
252 self.assertRaises(RuntimeError, blowstack, issubclass, str, str)
23265
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
253
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
254 def test_isinstance_recursion_limit(self):
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
255 # make sure that issubclass raises RuntimeError before the C stack is
32538
4fab4a5cce2f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 31723
diff changeset
256 # blown
31723
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
257 self.assertRaises(RuntimeError, blowstack, isinstance, '', str)
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
258
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
259 def blowstack(fxn, arg, compare_to):
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
260 # Make sure that calling isinstance with a deeply nested tuple for its
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
261 # argument will raise RuntimeError eventually.
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
262 tuple_arg = (compare_to,)
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
263 for cnt in xrange(sys.getrecursionlimit()+5):
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
264 tuple_arg = (tuple_arg,)
d69fb35f1eae Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
265 fxn(arg, tuple_arg)
23265
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
266
8d4b25ceeefe Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23262
diff changeset
267
23262
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
268 def test_main():
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 26622
diff changeset
269 test_support.run_unittest(
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 26622
diff changeset
270 TestIsInstanceExceptions,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 26622
diff changeset
271 TestIsSubclassExceptions,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 26622
diff changeset
272 TestIsInstanceIsSubclass
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 26622
diff changeset
273 )
23262
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
274
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
275
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
276 if __name__ == '__main__':
7353977d6b64 Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff changeset
277 test_main()