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