Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1 | # Test descriptor-related enhancements |
| 2 | |
| 3 | from test_support import verify, verbose |
| 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 | |
| 126 | binops = { |
| 127 | 'add': '+', |
| 128 | 'sub': '-', |
| 129 | 'mul': '*', |
| 130 | 'div': '/', |
| 131 | 'mod': '%', |
| 132 | 'divmod': 'divmod', |
| 133 | 'pow': '**', |
| 134 | 'lshift': '<<', |
| 135 | 'rshift': '>>', |
| 136 | 'and': '&', |
| 137 | 'xor': '^', |
| 138 | 'or': '|', |
| 139 | 'cmp': 'cmp', |
| 140 | 'lt': '<', |
| 141 | 'le': '<=', |
| 142 | 'eq': '==', |
| 143 | 'ne': '!=', |
| 144 | 'gt': '>', |
| 145 | 'ge': '>=', |
| 146 | } |
| 147 | |
| 148 | for name, expr in binops.items(): |
| 149 | if expr.islower(): |
| 150 | expr = expr + "(a, b)" |
| 151 | else: |
| 152 | expr = 'a %s b' % expr |
| 153 | binops[name] = expr |
| 154 | |
| 155 | unops = { |
| 156 | 'pos': '+', |
| 157 | 'neg': '-', |
| 158 | 'abs': 'abs', |
| 159 | 'invert': '~', |
| 160 | 'int': 'int', |
| 161 | 'long': 'long', |
| 162 | 'float': 'float', |
| 163 | 'oct': 'oct', |
| 164 | 'hex': 'hex', |
| 165 | } |
| 166 | |
| 167 | for name, expr in unops.items(): |
| 168 | if expr.islower(): |
| 169 | expr = expr + "(a)" |
| 170 | else: |
| 171 | expr = '%s a' % expr |
| 172 | unops[name] = expr |
| 173 | |
| 174 | def numops(a, b, skip=[]): |
| 175 | dict = {'a': a, 'b': b} |
| 176 | for name, expr in binops.items(): |
| 177 | if name not in skip: |
| 178 | name = "__%s__" % name |
| 179 | if hasattr(a, name): |
| 180 | res = eval(expr, dict) |
| 181 | testbinop(a, b, res, expr, name) |
| 182 | for name, expr in unops.items(): |
| 183 | name = "__%s__" % name |
| 184 | if hasattr(a, name): |
| 185 | res = eval(expr, dict) |
| 186 | testunop(a, res, expr, name) |
| 187 | |
| 188 | def ints(): |
| 189 | if verbose: print "Testing int operations..." |
| 190 | numops(100, 3) |
| 191 | |
| 192 | def longs(): |
| 193 | if verbose: print "Testing long operations..." |
| 194 | numops(100L, 3L) |
| 195 | |
| 196 | def floats(): |
| 197 | if verbose: print "Testing float operations..." |
| 198 | numops(100.0, 3.0) |
| 199 | |
| 200 | def complexes(): |
| 201 | if verbose: print "Testing complex operations..." |
| 202 | numops(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge']) |
| 203 | class Number(complex): |
| 204 | __slots__ = ['prec'] |
| 205 | def __init__(self, *args, **kwds): |
| 206 | self.prec = kwds.get('prec', 12) |
| 207 | def __repr__(self): |
| 208 | prec = self.prec |
| 209 | if self.imag == 0.0: |
| 210 | return "%.*g" % (prec, self.real) |
| 211 | if self.real == 0.0: |
| 212 | return "%.*gj" % (prec, self.imag) |
| 213 | return "(%.*g+%.*gj)" % (prec, self.real, prec, self.imag) |
| 214 | __str__ = __repr__ |
| 215 | a = Number(3.14, prec=6) |
| 216 | verify(`a` == "3.14") |
| 217 | verify(a.prec == 6) |
| 218 | |
| 219 | def spamlists(): |
| 220 | if verbose: print "Testing spamlist operations..." |
| 221 | import copy, xxsubtype as spam |
| 222 | def spamlist(l, memo=None): |
| 223 | import xxsubtype as spam |
| 224 | return spam.spamlist(l) |
| 225 | # This is an ugly hack: |
| 226 | copy._deepcopy_dispatch[spam.spamlist] = spamlist |
| 227 | |
| 228 | testbinop(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+b", "__add__") |
| 229 | testbinop(spamlist([1,2,3]), 2, 1, "b in a", "__contains__") |
| 230 | testbinop(spamlist([1,2,3]), 4, 0, "b in a", "__contains__") |
| 231 | testbinop(spamlist([1,2,3]), 1, 2, "a[b]", "__getitem__") |
| 232 | testternop(spamlist([1,2,3]), 0, 2, spamlist([1,2]), |
| 233 | "a[b:c]", "__getslice__") |
| 234 | testsetop(spamlist([1]), spamlist([2]), spamlist([1,2]), |
| 235 | "a+=b", "__iadd__") |
| 236 | testsetop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*=b", "__imul__") |
| 237 | testunop(spamlist([1,2,3]), 3, "len(a)", "__len__") |
| 238 | testbinop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*b", "__mul__") |
| 239 | testbinop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "b*a", "__rmul__") |
| 240 | testset2op(spamlist([1,2]), 1, 3, spamlist([1,3]), "a[b]=c", "__setitem__") |
| 241 | testset3op(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]), |
| 242 | spamlist([1,5,6,4]), "a[b:c]=d", "__setslice__") |
| 243 | # Test subclassing |
| 244 | class C(spam.spamlist): |
| 245 | def foo(self): return 1 |
| 246 | a = C() |
| 247 | verify(a == []) |
| 248 | verify(a.foo() == 1) |
| 249 | a.append(100) |
| 250 | verify(a == [100]) |
| 251 | verify(a.getstate() == 0) |
| 252 | a.setstate(42) |
| 253 | verify(a.getstate() == 42) |
| 254 | |
| 255 | def spamdicts(): |
| 256 | if verbose: print "Testing spamdict operations..." |
| 257 | import copy, xxsubtype as spam |
| 258 | def spamdict(d, memo=None): |
| 259 | import xxsubtype as spam |
| 260 | sd = spam.spamdict() |
| 261 | for k, v in d.items(): sd[k] = v |
| 262 | return sd |
| 263 | # This is an ugly hack: |
| 264 | copy._deepcopy_dispatch[spam.spamdict] = spamdict |
| 265 | |
| 266 | testbinop(spamdict({1:2}), spamdict({2:1}), -1, "cmp(a,b)", "__cmp__") |
| 267 | testbinop(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__") |
| 268 | testbinop(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__") |
| 269 | testbinop(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__") |
| 270 | d = spamdict({1:2,3:4}) |
| 271 | l1 = [] |
| 272 | for i in d.keys(): l1.append(i) |
| 273 | l = [] |
| 274 | for i in iter(d): l.append(i) |
| 275 | verify(l == l1) |
| 276 | l = [] |
| 277 | for i in d.__iter__(): l.append(i) |
| 278 | verify(l == l1) |
| 279 | l = [] |
| 280 | for i in type(spamdict({})).__iter__(d): l.append(i) |
| 281 | verify(l == l1) |
| 282 | straightd = {1:2, 3:4} |
| 283 | spamd = spamdict(straightd) |
| 284 | testunop(spamd, 2, "len(a)", "__len__") |
| 285 | testunop(spamd, repr(straightd), "repr(a)", "__repr__") |
| 286 | testset2op(spamdict({1:2,3:4}), 2, 3, spamdict({1:2,2:3,3:4}), |
| 287 | "a[b]=c", "__setitem__") |
| 288 | # Test subclassing |
| 289 | class C(spam.spamdict): |
| 290 | def foo(self): return 1 |
| 291 | a = C() |
| 292 | verify(a.items() == []) |
| 293 | verify(a.foo() == 1) |
| 294 | a['foo'] = 'bar' |
| 295 | verify(a.items() == [('foo', 'bar')]) |
| 296 | verify(a.getstate() == 0) |
| 297 | a.setstate(100) |
| 298 | verify(a.getstate() == 100) |
| 299 | |
| 300 | def pydicts(): |
| 301 | if verbose: print "Testing Python subclass of dict..." |
| 302 | verify(issubclass(dictionary, dictionary)) |
| 303 | verify(isinstance({}, dictionary)) |
| 304 | d = dictionary() |
| 305 | verify(d == {}) |
| 306 | verify(d.__class__ is dictionary) |
| 307 | verify(isinstance(d, dictionary)) |
| 308 | class C(dictionary): |
| 309 | state = -1 |
| 310 | def __init__(self, *a, **kw): |
| 311 | if a: |
| 312 | assert len(a) == 1 |
| 313 | self.state = a[0] |
| 314 | if kw: |
| 315 | for k, v in kw.items(): self[v] = k |
| 316 | def __getitem__(self, key): |
| 317 | return self.get(key, 0) |
| 318 | def __setitem__(self, key, value): |
| 319 | assert isinstance(key, type(0)) |
| 320 | dictionary.__setitem__(self, key, value) |
| 321 | def setstate(self, state): |
| 322 | self.state = state |
| 323 | def getstate(self): |
| 324 | return self.state |
| 325 | verify(issubclass(C, dictionary)) |
| 326 | a1 = C(12) |
| 327 | verify(a1.state == 12) |
| 328 | a2 = C(foo=1, bar=2) |
| 329 | verify(a2[1] == 'foo' and a2[2] == 'bar') |
| 330 | a = C() |
| 331 | verify(a.state == -1) |
| 332 | verify(a.getstate() == -1) |
| 333 | a.setstate(0) |
| 334 | verify(a.state == 0) |
| 335 | verify(a.getstate() == 0) |
| 336 | a.setstate(10) |
| 337 | verify(a.state == 10) |
| 338 | verify(a.getstate() == 10) |
| 339 | verify(a[42] == 0) |
| 340 | a[42] = 24 |
| 341 | verify(a[42] == 24) |
| 342 | if verbose: print "pydict stress test ..." |
| 343 | N = 50 |
| 344 | for i in range(N): |
| 345 | a[i] = C() |
| 346 | for j in range(N): |
| 347 | a[i][j] = i*j |
| 348 | for i in range(N): |
| 349 | for j in range(N): |
| 350 | verify(a[i][j] == i*j) |
| 351 | |
| 352 | def pylists(): |
| 353 | if verbose: print "Testing Python subclass of list..." |
| 354 | class C(list): |
| 355 | def __getitem__(self, i): |
| 356 | return list.__getitem__(self, i) + 100 |
| 357 | def __getslice__(self, i, j): |
| 358 | return (i, j) |
| 359 | a = C() |
| 360 | a.extend([0,1,2]) |
| 361 | verify(a[0] == 100) |
| 362 | verify(a[1] == 101) |
| 363 | verify(a[2] == 102) |
| 364 | verify(a[100:200] == (100,200)) |
| 365 | |
| 366 | def metaclass(): |
| 367 | if verbose: print "Testing __metaclass__..." |
| 368 | global C |
| 369 | class C: |
| 370 | __metaclass__ = type |
| 371 | def __init__(self): |
| 372 | self.__state = 0 |
| 373 | def getstate(self): |
| 374 | return self.__state |
| 375 | def setstate(self, state): |
| 376 | self.__state = state |
| 377 | a = C() |
| 378 | verify(a.getstate() == 0) |
| 379 | a.setstate(10) |
| 380 | verify(a.getstate() == 10) |
| 381 | class D: |
| 382 | class __metaclass__(type): |
| 383 | def myself(cls): return cls |
| 384 | verify(D.myself() == D) |
| 385 | |
| 386 | import sys |
| 387 | MT = type(sys) |
| 388 | |
| 389 | def pymods(): |
| 390 | if verbose: print "Testing Python subclass of module..." |
| 391 | global log |
| 392 | log = [] |
| 393 | class MM(MT): |
| 394 | def __init__(self): |
| 395 | MT.__init__(self) |
| 396 | def __getattr__(self, name): |
| 397 | log.append(("getattr", name)) |
| 398 | return MT.__getattr__(self, name) |
| 399 | def __setattr__(self, name, value): |
| 400 | log.append(("setattr", name, value)) |
| 401 | MT.__setattr__(self, name, value) |
| 402 | def __delattr__(self, name): |
| 403 | log.append(("delattr", name)) |
| 404 | MT.__delattr__(self, name) |
| 405 | a = MM() |
| 406 | a.foo = 12 |
| 407 | x = a.foo |
| 408 | del a.foo |
| 409 | verify(log == [('getattr', '__init__'), |
| 410 | ('getattr', '__setattr__'), |
| 411 | ("setattr", "foo", 12), |
| 412 | ("getattr", "foo"), |
| 413 | ('getattr', '__delattr__'), |
| 414 | ("delattr", "foo")], log) |
| 415 | |
| 416 | def multi(): |
| 417 | if verbose: print "Testing multiple inheritance..." |
| 418 | global C |
| 419 | class C(object): |
| 420 | def __init__(self): |
| 421 | self.__state = 0 |
| 422 | def getstate(self): |
| 423 | return self.__state |
| 424 | def setstate(self, state): |
| 425 | self.__state = state |
| 426 | a = C() |
| 427 | verify(a.getstate() == 0) |
| 428 | a.setstate(10) |
| 429 | verify(a.getstate() == 10) |
| 430 | class D(dictionary, C): |
| 431 | def __init__(self): |
| 432 | type({}).__init__(self) |
| 433 | C.__init__(self) |
| 434 | d = D() |
| 435 | verify(d.keys() == []) |
| 436 | d["hello"] = "world" |
| 437 | verify(d.items() == [("hello", "world")]) |
| 438 | verify(d["hello"] == "world") |
| 439 | verify(d.getstate() == 0) |
| 440 | d.setstate(10) |
| 441 | verify(d.getstate() == 10) |
| 442 | verify(D.__mro__ == (D, dictionary, C, object)) |
| 443 | |
| 444 | def diamond(): |
| 445 | if verbose: print "Testing multiple inheritance special cases..." |
| 446 | class A(object): |
| 447 | def spam(self): return "A" |
| 448 | verify(A().spam() == "A") |
| 449 | class B(A): |
| 450 | def boo(self): return "B" |
| 451 | def spam(self): return "B" |
| 452 | verify(B().spam() == "B") |
| 453 | verify(B().boo() == "B") |
| 454 | class C(A): |
| 455 | def boo(self): return "C" |
| 456 | verify(C().spam() == "A") |
| 457 | verify(C().boo() == "C") |
| 458 | class D(B, C): pass |
| 459 | verify(D().spam() == "B") |
| 460 | verify(D().boo() == "B") |
| 461 | verify(D.__mro__ == (D, B, C, A, object)) |
| 462 | class E(C, B): pass |
| 463 | verify(E().spam() == "B") |
| 464 | verify(E().boo() == "C") |
| 465 | verify(E.__mro__ == (E, C, B, A, object)) |
| 466 | class F(D, E): pass |
| 467 | verify(F().spam() == "B") |
| 468 | verify(F().boo() == "B") |
| 469 | verify(F.__mro__ == (F, D, E, B, C, A, object)) |
| 470 | class G(E, D): pass |
| 471 | verify(G().spam() == "B") |
| 472 | verify(G().boo() == "C") |
| 473 | verify(G.__mro__ == (G, E, D, C, B, A, object)) |
| 474 | |
| 475 | def objects(): |
| 476 | if verbose: print "Testing object class..." |
| 477 | a = object() |
| 478 | verify(a.__class__ == object == type(a)) |
| 479 | b = object() |
| 480 | verify(a is not b) |
| 481 | verify(not hasattr(a, "foo")) |
| 482 | try: |
| 483 | a.foo = 12 |
| 484 | except TypeError: |
| 485 | pass |
| 486 | else: |
| 487 | verify(0, "object() should not allow setting a foo attribute") |
| 488 | verify(not hasattr(object(), "__dict__")) |
| 489 | |
| 490 | class Cdict(object): |
| 491 | pass |
| 492 | x = Cdict() |
| 493 | verify(x.__dict__ is None) |
| 494 | x.foo = 1 |
| 495 | verify(x.foo == 1) |
| 496 | verify(x.__dict__ == {'foo': 1}) |
| 497 | |
| 498 | def slots(): |
| 499 | if verbose: print "Testing __slots__..." |
| 500 | class C0(object): |
| 501 | __slots__ = [] |
| 502 | x = C0() |
| 503 | verify(not hasattr(x, "__dict__")) |
| 504 | verify(not hasattr(x, "foo")) |
| 505 | |
| 506 | class C1(object): |
| 507 | __slots__ = ['a'] |
| 508 | x = C1() |
| 509 | verify(not hasattr(x, "__dict__")) |
| 510 | verify(x.a == None) |
| 511 | x.a = 1 |
| 512 | verify(x.a == 1) |
| 513 | del x.a |
| 514 | verify(x.a == None) |
| 515 | |
| 516 | class C3(object): |
| 517 | __slots__ = ['a', 'b', 'c'] |
| 518 | x = C3() |
| 519 | verify(not hasattr(x, "__dict__")) |
| 520 | verify(x.a is None) |
| 521 | verify(x.b is None) |
| 522 | verify(x.c is None) |
| 523 | x.a = 1 |
| 524 | x.b = 2 |
| 525 | x.c = 3 |
| 526 | verify(x.a == 1) |
| 527 | verify(x.b == 2) |
| 528 | verify(x.c == 3) |
| 529 | |
| 530 | def dynamics(): |
| 531 | if verbose: print "Testing __dynamic__..." |
| 532 | verify(object.__dynamic__ == 0) |
| 533 | verify(list.__dynamic__ == 0) |
| 534 | class S1: |
| 535 | __metaclass__ = type |
| 536 | verify(S1.__dynamic__ == 0) |
| 537 | class S(object): |
| 538 | pass |
| 539 | verify(C.__dynamic__ == 0) |
| 540 | class D(object): |
| 541 | __dynamic__ = 1 |
| 542 | verify(D.__dynamic__ == 1) |
| 543 | class E(D, S): |
| 544 | pass |
| 545 | verify(E.__dynamic__ == 1) |
| 546 | class F(S, D): |
| 547 | pass |
| 548 | verify(F.__dynamic__ == 1) |
| 549 | try: |
| 550 | S.foo = 1 |
| 551 | except (AttributeError, TypeError): |
| 552 | pass |
| 553 | else: |
| 554 | verify(0, "assignment to a static class attribute should be illegal") |
| 555 | D.foo = 1 |
| 556 | verify(D.foo == 1) |
| 557 | # Test that dynamic attributes are inherited |
| 558 | verify(E.foo == 1) |
| 559 | verify(F.foo == 1) |
| 560 | class SS(D): |
| 561 | __dynamic__ = 0 |
| 562 | verify(SS.__dynamic__ == 0) |
| 563 | verify(SS.foo == 1) |
| 564 | try: |
| 565 | SS.foo = 1 |
| 566 | except (AttributeError, TypeError): |
| 567 | pass |
| 568 | else: |
| 569 | verify(0, "assignment to SS.foo should be illegal") |
| 570 | |
| 571 | def errors(): |
| 572 | if verbose: print "Testing errors..." |
| 573 | |
| 574 | try: |
| 575 | class C(list, dictionary): |
| 576 | pass |
| 577 | except TypeError: |
| 578 | pass |
| 579 | else: |
| 580 | verify(0, "inheritance from both list and dict should be illegal") |
| 581 | |
| 582 | try: |
| 583 | class C(object, None): |
| 584 | pass |
| 585 | except TypeError: |
| 586 | pass |
| 587 | else: |
| 588 | verify(0, "inheritance from non-type should be illegal") |
| 589 | class Classic: |
| 590 | pass |
| 591 | |
| 592 | try: |
| 593 | class C(object, Classic): |
| 594 | pass |
| 595 | except TypeError: |
| 596 | pass |
| 597 | else: |
| 598 | verify(0, "inheritance from object and Classic should be illegal") |
| 599 | |
| 600 | try: |
| 601 | class C(int): |
| 602 | pass |
| 603 | except TypeError: |
| 604 | pass |
| 605 | else: |
| 606 | verify(0, "inheritance from int should be illegal") |
| 607 | |
| 608 | try: |
| 609 | class C(object): |
| 610 | __slots__ = 1 |
| 611 | except TypeError: |
| 612 | pass |
| 613 | else: |
| 614 | verify(0, "__slots__ = 1 should be illegal") |
| 615 | |
| 616 | try: |
| 617 | class C(object): |
| 618 | __slots__ = [1] |
| 619 | except TypeError: |
| 620 | pass |
| 621 | else: |
| 622 | verify(0, "__slots__ = [1] should be illegal") |
| 623 | |
| 624 | def classmethods(): |
| 625 | if verbose: print "Testing class methods..." |
| 626 | class C(object): |
| 627 | def foo(*a): return a |
| 628 | goo = classmethod(foo) |
| 629 | c = C() |
| 630 | verify(C.goo(1) == (C, 1)) |
| 631 | verify(c.goo(1) == (C, 1)) |
| 632 | verify(c.foo(1) == (c, 1)) |
| 633 | class D(C): |
| 634 | pass |
| 635 | d = D() |
| 636 | verify(D.goo(1) == (D, 1)) |
| 637 | verify(d.goo(1) == (D, 1)) |
| 638 | verify(d.foo(1) == (d, 1)) |
| 639 | verify(D.foo(d, 1) == (d, 1)) |
| 640 | |
| 641 | def staticmethods(): |
| 642 | if verbose: print "Testing static methods..." |
| 643 | class C(object): |
| 644 | def foo(*a): return a |
| 645 | goo = staticmethod(foo) |
| 646 | c = C() |
| 647 | verify(C.goo(1) == (1,)) |
| 648 | verify(c.goo(1) == (1,)) |
| 649 | verify(c.foo(1) == (c, 1,)) |
| 650 | class D(C): |
| 651 | pass |
| 652 | d = D() |
| 653 | verify(D.goo(1) == (1,)) |
| 654 | verify(d.goo(1) == (1,)) |
| 655 | verify(d.foo(1) == (d, 1)) |
| 656 | verify(D.foo(d, 1) == (d, 1)) |
| 657 | |
| 658 | def classic(): |
| 659 | if verbose: print "Testing classic classes..." |
| 660 | class C: |
| 661 | def foo(*a): return a |
| 662 | goo = classmethod(foo) |
| 663 | c = C() |
| 664 | verify(C.goo(1) == (C, 1)) |
| 665 | verify(c.goo(1) == (C, 1)) |
| 666 | verify(c.foo(1) == (c, 1)) |
| 667 | class D(C): |
| 668 | pass |
| 669 | d = D() |
| 670 | verify(D.goo(1) == (D, 1)) |
| 671 | verify(d.goo(1) == (D, 1)) |
| 672 | verify(d.foo(1) == (d, 1)) |
| 673 | verify(D.foo(d, 1) == (d, 1)) |
| 674 | |
| 675 | def compattr(): |
| 676 | if verbose: print "Testing computed attributes..." |
| 677 | class C(object): |
| 678 | class computed_attribute(object): |
| 679 | def __init__(self, get, set=None): |
| 680 | self.__get = get |
| 681 | self.__set = set |
| 682 | def __get__(self, obj, type=None): |
| 683 | return self.__get(obj) |
| 684 | def __set__(self, obj, value): |
| 685 | return self.__set(obj, value) |
| 686 | def __init__(self): |
| 687 | self.__x = 0 |
| 688 | def __get_x(self): |
| 689 | x = self.__x |
| 690 | self.__x = x+1 |
| 691 | return x |
| 692 | def __set_x(self, x): |
| 693 | self.__x = x |
| 694 | x = computed_attribute(__get_x, __set_x) |
| 695 | a = C() |
| 696 | verify(a.x == 0) |
| 697 | verify(a.x == 1) |
| 698 | a.x = 10 |
| 699 | verify(a.x == 10) |
| 700 | verify(a.x == 11) |
| 701 | |
| 702 | def newslot(): |
| 703 | if verbose: print "Testing __new__ slot override..." |
| 704 | class C(list): |
| 705 | def __new__(cls): |
| 706 | self = list.__new__(cls) |
| 707 | self.foo = 1 |
| 708 | return self |
| 709 | def __init__(self): |
| 710 | self.foo = self.foo + 2 |
| 711 | a = C() |
| 712 | verify(a.foo == 3) |
| 713 | verify(a.__class__ is C) |
| 714 | class D(C): |
| 715 | pass |
| 716 | b = D() |
| 717 | verify(b.foo == 3) |
| 718 | verify(b.__class__ is D) |
| 719 | |
| 720 | class PerverseMetaType(type): |
| 721 | def mro(cls): |
| 722 | L = type.mro(cls) |
| 723 | L.reverse() |
| 724 | return L |
| 725 | |
| 726 | def altmro(): |
| 727 | if verbose: print "Testing mro() and overriding it..." |
| 728 | class A(object): |
| 729 | def f(self): return "A" |
| 730 | class B(A): |
| 731 | pass |
| 732 | class C(A): |
| 733 | def f(self): return "C" |
| 734 | class D(B, C): |
| 735 | pass |
| 736 | verify(D.mro() == [D, B, C, A, object] == list(D.__mro__)) |
| 737 | verify(D().f() == "C") |
| 738 | class X(A,B,C,D): |
| 739 | __metaclass__ = PerverseMetaType |
| 740 | verify(X.__mro__ == (object, A, C, B, D, X)) |
| 741 | verify(X().f() == "A") |
| 742 | |
| 743 | def overloading(): |
| 744 | if verbose: print "testing operator overloading..." |
| 745 | |
| 746 | class B(object): |
| 747 | "Intermediate class because object doesn't have a __setattr__" |
| 748 | |
| 749 | class C(B): |
| 750 | |
| 751 | def __getattr__(self, name): |
| 752 | if name == "foo": |
| 753 | return ("getattr", name) |
| 754 | else: |
| 755 | return B.__getattr__(self, name) |
| 756 | def __setattr__(self, name, value): |
| 757 | if name == "foo": |
| 758 | self.setattr = (name, value) |
| 759 | else: |
| 760 | return B.__setattr__(self, name, value) |
| 761 | def __delattr__(self, name): |
| 762 | if name == "foo": |
| 763 | self.delattr = name |
| 764 | else: |
| 765 | return B.__delattr__(self, name) |
| 766 | |
| 767 | def __getitem__(self, key): |
| 768 | return ("getitem", key) |
| 769 | def __setitem__(self, key, value): |
| 770 | self.setitem = (key, value) |
| 771 | def __delitem__(self, key): |
| 772 | self.delitem = key |
| 773 | |
| 774 | def __getslice__(self, i, j): |
| 775 | return ("getslice", i, j) |
| 776 | def __setslice__(self, i, j, value): |
| 777 | self.setslice = (i, j, value) |
| 778 | def __delslice__(self, i, j): |
| 779 | self.delslice = (i, j) |
| 780 | |
| 781 | a = C() |
| 782 | verify(a.foo == ("getattr", "foo")) |
| 783 | a.foo = 12 |
| 784 | verify(a.setattr == ("foo", 12)) |
| 785 | del a.foo |
| 786 | verify(a.delattr == "foo") |
| 787 | |
| 788 | verify(a[12] == ("getitem", 12)) |
| 789 | a[12] = 21 |
| 790 | verify(a.setitem == (12, 21)) |
| 791 | del a[12] |
| 792 | verify(a.delitem == 12) |
| 793 | |
| 794 | verify(a[0:10] == ("getslice", 0, 10)) |
| 795 | a[0:10] = "foo" |
| 796 | verify(a.setslice == (0, 10, "foo")) |
| 797 | del a[0:10] |
| 798 | verify(a.delslice == (0, 10)) |
| 799 | |
| 800 | def all(): |
| 801 | lists() |
| 802 | dicts() |
| 803 | ints() |
| 804 | longs() |
| 805 | floats() |
| 806 | complexes() |
| 807 | spamlists() |
| 808 | spamdicts() |
| 809 | pydicts() |
| 810 | pylists() |
| 811 | metaclass() |
| 812 | pymods() |
| 813 | multi() |
| 814 | diamond() |
| 815 | objects() |
| 816 | slots() |
| 817 | dynamics() |
| 818 | errors() |
| 819 | classmethods() |
| 820 | staticmethods() |
| 821 | classic() |
| 822 | compattr() |
| 823 | newslot() |
| 824 | altmro() |
| 825 | overloading() |
| 826 | |
| 827 | all() |
| 828 | |
| 829 | if verbose: print "All OK" |