Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 1 | # Test enhancements related to descriptors and new-style classes |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 3 | from test_support import verify, verbose, TestFailed, TESTFN |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4 | from copy import deepcopy |
| 5 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 6 | def vereq(a, b): |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 7 | if not (a == b): |
| 8 | raise TestFailed, "%r == %r" % (a, b) |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 10 | def veris(a, b): |
| 11 | if a is not b: |
| 12 | raise TestFailed, "%r is %r" % (a, b) |
| 13 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14 | def testunop(a, res, expr="len(a)", meth="__len__"): |
| 15 | if verbose: print "checking", expr |
| 16 | dict = {'a': a} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 17 | vereq(eval(expr, dict), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 18 | t = type(a) |
| 19 | m = getattr(t, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 20 | vereq(m, t.__dict__[meth]) |
| 21 | vereq(m(a), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 22 | bm = getattr(a, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 23 | vereq(bm(), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 24 | |
| 25 | def testbinop(a, b, res, expr="a+b", meth="__add__"): |
| 26 | if verbose: print "checking", expr |
| 27 | dict = {'a': a, 'b': b} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 28 | vereq(eval(expr, dict), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 29 | t = type(a) |
| 30 | m = getattr(t, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 31 | vereq(m, t.__dict__[meth]) |
| 32 | vereq(m(a, b), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 33 | bm = getattr(a, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 34 | vereq(bm(b), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 35 | |
| 36 | def testternop(a, b, c, res, expr="a[b:c]", meth="__getslice__"): |
| 37 | if verbose: print "checking", expr |
| 38 | dict = {'a': a, 'b': b, 'c': c} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 39 | vereq(eval(expr, dict), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 40 | t = type(a) |
| 41 | m = getattr(t, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 42 | vereq(m, t.__dict__[meth]) |
| 43 | vereq(m(a, b, c), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 44 | bm = getattr(a, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 45 | vereq(bm(b, c), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 46 | |
| 47 | def testsetop(a, b, res, stmt="a+=b", meth="__iadd__"): |
| 48 | if verbose: print "checking", stmt |
| 49 | dict = {'a': deepcopy(a), 'b': b} |
| 50 | exec stmt in dict |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 51 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 52 | t = type(a) |
| 53 | m = getattr(t, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 54 | vereq(m, t.__dict__[meth]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 55 | dict['a'] = deepcopy(a) |
| 56 | m(dict['a'], b) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 57 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 58 | dict['a'] = deepcopy(a) |
| 59 | bm = getattr(dict['a'], meth) |
| 60 | bm(b) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 61 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 62 | |
| 63 | def testset2op(a, b, c, res, stmt="a[b]=c", meth="__setitem__"): |
| 64 | if verbose: print "checking", stmt |
| 65 | dict = {'a': deepcopy(a), 'b': b, 'c': c} |
| 66 | exec stmt in dict |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 67 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 68 | t = type(a) |
| 69 | m = getattr(t, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 70 | vereq(m, t.__dict__[meth]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 71 | dict['a'] = deepcopy(a) |
| 72 | m(dict['a'], b, c) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 73 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 74 | dict['a'] = deepcopy(a) |
| 75 | bm = getattr(dict['a'], meth) |
| 76 | bm(b, c) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 77 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 78 | |
| 79 | def testset3op(a, b, c, d, res, stmt="a[b:c]=d", meth="__setslice__"): |
| 80 | if verbose: print "checking", stmt |
| 81 | dict = {'a': deepcopy(a), 'b': b, 'c': c, 'd': d} |
| 82 | exec stmt in dict |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 83 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 84 | t = type(a) |
| 85 | m = getattr(t, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 86 | vereq(m, t.__dict__[meth]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 87 | dict['a'] = deepcopy(a) |
| 88 | m(dict['a'], b, c, d) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 89 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 90 | dict['a'] = deepcopy(a) |
| 91 | bm = getattr(dict['a'], meth) |
| 92 | bm(b, c, d) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 93 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 94 | |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 95 | def class_docstrings(): |
| 96 | class Classic: |
| 97 | "A classic docstring." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 98 | vereq(Classic.__doc__, "A classic docstring.") |
| 99 | vereq(Classic.__dict__['__doc__'], "A classic docstring.") |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 100 | |
| 101 | class Classic2: |
| 102 | pass |
| 103 | verify(Classic2.__doc__ is None) |
| 104 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 105 | class NewStatic(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 106 | "Another docstring." |
| 107 | __dynamic__ = 0 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 108 | vereq(NewStatic.__doc__, "Another docstring.") |
| 109 | vereq(NewStatic.__dict__['__doc__'], "Another docstring.") |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 110 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 111 | class NewStatic2(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 112 | __dynamic__ = 0 |
| 113 | pass |
| 114 | verify(NewStatic2.__doc__ is None) |
| 115 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 116 | class NewDynamic(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 117 | "Another docstring." |
| 118 | __dynamic__ = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 119 | vereq(NewDynamic.__doc__, "Another docstring.") |
| 120 | vereq(NewDynamic.__dict__['__doc__'], "Another docstring.") |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 121 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 122 | class NewDynamic2(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 123 | __dynamic__ = 1 |
| 124 | pass |
| 125 | verify(NewDynamic2.__doc__ is None) |
| 126 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 127 | def lists(): |
| 128 | if verbose: print "Testing list operations..." |
| 129 | testbinop([1], [2], [1,2], "a+b", "__add__") |
| 130 | testbinop([1,2,3], 2, 1, "b in a", "__contains__") |
| 131 | testbinop([1,2,3], 4, 0, "b in a", "__contains__") |
| 132 | testbinop([1,2,3], 1, 2, "a[b]", "__getitem__") |
| 133 | testternop([1,2,3], 0, 2, [1,2], "a[b:c]", "__getslice__") |
| 134 | testsetop([1], [2], [1,2], "a+=b", "__iadd__") |
| 135 | testsetop([1,2], 3, [1,2,1,2,1,2], "a*=b", "__imul__") |
| 136 | testunop([1,2,3], 3, "len(a)", "__len__") |
| 137 | testbinop([1,2], 3, [1,2,1,2,1,2], "a*b", "__mul__") |
| 138 | testbinop([1,2], 3, [1,2,1,2,1,2], "b*a", "__rmul__") |
| 139 | testset2op([1,2], 1, 3, [1,3], "a[b]=c", "__setitem__") |
| 140 | testset3op([1,2,3,4], 1, 3, [5,6], [1,5,6,4], "a[b:c]=d", "__setslice__") |
| 141 | |
| 142 | def dicts(): |
| 143 | if verbose: print "Testing dict operations..." |
| 144 | testbinop({1:2}, {2:1}, -1, "cmp(a,b)", "__cmp__") |
| 145 | testbinop({1:2,3:4}, 1, 1, "b in a", "__contains__") |
| 146 | testbinop({1:2,3:4}, 2, 0, "b in a", "__contains__") |
| 147 | testbinop({1:2,3:4}, 1, 2, "a[b]", "__getitem__") |
| 148 | d = {1:2,3:4} |
| 149 | l1 = [] |
| 150 | for i in d.keys(): l1.append(i) |
| 151 | l = [] |
| 152 | for i in iter(d): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 153 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 154 | l = [] |
| 155 | for i in d.__iter__(): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 156 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 157 | l = [] |
| 158 | for i in dictionary.__iter__(d): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 159 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 160 | d = {1:2, 3:4} |
| 161 | testunop(d, 2, "len(a)", "__len__") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 162 | vereq(eval(repr(d), {}), d) |
| 163 | vereq(eval(d.__repr__(), {}), d) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 164 | testset2op({1:2,3:4}, 2, 3, {1:2,2:3,3:4}, "a[b]=c", "__setitem__") |
| 165 | |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 166 | def dict_constructor(): |
| 167 | if verbose: |
| 168 | print "Testing dictionary constructor ..." |
| 169 | d = dictionary() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 170 | vereq(d, {}) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 171 | d = dictionary({}) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 172 | vereq(d, {}) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 173 | d = dictionary(mapping={}) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 174 | vereq(d, {}) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 175 | d = dictionary({1: 2, 'a': 'b'}) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 176 | vereq(d, {1: 2, 'a': 'b'}) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 177 | for badarg in 0, 0L, 0j, "0", [0], (0,): |
| 178 | try: |
| 179 | dictionary(badarg) |
| 180 | except TypeError: |
| 181 | pass |
| 182 | else: |
| 183 | raise TestFailed("no TypeError from dictionary(%r)" % badarg) |
| 184 | try: |
| 185 | dictionary(senseless={}) |
| 186 | except TypeError: |
| 187 | pass |
| 188 | else: |
| 189 | raise TestFailed("no TypeError from dictionary(senseless={}") |
| 190 | |
| 191 | try: |
| 192 | dictionary({}, {}) |
| 193 | except TypeError: |
| 194 | pass |
| 195 | else: |
| 196 | raise TestFailed("no TypeError from dictionary({}, {})") |
| 197 | |
| 198 | class Mapping: |
| 199 | dict = {1:2, 3:4, 'a':1j} |
| 200 | |
| 201 | def __getitem__(self, i): |
| 202 | return self.dict[i] |
| 203 | |
| 204 | try: |
| 205 | dictionary(Mapping()) |
| 206 | except TypeError: |
| 207 | pass |
| 208 | else: |
| 209 | raise TestFailed("no TypeError from dictionary(incomplete mapping)") |
| 210 | |
| 211 | Mapping.keys = lambda self: self.dict.keys() |
| 212 | d = dictionary(mapping=Mapping()) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 213 | vereq(d, Mapping.dict) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 214 | |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 215 | def test_dir(): |
| 216 | if verbose: |
| 217 | print "Testing dir() ..." |
| 218 | junk = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 219 | vereq(dir(), ['junk']) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 220 | del junk |
| 221 | |
| 222 | # Just make sure these don't blow up! |
| 223 | for arg in 2, 2L, 2j, 2e0, [2], "2", u"2", (2,), {2:2}, type, test_dir: |
| 224 | dir(arg) |
| 225 | |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 226 | # Try classic classes. |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 227 | class C: |
| 228 | Cdata = 1 |
| 229 | def Cmethod(self): pass |
| 230 | |
| 231 | cstuff = ['Cdata', 'Cmethod', '__doc__', '__module__'] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 232 | vereq(dir(C), cstuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 233 | verify('im_self' in dir(C.Cmethod)) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 234 | |
| 235 | c = C() # c.__doc__ is an odd thing to see here; ditto c.__module__. |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 236 | vereq(dir(c), cstuff) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 237 | |
| 238 | c.cdata = 2 |
| 239 | c.cmethod = lambda self: 0 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 240 | vereq(dir(c), cstuff + ['cdata', 'cmethod']) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 241 | verify('im_self' in dir(c.Cmethod)) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 242 | |
| 243 | class A(C): |
| 244 | Adata = 1 |
| 245 | def Amethod(self): pass |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 246 | |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 247 | astuff = ['Adata', 'Amethod'] + cstuff |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 248 | vereq(dir(A), astuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 249 | verify('im_self' in dir(A.Amethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 250 | a = A() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 251 | vereq(dir(a), astuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 252 | verify('im_self' in dir(a.Amethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 253 | a.adata = 42 |
| 254 | a.amethod = lambda self: 3 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 255 | vereq(dir(a), astuff + ['adata', 'amethod']) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 256 | |
| 257 | # The same, but with new-style classes. Since these have object as a |
| 258 | # base class, a lot more gets sucked in. |
| 259 | def interesting(strings): |
| 260 | return [s for s in strings if not s.startswith('_')] |
| 261 | |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 262 | class C(object): |
| 263 | Cdata = 1 |
| 264 | def Cmethod(self): pass |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 265 | |
| 266 | cstuff = ['Cdata', 'Cmethod'] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 267 | vereq(interesting(dir(C)), cstuff) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 268 | |
| 269 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 270 | vereq(interesting(dir(c)), cstuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 271 | verify('im_self' in dir(C.Cmethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 272 | |
| 273 | c.cdata = 2 |
| 274 | c.cmethod = lambda self: 0 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 275 | vereq(interesting(dir(c)), cstuff + ['cdata', 'cmethod']) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 276 | verify('im_self' in dir(c.Cmethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 277 | |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 278 | class A(C): |
| 279 | Adata = 1 |
| 280 | def Amethod(self): pass |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 281 | |
| 282 | astuff = ['Adata', 'Amethod'] + cstuff |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 283 | vereq(interesting(dir(A)), astuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 284 | verify('im_self' in dir(A.Amethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 285 | a = A() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 286 | vereq(interesting(dir(a)), astuff) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 287 | a.adata = 42 |
| 288 | a.amethod = lambda self: 3 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 289 | vereq(interesting(dir(a)), astuff + ['adata', 'amethod']) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 290 | verify('im_self' in dir(a.Amethod)) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 291 | |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 292 | # Try a module subclass. |
| 293 | import sys |
| 294 | class M(type(sys)): |
| 295 | pass |
| 296 | minstance = M() |
| 297 | minstance.b = 2 |
| 298 | minstance.a = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 299 | vereq(dir(minstance), ['a', 'b']) |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 300 | |
| 301 | class M2(M): |
| 302 | def getdict(self): |
| 303 | return "Not a dict!" |
| 304 | __dict__ = property(getdict) |
| 305 | |
| 306 | m2instance = M2() |
| 307 | m2instance.b = 2 |
| 308 | m2instance.a = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 309 | vereq(m2instance.__dict__, "Not a dict!") |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 310 | try: |
| 311 | dir(m2instance) |
| 312 | except TypeError: |
| 313 | pass |
| 314 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 315 | binops = { |
| 316 | 'add': '+', |
| 317 | 'sub': '-', |
| 318 | 'mul': '*', |
| 319 | 'div': '/', |
| 320 | 'mod': '%', |
| 321 | 'divmod': 'divmod', |
| 322 | 'pow': '**', |
| 323 | 'lshift': '<<', |
| 324 | 'rshift': '>>', |
| 325 | 'and': '&', |
| 326 | 'xor': '^', |
| 327 | 'or': '|', |
| 328 | 'cmp': 'cmp', |
| 329 | 'lt': '<', |
| 330 | 'le': '<=', |
| 331 | 'eq': '==', |
| 332 | 'ne': '!=', |
| 333 | 'gt': '>', |
| 334 | 'ge': '>=', |
| 335 | } |
| 336 | |
| 337 | for name, expr in binops.items(): |
| 338 | if expr.islower(): |
| 339 | expr = expr + "(a, b)" |
| 340 | else: |
| 341 | expr = 'a %s b' % expr |
| 342 | binops[name] = expr |
| 343 | |
| 344 | unops = { |
| 345 | 'pos': '+', |
| 346 | 'neg': '-', |
| 347 | 'abs': 'abs', |
| 348 | 'invert': '~', |
| 349 | 'int': 'int', |
| 350 | 'long': 'long', |
| 351 | 'float': 'float', |
| 352 | 'oct': 'oct', |
| 353 | 'hex': 'hex', |
| 354 | } |
| 355 | |
| 356 | for name, expr in unops.items(): |
| 357 | if expr.islower(): |
| 358 | expr = expr + "(a)" |
| 359 | else: |
| 360 | expr = '%s a' % expr |
| 361 | unops[name] = expr |
| 362 | |
| 363 | def numops(a, b, skip=[]): |
| 364 | dict = {'a': a, 'b': b} |
| 365 | for name, expr in binops.items(): |
| 366 | if name not in skip: |
| 367 | name = "__%s__" % name |
| 368 | if hasattr(a, name): |
| 369 | res = eval(expr, dict) |
| 370 | testbinop(a, b, res, expr, name) |
| 371 | for name, expr in unops.items(): |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 372 | if name not in skip: |
| 373 | name = "__%s__" % name |
| 374 | if hasattr(a, name): |
| 375 | res = eval(expr, dict) |
| 376 | testunop(a, res, expr, name) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 377 | |
| 378 | def ints(): |
| 379 | if verbose: print "Testing int operations..." |
| 380 | numops(100, 3) |
| 381 | |
| 382 | def longs(): |
| 383 | if verbose: print "Testing long operations..." |
| 384 | numops(100L, 3L) |
| 385 | |
| 386 | def floats(): |
| 387 | if verbose: print "Testing float operations..." |
| 388 | numops(100.0, 3.0) |
| 389 | |
| 390 | def complexes(): |
| 391 | if verbose: print "Testing complex operations..." |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 392 | numops(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge', 'int', 'long', 'float']) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 393 | class Number(complex): |
| 394 | __slots__ = ['prec'] |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 395 | def __new__(cls, *args, **kwds): |
| 396 | result = complex.__new__(cls, *args) |
| 397 | result.prec = kwds.get('prec', 12) |
| 398 | return result |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 399 | def __repr__(self): |
| 400 | prec = self.prec |
| 401 | if self.imag == 0.0: |
| 402 | return "%.*g" % (prec, self.real) |
| 403 | if self.real == 0.0: |
| 404 | return "%.*gj" % (prec, self.imag) |
| 405 | return "(%.*g+%.*gj)" % (prec, self.real, prec, self.imag) |
| 406 | __str__ = __repr__ |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 407 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 408 | a = Number(3.14, prec=6) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 409 | vereq(`a`, "3.14") |
| 410 | vereq(a.prec, 6) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 411 | |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 412 | a = Number(a, prec=2) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 413 | vereq(`a`, "3.1") |
| 414 | vereq(a.prec, 2) |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 415 | |
| 416 | a = Number(234.5) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 417 | vereq(`a`, "234.5") |
| 418 | vereq(a.prec, 12) |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 419 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 420 | def spamlists(): |
| 421 | if verbose: print "Testing spamlist operations..." |
| 422 | import copy, xxsubtype as spam |
| 423 | def spamlist(l, memo=None): |
| 424 | import xxsubtype as spam |
| 425 | return spam.spamlist(l) |
| 426 | # This is an ugly hack: |
| 427 | copy._deepcopy_dispatch[spam.spamlist] = spamlist |
| 428 | |
| 429 | testbinop(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+b", "__add__") |
| 430 | testbinop(spamlist([1,2,3]), 2, 1, "b in a", "__contains__") |
| 431 | testbinop(spamlist([1,2,3]), 4, 0, "b in a", "__contains__") |
| 432 | testbinop(spamlist([1,2,3]), 1, 2, "a[b]", "__getitem__") |
| 433 | testternop(spamlist([1,2,3]), 0, 2, spamlist([1,2]), |
| 434 | "a[b:c]", "__getslice__") |
| 435 | testsetop(spamlist([1]), spamlist([2]), spamlist([1,2]), |
| 436 | "a+=b", "__iadd__") |
| 437 | testsetop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*=b", "__imul__") |
| 438 | testunop(spamlist([1,2,3]), 3, "len(a)", "__len__") |
| 439 | testbinop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*b", "__mul__") |
| 440 | testbinop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "b*a", "__rmul__") |
| 441 | testset2op(spamlist([1,2]), 1, 3, spamlist([1,3]), "a[b]=c", "__setitem__") |
| 442 | testset3op(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]), |
| 443 | spamlist([1,5,6,4]), "a[b:c]=d", "__setslice__") |
| 444 | # Test subclassing |
| 445 | class C(spam.spamlist): |
| 446 | def foo(self): return 1 |
| 447 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 448 | vereq(a, []) |
| 449 | vereq(a.foo(), 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 450 | a.append(100) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 451 | vereq(a, [100]) |
| 452 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 453 | a.setstate(42) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 454 | vereq(a.getstate(), 42) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 455 | |
| 456 | def spamdicts(): |
| 457 | if verbose: print "Testing spamdict operations..." |
| 458 | import copy, xxsubtype as spam |
| 459 | def spamdict(d, memo=None): |
| 460 | import xxsubtype as spam |
| 461 | sd = spam.spamdict() |
| 462 | for k, v in d.items(): sd[k] = v |
| 463 | return sd |
| 464 | # This is an ugly hack: |
| 465 | copy._deepcopy_dispatch[spam.spamdict] = spamdict |
| 466 | |
| 467 | testbinop(spamdict({1:2}), spamdict({2:1}), -1, "cmp(a,b)", "__cmp__") |
| 468 | testbinop(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__") |
| 469 | testbinop(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__") |
| 470 | testbinop(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__") |
| 471 | d = spamdict({1:2,3:4}) |
| 472 | l1 = [] |
| 473 | for i in d.keys(): l1.append(i) |
| 474 | l = [] |
| 475 | for i in iter(d): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 476 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 477 | l = [] |
| 478 | for i in d.__iter__(): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 479 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 480 | l = [] |
| 481 | for i in type(spamdict({})).__iter__(d): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 482 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 483 | straightd = {1:2, 3:4} |
| 484 | spamd = spamdict(straightd) |
| 485 | testunop(spamd, 2, "len(a)", "__len__") |
| 486 | testunop(spamd, repr(straightd), "repr(a)", "__repr__") |
| 487 | testset2op(spamdict({1:2,3:4}), 2, 3, spamdict({1:2,2:3,3:4}), |
| 488 | "a[b]=c", "__setitem__") |
| 489 | # Test subclassing |
| 490 | class C(spam.spamdict): |
| 491 | def foo(self): return 1 |
| 492 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 493 | vereq(a.items(), []) |
| 494 | vereq(a.foo(), 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 495 | a['foo'] = 'bar' |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 496 | vereq(a.items(), [('foo', 'bar')]) |
| 497 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 498 | a.setstate(100) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 499 | vereq(a.getstate(), 100) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 500 | |
| 501 | def pydicts(): |
| 502 | if verbose: print "Testing Python subclass of dict..." |
| 503 | verify(issubclass(dictionary, dictionary)) |
| 504 | verify(isinstance({}, dictionary)) |
| 505 | d = dictionary() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 506 | vereq(d, {}) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 507 | verify(d.__class__ is dictionary) |
| 508 | verify(isinstance(d, dictionary)) |
| 509 | class C(dictionary): |
| 510 | state = -1 |
| 511 | def __init__(self, *a, **kw): |
| 512 | if a: |
| 513 | assert len(a) == 1 |
| 514 | self.state = a[0] |
| 515 | if kw: |
| 516 | for k, v in kw.items(): self[v] = k |
| 517 | def __getitem__(self, key): |
| 518 | return self.get(key, 0) |
| 519 | def __setitem__(self, key, value): |
| 520 | assert isinstance(key, type(0)) |
| 521 | dictionary.__setitem__(self, key, value) |
| 522 | def setstate(self, state): |
| 523 | self.state = state |
| 524 | def getstate(self): |
| 525 | return self.state |
| 526 | verify(issubclass(C, dictionary)) |
| 527 | a1 = C(12) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 528 | vereq(a1.state, 12) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 529 | a2 = C(foo=1, bar=2) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 530 | vereq(a2[1] == 'foo' and a2[2], 'bar') |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 531 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 532 | vereq(a.state, -1) |
| 533 | vereq(a.getstate(), -1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 534 | a.setstate(0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 535 | vereq(a.state, 0) |
| 536 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 537 | a.setstate(10) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 538 | vereq(a.state, 10) |
| 539 | vereq(a.getstate(), 10) |
| 540 | vereq(a[42], 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 541 | a[42] = 24 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 542 | vereq(a[42], 24) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 543 | if verbose: print "pydict stress test ..." |
| 544 | N = 50 |
| 545 | for i in range(N): |
| 546 | a[i] = C() |
| 547 | for j in range(N): |
| 548 | a[i][j] = i*j |
| 549 | for i in range(N): |
| 550 | for j in range(N): |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 551 | vereq(a[i][j], i*j) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 552 | |
| 553 | def pylists(): |
| 554 | if verbose: print "Testing Python subclass of list..." |
| 555 | class C(list): |
| 556 | def __getitem__(self, i): |
| 557 | return list.__getitem__(self, i) + 100 |
| 558 | def __getslice__(self, i, j): |
| 559 | return (i, j) |
| 560 | a = C() |
| 561 | a.extend([0,1,2]) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 562 | vereq(a[0], 100) |
| 563 | vereq(a[1], 101) |
| 564 | vereq(a[2], 102) |
| 565 | vereq(a[100:200], (100,200)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 566 | |
| 567 | def metaclass(): |
| 568 | if verbose: print "Testing __metaclass__..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 569 | class C: |
| 570 | __metaclass__ = type |
| 571 | def __init__(self): |
| 572 | self.__state = 0 |
| 573 | def getstate(self): |
| 574 | return self.__state |
| 575 | def setstate(self, state): |
| 576 | self.__state = state |
| 577 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 578 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 579 | a.setstate(10) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 580 | vereq(a.getstate(), 10) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 581 | class D: |
| 582 | class __metaclass__(type): |
| 583 | def myself(cls): return cls |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 584 | vereq(D.myself(), D) |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 585 | d = D() |
| 586 | verify(d.__class__ is D) |
| 587 | class M1(type): |
| 588 | def __new__(cls, name, bases, dict): |
| 589 | dict['__spam__'] = 1 |
| 590 | return type.__new__(cls, name, bases, dict) |
| 591 | class C: |
| 592 | __metaclass__ = M1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 593 | vereq(C.__spam__, 1) |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 594 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 595 | vereq(c.__spam__, 1) |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 596 | |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 597 | class _instance(object): |
| 598 | pass |
| 599 | class M2(object): |
| 600 | def __new__(cls, name, bases, dict): |
| 601 | self = object.__new__(cls) |
| 602 | self.name = name |
| 603 | self.bases = bases |
| 604 | self.dict = dict |
| 605 | return self |
| 606 | __new__ = staticmethod(__new__) |
| 607 | def __call__(self): |
| 608 | it = _instance() |
Guido van Rossum | 7e1ff69 | 2001-08-17 11:55:58 +0000 | [diff] [blame] | 609 | # Early binding of methods |
| 610 | for key in self.dict: |
| 611 | if key.startswith("__"): |
| 612 | continue |
| 613 | setattr(it, key, self.dict[key].__get__(it, self)) |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 614 | return it |
| 615 | class C: |
| 616 | __metaclass__ = M2 |
| 617 | def spam(self): |
| 618 | return 42 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 619 | vereq(C.name, 'C') |
| 620 | vereq(C.bases, ()) |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 621 | verify('spam' in C.dict) |
| 622 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 623 | vereq(c.spam(), 42) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 624 | |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 625 | # More metaclass examples |
| 626 | |
| 627 | class autosuper(type): |
| 628 | # Automatically add __super to the class |
| 629 | # This trick only works for dynamic classes |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 630 | def __new__(metaclass, name, bases, dict): |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 631 | assert dict.get("__dynamic__", 1) |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 632 | cls = super(autosuper, metaclass).__new__(metaclass, |
| 633 | name, bases, dict) |
Guido van Rossum | bfa47b0 | 2001-08-31 04:35:14 +0000 | [diff] [blame] | 634 | # Name mangling for __super removes leading underscores |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 635 | while name[:1] == "_": |
| 636 | name = name[1:] |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 637 | if name: |
| 638 | name = "_%s__super" % name |
| 639 | else: |
| 640 | name = "__super" |
| 641 | setattr(cls, name, super(cls)) |
| 642 | return cls |
| 643 | class A: |
| 644 | __metaclass__ = autosuper |
| 645 | def meth(self): |
| 646 | return "A" |
| 647 | class B(A): |
| 648 | def meth(self): |
| 649 | return "B" + self.__super.meth() |
| 650 | class C(A): |
| 651 | def meth(self): |
| 652 | return "C" + self.__super.meth() |
| 653 | class D(C, B): |
| 654 | def meth(self): |
| 655 | return "D" + self.__super.meth() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 656 | vereq(D().meth(), "DCBA") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 657 | class E(B, C): |
| 658 | def meth(self): |
| 659 | return "E" + self.__super.meth() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 660 | vereq(E().meth(), "EBCA") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 661 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 662 | class autoproperty(type): |
| 663 | # Automatically create property attributes when methods |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 664 | # named _get_x and/or _set_x are found |
| 665 | def __new__(metaclass, name, bases, dict): |
| 666 | hits = {} |
| 667 | for key, val in dict.iteritems(): |
| 668 | if key.startswith("_get_"): |
| 669 | key = key[5:] |
| 670 | get, set = hits.get(key, (None, None)) |
| 671 | get = val |
| 672 | hits[key] = get, set |
| 673 | elif key.startswith("_set_"): |
| 674 | key = key[5:] |
| 675 | get, set = hits.get(key, (None, None)) |
| 676 | set = val |
| 677 | hits[key] = get, set |
| 678 | for key, (get, set) in hits.iteritems(): |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 679 | dict[key] = property(get, set) |
| 680 | return super(autoproperty, metaclass).__new__(metaclass, |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 681 | name, bases, dict) |
| 682 | class A: |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 683 | __metaclass__ = autoproperty |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 684 | def _get_x(self): |
| 685 | return -self.__x |
| 686 | def _set_x(self, x): |
| 687 | self.__x = -x |
| 688 | a = A() |
| 689 | verify(not hasattr(a, "x")) |
| 690 | a.x = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 691 | vereq(a.x, 12) |
| 692 | vereq(a._A__x, -12) |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 693 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 694 | class multimetaclass(autoproperty, autosuper): |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 695 | # Merge of multiple cooperating metaclasses |
| 696 | pass |
| 697 | class A: |
| 698 | __metaclass__ = multimetaclass |
| 699 | def _get_x(self): |
| 700 | return "A" |
| 701 | class B(A): |
| 702 | def _get_x(self): |
| 703 | return "B" + self.__super._get_x() |
| 704 | class C(A): |
| 705 | def _get_x(self): |
| 706 | return "C" + self.__super._get_x() |
| 707 | class D(C, B): |
| 708 | def _get_x(self): |
| 709 | return "D" + self.__super._get_x() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 710 | vereq(D().x, "DCBA") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 711 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 712 | def pymods(): |
| 713 | if verbose: print "Testing Python subclass of module..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 714 | log = [] |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 715 | import sys |
| 716 | MT = type(sys) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 717 | class MM(MT): |
| 718 | def __init__(self): |
| 719 | MT.__init__(self) |
Guido van Rossum | 867a8d2 | 2001-09-21 19:29:08 +0000 | [diff] [blame] | 720 | def __getattribute__(self, name): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 721 | log.append(("getattr", name)) |
Guido van Rossum | 867a8d2 | 2001-09-21 19:29:08 +0000 | [diff] [blame] | 722 | return MT.__getattribute__(self, name) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 723 | def __setattr__(self, name, value): |
| 724 | log.append(("setattr", name, value)) |
| 725 | MT.__setattr__(self, name, value) |
| 726 | def __delattr__(self, name): |
| 727 | log.append(("delattr", name)) |
| 728 | MT.__delattr__(self, name) |
| 729 | a = MM() |
| 730 | a.foo = 12 |
| 731 | x = a.foo |
| 732 | del a.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 733 | vereq(log, [("setattr", "foo", 12), |
| 734 | ("getattr", "foo"), |
| 735 | ("delattr", "foo")]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 736 | |
| 737 | def multi(): |
| 738 | if verbose: print "Testing multiple inheritance..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 739 | class C(object): |
| 740 | def __init__(self): |
| 741 | self.__state = 0 |
| 742 | def getstate(self): |
| 743 | return self.__state |
| 744 | def setstate(self, state): |
| 745 | self.__state = state |
| 746 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 747 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 748 | a.setstate(10) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 749 | vereq(a.getstate(), 10) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 750 | class D(dictionary, C): |
| 751 | def __init__(self): |
| 752 | type({}).__init__(self) |
| 753 | C.__init__(self) |
| 754 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 755 | vereq(d.keys(), []) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 756 | d["hello"] = "world" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 757 | vereq(d.items(), [("hello", "world")]) |
| 758 | vereq(d["hello"], "world") |
| 759 | vereq(d.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 760 | d.setstate(10) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 761 | vereq(d.getstate(), 10) |
| 762 | vereq(D.__mro__, (D, dictionary, C, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 763 | |
Guido van Rossum | e45763a | 2001-08-10 21:28:46 +0000 | [diff] [blame] | 764 | # SF bug #442833 |
| 765 | class Node(object): |
| 766 | def __int__(self): |
| 767 | return int(self.foo()) |
| 768 | def foo(self): |
| 769 | return "23" |
| 770 | class Frag(Node, list): |
| 771 | def foo(self): |
| 772 | return "42" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 773 | vereq(Node().__int__(), 23) |
| 774 | vereq(int(Node()), 23) |
| 775 | vereq(Frag().__int__(), 42) |
| 776 | vereq(int(Frag()), 42) |
Guido van Rossum | e45763a | 2001-08-10 21:28:46 +0000 | [diff] [blame] | 777 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 778 | def diamond(): |
| 779 | if verbose: print "Testing multiple inheritance special cases..." |
| 780 | class A(object): |
| 781 | def spam(self): return "A" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 782 | vereq(A().spam(), "A") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 783 | class B(A): |
| 784 | def boo(self): return "B" |
| 785 | def spam(self): return "B" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 786 | vereq(B().spam(), "B") |
| 787 | vereq(B().boo(), "B") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 788 | class C(A): |
| 789 | def boo(self): return "C" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 790 | vereq(C().spam(), "A") |
| 791 | vereq(C().boo(), "C") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 792 | class D(B, C): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 793 | vereq(D().spam(), "B") |
| 794 | vereq(D().boo(), "B") |
| 795 | vereq(D.__mro__, (D, B, C, A, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 796 | class E(C, B): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 797 | vereq(E().spam(), "B") |
| 798 | vereq(E().boo(), "C") |
| 799 | vereq(E.__mro__, (E, C, B, A, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 800 | class F(D, E): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 801 | vereq(F().spam(), "B") |
| 802 | vereq(F().boo(), "B") |
| 803 | vereq(F.__mro__, (F, D, E, B, C, A, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 804 | class G(E, D): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 805 | vereq(G().spam(), "B") |
| 806 | vereq(G().boo(), "C") |
| 807 | vereq(G.__mro__, (G, E, D, C, B, A, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 808 | |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 809 | def objects(): |
| 810 | if verbose: print "Testing object class..." |
| 811 | a = object() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 812 | vereq(a.__class__, object) |
| 813 | vereq(type(a), object) |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 814 | b = object() |
| 815 | verify(a is not b) |
| 816 | verify(not hasattr(a, "foo")) |
| 817 | try: |
| 818 | a.foo = 12 |
Guido van Rossum | 6d94627 | 2001-08-10 19:42:38 +0000 | [diff] [blame] | 819 | except (AttributeError, TypeError): |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 820 | pass |
| 821 | else: |
| 822 | verify(0, "object() should not allow setting a foo attribute") |
| 823 | verify(not hasattr(object(), "__dict__")) |
| 824 | |
| 825 | class Cdict(object): |
| 826 | pass |
| 827 | x = Cdict() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 828 | vereq(x.__dict__, {}) |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 829 | x.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 830 | vereq(x.foo, 1) |
| 831 | vereq(x.__dict__, {'foo': 1}) |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 832 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 833 | def slots(): |
| 834 | if verbose: print "Testing __slots__..." |
| 835 | class C0(object): |
| 836 | __slots__ = [] |
| 837 | x = C0() |
| 838 | verify(not hasattr(x, "__dict__")) |
| 839 | verify(not hasattr(x, "foo")) |
| 840 | |
| 841 | class C1(object): |
| 842 | __slots__ = ['a'] |
| 843 | x = C1() |
| 844 | verify(not hasattr(x, "__dict__")) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 845 | vereq(x.a, None) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 846 | x.a = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 847 | vereq(x.a, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 848 | del x.a |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 849 | vereq(x.a, None) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 850 | |
| 851 | class C3(object): |
| 852 | __slots__ = ['a', 'b', 'c'] |
| 853 | x = C3() |
| 854 | verify(not hasattr(x, "__dict__")) |
| 855 | verify(x.a is None) |
| 856 | verify(x.b is None) |
| 857 | verify(x.c is None) |
| 858 | x.a = 1 |
| 859 | x.b = 2 |
| 860 | x.c = 3 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 861 | vereq(x.a, 1) |
| 862 | vereq(x.b, 2) |
| 863 | vereq(x.c, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 864 | |
| 865 | def dynamics(): |
| 866 | if verbose: print "Testing __dynamic__..." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 867 | vereq(object.__dynamic__, 0) |
| 868 | vereq(list.__dynamic__, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 869 | class S1: |
| 870 | __metaclass__ = type |
Guido van Rossum | 751c4c8 | 2001-09-29 00:40:25 +0000 | [diff] [blame] | 871 | __dynamic__ = 0 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 872 | vereq(S1.__dynamic__, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 873 | class S(object): |
Guido van Rossum | 751c4c8 | 2001-09-29 00:40:25 +0000 | [diff] [blame] | 874 | __dynamic__ = 0 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 875 | vereq(S.__dynamic__, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 876 | class D(object): |
| 877 | __dynamic__ = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 878 | vereq(D.__dynamic__, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 879 | class E(D, S): |
| 880 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 881 | vereq(E.__dynamic__, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 882 | class F(S, D): |
| 883 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 884 | vereq(F.__dynamic__, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 885 | try: |
| 886 | S.foo = 1 |
| 887 | except (AttributeError, TypeError): |
| 888 | pass |
| 889 | else: |
| 890 | verify(0, "assignment to a static class attribute should be illegal") |
| 891 | D.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 892 | vereq(D.foo, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 893 | # Test that dynamic attributes are inherited |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 894 | vereq(E.foo, 1) |
| 895 | vereq(F.foo, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 896 | class SS(D): |
| 897 | __dynamic__ = 0 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 898 | vereq(SS.__dynamic__, 0) |
| 899 | vereq(SS.foo, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 900 | try: |
| 901 | SS.foo = 1 |
| 902 | except (AttributeError, TypeError): |
| 903 | pass |
| 904 | else: |
| 905 | verify(0, "assignment to SS.foo should be illegal") |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 906 | # Test dynamic instances |
| 907 | class C(object): |
| 908 | __dynamic__ = 1 |
Guido van Rossum | 4a5a2bc | 2001-10-03 13:59:54 +0000 | [diff] [blame] | 909 | # XXX Ideally the following def shouldn't be necessary, |
| 910 | # but it's too much of a performance burden. |
| 911 | # See XXX comment in slot_tp_getattr_hook. |
| 912 | def __getattr__(self, name): |
| 913 | raise AttributeError, name |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 914 | a = C() |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 915 | verify(not hasattr(a, "foobar")) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 916 | C.foobar = 2 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 917 | vereq(a.foobar, 2) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 918 | C.method = lambda self: 42 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 919 | vereq(a.method(), 42) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 920 | C.__repr__ = lambda self: "C()" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 921 | vereq(repr(a), "C()") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 922 | C.__int__ = lambda self: 100 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 923 | vereq(int(a), 100) |
| 924 | vereq(a.foobar, 2) |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 925 | verify(not hasattr(a, "spam")) |
| 926 | def mygetattr(self, name): |
| 927 | if name == "spam": |
| 928 | return "spam" |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 929 | raise AttributeError |
| 930 | C.__getattr__ = mygetattr |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 931 | vereq(a.spam, "spam") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 932 | a.new = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 933 | vereq(a.new, 12) |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 934 | def mysetattr(self, name, value): |
| 935 | if name == "spam": |
| 936 | raise AttributeError |
| 937 | return object.__setattr__(self, name, value) |
| 938 | C.__setattr__ = mysetattr |
| 939 | try: |
| 940 | a.spam = "not spam" |
| 941 | except AttributeError: |
| 942 | pass |
| 943 | else: |
| 944 | verify(0, "expected AttributeError") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 945 | vereq(a.spam, "spam") |
Guido van Rossum | 80e3675 | 2001-08-14 20:00:33 +0000 | [diff] [blame] | 946 | class D(C): |
| 947 | pass |
| 948 | d = D() |
| 949 | d.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 950 | vereq(d.foo, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 951 | |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 952 | # Test handling of int*seq and seq*int |
| 953 | class I(int): |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 954 | __dynamic__ = 1 # XXX why? |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 955 | vereq("a"*I(2), "aa") |
| 956 | vereq(I(2)*"a", "aa") |
| 957 | vereq(2*I(3), 6) |
| 958 | vereq(I(3)*2, 6) |
| 959 | vereq(I(3)*I(2), 6) |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 960 | |
| 961 | # Test handling of long*seq and seq*long |
| 962 | class L(long): |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 963 | __dynamic__ = 1 # XXX why? |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 964 | vereq("a"*L(2L), "aa") |
| 965 | vereq(L(2L)*"a", "aa") |
| 966 | vereq(2*L(3), 6) |
| 967 | vereq(L(3)*2, 6) |
| 968 | vereq(L(3)*L(2), 6) |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 969 | |
Guido van Rossum | 3d45d8f | 2001-09-24 18:47:40 +0000 | [diff] [blame] | 970 | # Test comparison of classes with dynamic metaclasses |
| 971 | class dynamicmetaclass(type): |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 972 | __dynamic__ = 1 # XXX ??? |
Guido van Rossum | 3d45d8f | 2001-09-24 18:47:40 +0000 | [diff] [blame] | 973 | class someclass: |
| 974 | __metaclass__ = dynamicmetaclass |
| 975 | verify(someclass != object) |
| 976 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 977 | def errors(): |
| 978 | if verbose: print "Testing errors..." |
| 979 | |
| 980 | try: |
| 981 | class C(list, dictionary): |
| 982 | pass |
| 983 | except TypeError: |
| 984 | pass |
| 985 | else: |
| 986 | verify(0, "inheritance from both list and dict should be illegal") |
| 987 | |
| 988 | try: |
| 989 | class C(object, None): |
| 990 | pass |
| 991 | except TypeError: |
| 992 | pass |
| 993 | else: |
| 994 | verify(0, "inheritance from non-type should be illegal") |
| 995 | class Classic: |
| 996 | pass |
| 997 | |
| 998 | try: |
| 999 | class C(object, Classic): |
| 1000 | pass |
| 1001 | except TypeError: |
| 1002 | pass |
| 1003 | else: |
| 1004 | verify(0, "inheritance from object and Classic should be illegal") |
| 1005 | |
| 1006 | try: |
Guido van Rossum | 8aea0cc | 2001-08-29 15:48:43 +0000 | [diff] [blame] | 1007 | class C(type(len)): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1008 | pass |
| 1009 | except TypeError: |
| 1010 | pass |
| 1011 | else: |
Guido van Rossum | 8aea0cc | 2001-08-29 15:48:43 +0000 | [diff] [blame] | 1012 | verify(0, "inheritance from CFunction should be illegal") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1013 | |
| 1014 | try: |
| 1015 | class C(object): |
| 1016 | __slots__ = 1 |
| 1017 | except TypeError: |
| 1018 | pass |
| 1019 | else: |
| 1020 | verify(0, "__slots__ = 1 should be illegal") |
| 1021 | |
| 1022 | try: |
| 1023 | class C(object): |
| 1024 | __slots__ = [1] |
| 1025 | except TypeError: |
| 1026 | pass |
| 1027 | else: |
| 1028 | verify(0, "__slots__ = [1] should be illegal") |
| 1029 | |
| 1030 | def classmethods(): |
| 1031 | if verbose: print "Testing class methods..." |
| 1032 | class C(object): |
| 1033 | def foo(*a): return a |
| 1034 | goo = classmethod(foo) |
| 1035 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1036 | vereq(C.goo(1), (C, 1)) |
| 1037 | vereq(c.goo(1), (C, 1)) |
| 1038 | vereq(c.foo(1), (c, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1039 | class D(C): |
| 1040 | pass |
| 1041 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1042 | vereq(D.goo(1), (D, 1)) |
| 1043 | vereq(d.goo(1), (D, 1)) |
| 1044 | vereq(d.foo(1), (d, 1)) |
| 1045 | vereq(D.foo(d, 1), (d, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1046 | |
| 1047 | def staticmethods(): |
| 1048 | if verbose: print "Testing static methods..." |
| 1049 | class C(object): |
| 1050 | def foo(*a): return a |
| 1051 | goo = staticmethod(foo) |
| 1052 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1053 | vereq(C.goo(1), (1,)) |
| 1054 | vereq(c.goo(1), (1,)) |
| 1055 | vereq(c.foo(1), (c, 1,)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1056 | class D(C): |
| 1057 | pass |
| 1058 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1059 | vereq(D.goo(1), (1,)) |
| 1060 | vereq(d.goo(1), (1,)) |
| 1061 | vereq(d.foo(1), (d, 1)) |
| 1062 | vereq(D.foo(d, 1), (d, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1063 | |
| 1064 | def classic(): |
| 1065 | if verbose: print "Testing classic classes..." |
| 1066 | class C: |
| 1067 | def foo(*a): return a |
| 1068 | goo = classmethod(foo) |
| 1069 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1070 | vereq(C.goo(1), (C, 1)) |
| 1071 | vereq(c.goo(1), (C, 1)) |
| 1072 | vereq(c.foo(1), (c, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1073 | class D(C): |
| 1074 | pass |
| 1075 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1076 | vereq(D.goo(1), (D, 1)) |
| 1077 | vereq(d.goo(1), (D, 1)) |
| 1078 | vereq(d.foo(1), (d, 1)) |
| 1079 | vereq(D.foo(d, 1), (d, 1)) |
Guido van Rossum | 9301876 | 2001-08-17 13:40:47 +0000 | [diff] [blame] | 1080 | class E: # *not* subclassing from C |
| 1081 | foo = C.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1082 | vereq(E().foo, C.foo) # i.e., unbound |
Guido van Rossum | 84a79a8 | 2001-08-17 13:58:31 +0000 | [diff] [blame] | 1083 | verify(repr(C.foo.__get__(C())).startswith("<bound method ")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1084 | |
| 1085 | def compattr(): |
| 1086 | if verbose: print "Testing computed attributes..." |
| 1087 | class C(object): |
| 1088 | class computed_attribute(object): |
| 1089 | def __init__(self, get, set=None): |
| 1090 | self.__get = get |
| 1091 | self.__set = set |
| 1092 | def __get__(self, obj, type=None): |
| 1093 | return self.__get(obj) |
| 1094 | def __set__(self, obj, value): |
| 1095 | return self.__set(obj, value) |
| 1096 | def __init__(self): |
| 1097 | self.__x = 0 |
| 1098 | def __get_x(self): |
| 1099 | x = self.__x |
| 1100 | self.__x = x+1 |
| 1101 | return x |
| 1102 | def __set_x(self, x): |
| 1103 | self.__x = x |
| 1104 | x = computed_attribute(__get_x, __set_x) |
| 1105 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1106 | vereq(a.x, 0) |
| 1107 | vereq(a.x, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1108 | a.x = 10 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1109 | vereq(a.x, 10) |
| 1110 | vereq(a.x, 11) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1111 | |
| 1112 | def newslot(): |
| 1113 | if verbose: print "Testing __new__ slot override..." |
| 1114 | class C(list): |
| 1115 | def __new__(cls): |
| 1116 | self = list.__new__(cls) |
| 1117 | self.foo = 1 |
| 1118 | return self |
| 1119 | def __init__(self): |
| 1120 | self.foo = self.foo + 2 |
| 1121 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1122 | vereq(a.foo, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1123 | verify(a.__class__ is C) |
| 1124 | class D(C): |
| 1125 | pass |
| 1126 | b = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1127 | vereq(b.foo, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1128 | verify(b.__class__ is D) |
| 1129 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1130 | def altmro(): |
| 1131 | if verbose: print "Testing mro() and overriding it..." |
| 1132 | class A(object): |
| 1133 | def f(self): return "A" |
| 1134 | class B(A): |
| 1135 | pass |
| 1136 | class C(A): |
| 1137 | def f(self): return "C" |
| 1138 | class D(B, C): |
| 1139 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1140 | vereq(D.mro(), [D, B, C, A, object]) |
| 1141 | vereq(D.__mro__, (D, B, C, A, object)) |
| 1142 | vereq(D().f(), "C") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1143 | class PerverseMetaType(type): |
| 1144 | def mro(cls): |
| 1145 | L = type.mro(cls) |
| 1146 | L.reverse() |
| 1147 | return L |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1148 | class X(A,B,C,D): |
| 1149 | __metaclass__ = PerverseMetaType |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1150 | vereq(X.__mro__, (object, A, C, B, D, X)) |
| 1151 | vereq(X().f(), "A") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1152 | |
| 1153 | def overloading(): |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1154 | if verbose: print "Testing operator overloading..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1155 | |
| 1156 | class B(object): |
| 1157 | "Intermediate class because object doesn't have a __setattr__" |
| 1158 | |
| 1159 | class C(B): |
| 1160 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 1161 | def __getattr__(self, name): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1162 | if name == "foo": |
| 1163 | return ("getattr", name) |
| 1164 | else: |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 1165 | raise AttributeError |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1166 | def __setattr__(self, name, value): |
| 1167 | if name == "foo": |
| 1168 | self.setattr = (name, value) |
| 1169 | else: |
| 1170 | return B.__setattr__(self, name, value) |
| 1171 | def __delattr__(self, name): |
| 1172 | if name == "foo": |
| 1173 | self.delattr = name |
| 1174 | else: |
| 1175 | return B.__delattr__(self, name) |
| 1176 | |
| 1177 | def __getitem__(self, key): |
| 1178 | return ("getitem", key) |
| 1179 | def __setitem__(self, key, value): |
| 1180 | self.setitem = (key, value) |
| 1181 | def __delitem__(self, key): |
| 1182 | self.delitem = key |
| 1183 | |
| 1184 | def __getslice__(self, i, j): |
| 1185 | return ("getslice", i, j) |
| 1186 | def __setslice__(self, i, j, value): |
| 1187 | self.setslice = (i, j, value) |
| 1188 | def __delslice__(self, i, j): |
| 1189 | self.delslice = (i, j) |
| 1190 | |
| 1191 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1192 | vereq(a.foo, ("getattr", "foo")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1193 | a.foo = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1194 | vereq(a.setattr, ("foo", 12)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1195 | del a.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1196 | vereq(a.delattr, "foo") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1197 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1198 | vereq(a[12], ("getitem", 12)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1199 | a[12] = 21 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1200 | vereq(a.setitem, (12, 21)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1201 | del a[12] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1202 | vereq(a.delitem, 12) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1203 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1204 | vereq(a[0:10], ("getslice", 0, 10)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1205 | a[0:10] = "foo" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1206 | vereq(a.setslice, (0, 10, "foo")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1207 | del a[0:10] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1208 | vereq(a.delslice, (0, 10)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1209 | |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1210 | def methods(): |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1211 | if verbose: print "Testing methods..." |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1212 | class C(object): |
| 1213 | def __init__(self, x): |
| 1214 | self.x = x |
| 1215 | def foo(self): |
| 1216 | return self.x |
| 1217 | c1 = C(1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1218 | vereq(c1.foo(), 1) |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1219 | class D(C): |
| 1220 | boo = C.foo |
| 1221 | goo = c1.foo |
| 1222 | d2 = D(2) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1223 | vereq(d2.foo(), 2) |
| 1224 | vereq(d2.boo(), 2) |
| 1225 | vereq(d2.goo(), 1) |
Guido van Rossum | 9301876 | 2001-08-17 13:40:47 +0000 | [diff] [blame] | 1226 | class E(object): |
| 1227 | foo = C.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1228 | vereq(E().foo, C.foo) # i.e., unbound |
Guido van Rossum | 84a79a8 | 2001-08-17 13:58:31 +0000 | [diff] [blame] | 1229 | verify(repr(C.foo.__get__(C(1))).startswith("<bound method ")) |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1230 | |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1231 | def specials(): |
| 1232 | # Test operators like __hash__ for which a built-in default exists |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1233 | if verbose: print "Testing special operators..." |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1234 | # Test the default behavior for static classes |
| 1235 | class C(object): |
| 1236 | def __getitem__(self, i): |
| 1237 | if 0 <= i < 10: return i |
| 1238 | raise IndexError |
| 1239 | c1 = C() |
| 1240 | c2 = C() |
| 1241 | verify(not not c1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1242 | vereq(hash(c1), id(c1)) |
| 1243 | vereq(cmp(c1, c2), cmp(id(c1), id(c2))) |
| 1244 | vereq(c1, c1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1245 | verify(c1 != c2) |
| 1246 | verify(not c1 != c1) |
| 1247 | verify(not c1 == c2) |
Tim Peters | 4d2dded | 2001-08-16 19:50:51 +0000 | [diff] [blame] | 1248 | # Note that the module name appears in str/repr, and that varies |
| 1249 | # depending on whether this test is run standalone or from a framework. |
Guido van Rossum | ff0e6d6 | 2001-09-24 16:03:59 +0000 | [diff] [blame] | 1250 | verify(str(c1).find('C object at ') >= 0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1251 | vereq(str(c1), repr(c1)) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1252 | verify(-1 not in c1) |
| 1253 | for i in range(10): |
| 1254 | verify(i in c1) |
| 1255 | verify(10 not in c1) |
| 1256 | # Test the default behavior for dynamic classes |
| 1257 | class D(object): |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 1258 | __dynamic__ = 1 # XXX why? |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1259 | def __getitem__(self, i): |
| 1260 | if 0 <= i < 10: return i |
| 1261 | raise IndexError |
| 1262 | d1 = D() |
| 1263 | d2 = D() |
| 1264 | verify(not not d1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1265 | vereq(hash(d1), id(d1)) |
| 1266 | vereq(cmp(d1, d2), cmp(id(d1), id(d2))) |
| 1267 | vereq(d1, d1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1268 | verify(d1 != d2) |
| 1269 | verify(not d1 != d1) |
| 1270 | verify(not d1 == d2) |
Tim Peters | 4d2dded | 2001-08-16 19:50:51 +0000 | [diff] [blame] | 1271 | # Note that the module name appears in str/repr, and that varies |
| 1272 | # depending on whether this test is run standalone or from a framework. |
Guido van Rossum | ff0e6d6 | 2001-09-24 16:03:59 +0000 | [diff] [blame] | 1273 | verify(str(d1).find('D object at ') >= 0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1274 | vereq(str(d1), repr(d1)) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1275 | verify(-1 not in d1) |
| 1276 | for i in range(10): |
| 1277 | verify(i in d1) |
| 1278 | verify(10 not in d1) |
| 1279 | # Test overridden behavior for static classes |
| 1280 | class Proxy(object): |
| 1281 | def __init__(self, x): |
| 1282 | self.x = x |
| 1283 | def __nonzero__(self): |
| 1284 | return not not self.x |
| 1285 | def __hash__(self): |
| 1286 | return hash(self.x) |
| 1287 | def __eq__(self, other): |
| 1288 | return self.x == other |
| 1289 | def __ne__(self, other): |
| 1290 | return self.x != other |
| 1291 | def __cmp__(self, other): |
| 1292 | return cmp(self.x, other.x) |
| 1293 | def __str__(self): |
| 1294 | return "Proxy:%s" % self.x |
| 1295 | def __repr__(self): |
| 1296 | return "Proxy(%r)" % self.x |
| 1297 | def __contains__(self, value): |
| 1298 | return value in self.x |
| 1299 | p0 = Proxy(0) |
| 1300 | p1 = Proxy(1) |
| 1301 | p_1 = Proxy(-1) |
| 1302 | verify(not p0) |
| 1303 | verify(not not p1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1304 | vereq(hash(p0), hash(0)) |
| 1305 | vereq(p0, p0) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1306 | verify(p0 != p1) |
| 1307 | verify(not p0 != p0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1308 | vereq(not p0, p1) |
| 1309 | vereq(cmp(p0, p1), -1) |
| 1310 | vereq(cmp(p0, p0), 0) |
| 1311 | vereq(cmp(p0, p_1), 1) |
| 1312 | vereq(str(p0), "Proxy:0") |
| 1313 | vereq(repr(p0), "Proxy(0)") |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1314 | p10 = Proxy(range(10)) |
| 1315 | verify(-1 not in p10) |
| 1316 | for i in range(10): |
| 1317 | verify(i in p10) |
| 1318 | verify(10 not in p10) |
| 1319 | # Test overridden behavior for dynamic classes |
| 1320 | class DProxy(object): |
| 1321 | __dynamic__ = 1 |
| 1322 | def __init__(self, x): |
| 1323 | self.x = x |
| 1324 | def __nonzero__(self): |
| 1325 | return not not self.x |
| 1326 | def __hash__(self): |
| 1327 | return hash(self.x) |
| 1328 | def __eq__(self, other): |
| 1329 | return self.x == other |
| 1330 | def __ne__(self, other): |
| 1331 | return self.x != other |
| 1332 | def __cmp__(self, other): |
| 1333 | return cmp(self.x, other.x) |
| 1334 | def __str__(self): |
| 1335 | return "DProxy:%s" % self.x |
| 1336 | def __repr__(self): |
| 1337 | return "DProxy(%r)" % self.x |
| 1338 | def __contains__(self, value): |
| 1339 | return value in self.x |
| 1340 | p0 = DProxy(0) |
| 1341 | p1 = DProxy(1) |
| 1342 | p_1 = DProxy(-1) |
| 1343 | verify(not p0) |
| 1344 | verify(not not p1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1345 | vereq(hash(p0), hash(0)) |
| 1346 | vereq(p0, p0) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1347 | verify(p0 != p1) |
| 1348 | verify(not p0 != p0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1349 | vereq(not p0, p1) |
| 1350 | vereq(cmp(p0, p1), -1) |
| 1351 | vereq(cmp(p0, p0), 0) |
| 1352 | vereq(cmp(p0, p_1), 1) |
| 1353 | vereq(str(p0), "DProxy:0") |
| 1354 | vereq(repr(p0), "DProxy(0)") |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1355 | p10 = DProxy(range(10)) |
| 1356 | verify(-1 not in p10) |
| 1357 | for i in range(10): |
| 1358 | verify(i in p10) |
| 1359 | verify(10 not in p10) |
Guido van Rossum | 843daa8 | 2001-09-18 20:04:26 +0000 | [diff] [blame] | 1360 | # Safety test for __cmp__ |
| 1361 | def unsafecmp(a, b): |
| 1362 | try: |
| 1363 | a.__class__.__cmp__(a, b) |
| 1364 | except TypeError: |
| 1365 | pass |
| 1366 | else: |
| 1367 | raise TestFailed, "shouldn't allow %s.__cmp__(%r, %r)" % ( |
| 1368 | a.__class__, a, b) |
| 1369 | unsafecmp(u"123", "123") |
| 1370 | unsafecmp("123", u"123") |
| 1371 | unsafecmp(1, 1.0) |
| 1372 | unsafecmp(1.0, 1) |
| 1373 | unsafecmp(1, 1L) |
| 1374 | unsafecmp(1L, 1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1375 | |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1376 | def weakrefs(): |
| 1377 | if verbose: print "Testing weak references..." |
| 1378 | import weakref |
| 1379 | class C(object): |
| 1380 | pass |
| 1381 | c = C() |
| 1382 | r = weakref.ref(c) |
| 1383 | verify(r() is c) |
| 1384 | del c |
| 1385 | verify(r() is None) |
| 1386 | del r |
| 1387 | class NoWeak(object): |
| 1388 | __slots__ = ['foo'] |
| 1389 | no = NoWeak() |
| 1390 | try: |
| 1391 | weakref.ref(no) |
| 1392 | except TypeError, msg: |
| 1393 | verify(str(msg).find("weakly") >= 0) |
| 1394 | else: |
| 1395 | verify(0, "weakref.ref(no) should be illegal") |
| 1396 | class Weak(object): |
| 1397 | __slots__ = ['foo', '__weakref__'] |
| 1398 | yes = Weak() |
| 1399 | r = weakref.ref(yes) |
| 1400 | verify(r() is yes) |
| 1401 | del yes |
| 1402 | verify(r() is None) |
| 1403 | del r |
| 1404 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1405 | def properties(): |
| 1406 | if verbose: print "Testing property..." |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1407 | class C(object): |
| 1408 | def getx(self): |
| 1409 | return self.__x |
| 1410 | def setx(self, value): |
| 1411 | self.__x = value |
| 1412 | def delx(self): |
| 1413 | del self.__x |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1414 | x = property(getx, setx, delx, doc="I'm the x property.") |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1415 | a = C() |
| 1416 | verify(not hasattr(a, "x")) |
| 1417 | a.x = 42 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1418 | vereq(a._C__x, 42) |
| 1419 | vereq(a.x, 42) |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1420 | del a.x |
| 1421 | verify(not hasattr(a, "x")) |
| 1422 | verify(not hasattr(a, "_C__x")) |
| 1423 | C.x.__set__(a, 100) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1424 | vereq(C.x.__get__(a), 100) |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1425 | ## C.x.__set__(a) |
| 1426 | ## verify(not hasattr(a, "x")) |
| 1427 | |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1428 | raw = C.__dict__['x'] |
| 1429 | verify(isinstance(raw, property)) |
| 1430 | |
| 1431 | attrs = dir(raw) |
| 1432 | verify("__doc__" in attrs) |
| 1433 | verify("fget" in attrs) |
| 1434 | verify("fset" in attrs) |
| 1435 | verify("fdel" in attrs) |
| 1436 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1437 | vereq(raw.__doc__, "I'm the x property.") |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1438 | verify(raw.fget is C.__dict__['getx']) |
| 1439 | verify(raw.fset is C.__dict__['setx']) |
| 1440 | verify(raw.fdel is C.__dict__['delx']) |
| 1441 | |
| 1442 | for attr in "__doc__", "fget", "fset", "fdel": |
| 1443 | try: |
| 1444 | setattr(raw, attr, 42) |
| 1445 | except TypeError, msg: |
| 1446 | if str(msg).find('readonly') < 0: |
| 1447 | raise TestFailed("when setting readonly attr %r on a " |
| 1448 | "property, got unexpected TypeError " |
| 1449 | "msg %r" % (attr, str(msg))) |
| 1450 | else: |
| 1451 | raise TestFailed("expected TypeError from trying to set " |
| 1452 | "readonly %r attr on a property" % attr) |
| 1453 | |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1454 | def supers(): |
Guido van Rossum | 9881fc1 | 2001-08-24 17:07:20 +0000 | [diff] [blame] | 1455 | if verbose: print "Testing super..." |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1456 | |
| 1457 | class A(object): |
| 1458 | def meth(self, a): |
| 1459 | return "A(%r)" % a |
| 1460 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1461 | vereq(A().meth(1), "A(1)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1462 | |
| 1463 | class B(A): |
| 1464 | def __init__(self): |
| 1465 | self.__super = super(B, self) |
| 1466 | def meth(self, a): |
| 1467 | return "B(%r)" % a + self.__super.meth(a) |
| 1468 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1469 | vereq(B().meth(2), "B(2)A(2)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1470 | |
| 1471 | class C(A): |
| 1472 | __dynamic__ = 1 |
| 1473 | def meth(self, a): |
| 1474 | return "C(%r)" % a + self.__super.meth(a) |
| 1475 | C._C__super = super(C) |
| 1476 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1477 | vereq(C().meth(3), "C(3)A(3)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1478 | |
| 1479 | class D(C, B): |
| 1480 | def meth(self, a): |
| 1481 | return "D(%r)" % a + super(D, self).meth(a) |
| 1482 | |
| 1483 | verify (D().meth(4) == "D(4)C(4)B(4)A(4)") |
| 1484 | |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1485 | def inherits(): |
| 1486 | if verbose: print "Testing inheritance from basic types..." |
| 1487 | |
| 1488 | class hexint(int): |
| 1489 | def __repr__(self): |
| 1490 | return hex(self) |
| 1491 | def __add__(self, other): |
| 1492 | return hexint(int.__add__(self, other)) |
| 1493 | # (Note that overriding __radd__ doesn't work, |
| 1494 | # because the int type gets first dibs.) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1495 | vereq(repr(hexint(7) + 9), "0x10") |
| 1496 | vereq(repr(hexint(1000) + 7), "0x3ef") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1497 | a = hexint(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1498 | vereq(a, 12345) |
| 1499 | vereq(int(a), 12345) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1500 | verify(int(a).__class__ is int) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1501 | vereq(hash(a), hash(12345)) |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 1502 | verify((+a).__class__ is int) |
| 1503 | verify((a >> 0).__class__ is int) |
| 1504 | verify((a << 0).__class__ is int) |
| 1505 | verify((hexint(0) << 12).__class__ is int) |
| 1506 | verify((hexint(0) >> 12).__class__ is int) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1507 | |
| 1508 | class octlong(long): |
| 1509 | __slots__ = [] |
| 1510 | def __str__(self): |
| 1511 | s = oct(self) |
| 1512 | if s[-1] == 'L': |
| 1513 | s = s[:-1] |
| 1514 | return s |
| 1515 | def __add__(self, other): |
| 1516 | return self.__class__(super(octlong, self).__add__(other)) |
| 1517 | __radd__ = __add__ |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1518 | vereq(str(octlong(3) + 5), "010") |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1519 | # (Note that overriding __radd__ here only seems to work |
| 1520 | # because the example uses a short int left argument.) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1521 | vereq(str(5 + octlong(3000)), "05675") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1522 | a = octlong(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1523 | vereq(a, 12345L) |
| 1524 | vereq(long(a), 12345L) |
| 1525 | vereq(hash(a), hash(12345L)) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1526 | verify(long(a).__class__ is long) |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 1527 | verify((+a).__class__ is long) |
| 1528 | verify((-a).__class__ is long) |
| 1529 | verify((-octlong(0)).__class__ is long) |
| 1530 | verify((a >> 0).__class__ is long) |
| 1531 | verify((a << 0).__class__ is long) |
| 1532 | verify((a - 0).__class__ is long) |
| 1533 | verify((a * 1).__class__ is long) |
| 1534 | verify((a ** 1).__class__ is long) |
| 1535 | verify((a // 1).__class__ is long) |
| 1536 | verify((1 * a).__class__ is long) |
| 1537 | verify((a | 0).__class__ is long) |
| 1538 | verify((a ^ 0).__class__ is long) |
| 1539 | verify((a & -1L).__class__ is long) |
| 1540 | verify((octlong(0) << 12).__class__ is long) |
| 1541 | verify((octlong(0) >> 12).__class__ is long) |
| 1542 | verify(abs(octlong(0)).__class__ is long) |
| 1543 | |
| 1544 | # Because octlong overrides __add__, we can't check the absence of +0 |
| 1545 | # optimizations using octlong. |
| 1546 | class longclone(long): |
| 1547 | pass |
| 1548 | a = longclone(1) |
| 1549 | verify((a + 0).__class__ is long) |
| 1550 | verify((0 + a).__class__ is long) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1551 | |
| 1552 | class precfloat(float): |
| 1553 | __slots__ = ['prec'] |
| 1554 | def __init__(self, value=0.0, prec=12): |
| 1555 | self.prec = int(prec) |
| 1556 | float.__init__(value) |
| 1557 | def __repr__(self): |
| 1558 | return "%.*g" % (self.prec, self) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1559 | vereq(repr(precfloat(1.1)), "1.1") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1560 | a = precfloat(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1561 | vereq(a, 12345.0) |
| 1562 | vereq(float(a), 12345.0) |
Tim Peters | 7a50f25 | 2001-09-10 21:28:20 +0000 | [diff] [blame] | 1563 | verify(float(a).__class__ is float) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1564 | vereq(hash(a), hash(12345.0)) |
Tim Peters | 0280cf7 | 2001-09-11 21:53:35 +0000 | [diff] [blame] | 1565 | verify((+a).__class__ is float) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1566 | |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1567 | class madcomplex(complex): |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 1568 | __dynamic__ = 0 # XXX Shouldn't be necessary |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1569 | def __repr__(self): |
| 1570 | return "%.17gj%+.17g" % (self.imag, self.real) |
| 1571 | a = madcomplex(-3, 4) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1572 | vereq(repr(a), "4j-3") |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1573 | base = complex(-3, 4) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 1574 | veris(base.__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1575 | vereq(a, base) |
| 1576 | vereq(complex(a), base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 1577 | veris(complex(a).__class__, complex) |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1578 | a = madcomplex(a) # just trying another form of the constructor |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1579 | vereq(repr(a), "4j-3") |
| 1580 | vereq(a, base) |
| 1581 | vereq(complex(a), base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 1582 | veris(complex(a).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1583 | vereq(hash(a), hash(base)) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 1584 | veris((+a).__class__, complex) |
| 1585 | veris((a + 0).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1586 | vereq(a + 0, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 1587 | veris((a - 0).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1588 | vereq(a - 0, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 1589 | veris((a * 1).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1590 | vereq(a * 1, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 1591 | veris((a / 1).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1592 | vereq(a / 1, base) |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1593 | |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1594 | class madtuple(tuple): |
| 1595 | _rev = None |
| 1596 | def rev(self): |
| 1597 | if self._rev is not None: |
| 1598 | return self._rev |
| 1599 | L = list(self) |
| 1600 | L.reverse() |
| 1601 | self._rev = self.__class__(L) |
| 1602 | return self._rev |
| 1603 | a = madtuple((1,2,3,4,5,6,7,8,9,0)) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1604 | vereq(a, (1,2,3,4,5,6,7,8,9,0)) |
| 1605 | vereq(a.rev(), madtuple((0,9,8,7,6,5,4,3,2,1))) |
| 1606 | vereq(a.rev().rev(), madtuple((1,2,3,4,5,6,7,8,9,0))) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1607 | for i in range(512): |
| 1608 | t = madtuple(range(i)) |
| 1609 | u = t.rev() |
| 1610 | v = u.rev() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1611 | vereq(v, t) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1612 | a = madtuple((1,2,3,4,5)) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1613 | vereq(tuple(a), (1,2,3,4,5)) |
Tim Peters | 4c3a0a3 | 2001-09-10 23:37:46 +0000 | [diff] [blame] | 1614 | verify(tuple(a).__class__ is tuple) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1615 | vereq(hash(a), hash((1,2,3,4,5))) |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 1616 | verify(a[:].__class__ is tuple) |
| 1617 | verify((a * 1).__class__ is tuple) |
| 1618 | verify((a * 0).__class__ is tuple) |
| 1619 | verify((a + ()).__class__ is tuple) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1620 | a = madtuple(()) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1621 | vereq(tuple(a), ()) |
Guido van Rossum | 779ce4a | 2001-09-11 14:02:22 +0000 | [diff] [blame] | 1622 | verify(tuple(a).__class__ is tuple) |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 1623 | verify((a + a).__class__ is tuple) |
| 1624 | verify((a * 0).__class__ is tuple) |
| 1625 | verify((a * 1).__class__ is tuple) |
| 1626 | verify((a * 2).__class__ is tuple) |
| 1627 | verify(a[:].__class__ is tuple) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1628 | |
| 1629 | class madstring(str): |
| 1630 | _rev = None |
| 1631 | def rev(self): |
| 1632 | if self._rev is not None: |
| 1633 | return self._rev |
| 1634 | L = list(self) |
| 1635 | L.reverse() |
| 1636 | self._rev = self.__class__("".join(L)) |
| 1637 | return self._rev |
| 1638 | s = madstring("abcdefghijklmnopqrstuvwxyz") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1639 | vereq(s, "abcdefghijklmnopqrstuvwxyz") |
| 1640 | vereq(s.rev(), madstring("zyxwvutsrqponmlkjihgfedcba")) |
| 1641 | vereq(s.rev().rev(), madstring("abcdefghijklmnopqrstuvwxyz")) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1642 | for i in range(256): |
| 1643 | s = madstring("".join(map(chr, range(i)))) |
| 1644 | t = s.rev() |
| 1645 | u = t.rev() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1646 | vereq(u, s) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1647 | s = madstring("12345") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1648 | vereq(str(s), "12345") |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 1649 | verify(str(s).__class__ is str) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1650 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1651 | base = "\x00" * 5 |
| 1652 | s = madstring(base) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1653 | vereq(s, base) |
| 1654 | vereq(str(s), base) |
Tim Peters | c636f56 | 2001-09-11 01:52:02 +0000 | [diff] [blame] | 1655 | verify(str(s).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1656 | vereq(hash(s), hash(base)) |
| 1657 | vereq({s: 1}[base], 1) |
| 1658 | vereq({base: 1}[s], 1) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1659 | verify((s + "").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1660 | vereq(s + "", base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1661 | verify(("" + s).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1662 | vereq("" + s, base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1663 | verify((s * 0).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1664 | vereq(s * 0, "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1665 | verify((s * 1).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1666 | vereq(s * 1, base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1667 | verify((s * 2).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1668 | vereq(s * 2, base + base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1669 | verify(s[:].__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1670 | vereq(s[:], base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1671 | verify(s[0:0].__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1672 | vereq(s[0:0], "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1673 | verify(s.strip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1674 | vereq(s.strip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1675 | verify(s.lstrip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1676 | vereq(s.lstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1677 | verify(s.rstrip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1678 | vereq(s.rstrip(), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1679 | identitytab = ''.join([chr(i) for i in range(256)]) |
| 1680 | verify(s.translate(identitytab).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1681 | vereq(s.translate(identitytab), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1682 | verify(s.translate(identitytab, "x").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1683 | vereq(s.translate(identitytab, "x"), base) |
| 1684 | vereq(s.translate(identitytab, "\x00"), "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1685 | verify(s.replace("x", "x").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1686 | vereq(s.replace("x", "x"), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1687 | verify(s.ljust(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1688 | vereq(s.ljust(len(s)), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1689 | verify(s.rjust(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1690 | vereq(s.rjust(len(s)), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1691 | verify(s.center(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1692 | vereq(s.center(len(s)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1693 | verify(s.lower().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1694 | vereq(s.lower(), base) |
Tim Peters | c636f56 | 2001-09-11 01:52:02 +0000 | [diff] [blame] | 1695 | |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1696 | s = madstring("x y") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1697 | vereq(s, "x y") |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1698 | verify(intern(s).__class__ is str) |
| 1699 | verify(intern(s) is intern("x y")) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1700 | vereq(intern(s), "x y") |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1701 | |
| 1702 | i = intern("y x") |
| 1703 | s = madstring("y x") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1704 | vereq(s, i) |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1705 | verify(intern(s).__class__ is str) |
| 1706 | verify(intern(s) is i) |
| 1707 | |
| 1708 | s = madstring(i) |
| 1709 | verify(intern(s).__class__ is str) |
| 1710 | verify(intern(s) is i) |
| 1711 | |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 1712 | class madunicode(unicode): |
| 1713 | _rev = None |
| 1714 | def rev(self): |
| 1715 | if self._rev is not None: |
| 1716 | return self._rev |
| 1717 | L = list(self) |
| 1718 | L.reverse() |
| 1719 | self._rev = self.__class__(u"".join(L)) |
| 1720 | return self._rev |
| 1721 | u = madunicode("ABCDEF") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1722 | vereq(u, u"ABCDEF") |
| 1723 | vereq(u.rev(), madunicode(u"FEDCBA")) |
| 1724 | vereq(u.rev().rev(), madunicode(u"ABCDEF")) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1725 | base = u"12345" |
| 1726 | u = madunicode(base) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1727 | vereq(unicode(u), base) |
Tim Peters | 78e0fc7 | 2001-09-11 03:07:38 +0000 | [diff] [blame] | 1728 | verify(unicode(u).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1729 | vereq(hash(u), hash(base)) |
| 1730 | vereq({u: 1}[base], 1) |
| 1731 | vereq({base: 1}[u], 1) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1732 | verify(u.strip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1733 | vereq(u.strip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1734 | verify(u.lstrip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1735 | vereq(u.lstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1736 | verify(u.rstrip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1737 | vereq(u.rstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1738 | verify(u.replace(u"x", u"x").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1739 | vereq(u.replace(u"x", u"x"), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1740 | verify(u.replace(u"xy", u"xy").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1741 | vereq(u.replace(u"xy", u"xy"), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1742 | verify(u.center(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1743 | vereq(u.center(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1744 | verify(u.ljust(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1745 | vereq(u.ljust(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1746 | verify(u.rjust(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1747 | vereq(u.rjust(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1748 | verify(u.lower().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1749 | vereq(u.lower(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1750 | verify(u.upper().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1751 | vereq(u.upper(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1752 | verify(u.capitalize().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1753 | vereq(u.capitalize(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1754 | verify(u.title().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1755 | vereq(u.title(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1756 | verify((u + u"").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1757 | vereq(u + u"", base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1758 | verify((u"" + u).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1759 | vereq(u"" + u, base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1760 | verify((u * 0).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1761 | vereq(u * 0, u"") |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1762 | verify((u * 1).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1763 | vereq(u * 1, base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1764 | verify((u * 2).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1765 | vereq(u * 2, base + base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1766 | verify(u[:].__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1767 | vereq(u[:], base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1768 | verify(u[0:0].__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1769 | vereq(u[0:0], u"") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 1770 | |
Guido van Rossum | 12b22ff | 2001-10-09 20:36:44 +0000 | [diff] [blame] | 1771 | class sublist(list): |
| 1772 | pass |
| 1773 | a = sublist(range(5)) |
| 1774 | vereq(a, range(5)) |
| 1775 | a.append("hello") |
| 1776 | vereq(a, range(5) + ["hello"]) |
| 1777 | a[5] = 5 |
| 1778 | vereq(a, range(6)) |
| 1779 | a.extend(range(6, 20)) |
| 1780 | vereq(a, range(20)) |
| 1781 | a[-5:] = [] |
| 1782 | vereq(a, range(15)) |
| 1783 | del a[10:15] |
| 1784 | vereq(len(a), 10) |
| 1785 | vereq(a, range(10)) |
| 1786 | vereq(list(a), range(10)) |
| 1787 | vereq(a[0], 0) |
| 1788 | vereq(a[9], 9) |
| 1789 | vereq(a[-10], 0) |
| 1790 | vereq(a[-1], 9) |
| 1791 | vereq(a[:5], range(5)) |
| 1792 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1793 | class CountedInput(file): |
| 1794 | """Counts lines read by self.readline(). |
| 1795 | |
| 1796 | self.lineno is the 0-based ordinal of the last line read, up to |
| 1797 | a maximum of one greater than the number of lines in the file. |
| 1798 | |
| 1799 | self.ateof is true if and only if the final "" line has been read, |
| 1800 | at which point self.lineno stops incrementing, and further calls |
| 1801 | to readline() continue to return "". |
| 1802 | """ |
| 1803 | |
| 1804 | lineno = 0 |
| 1805 | ateof = 0 |
| 1806 | def readline(self): |
| 1807 | if self.ateof: |
| 1808 | return "" |
| 1809 | s = file.readline(self) |
| 1810 | # Next line works too. |
| 1811 | # s = super(CountedInput, self).readline() |
| 1812 | self.lineno += 1 |
| 1813 | if s == "": |
| 1814 | self.ateof = 1 |
| 1815 | return s |
| 1816 | |
Tim Peters | 561f899 | 2001-09-13 19:36:36 +0000 | [diff] [blame] | 1817 | f = file(name=TESTFN, mode='w') |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1818 | lines = ['a\n', 'b\n', 'c\n'] |
| 1819 | try: |
| 1820 | f.writelines(lines) |
| 1821 | f.close() |
| 1822 | f = CountedInput(TESTFN) |
| 1823 | for (i, expected) in zip(range(1, 5) + [4], lines + 2 * [""]): |
| 1824 | got = f.readline() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1825 | vereq(expected, got) |
| 1826 | vereq(f.lineno, i) |
| 1827 | vereq(f.ateof, (i > len(lines))) |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1828 | f.close() |
| 1829 | finally: |
| 1830 | try: |
| 1831 | f.close() |
| 1832 | except: |
| 1833 | pass |
| 1834 | try: |
| 1835 | import os |
| 1836 | os.unlink(TESTFN) |
| 1837 | except: |
| 1838 | pass |
| 1839 | |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 1840 | def keywords(): |
| 1841 | if verbose: |
| 1842 | print "Testing keyword args to basic type constructors ..." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1843 | vereq(int(x=1), 1) |
| 1844 | vereq(float(x=2), 2.0) |
| 1845 | vereq(long(x=3), 3L) |
| 1846 | vereq(complex(imag=42, real=666), complex(666, 42)) |
| 1847 | vereq(str(object=500), '500') |
| 1848 | vereq(unicode(string='abc', errors='strict'), u'abc') |
| 1849 | vereq(tuple(sequence=range(3)), (0, 1, 2)) |
| 1850 | vereq(list(sequence=(0, 1, 2)), range(3)) |
| 1851 | vereq(dictionary(mapping={1: 2}), {1: 2}) |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 1852 | |
| 1853 | for constructor in (int, float, long, complex, str, unicode, |
| 1854 | tuple, list, dictionary, file): |
| 1855 | try: |
| 1856 | constructor(bogus_keyword_arg=1) |
| 1857 | except TypeError: |
| 1858 | pass |
| 1859 | else: |
| 1860 | raise TestFailed("expected TypeError from bogus keyword " |
| 1861 | "argument to %r" % constructor) |
Tim Peters | 561f899 | 2001-09-13 19:36:36 +0000 | [diff] [blame] | 1862 | |
Tim Peters | 8fa4567 | 2001-09-13 21:01:29 +0000 | [diff] [blame] | 1863 | def restricted(): |
| 1864 | import rexec |
| 1865 | if verbose: |
| 1866 | print "Testing interaction with restricted execution ..." |
| 1867 | |
| 1868 | sandbox = rexec.RExec() |
| 1869 | |
| 1870 | code1 = """f = open(%r, 'w')""" % TESTFN |
| 1871 | code2 = """f = file(%r, 'w')""" % TESTFN |
| 1872 | code3 = """\ |
| 1873 | f = open(%r) |
| 1874 | t = type(f) # a sneaky way to get the file() constructor |
| 1875 | f.close() |
| 1876 | f = t(%r, 'w') # rexec can't catch this by itself |
| 1877 | """ % (TESTFN, TESTFN) |
| 1878 | |
| 1879 | f = open(TESTFN, 'w') # Create the file so code3 can find it. |
| 1880 | f.close() |
| 1881 | |
| 1882 | try: |
| 1883 | for code in code1, code2, code3: |
| 1884 | try: |
| 1885 | sandbox.r_exec(code) |
| 1886 | except IOError, msg: |
| 1887 | if str(msg).find("restricted") >= 0: |
| 1888 | outcome = "OK" |
| 1889 | else: |
| 1890 | outcome = "got an exception, but not an expected one" |
| 1891 | else: |
| 1892 | outcome = "expected a restricted-execution exception" |
| 1893 | |
| 1894 | if outcome != "OK": |
| 1895 | raise TestFailed("%s, in %r" % (outcome, code)) |
| 1896 | |
| 1897 | finally: |
| 1898 | try: |
| 1899 | import os |
| 1900 | os.unlink(TESTFN) |
| 1901 | except: |
| 1902 | pass |
| 1903 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1904 | def str_subclass_as_dict_key(): |
| 1905 | if verbose: |
| 1906 | print "Testing a str subclass used as dict key .." |
| 1907 | |
| 1908 | class cistr(str): |
| 1909 | """Sublcass of str that computes __eq__ case-insensitively. |
| 1910 | |
| 1911 | Also computes a hash code of the string in canonical form. |
| 1912 | """ |
| 1913 | |
| 1914 | def __init__(self, value): |
| 1915 | self.canonical = value.lower() |
| 1916 | self.hashcode = hash(self.canonical) |
| 1917 | |
| 1918 | def __eq__(self, other): |
| 1919 | if not isinstance(other, cistr): |
| 1920 | other = cistr(other) |
| 1921 | return self.canonical == other.canonical |
| 1922 | |
| 1923 | def __hash__(self): |
| 1924 | return self.hashcode |
| 1925 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1926 | vereq(cistr('ABC'), 'abc') |
| 1927 | vereq('aBc', cistr('ABC')) |
| 1928 | vereq(str(cistr('ABC')), 'ABC') |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1929 | |
| 1930 | d = {cistr('one'): 1, cistr('two'): 2, cistr('tHree'): 3} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1931 | vereq(d[cistr('one')], 1) |
| 1932 | vereq(d[cistr('tWo')], 2) |
| 1933 | vereq(d[cistr('THrEE')], 3) |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1934 | verify(cistr('ONe') in d) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1935 | vereq(d.get(cistr('thrEE')), 3) |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1936 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 1937 | def classic_comparisons(): |
| 1938 | if verbose: print "Testing classic comparisons..." |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 1939 | class classic: |
| 1940 | pass |
| 1941 | for base in (classic, int, object): |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 1942 | if verbose: print " (base = %s)" % base |
| 1943 | class C(base): |
| 1944 | def __init__(self, value): |
| 1945 | self.value = int(value) |
| 1946 | def __cmp__(self, other): |
| 1947 | if isinstance(other, C): |
| 1948 | return cmp(self.value, other.value) |
| 1949 | if isinstance(other, int) or isinstance(other, long): |
| 1950 | return cmp(self.value, other) |
| 1951 | return NotImplemented |
| 1952 | c1 = C(1) |
| 1953 | c2 = C(2) |
| 1954 | c3 = C(3) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1955 | vereq(c1, 1) |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 1956 | c = {1: c1, 2: c2, 3: c3} |
| 1957 | for x in 1, 2, 3: |
| 1958 | for y in 1, 2, 3: |
| 1959 | verify(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 1960 | for op in "<", "<=", "==", "!=", ">", ">=": |
| 1961 | verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op), |
| 1962 | "x=%d, y=%d" % (x, y)) |
| 1963 | verify(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 1964 | verify(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 1965 | |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 1966 | def rich_comparisons(): |
| 1967 | if verbose: |
| 1968 | print "Testing rich comparisons..." |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 1969 | class Z(complex): |
Guido van Rossum | 751c4c8 | 2001-09-29 00:40:25 +0000 | [diff] [blame] | 1970 | __dynamic__ = 0 |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 1971 | z = Z(1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1972 | vereq(z, 1+0j) |
| 1973 | vereq(1+0j, z) |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 1974 | class ZZ(complex): |
Guido van Rossum | 751c4c8 | 2001-09-29 00:40:25 +0000 | [diff] [blame] | 1975 | __dynamic__ = 0 |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 1976 | def __eq__(self, other): |
| 1977 | try: |
| 1978 | return abs(self - other) <= 1e-6 |
| 1979 | except: |
| 1980 | return NotImplemented |
| 1981 | zz = ZZ(1.0000003) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1982 | vereq(zz, 1+0j) |
| 1983 | vereq(1+0j, zz) |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1984 | |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 1985 | class classic: |
| 1986 | pass |
| 1987 | for base in (classic, int, object, list): |
| 1988 | if verbose: print " (base = %s)" % base |
| 1989 | class C(base): |
| 1990 | def __init__(self, value): |
| 1991 | self.value = int(value) |
| 1992 | def __cmp__(self, other): |
| 1993 | raise TestFailed, "shouldn't call __cmp__" |
| 1994 | def __eq__(self, other): |
| 1995 | if isinstance(other, C): |
| 1996 | return self.value == other.value |
| 1997 | if isinstance(other, int) or isinstance(other, long): |
| 1998 | return self.value == other |
| 1999 | return NotImplemented |
| 2000 | def __ne__(self, other): |
| 2001 | if isinstance(other, C): |
| 2002 | return self.value != other.value |
| 2003 | if isinstance(other, int) or isinstance(other, long): |
| 2004 | return self.value != other |
| 2005 | return NotImplemented |
| 2006 | def __lt__(self, other): |
| 2007 | if isinstance(other, C): |
| 2008 | return self.value < other.value |
| 2009 | if isinstance(other, int) or isinstance(other, long): |
| 2010 | return self.value < other |
| 2011 | return NotImplemented |
| 2012 | def __le__(self, other): |
| 2013 | if isinstance(other, C): |
| 2014 | return self.value <= other.value |
| 2015 | if isinstance(other, int) or isinstance(other, long): |
| 2016 | return self.value <= other |
| 2017 | return NotImplemented |
| 2018 | def __gt__(self, other): |
| 2019 | if isinstance(other, C): |
| 2020 | return self.value > other.value |
| 2021 | if isinstance(other, int) or isinstance(other, long): |
| 2022 | return self.value > other |
| 2023 | return NotImplemented |
| 2024 | def __ge__(self, other): |
| 2025 | if isinstance(other, C): |
| 2026 | return self.value >= other.value |
| 2027 | if isinstance(other, int) or isinstance(other, long): |
| 2028 | return self.value >= other |
| 2029 | return NotImplemented |
| 2030 | c1 = C(1) |
| 2031 | c2 = C(2) |
| 2032 | c3 = C(3) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2033 | vereq(c1, 1) |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2034 | c = {1: c1, 2: c2, 3: c3} |
| 2035 | for x in 1, 2, 3: |
| 2036 | for y in 1, 2, 3: |
| 2037 | for op in "<", "<=", "==", "!=", ">", ">=": |
| 2038 | verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op), |
| 2039 | "x=%d, y=%d" % (x, y)) |
| 2040 | verify(eval("c[x] %s y" % op) == eval("x %s y" % op), |
| 2041 | "x=%d, y=%d" % (x, y)) |
| 2042 | verify(eval("x %s c[y]" % op) == eval("x %s y" % op), |
| 2043 | "x=%d, y=%d" % (x, y)) |
| 2044 | |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2045 | def coercions(): |
| 2046 | if verbose: print "Testing coercions..." |
| 2047 | class I(int): pass |
| 2048 | coerce(I(0), 0) |
| 2049 | coerce(0, I(0)) |
| 2050 | class L(long): pass |
| 2051 | coerce(L(0), 0) |
| 2052 | coerce(L(0), 0L) |
| 2053 | coerce(0, L(0)) |
| 2054 | coerce(0L, L(0)) |
| 2055 | class F(float): pass |
| 2056 | coerce(F(0), 0) |
| 2057 | coerce(F(0), 0L) |
| 2058 | coerce(F(0), 0.) |
| 2059 | coerce(0, F(0)) |
| 2060 | coerce(0L, F(0)) |
| 2061 | coerce(0., F(0)) |
Guido van Rossum | 751c4c8 | 2001-09-29 00:40:25 +0000 | [diff] [blame] | 2062 | class C(complex): |
| 2063 | __dynamic__ = 0 |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2064 | coerce(C(0), 0) |
| 2065 | coerce(C(0), 0L) |
| 2066 | coerce(C(0), 0.) |
| 2067 | coerce(C(0), 0j) |
| 2068 | coerce(0, C(0)) |
| 2069 | coerce(0L, C(0)) |
| 2070 | coerce(0., C(0)) |
| 2071 | coerce(0j, C(0)) |
| 2072 | |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2073 | def descrdoc(): |
| 2074 | if verbose: print "Testing descriptor doc strings..." |
| 2075 | def check(descr, what): |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2076 | vereq(descr.__doc__, what) |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2077 | check(file.closed, "flag set if the file is closed") # getset descriptor |
| 2078 | check(file.name, "file name") # member descriptor |
| 2079 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2080 | def setclass(): |
| 2081 | if verbose: print "Testing __class__ assignment..." |
| 2082 | class C(object): pass |
| 2083 | class D(object): pass |
| 2084 | class E(object): pass |
| 2085 | class F(D, E): pass |
| 2086 | for cls in C, D, E, F: |
| 2087 | for cls2 in C, D, E, F: |
| 2088 | x = cls() |
| 2089 | x.__class__ = cls2 |
| 2090 | verify(x.__class__ is cls2) |
| 2091 | x.__class__ = cls |
| 2092 | verify(x.__class__ is cls) |
| 2093 | def cant(x, C): |
| 2094 | try: |
| 2095 | x.__class__ = C |
| 2096 | except TypeError: |
| 2097 | pass |
| 2098 | else: |
| 2099 | raise TestFailed, "shouldn't allow %r.__class__ = %r" % (x, C) |
| 2100 | cant(C(), list) |
| 2101 | cant(list(), C) |
| 2102 | cant(C(), 1) |
| 2103 | cant(C(), object) |
| 2104 | cant(object(), list) |
| 2105 | cant(list(), object) |
| 2106 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2107 | def pickles(): |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2108 | if verbose: |
| 2109 | print "Testing pickling and copying new-style classes and objects..." |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2110 | import pickle, cPickle |
| 2111 | |
| 2112 | def sorteditems(d): |
| 2113 | L = d.items() |
| 2114 | L.sort() |
| 2115 | return L |
| 2116 | |
| 2117 | global C |
| 2118 | class C(object): |
| 2119 | def __init__(self, a, b): |
| 2120 | super(C, self).__init__() |
| 2121 | self.a = a |
| 2122 | self.b = b |
| 2123 | def __repr__(self): |
| 2124 | return "C(%r, %r)" % (self.a, self.b) |
| 2125 | |
| 2126 | global C1 |
| 2127 | class C1(list): |
| 2128 | def __new__(cls, a, b): |
| 2129 | return super(C1, cls).__new__(cls) |
| 2130 | def __init__(self, a, b): |
| 2131 | self.a = a |
| 2132 | self.b = b |
| 2133 | def __repr__(self): |
| 2134 | return "C1(%r, %r)<%r>" % (self.a, self.b, list(self)) |
| 2135 | |
| 2136 | global C2 |
| 2137 | class C2(int): |
| 2138 | def __new__(cls, a, b, val=0): |
| 2139 | return super(C2, cls).__new__(cls, val) |
| 2140 | def __init__(self, a, b, val=0): |
| 2141 | self.a = a |
| 2142 | self.b = b |
| 2143 | def __repr__(self): |
| 2144 | return "C2(%r, %r)<%r>" % (self.a, self.b, int(self)) |
| 2145 | |
| 2146 | for p in pickle, cPickle: |
| 2147 | for bin in 0, 1: |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2148 | if verbose: |
| 2149 | print p.__name__, ["text", "binary"][bin] |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2150 | |
| 2151 | for cls in C, C1, C2: |
| 2152 | s = p.dumps(cls, bin) |
| 2153 | cls2 = p.loads(s) |
| 2154 | verify(cls2 is cls) |
| 2155 | |
| 2156 | a = C1(1, 2); a.append(42); a.append(24) |
| 2157 | b = C2("hello", "world", 42) |
| 2158 | s = p.dumps((a, b), bin) |
| 2159 | x, y = p.loads(s) |
| 2160 | assert x.__class__ == a.__class__ |
| 2161 | assert sorteditems(x.__dict__) == sorteditems(a.__dict__) |
| 2162 | assert y.__class__ == b.__class__ |
| 2163 | assert sorteditems(y.__dict__) == sorteditems(b.__dict__) |
| 2164 | assert `x` == `a` |
| 2165 | assert `y` == `b` |
| 2166 | if verbose: |
| 2167 | print "a = x =", a |
| 2168 | print "b = y =", b |
| 2169 | |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2170 | # Testing copy.deepcopy() |
| 2171 | if verbose: |
| 2172 | print "deepcopy" |
| 2173 | import copy |
| 2174 | for cls in C, C1, C2: |
| 2175 | cls2 = copy.deepcopy(cls) |
| 2176 | verify(cls2 is cls) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2177 | |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2178 | a = C1(1, 2); a.append(42); a.append(24) |
| 2179 | b = C2("hello", "world", 42) |
| 2180 | x, y = copy.deepcopy((a, b)) |
| 2181 | assert x.__class__ == a.__class__ |
| 2182 | assert sorteditems(x.__dict__) == sorteditems(a.__dict__) |
| 2183 | assert y.__class__ == b.__class__ |
| 2184 | assert sorteditems(y.__dict__) == sorteditems(b.__dict__) |
| 2185 | assert `x` == `a` |
| 2186 | assert `y` == `b` |
| 2187 | if verbose: |
| 2188 | print "a = x =", a |
| 2189 | print "b = y =", b |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2190 | |
| 2191 | def copies(): |
| 2192 | if verbose: print "Testing copy.copy() and copy.deepcopy()..." |
| 2193 | import copy |
| 2194 | class C(object): |
| 2195 | pass |
| 2196 | |
| 2197 | a = C() |
| 2198 | a.foo = 12 |
| 2199 | b = copy.copy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2200 | vereq(b.__dict__, a.__dict__) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2201 | |
| 2202 | a.bar = [1,2,3] |
| 2203 | c = copy.copy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2204 | vereq(c.bar, a.bar) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2205 | verify(c.bar is a.bar) |
| 2206 | |
| 2207 | d = copy.deepcopy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2208 | vereq(d.__dict__, a.__dict__) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2209 | a.bar.append(4) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2210 | vereq(d.bar, [1,2,3]) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2211 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2212 | def binopoverride(): |
| 2213 | if verbose: print "Testing overrides of binary operations..." |
| 2214 | class I(int): |
| 2215 | def __repr__(self): |
| 2216 | return "I(%r)" % int(self) |
| 2217 | def __add__(self, other): |
| 2218 | return I(int(self) + int(other)) |
| 2219 | __radd__ = __add__ |
| 2220 | def __pow__(self, other, mod=None): |
| 2221 | if mod is None: |
| 2222 | return I(pow(int(self), int(other))) |
| 2223 | else: |
| 2224 | return I(pow(int(self), int(other), int(mod))) |
| 2225 | def __rpow__(self, other, mod=None): |
| 2226 | if mod is None: |
| 2227 | return I(pow(int(other), int(self), mod)) |
| 2228 | else: |
| 2229 | return I(pow(int(other), int(self), int(mod))) |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2230 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2231 | vereq(`I(1) + I(2)`, "I(3)") |
| 2232 | vereq(`I(1) + 2`, "I(3)") |
| 2233 | vereq(`1 + I(2)`, "I(3)") |
| 2234 | vereq(`I(2) ** I(3)`, "I(8)") |
| 2235 | vereq(`2 ** I(3)`, "I(8)") |
| 2236 | vereq(`I(2) ** 3`, "I(8)") |
| 2237 | vereq(`pow(I(2), I(3), I(5))`, "I(3)") |
| 2238 | class S(str): |
| 2239 | def __eq__(self, other): |
| 2240 | return self.lower() == other.lower() |
| 2241 | |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 2242 | def subclasspropagation(): |
| 2243 | if verbose: print "Testing propagation of slot functions to subclasses..." |
| 2244 | class A(object): |
| 2245 | pass |
| 2246 | class B(A): |
| 2247 | pass |
| 2248 | class C(A): |
| 2249 | pass |
| 2250 | class D(B, C): |
| 2251 | pass |
| 2252 | d = D() |
| 2253 | vereq(hash(d), id(d)) |
| 2254 | A.__hash__ = lambda self: 42 |
| 2255 | vereq(hash(d), 42) |
| 2256 | C.__hash__ = lambda self: 314 |
| 2257 | vereq(hash(d), 314) |
| 2258 | B.__hash__ = lambda self: 144 |
| 2259 | vereq(hash(d), 144) |
| 2260 | D.__hash__ = lambda self: 100 |
| 2261 | vereq(hash(d), 100) |
| 2262 | del D.__hash__ |
| 2263 | vereq(hash(d), 144) |
| 2264 | del B.__hash__ |
| 2265 | vereq(hash(d), 314) |
| 2266 | del C.__hash__ |
| 2267 | vereq(hash(d), 42) |
| 2268 | del A.__hash__ |
| 2269 | vereq(hash(d), id(d)) |
| 2270 | d.foo = 42 |
| 2271 | d.bar = 42 |
| 2272 | vereq(d.foo, 42) |
| 2273 | vereq(d.bar, 42) |
| 2274 | def __getattribute__(self, name): |
| 2275 | if name == "foo": |
| 2276 | return 24 |
| 2277 | return object.__getattribute__(self, name) |
| 2278 | A.__getattribute__ = __getattribute__ |
| 2279 | vereq(d.foo, 24) |
| 2280 | vereq(d.bar, 42) |
| 2281 | def __getattr__(self, name): |
| 2282 | if name in ("spam", "foo", "bar"): |
| 2283 | return "hello" |
| 2284 | raise AttributeError, name |
| 2285 | B.__getattr__ = __getattr__ |
| 2286 | vereq(d.spam, "hello") |
| 2287 | vereq(d.foo, 24) |
| 2288 | vereq(d.bar, 42) |
| 2289 | del A.__getattribute__ |
| 2290 | vereq(d.foo, 42) |
| 2291 | del d.foo |
| 2292 | vereq(d.foo, "hello") |
| 2293 | vereq(d.bar, 42) |
| 2294 | del B.__getattr__ |
| 2295 | try: |
| 2296 | d.foo |
| 2297 | except AttributeError: |
| 2298 | pass |
| 2299 | else: |
| 2300 | raise TestFailed, "d.foo should be undefined now" |
| 2301 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2302 | |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 2303 | def test_main(): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2304 | class_docstrings() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2305 | lists() |
| 2306 | dicts() |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2307 | dict_constructor() |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 2308 | test_dir() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2309 | ints() |
| 2310 | longs() |
| 2311 | floats() |
| 2312 | complexes() |
| 2313 | spamlists() |
| 2314 | spamdicts() |
| 2315 | pydicts() |
| 2316 | pylists() |
| 2317 | metaclass() |
| 2318 | pymods() |
| 2319 | multi() |
| 2320 | diamond() |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 2321 | objects() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2322 | slots() |
| 2323 | dynamics() |
| 2324 | errors() |
| 2325 | classmethods() |
| 2326 | staticmethods() |
| 2327 | classic() |
| 2328 | compattr() |
| 2329 | newslot() |
| 2330 | altmro() |
| 2331 | overloading() |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 2332 | methods() |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 2333 | specials() |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 2334 | weakrefs() |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 2335 | properties() |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 2336 | supers() |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2337 | inherits() |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 2338 | keywords() |
Tim Peters | 8fa4567 | 2001-09-13 21:01:29 +0000 | [diff] [blame] | 2339 | restricted() |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2340 | str_subclass_as_dict_key() |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 2341 | classic_comparisons() |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2342 | rich_comparisons() |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2343 | coercions() |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2344 | descrdoc() |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2345 | setclass() |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2346 | pickles() |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2347 | copies() |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2348 | binopoverride() |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame^] | 2349 | subclasspropagation() |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 2350 | if verbose: print "All OK" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2351 | |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 2352 | if __name__ == "__main__": |
| 2353 | test_main() |