Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_isinstance.py @ 70626:aa07c1237f4e
(Merge 3.2) Issue #12016: Reindent decoders of HK and JP codecs
| author | Victor Stinner <victor.stinner@haypocalc.com> |
|---|---|
| date | Fri, 03 Jun 2011 23:34:32 +0200 |
| parents | 3a0e7000dc8f |
| children | 07488c3c85f1 |
| 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 |
|
66291
80b48a3a2473
#1574217: only swallow AttributeErrors in isinstance, not everything.
R. David Murray <rdmurray@bitdance.com>
parents:
47201
diff
changeset
|
84 # check that we don't mask non AttributeErrors |
|
80b48a3a2473
#1574217: only swallow AttributeErrors in isinstance, not everything.
R. David Murray <rdmurray@bitdance.com>
parents:
47201
diff
changeset
|
85 # see: http://bugs.python.org/issue1574217 |
|
80b48a3a2473
#1574217: only swallow AttributeErrors in isinstance, not everything.
R. David Murray <rdmurray@bitdance.com>
parents:
47201
diff
changeset
|
86 def test_isinstance_dont_mask_non_attribute_error(self): |
|
80b48a3a2473
#1574217: only swallow AttributeErrors in isinstance, not everything.
R. David Murray <rdmurray@bitdance.com>
parents:
47201
diff
changeset
|
87 class C(object): |
|
80b48a3a2473
#1574217: only swallow AttributeErrors in isinstance, not everything.
R. David Murray <rdmurray@bitdance.com>
parents:
47201
diff
changeset
|
88 def getclass(self): |
| 66298 | 89 raise RuntimeError |
| 90 __class__ = property(getclass) | |
|
66291
80b48a3a2473
#1574217: only swallow AttributeErrors in isinstance, not everything.
R. David Murray <rdmurray@bitdance.com>
parents:
47201
diff
changeset
|
91 |
| 66298 | 92 c = C() |
|
66291
80b48a3a2473
#1574217: only swallow AttributeErrors in isinstance, not everything.
R. David Murray <rdmurray@bitdance.com>
parents:
47201
diff
changeset
|
93 self.assertRaises(RuntimeError, isinstance, c, bool) |
|
80b48a3a2473
#1574217: only swallow AttributeErrors in isinstance, not everything.
R. David Murray <rdmurray@bitdance.com>
parents:
47201
diff
changeset
|
94 |
|
80b48a3a2473
#1574217: only swallow AttributeErrors in isinstance, not everything.
R. David Murray <rdmurray@bitdance.com>
parents:
47201
diff
changeset
|
95 # test another code path |
|
80b48a3a2473
#1574217: only swallow AttributeErrors in isinstance, not everything.
R. David Murray <rdmurray@bitdance.com>
parents:
47201
diff
changeset
|
96 class D: pass |
|
80b48a3a2473
#1574217: only swallow AttributeErrors in isinstance, not everything.
R. David Murray <rdmurray@bitdance.com>
parents:
47201
diff
changeset
|
97 self.assertRaises(RuntimeError, isinstance, c, D) |
|
23262
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 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
100 # 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
|
101 # 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
|
102 # vs. PyObject_IsInstance(). |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
103 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
|
104 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
|
105 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
|
106 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
|
107 raise RuntimeError |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
108 __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
|
109 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
110 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
|
111 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
112 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
|
113 |
|
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_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
|
115 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
|
116 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
|
117 raise AttributeError |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
118 __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
|
119 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
120 class S(C): pass |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23265
diff
changeset
|
121 |
|
23262
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(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
|
123 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
124 # 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
|
125 # 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
|
126 # 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
|
127 # 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
|
128 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
|
129 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
|
130 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
131 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
|
132 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
|
133 raise RuntimeError |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
134 __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
|
135 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
136 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
|
137 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
138 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
|
139 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
|
140 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
141 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
|
142 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
|
143 raise AttributeError |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
144 __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
|
145 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
146 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
|
147 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
148 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
149 |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
150 # 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
|
151 class AbstractClass(object): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
152 def __init__(self, bases): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
153 self.bases = bases |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
154 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
155 def getbases(self): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
156 return self.bases |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
157 __bases__ = property(getbases) |
|
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 def __call__(self): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
160 return AbstractInstance(self) |
|
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 class AbstractInstance(object): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
163 def __init__(self, klass): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
164 self.klass = klass |
|
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 def getclass(self): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
167 return self.klass |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
168 __class__ = property(getclass) |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
169 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
170 # abstract classes |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
171 AbstractSuper = AbstractClass(bases=()) |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
172 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
173 AbstractChild = AbstractClass(bases=(AbstractSuper,)) |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
174 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
175 # normal classes |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
176 class Super: |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
177 pass |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
178 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
179 class Child(Super): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
180 pass |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
181 |
|
26622
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
182 # new-style classes |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
183 class NewSuper(object): |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
184 pass |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
185 |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
186 class NewChild(NewSuper): |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
187 pass |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
188 |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
189 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
190 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
191 class TestIsInstanceIsSubclass(unittest.TestCase): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
192 # 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
|
193 # 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
|
194 # 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
|
195 # 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
|
196 # '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
|
197 # combinations. |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
198 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
199 def test_isinstance_normal(self): |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23265
diff
changeset
|
200 # normal instances |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
201 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
|
202 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
|
203 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
|
204 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
|
205 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
206 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
|
207 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
|
208 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
209 def test_isinstance_abstract(self): |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23265
diff
changeset
|
210 # abstract instances |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
211 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
|
212 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
|
213 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
|
214 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
|
215 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
216 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
|
217 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
|
218 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
|
219 self.assertEqual(False, isinstance(AbstractChild(), Child)) |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23265
diff
changeset
|
220 |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
221 def test_subclass_normal(self): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
222 # normal classes |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
223 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
|
224 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
|
225 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
|
226 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
227 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
|
228 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
|
229 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
|
230 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
231 def test_subclass_abstract(self): |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
232 # abstract classes |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
233 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
|
234 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
|
235 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
|
236 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
237 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
|
238 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
|
239 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
|
240 self.assertEqual(False, issubclass(AbstractChild, Child)) |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23265
diff
changeset
|
241 |
|
26612
97f83542f8ee
Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents:
24364
diff
changeset
|
242 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
|
243 # 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
|
244 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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 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
|
250 |
|
19507a1d0254
Change issubclass() so that recursive tuples (directly or indirectly
Walter Dörwald <walter@livinglogic.de>
parents:
26612
diff
changeset
|
251 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
|
252 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
|
253 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
|
254 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
|
255 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
|
256 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
|
257 |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
35309
diff
changeset
|
258 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
|
259 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
|
260 |
|
31723
d69fb35f1eae
Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents:
28917
diff
changeset
|
261 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
|
262 # 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
|
263 # blown |
|
d69fb35f1eae
Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents:
28917
diff
changeset
|
264 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
|
265 |
|
31723
d69fb35f1eae
Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents:
28917
diff
changeset
|
266 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
|
267 # 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
|
268 # 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
|
269 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
|
270 |
|
d69fb35f1eae
Limit the nesting depth of a tuple passed as the second argument to
Brett Cannon <bcannon@gmail.com>
parents:
28917
diff
changeset
|
271 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
|
272 # 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
|
273 # 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
|
274 tuple_arg = (compare_to,) |
|
41642
a6eb6acfe04a
Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
275 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
|
276 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
|
277 fxn(arg, tuple_arg) |
|
23265
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
278 |
|
8d4b25ceeefe
Add more tests for abstract isinstance() and issubclass().
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23262
diff
changeset
|
279 |
|
23262
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
280 def test_main(): |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
43577
diff
changeset
|
281 support.run_unittest( |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
26622
diff
changeset
|
282 TestIsInstanceExceptions, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
26622
diff
changeset
|
283 TestIsSubclassExceptions, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
26622
diff
changeset
|
284 TestIsInstanceIsSubclass |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
26622
diff
changeset
|
285 ) |
|
23262
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
286 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
287 |
|
7353977d6b64
Unit tests for the changes in abstract.c version 2.101. The debug
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
288 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
|
289 test_main() |
