Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_isinstance.py @ 64166:d0e9febfa00a 3.1
Merged revisions 84172 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r84172 | martin.v.loewis | 2010-08-18 18:12:23 +0200 (Mi, 18 Aug 2010) | 2 lines
Restore GIL in nis_cat in case of error.
........
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Thu, 19 Aug 2010 09:03:03 +0000 |
| parents | 9c930a32cebb |
| children | 80b48a3a2473 |
| 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 |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
43577
diff
changeset
|
6 from test import 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 # |
|
41986
6464cc638faa
Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41821
diff
changeset
|
18 # - cls isn't a a type, or a tuple |
|
23262
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
19 # - 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
|
20 # - 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
|
21 # - 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
|
22 # |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
23 # 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
|
24 # 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
|
25 # 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
|
26 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
|
27 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
|
28 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
|
29 # 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
|
30 return None |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
31 __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
|
32 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
33 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
|
34 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
|
35 return () |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
36 __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
|
37 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
38 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
|
39 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
40 # 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
|
41 # 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
|
42 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
|
43 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
|
44 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
|
45 raise RuntimeError |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
46 __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
|
47 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
48 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
|
49 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
|
50 return E() |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
51 __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
|
52 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
53 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
|
54 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
|
55 return () |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
56 __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
|
57 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
58 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
|
59 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
60 # 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
|
61 # 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
|
62 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
|
63 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
|
64 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
65 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
|
66 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
|
67 raise RuntimeError |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
68 __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
|
69 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
70 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
|
71 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
72 # 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
|
73 # 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
|
74 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
|
75 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
|
76 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
77 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
|
78 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
|
79 raise AttributeError |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
80 __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
|
81 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
82 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
|
83 |
|
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 # 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
|
87 # 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
|
88 # vs. PyObject_IsInstance(). |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
89 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
|
90 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
|
91 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
|
92 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
|
93 raise RuntimeError |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
94 __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
|
95 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
96 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
|
97 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
98 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
|
99 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
100 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
|
101 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
|
102 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
|
103 raise AttributeError |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
104 __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
|
105 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
106 class S(C): pass |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23265
diff
changeset
|
107 |
|
23262
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
108 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
|
109 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
110 # 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
|
111 # 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
|
112 # 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
|
113 # 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
|
114 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
|
115 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
|
116 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
117 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
|
118 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
|
119 raise RuntimeError |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
120 __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
|
121 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
122 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
|
123 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
124 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
|
125 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
|
126 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
127 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
|
128 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
|
129 raise AttributeError |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
130 __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
|
131 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
132 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
|
133 |
|
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 |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
136 # 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
|
137 class AbstractClass(object): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
138 def __init__(self, bases): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
139 self.bases = bases |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
140 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
141 def getbases(self): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
142 return self.bases |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
143 __bases__ = property(getbases) |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
144 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
145 def __call__(self): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
146 return AbstractInstance(self) |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
147 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
148 class AbstractInstance(object): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
149 def __init__(self, klass): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
150 self.klass = klass |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
151 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
152 def getclass(self): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
153 return self.klass |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
154 __class__ = property(getclass) |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
155 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
156 # abstract classes |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
157 AbstractSuper = AbstractClass(bases=()) |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
158 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
159 AbstractChild = AbstractClass(bases=(AbstractSuper,)) |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
160 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
161 # normal classes |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
162 class Super: |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
163 pass |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
164 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
165 class Child(Super): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
166 pass |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
167 |
|
26622
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
168 # new-style classes |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
169 class NewSuper(object): |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
170 pass |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
171 |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
172 class NewChild(NewSuper): |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
173 pass |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
174 |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
175 |
|
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 class TestIsInstanceIsSubclass(unittest.TestCase): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
178 # 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
|
179 # 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
|
180 # 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
|
181 # 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
|
182 # '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
|
183 # combinations. |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
184 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
185 def test_isinstance_normal(self): |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23265
diff
changeset
|
186 # normal instances |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
187 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
|
188 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
|
189 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
|
190 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
|
191 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
192 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
|
193 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
|
194 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
195 def test_isinstance_abstract(self): |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23265
diff
changeset
|
196 # abstract instances |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
197 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
|
198 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
|
199 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
|
200 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
|
201 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
202 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
|
203 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
|
204 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
|
205 self.assertEqual(False, isinstance(AbstractChild(), Child)) |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23265
diff
changeset
|
206 |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
207 def test_subclass_normal(self): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
208 # normal classes |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
209 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
|
210 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
|
211 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
|
212 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
213 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
|
214 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
|
215 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
|
216 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
217 def test_subclass_abstract(self): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
218 # abstract classes |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
219 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
|
220 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
|
221 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
|
222 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
223 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
|
224 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
|
225 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
|
226 self.assertEqual(False, issubclass(AbstractChild, Child)) |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23265
diff
changeset
|
227 |
|
26612
97f83542f8ee
Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
228 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
|
229 # 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
|
230 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
|
231 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
|
232 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
|
233 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
|
234 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
|
235 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
|
236 |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
237 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
|
238 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
|
239 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
|
240 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
|
241 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
|
242 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
|
243 |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
35309
diff
changeset
|
244 self.assertEqual(True, issubclass(int, (int, (float, int)))) |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
41986
diff
changeset
|
245 self.assertEqual(True, issubclass(str, (str, (Child, NewChild, str)))) |
|
26612
97f83542f8ee
Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
246 |
|
31723
d69fb35f1eae
Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents:
28917
diff
changeset
|
247 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
|
248 # 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
|
249 # blown |
|
d69fb35f1eae
Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents:
28917
diff
changeset
|
250 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
|
251 |
|
31723
d69fb35f1eae
Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents:
28917
diff
changeset
|
252 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
|
253 # 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
|
254 # 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
|
255 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
|
256 |
|
d69fb35f1eae
Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents:
28917
diff
changeset
|
257 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
|
258 # 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
|
259 # 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
|
260 tuple_arg = (compare_to,) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
261 for cnt in range(sys.getrecursionlimit()+5): |
|
31723
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 = (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
|
263 fxn(arg, tuple_arg) |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
264 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
265 |
|
23262
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
266 def test_main(): |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
43577
diff
changeset
|
267 support.run_unittest( |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
26622
diff
changeset
|
268 TestIsInstanceExceptions, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
26622
diff
changeset
|
269 TestIsSubclassExceptions, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
26622
diff
changeset
|
270 TestIsInstanceIsSubclass |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
26622
diff
changeset
|
271 ) |
|
23262
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
272 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
273 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
274 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
|
275 test_main() |
