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