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 | |
| 1003 | def dynamics(): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1004 | if verbose: print "Testing class attribute propagation..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1005 | class D(object): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1006 | pass |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1007 | class E(D): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1008 | pass |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1009 | class F(D): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1010 | pass |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1011 | D.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1012 | vereq(D.foo, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1013 | # Test that dynamic attributes are inherited |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1014 | vereq(E.foo, 1) |
| 1015 | vereq(F.foo, 1) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 1016 | # Test dynamic instances |
| 1017 | class C(object): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1018 | pass |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 1019 | a = C() |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1020 | verify(not hasattr(a, "foobar")) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 1021 | C.foobar = 2 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1022 | vereq(a.foobar, 2) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 1023 | C.method = lambda self: 42 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1024 | vereq(a.method(), 42) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 1025 | C.__repr__ = lambda self: "C()" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1026 | vereq(repr(a), "C()") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1027 | C.__int__ = lambda self: 100 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1028 | vereq(int(a), 100) |
| 1029 | vereq(a.foobar, 2) |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1030 | verify(not hasattr(a, "spam")) |
| 1031 | def mygetattr(self, name): |
| 1032 | if name == "spam": |
| 1033 | return "spam" |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 1034 | raise AttributeError |
| 1035 | C.__getattr__ = mygetattr |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1036 | vereq(a.spam, "spam") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1037 | a.new = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1038 | vereq(a.new, 12) |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1039 | def mysetattr(self, name, value): |
| 1040 | if name == "spam": |
| 1041 | raise AttributeError |
| 1042 | return object.__setattr__(self, name, value) |
| 1043 | C.__setattr__ = mysetattr |
| 1044 | try: |
| 1045 | a.spam = "not spam" |
| 1046 | except AttributeError: |
| 1047 | pass |
| 1048 | else: |
| 1049 | verify(0, "expected AttributeError") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1050 | vereq(a.spam, "spam") |
Guido van Rossum | 80e3675 | 2001-08-14 20:00:33 +0000 | [diff] [blame] | 1051 | class D(C): |
| 1052 | pass |
| 1053 | d = D() |
| 1054 | d.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1055 | vereq(d.foo, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1056 | |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 1057 | # Test handling of int*seq and seq*int |
| 1058 | class I(int): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1059 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1060 | vereq("a"*I(2), "aa") |
| 1061 | vereq(I(2)*"a", "aa") |
| 1062 | vereq(2*I(3), 6) |
| 1063 | vereq(I(3)*2, 6) |
| 1064 | vereq(I(3)*I(2), 6) |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 1065 | |
| 1066 | # Test handling of long*seq and seq*long |
| 1067 | class L(long): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1068 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1069 | vereq("a"*L(2L), "aa") |
| 1070 | vereq(L(2L)*"a", "aa") |
| 1071 | vereq(2*L(3), 6) |
| 1072 | vereq(L(3)*2, 6) |
| 1073 | vereq(L(3)*L(2), 6) |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 1074 | |
Guido van Rossum | 3d45d8f | 2001-09-24 18:47:40 +0000 | [diff] [blame] | 1075 | # Test comparison of classes with dynamic metaclasses |
| 1076 | class dynamicmetaclass(type): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1077 | pass |
Guido van Rossum | 3d45d8f | 2001-09-24 18:47:40 +0000 | [diff] [blame] | 1078 | class someclass: |
| 1079 | __metaclass__ = dynamicmetaclass |
| 1080 | verify(someclass != object) |
| 1081 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1082 | def errors(): |
| 1083 | if verbose: print "Testing errors..." |
| 1084 | |
| 1085 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 1086 | class C(list, dict): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1087 | pass |
| 1088 | except TypeError: |
| 1089 | pass |
| 1090 | else: |
| 1091 | verify(0, "inheritance from both list and dict should be illegal") |
| 1092 | |
| 1093 | try: |
| 1094 | class C(object, None): |
| 1095 | pass |
| 1096 | except TypeError: |
| 1097 | pass |
| 1098 | else: |
| 1099 | verify(0, "inheritance from non-type should be illegal") |
| 1100 | class Classic: |
| 1101 | pass |
| 1102 | |
| 1103 | try: |
Guido van Rossum | 8aea0cc | 2001-08-29 15:48:43 +0000 | [diff] [blame] | 1104 | class C(type(len)): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1105 | pass |
| 1106 | except TypeError: |
| 1107 | pass |
| 1108 | else: |
Guido van Rossum | 8aea0cc | 2001-08-29 15:48:43 +0000 | [diff] [blame] | 1109 | verify(0, "inheritance from CFunction should be illegal") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1110 | |
| 1111 | try: |
| 1112 | class C(object): |
| 1113 | __slots__ = 1 |
| 1114 | except TypeError: |
| 1115 | pass |
| 1116 | else: |
| 1117 | verify(0, "__slots__ = 1 should be illegal") |
| 1118 | |
| 1119 | try: |
| 1120 | class C(object): |
| 1121 | __slots__ = [1] |
| 1122 | except TypeError: |
| 1123 | pass |
| 1124 | else: |
| 1125 | verify(0, "__slots__ = [1] should be illegal") |
| 1126 | |
| 1127 | def classmethods(): |
| 1128 | if verbose: print "Testing class methods..." |
| 1129 | class C(object): |
| 1130 | def foo(*a): return a |
| 1131 | goo = classmethod(foo) |
| 1132 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1133 | vereq(C.goo(1), (C, 1)) |
| 1134 | vereq(c.goo(1), (C, 1)) |
| 1135 | vereq(c.foo(1), (c, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1136 | class D(C): |
| 1137 | pass |
| 1138 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1139 | vereq(D.goo(1), (D, 1)) |
| 1140 | vereq(d.goo(1), (D, 1)) |
| 1141 | vereq(d.foo(1), (d, 1)) |
| 1142 | vereq(D.foo(d, 1), (d, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1143 | |
| 1144 | def staticmethods(): |
| 1145 | if verbose: print "Testing static methods..." |
| 1146 | class C(object): |
| 1147 | def foo(*a): return a |
| 1148 | goo = staticmethod(foo) |
| 1149 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1150 | vereq(C.goo(1), (1,)) |
| 1151 | vereq(c.goo(1), (1,)) |
| 1152 | vereq(c.foo(1), (c, 1,)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1153 | class D(C): |
| 1154 | pass |
| 1155 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1156 | vereq(D.goo(1), (1,)) |
| 1157 | vereq(d.goo(1), (1,)) |
| 1158 | vereq(d.foo(1), (d, 1)) |
| 1159 | vereq(D.foo(d, 1), (d, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1160 | |
| 1161 | def classic(): |
| 1162 | if verbose: print "Testing classic classes..." |
| 1163 | class C: |
| 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)) |
Guido van Rossum | 9301876 | 2001-08-17 13:40:47 +0000 | [diff] [blame] | 1177 | class E: # *not* subclassing from C |
| 1178 | foo = C.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1179 | vereq(E().foo, C.foo) # i.e., unbound |
Guido van Rossum | 84a79a8 | 2001-08-17 13:58:31 +0000 | [diff] [blame] | 1180 | verify(repr(C.foo.__get__(C())).startswith("<bound method ")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1181 | |
| 1182 | def compattr(): |
| 1183 | if verbose: print "Testing computed attributes..." |
| 1184 | class C(object): |
| 1185 | class computed_attribute(object): |
| 1186 | def __init__(self, get, set=None): |
| 1187 | self.__get = get |
| 1188 | self.__set = set |
| 1189 | def __get__(self, obj, type=None): |
| 1190 | return self.__get(obj) |
| 1191 | def __set__(self, obj, value): |
| 1192 | return self.__set(obj, value) |
| 1193 | def __init__(self): |
| 1194 | self.__x = 0 |
| 1195 | def __get_x(self): |
| 1196 | x = self.__x |
| 1197 | self.__x = x+1 |
| 1198 | return x |
| 1199 | def __set_x(self, x): |
| 1200 | self.__x = x |
| 1201 | x = computed_attribute(__get_x, __set_x) |
| 1202 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1203 | vereq(a.x, 0) |
| 1204 | vereq(a.x, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1205 | a.x = 10 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1206 | vereq(a.x, 10) |
| 1207 | vereq(a.x, 11) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1208 | |
| 1209 | def newslot(): |
| 1210 | if verbose: print "Testing __new__ slot override..." |
| 1211 | class C(list): |
| 1212 | def __new__(cls): |
| 1213 | self = list.__new__(cls) |
| 1214 | self.foo = 1 |
| 1215 | return self |
| 1216 | def __init__(self): |
| 1217 | self.foo = self.foo + 2 |
| 1218 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1219 | vereq(a.foo, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1220 | verify(a.__class__ is C) |
| 1221 | class D(C): |
| 1222 | pass |
| 1223 | b = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1224 | vereq(b.foo, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1225 | verify(b.__class__ is D) |
| 1226 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1227 | def altmro(): |
| 1228 | if verbose: print "Testing mro() and overriding it..." |
| 1229 | class A(object): |
| 1230 | def f(self): return "A" |
| 1231 | class B(A): |
| 1232 | pass |
| 1233 | class C(A): |
| 1234 | def f(self): return "C" |
| 1235 | class D(B, C): |
| 1236 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1237 | vereq(D.mro(), [D, B, C, A, object]) |
| 1238 | vereq(D.__mro__, (D, B, C, A, object)) |
| 1239 | vereq(D().f(), "C") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1240 | class PerverseMetaType(type): |
| 1241 | def mro(cls): |
| 1242 | L = type.mro(cls) |
| 1243 | L.reverse() |
| 1244 | return L |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1245 | class X(A,B,C,D): |
| 1246 | __metaclass__ = PerverseMetaType |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1247 | vereq(X.__mro__, (object, A, C, B, D, X)) |
| 1248 | vereq(X().f(), "A") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1249 | |
| 1250 | def overloading(): |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1251 | if verbose: print "Testing operator overloading..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1252 | |
| 1253 | class B(object): |
| 1254 | "Intermediate class because object doesn't have a __setattr__" |
| 1255 | |
| 1256 | class C(B): |
| 1257 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 1258 | def __getattr__(self, name): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1259 | if name == "foo": |
| 1260 | return ("getattr", name) |
| 1261 | else: |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 1262 | raise AttributeError |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1263 | def __setattr__(self, name, value): |
| 1264 | if name == "foo": |
| 1265 | self.setattr = (name, value) |
| 1266 | else: |
| 1267 | return B.__setattr__(self, name, value) |
| 1268 | def __delattr__(self, name): |
| 1269 | if name == "foo": |
| 1270 | self.delattr = name |
| 1271 | else: |
| 1272 | return B.__delattr__(self, name) |
| 1273 | |
| 1274 | def __getitem__(self, key): |
| 1275 | return ("getitem", key) |
| 1276 | def __setitem__(self, key, value): |
| 1277 | self.setitem = (key, value) |
| 1278 | def __delitem__(self, key): |
| 1279 | self.delitem = key |
| 1280 | |
| 1281 | def __getslice__(self, i, j): |
| 1282 | return ("getslice", i, j) |
| 1283 | def __setslice__(self, i, j, value): |
| 1284 | self.setslice = (i, j, value) |
| 1285 | def __delslice__(self, i, j): |
| 1286 | self.delslice = (i, j) |
| 1287 | |
| 1288 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1289 | vereq(a.foo, ("getattr", "foo")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1290 | a.foo = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1291 | vereq(a.setattr, ("foo", 12)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1292 | del a.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1293 | vereq(a.delattr, "foo") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1294 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1295 | vereq(a[12], ("getitem", 12)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1296 | a[12] = 21 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1297 | vereq(a.setitem, (12, 21)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1298 | del a[12] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1299 | vereq(a.delitem, 12) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1300 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1301 | vereq(a[0:10], ("getslice", 0, 10)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1302 | a[0:10] = "foo" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1303 | vereq(a.setslice, (0, 10, "foo")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1304 | del a[0:10] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1305 | vereq(a.delslice, (0, 10)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1306 | |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1307 | def methods(): |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1308 | if verbose: print "Testing methods..." |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1309 | class C(object): |
| 1310 | def __init__(self, x): |
| 1311 | self.x = x |
| 1312 | def foo(self): |
| 1313 | return self.x |
| 1314 | c1 = C(1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1315 | vereq(c1.foo(), 1) |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1316 | class D(C): |
| 1317 | boo = C.foo |
| 1318 | goo = c1.foo |
| 1319 | d2 = D(2) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1320 | vereq(d2.foo(), 2) |
| 1321 | vereq(d2.boo(), 2) |
| 1322 | vereq(d2.goo(), 1) |
Guido van Rossum | 9301876 | 2001-08-17 13:40:47 +0000 | [diff] [blame] | 1323 | class E(object): |
| 1324 | foo = C.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1325 | vereq(E().foo, C.foo) # i.e., unbound |
Guido van Rossum | 84a79a8 | 2001-08-17 13:58:31 +0000 | [diff] [blame] | 1326 | verify(repr(C.foo.__get__(C(1))).startswith("<bound method ")) |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1327 | |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1328 | def specials(): |
| 1329 | # Test operators like __hash__ for which a built-in default exists |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1330 | if verbose: print "Testing special operators..." |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1331 | # Test the default behavior for static classes |
| 1332 | class C(object): |
| 1333 | def __getitem__(self, i): |
| 1334 | if 0 <= i < 10: return i |
| 1335 | raise IndexError |
| 1336 | c1 = C() |
| 1337 | c2 = C() |
| 1338 | verify(not not c1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1339 | vereq(hash(c1), id(c1)) |
| 1340 | vereq(cmp(c1, c2), cmp(id(c1), id(c2))) |
| 1341 | vereq(c1, c1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1342 | verify(c1 != c2) |
| 1343 | verify(not c1 != c1) |
| 1344 | verify(not c1 == c2) |
Tim Peters | 4d2dded | 2001-08-16 19:50:51 +0000 | [diff] [blame] | 1345 | # Note that the module name appears in str/repr, and that varies |
| 1346 | # 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] | 1347 | verify(str(c1).find('C object at ') >= 0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1348 | vereq(str(c1), repr(c1)) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1349 | verify(-1 not in c1) |
| 1350 | for i in range(10): |
| 1351 | verify(i in c1) |
| 1352 | verify(10 not in c1) |
| 1353 | # Test the default behavior for dynamic classes |
| 1354 | class D(object): |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1355 | def __getitem__(self, i): |
| 1356 | if 0 <= i < 10: return i |
| 1357 | raise IndexError |
| 1358 | d1 = D() |
| 1359 | d2 = D() |
| 1360 | verify(not not d1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1361 | vereq(hash(d1), id(d1)) |
| 1362 | vereq(cmp(d1, d2), cmp(id(d1), id(d2))) |
| 1363 | vereq(d1, d1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1364 | verify(d1 != d2) |
| 1365 | verify(not d1 != d1) |
| 1366 | verify(not d1 == d2) |
Tim Peters | 4d2dded | 2001-08-16 19:50:51 +0000 | [diff] [blame] | 1367 | # Note that the module name appears in str/repr, and that varies |
| 1368 | # 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] | 1369 | verify(str(d1).find('D object at ') >= 0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1370 | vereq(str(d1), repr(d1)) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1371 | verify(-1 not in d1) |
| 1372 | for i in range(10): |
| 1373 | verify(i in d1) |
| 1374 | verify(10 not in d1) |
| 1375 | # Test overridden behavior for static classes |
| 1376 | class Proxy(object): |
| 1377 | def __init__(self, x): |
| 1378 | self.x = x |
| 1379 | def __nonzero__(self): |
| 1380 | return not not self.x |
| 1381 | def __hash__(self): |
| 1382 | return hash(self.x) |
| 1383 | def __eq__(self, other): |
| 1384 | return self.x == other |
| 1385 | def __ne__(self, other): |
| 1386 | return self.x != other |
| 1387 | def __cmp__(self, other): |
| 1388 | return cmp(self.x, other.x) |
| 1389 | def __str__(self): |
| 1390 | return "Proxy:%s" % self.x |
| 1391 | def __repr__(self): |
| 1392 | return "Proxy(%r)" % self.x |
| 1393 | def __contains__(self, value): |
| 1394 | return value in self.x |
| 1395 | p0 = Proxy(0) |
| 1396 | p1 = Proxy(1) |
| 1397 | p_1 = Proxy(-1) |
| 1398 | verify(not p0) |
| 1399 | verify(not not p1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1400 | vereq(hash(p0), hash(0)) |
| 1401 | vereq(p0, p0) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1402 | verify(p0 != p1) |
| 1403 | verify(not p0 != p0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1404 | vereq(not p0, p1) |
| 1405 | vereq(cmp(p0, p1), -1) |
| 1406 | vereq(cmp(p0, p0), 0) |
| 1407 | vereq(cmp(p0, p_1), 1) |
| 1408 | vereq(str(p0), "Proxy:0") |
| 1409 | vereq(repr(p0), "Proxy(0)") |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1410 | p10 = Proxy(range(10)) |
| 1411 | verify(-1 not in p10) |
| 1412 | for i in range(10): |
| 1413 | verify(i in p10) |
| 1414 | verify(10 not in p10) |
| 1415 | # Test overridden behavior for dynamic classes |
| 1416 | class DProxy(object): |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1417 | def __init__(self, x): |
| 1418 | self.x = x |
| 1419 | def __nonzero__(self): |
| 1420 | return not not self.x |
| 1421 | def __hash__(self): |
| 1422 | return hash(self.x) |
| 1423 | def __eq__(self, other): |
| 1424 | return self.x == other |
| 1425 | def __ne__(self, other): |
| 1426 | return self.x != other |
| 1427 | def __cmp__(self, other): |
| 1428 | return cmp(self.x, other.x) |
| 1429 | def __str__(self): |
| 1430 | return "DProxy:%s" % self.x |
| 1431 | def __repr__(self): |
| 1432 | return "DProxy(%r)" % self.x |
| 1433 | def __contains__(self, value): |
| 1434 | return value in self.x |
| 1435 | p0 = DProxy(0) |
| 1436 | p1 = DProxy(1) |
| 1437 | p_1 = DProxy(-1) |
| 1438 | verify(not p0) |
| 1439 | verify(not not p1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1440 | vereq(hash(p0), hash(0)) |
| 1441 | vereq(p0, p0) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1442 | verify(p0 != p1) |
| 1443 | verify(not p0 != p0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1444 | vereq(not p0, p1) |
| 1445 | vereq(cmp(p0, p1), -1) |
| 1446 | vereq(cmp(p0, p0), 0) |
| 1447 | vereq(cmp(p0, p_1), 1) |
| 1448 | vereq(str(p0), "DProxy:0") |
| 1449 | vereq(repr(p0), "DProxy(0)") |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1450 | p10 = DProxy(range(10)) |
| 1451 | verify(-1 not in p10) |
| 1452 | for i in range(10): |
| 1453 | verify(i in p10) |
| 1454 | verify(10 not in p10) |
Guido van Rossum | 843daa8 | 2001-09-18 20:04:26 +0000 | [diff] [blame] | 1455 | # Safety test for __cmp__ |
| 1456 | def unsafecmp(a, b): |
| 1457 | try: |
| 1458 | a.__class__.__cmp__(a, b) |
| 1459 | except TypeError: |
| 1460 | pass |
| 1461 | else: |
| 1462 | raise TestFailed, "shouldn't allow %s.__cmp__(%r, %r)" % ( |
| 1463 | a.__class__, a, b) |
| 1464 | unsafecmp(u"123", "123") |
| 1465 | unsafecmp("123", u"123") |
| 1466 | unsafecmp(1, 1.0) |
| 1467 | unsafecmp(1.0, 1) |
| 1468 | unsafecmp(1, 1L) |
| 1469 | unsafecmp(1L, 1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1470 | |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1471 | def weakrefs(): |
| 1472 | if verbose: print "Testing weak references..." |
| 1473 | import weakref |
| 1474 | class C(object): |
| 1475 | pass |
| 1476 | c = C() |
| 1477 | r = weakref.ref(c) |
| 1478 | verify(r() is c) |
| 1479 | del c |
| 1480 | verify(r() is None) |
| 1481 | del r |
| 1482 | class NoWeak(object): |
| 1483 | __slots__ = ['foo'] |
| 1484 | no = NoWeak() |
| 1485 | try: |
| 1486 | weakref.ref(no) |
| 1487 | except TypeError, msg: |
Fred Drake | 4bf018b | 2001-10-22 21:45:25 +0000 | [diff] [blame] | 1488 | verify(str(msg).find("weak reference") >= 0) |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1489 | else: |
| 1490 | verify(0, "weakref.ref(no) should be illegal") |
| 1491 | class Weak(object): |
| 1492 | __slots__ = ['foo', '__weakref__'] |
| 1493 | yes = Weak() |
| 1494 | r = weakref.ref(yes) |
| 1495 | verify(r() is yes) |
| 1496 | del yes |
| 1497 | verify(r() is None) |
| 1498 | del r |
| 1499 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1500 | def properties(): |
| 1501 | if verbose: print "Testing property..." |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1502 | class C(object): |
| 1503 | def getx(self): |
| 1504 | return self.__x |
| 1505 | def setx(self, value): |
| 1506 | self.__x = value |
| 1507 | def delx(self): |
| 1508 | del self.__x |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1509 | x = property(getx, setx, delx, doc="I'm the x property.") |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1510 | a = C() |
| 1511 | verify(not hasattr(a, "x")) |
| 1512 | a.x = 42 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1513 | vereq(a._C__x, 42) |
| 1514 | vereq(a.x, 42) |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1515 | del a.x |
| 1516 | verify(not hasattr(a, "x")) |
| 1517 | verify(not hasattr(a, "_C__x")) |
| 1518 | C.x.__set__(a, 100) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1519 | vereq(C.x.__get__(a), 100) |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1520 | ## C.x.__set__(a) |
| 1521 | ## verify(not hasattr(a, "x")) |
| 1522 | |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1523 | raw = C.__dict__['x'] |
| 1524 | verify(isinstance(raw, property)) |
| 1525 | |
| 1526 | attrs = dir(raw) |
| 1527 | verify("__doc__" in attrs) |
| 1528 | verify("fget" in attrs) |
| 1529 | verify("fset" in attrs) |
| 1530 | verify("fdel" in attrs) |
| 1531 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1532 | vereq(raw.__doc__, "I'm the x property.") |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1533 | verify(raw.fget is C.__dict__['getx']) |
| 1534 | verify(raw.fset is C.__dict__['setx']) |
| 1535 | verify(raw.fdel is C.__dict__['delx']) |
| 1536 | |
| 1537 | for attr in "__doc__", "fget", "fset", "fdel": |
| 1538 | try: |
| 1539 | setattr(raw, attr, 42) |
| 1540 | except TypeError, msg: |
| 1541 | if str(msg).find('readonly') < 0: |
| 1542 | raise TestFailed("when setting readonly attr %r on a " |
| 1543 | "property, got unexpected TypeError " |
| 1544 | "msg %r" % (attr, str(msg))) |
| 1545 | else: |
| 1546 | raise TestFailed("expected TypeError from trying to set " |
| 1547 | "readonly %r attr on a property" % attr) |
| 1548 | |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1549 | def supers(): |
Guido van Rossum | 9881fc1 | 2001-08-24 17:07:20 +0000 | [diff] [blame] | 1550 | if verbose: print "Testing super..." |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1551 | |
| 1552 | class A(object): |
| 1553 | def meth(self, a): |
| 1554 | return "A(%r)" % a |
| 1555 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1556 | vereq(A().meth(1), "A(1)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1557 | |
| 1558 | class B(A): |
| 1559 | def __init__(self): |
| 1560 | self.__super = super(B, self) |
| 1561 | def meth(self, a): |
| 1562 | return "B(%r)" % a + self.__super.meth(a) |
| 1563 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1564 | vereq(B().meth(2), "B(2)A(2)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1565 | |
| 1566 | class C(A): |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1567 | def meth(self, a): |
| 1568 | return "C(%r)" % a + self.__super.meth(a) |
| 1569 | C._C__super = super(C) |
| 1570 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1571 | vereq(C().meth(3), "C(3)A(3)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1572 | |
| 1573 | class D(C, B): |
| 1574 | def meth(self, a): |
| 1575 | return "D(%r)" % a + super(D, self).meth(a) |
| 1576 | |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 1577 | vereq(D().meth(4), "D(4)C(4)B(4)A(4)") |
| 1578 | |
| 1579 | # Test for subclassing super |
| 1580 | |
| 1581 | class mysuper(super): |
| 1582 | def __init__(self, *args): |
| 1583 | return super(mysuper, self).__init__(*args) |
| 1584 | |
| 1585 | class E(D): |
| 1586 | def meth(self, a): |
| 1587 | return "E(%r)" % a + mysuper(E, self).meth(a) |
| 1588 | |
| 1589 | vereq(E().meth(5), "E(5)D(5)C(5)B(5)A(5)") |
| 1590 | |
| 1591 | class F(E): |
| 1592 | def meth(self, a): |
| 1593 | s = self.__super |
| 1594 | return "F(%r)[%s]" % (a, s.__class__.__name__) + s.meth(a) |
| 1595 | F._F__super = mysuper(F) |
| 1596 | |
| 1597 | vereq(F().meth(6), "F(6)[mysuper]E(6)D(6)C(6)B(6)A(6)") |
| 1598 | |
| 1599 | # Make sure certain errors are raised |
| 1600 | |
| 1601 | try: |
| 1602 | super(D, 42) |
| 1603 | except TypeError: |
| 1604 | pass |
| 1605 | else: |
| 1606 | raise TestFailed, "shouldn't allow super(D, 42)" |
| 1607 | |
| 1608 | try: |
| 1609 | super(D, C()) |
| 1610 | except TypeError: |
| 1611 | pass |
| 1612 | else: |
| 1613 | raise TestFailed, "shouldn't allow super(D, C())" |
| 1614 | |
| 1615 | try: |
| 1616 | super(D).__get__(12) |
| 1617 | except TypeError: |
| 1618 | pass |
| 1619 | else: |
| 1620 | raise TestFailed, "shouldn't allow super(D).__get__(12)" |
| 1621 | |
| 1622 | try: |
| 1623 | super(D).__get__(C()) |
| 1624 | except TypeError: |
| 1625 | pass |
| 1626 | else: |
| 1627 | raise TestFailed, "shouldn't allow super(D).__get__(C())" |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1628 | |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1629 | def inherits(): |
| 1630 | if verbose: print "Testing inheritance from basic types..." |
| 1631 | |
| 1632 | class hexint(int): |
| 1633 | def __repr__(self): |
| 1634 | return hex(self) |
| 1635 | def __add__(self, other): |
| 1636 | return hexint(int.__add__(self, other)) |
| 1637 | # (Note that overriding __radd__ doesn't work, |
| 1638 | # because the int type gets first dibs.) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1639 | vereq(repr(hexint(7) + 9), "0x10") |
| 1640 | vereq(repr(hexint(1000) + 7), "0x3ef") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1641 | a = hexint(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1642 | vereq(a, 12345) |
| 1643 | vereq(int(a), 12345) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1644 | verify(int(a).__class__ is int) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1645 | vereq(hash(a), hash(12345)) |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 1646 | verify((+a).__class__ is int) |
| 1647 | verify((a >> 0).__class__ is int) |
| 1648 | verify((a << 0).__class__ is int) |
| 1649 | verify((hexint(0) << 12).__class__ is int) |
| 1650 | verify((hexint(0) >> 12).__class__ is int) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1651 | |
| 1652 | class octlong(long): |
| 1653 | __slots__ = [] |
| 1654 | def __str__(self): |
| 1655 | s = oct(self) |
| 1656 | if s[-1] == 'L': |
| 1657 | s = s[:-1] |
| 1658 | return s |
| 1659 | def __add__(self, other): |
| 1660 | return self.__class__(super(octlong, self).__add__(other)) |
| 1661 | __radd__ = __add__ |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1662 | vereq(str(octlong(3) + 5), "010") |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1663 | # (Note that overriding __radd__ here only seems to work |
| 1664 | # because the example uses a short int left argument.) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1665 | vereq(str(5 + octlong(3000)), "05675") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1666 | a = octlong(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1667 | vereq(a, 12345L) |
| 1668 | vereq(long(a), 12345L) |
| 1669 | vereq(hash(a), hash(12345L)) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1670 | verify(long(a).__class__ is long) |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 1671 | verify((+a).__class__ is long) |
| 1672 | verify((-a).__class__ is long) |
| 1673 | verify((-octlong(0)).__class__ is long) |
| 1674 | verify((a >> 0).__class__ is long) |
| 1675 | verify((a << 0).__class__ is long) |
| 1676 | verify((a - 0).__class__ is long) |
| 1677 | verify((a * 1).__class__ is long) |
| 1678 | verify((a ** 1).__class__ is long) |
| 1679 | verify((a // 1).__class__ is long) |
| 1680 | verify((1 * a).__class__ is long) |
| 1681 | verify((a | 0).__class__ is long) |
| 1682 | verify((a ^ 0).__class__ is long) |
| 1683 | verify((a & -1L).__class__ is long) |
| 1684 | verify((octlong(0) << 12).__class__ is long) |
| 1685 | verify((octlong(0) >> 12).__class__ is long) |
| 1686 | verify(abs(octlong(0)).__class__ is long) |
| 1687 | |
| 1688 | # Because octlong overrides __add__, we can't check the absence of +0 |
| 1689 | # optimizations using octlong. |
| 1690 | class longclone(long): |
| 1691 | pass |
| 1692 | a = longclone(1) |
| 1693 | verify((a + 0).__class__ is long) |
| 1694 | verify((0 + a).__class__ is long) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1695 | |
| 1696 | class precfloat(float): |
| 1697 | __slots__ = ['prec'] |
| 1698 | def __init__(self, value=0.0, prec=12): |
| 1699 | self.prec = int(prec) |
| 1700 | float.__init__(value) |
| 1701 | def __repr__(self): |
| 1702 | return "%.*g" % (self.prec, self) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1703 | vereq(repr(precfloat(1.1)), "1.1") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1704 | a = precfloat(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1705 | vereq(a, 12345.0) |
| 1706 | vereq(float(a), 12345.0) |
Tim Peters | 7a50f25 | 2001-09-10 21:28:20 +0000 | [diff] [blame] | 1707 | verify(float(a).__class__ is float) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1708 | vereq(hash(a), hash(12345.0)) |
Tim Peters | 0280cf7 | 2001-09-11 21:53:35 +0000 | [diff] [blame] | 1709 | verify((+a).__class__ is float) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1710 | |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1711 | class madcomplex(complex): |
| 1712 | def __repr__(self): |
| 1713 | return "%.17gj%+.17g" % (self.imag, self.real) |
| 1714 | a = madcomplex(-3, 4) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1715 | vereq(repr(a), "4j-3") |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1716 | base = complex(-3, 4) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1717 | veris(base.__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1718 | vereq(a, base) |
| 1719 | vereq(complex(a), base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1720 | veris(complex(a).__class__, complex) |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1721 | a = madcomplex(a) # just trying another form of the constructor |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1722 | vereq(repr(a), "4j-3") |
| 1723 | vereq(a, base) |
| 1724 | vereq(complex(a), base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1725 | veris(complex(a).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1726 | vereq(hash(a), hash(base)) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1727 | veris((+a).__class__, complex) |
| 1728 | veris((a + 0).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1729 | vereq(a + 0, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1730 | veris((a - 0).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1731 | vereq(a - 0, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1732 | veris((a * 1).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1733 | vereq(a * 1, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1734 | veris((a / 1).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1735 | vereq(a / 1, base) |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1736 | |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1737 | class madtuple(tuple): |
| 1738 | _rev = None |
| 1739 | def rev(self): |
| 1740 | if self._rev is not None: |
| 1741 | return self._rev |
| 1742 | L = list(self) |
| 1743 | L.reverse() |
| 1744 | self._rev = self.__class__(L) |
| 1745 | return self._rev |
| 1746 | 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] | 1747 | vereq(a, (1,2,3,4,5,6,7,8,9,0)) |
| 1748 | vereq(a.rev(), madtuple((0,9,8,7,6,5,4,3,2,1))) |
| 1749 | 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] | 1750 | for i in range(512): |
| 1751 | t = madtuple(range(i)) |
| 1752 | u = t.rev() |
| 1753 | v = u.rev() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1754 | vereq(v, t) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1755 | a = madtuple((1,2,3,4,5)) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1756 | vereq(tuple(a), (1,2,3,4,5)) |
Tim Peters | 4c3a0a3 | 2001-09-10 23:37:46 +0000 | [diff] [blame] | 1757 | verify(tuple(a).__class__ is tuple) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1758 | vereq(hash(a), hash((1,2,3,4,5))) |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 1759 | verify(a[:].__class__ is tuple) |
| 1760 | verify((a * 1).__class__ is tuple) |
| 1761 | verify((a * 0).__class__ is tuple) |
| 1762 | verify((a + ()).__class__ is tuple) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1763 | a = madtuple(()) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1764 | vereq(tuple(a), ()) |
Guido van Rossum | 779ce4a | 2001-09-11 14:02:22 +0000 | [diff] [blame] | 1765 | verify(tuple(a).__class__ is tuple) |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 1766 | verify((a + a).__class__ is tuple) |
| 1767 | verify((a * 0).__class__ is tuple) |
| 1768 | verify((a * 1).__class__ is tuple) |
| 1769 | verify((a * 2).__class__ is tuple) |
| 1770 | verify(a[:].__class__ is tuple) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1771 | |
| 1772 | class madstring(str): |
| 1773 | _rev = None |
| 1774 | def rev(self): |
| 1775 | if self._rev is not None: |
| 1776 | return self._rev |
| 1777 | L = list(self) |
| 1778 | L.reverse() |
| 1779 | self._rev = self.__class__("".join(L)) |
| 1780 | return self._rev |
| 1781 | s = madstring("abcdefghijklmnopqrstuvwxyz") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1782 | vereq(s, "abcdefghijklmnopqrstuvwxyz") |
| 1783 | vereq(s.rev(), madstring("zyxwvutsrqponmlkjihgfedcba")) |
| 1784 | vereq(s.rev().rev(), madstring("abcdefghijklmnopqrstuvwxyz")) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1785 | for i in range(256): |
| 1786 | s = madstring("".join(map(chr, range(i)))) |
| 1787 | t = s.rev() |
| 1788 | u = t.rev() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1789 | vereq(u, s) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1790 | s = madstring("12345") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1791 | vereq(str(s), "12345") |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 1792 | verify(str(s).__class__ is str) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1793 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1794 | base = "\x00" * 5 |
| 1795 | s = madstring(base) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1796 | vereq(s, base) |
| 1797 | vereq(str(s), base) |
Tim Peters | c636f56 | 2001-09-11 01:52:02 +0000 | [diff] [blame] | 1798 | verify(str(s).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1799 | vereq(hash(s), hash(base)) |
| 1800 | vereq({s: 1}[base], 1) |
| 1801 | vereq({base: 1}[s], 1) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1802 | verify((s + "").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1803 | vereq(s + "", base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1804 | verify(("" + s).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1805 | vereq("" + s, base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1806 | verify((s * 0).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1807 | vereq(s * 0, "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1808 | verify((s * 1).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1809 | vereq(s * 1, base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1810 | verify((s * 2).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1811 | vereq(s * 2, base + base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1812 | verify(s[:].__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1813 | vereq(s[:], base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1814 | verify(s[0:0].__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1815 | vereq(s[0:0], "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1816 | verify(s.strip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1817 | vereq(s.strip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1818 | verify(s.lstrip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1819 | vereq(s.lstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1820 | verify(s.rstrip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1821 | vereq(s.rstrip(), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1822 | identitytab = ''.join([chr(i) for i in range(256)]) |
| 1823 | verify(s.translate(identitytab).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1824 | vereq(s.translate(identitytab), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1825 | verify(s.translate(identitytab, "x").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1826 | vereq(s.translate(identitytab, "x"), base) |
| 1827 | vereq(s.translate(identitytab, "\x00"), "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1828 | verify(s.replace("x", "x").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1829 | vereq(s.replace("x", "x"), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1830 | verify(s.ljust(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1831 | vereq(s.ljust(len(s)), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1832 | verify(s.rjust(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1833 | vereq(s.rjust(len(s)), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1834 | verify(s.center(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1835 | vereq(s.center(len(s)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1836 | verify(s.lower().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1837 | vereq(s.lower(), base) |
Tim Peters | c636f56 | 2001-09-11 01:52:02 +0000 | [diff] [blame] | 1838 | |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1839 | s = madstring("x y") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1840 | vereq(s, "x y") |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1841 | verify(intern(s).__class__ is str) |
| 1842 | verify(intern(s) is intern("x y")) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1843 | vereq(intern(s), "x y") |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1844 | |
| 1845 | i = intern("y x") |
| 1846 | s = madstring("y x") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1847 | vereq(s, i) |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1848 | verify(intern(s).__class__ is str) |
| 1849 | verify(intern(s) is i) |
| 1850 | |
| 1851 | s = madstring(i) |
| 1852 | verify(intern(s).__class__ is str) |
| 1853 | verify(intern(s) is i) |
| 1854 | |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 1855 | class madunicode(unicode): |
| 1856 | _rev = None |
| 1857 | def rev(self): |
| 1858 | if self._rev is not None: |
| 1859 | return self._rev |
| 1860 | L = list(self) |
| 1861 | L.reverse() |
| 1862 | self._rev = self.__class__(u"".join(L)) |
| 1863 | return self._rev |
| 1864 | u = madunicode("ABCDEF") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1865 | vereq(u, u"ABCDEF") |
| 1866 | vereq(u.rev(), madunicode(u"FEDCBA")) |
| 1867 | vereq(u.rev().rev(), madunicode(u"ABCDEF")) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1868 | base = u"12345" |
| 1869 | u = madunicode(base) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1870 | vereq(unicode(u), base) |
Tim Peters | 78e0fc7 | 2001-09-11 03:07:38 +0000 | [diff] [blame] | 1871 | verify(unicode(u).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1872 | vereq(hash(u), hash(base)) |
| 1873 | vereq({u: 1}[base], 1) |
| 1874 | vereq({base: 1}[u], 1) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1875 | verify(u.strip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1876 | vereq(u.strip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1877 | verify(u.lstrip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1878 | vereq(u.lstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1879 | verify(u.rstrip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1880 | vereq(u.rstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1881 | verify(u.replace(u"x", u"x").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1882 | vereq(u.replace(u"x", u"x"), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1883 | verify(u.replace(u"xy", u"xy").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1884 | vereq(u.replace(u"xy", u"xy"), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1885 | verify(u.center(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1886 | vereq(u.center(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1887 | verify(u.ljust(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1888 | vereq(u.ljust(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1889 | verify(u.rjust(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1890 | vereq(u.rjust(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1891 | verify(u.lower().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1892 | vereq(u.lower(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1893 | verify(u.upper().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1894 | vereq(u.upper(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1895 | verify(u.capitalize().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1896 | vereq(u.capitalize(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1897 | verify(u.title().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1898 | vereq(u.title(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1899 | verify((u + u"").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1900 | vereq(u + u"", base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1901 | verify((u"" + u).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1902 | vereq(u"" + u, base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1903 | verify((u * 0).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1904 | vereq(u * 0, u"") |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1905 | verify((u * 1).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1906 | vereq(u * 1, base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1907 | verify((u * 2).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1908 | vereq(u * 2, base + base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1909 | verify(u[:].__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1910 | vereq(u[:], base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1911 | verify(u[0:0].__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1912 | vereq(u[0:0], u"") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 1913 | |
Guido van Rossum | 12b22ff | 2001-10-09 20:36:44 +0000 | [diff] [blame] | 1914 | class sublist(list): |
| 1915 | pass |
| 1916 | a = sublist(range(5)) |
| 1917 | vereq(a, range(5)) |
| 1918 | a.append("hello") |
| 1919 | vereq(a, range(5) + ["hello"]) |
| 1920 | a[5] = 5 |
| 1921 | vereq(a, range(6)) |
| 1922 | a.extend(range(6, 20)) |
| 1923 | vereq(a, range(20)) |
| 1924 | a[-5:] = [] |
| 1925 | vereq(a, range(15)) |
| 1926 | del a[10:15] |
| 1927 | vereq(len(a), 10) |
| 1928 | vereq(a, range(10)) |
| 1929 | vereq(list(a), range(10)) |
| 1930 | vereq(a[0], 0) |
| 1931 | vereq(a[9], 9) |
| 1932 | vereq(a[-10], 0) |
| 1933 | vereq(a[-1], 9) |
| 1934 | vereq(a[:5], range(5)) |
| 1935 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1936 | class CountedInput(file): |
| 1937 | """Counts lines read by self.readline(). |
| 1938 | |
| 1939 | self.lineno is the 0-based ordinal of the last line read, up to |
| 1940 | a maximum of one greater than the number of lines in the file. |
| 1941 | |
| 1942 | self.ateof is true if and only if the final "" line has been read, |
| 1943 | at which point self.lineno stops incrementing, and further calls |
| 1944 | to readline() continue to return "". |
| 1945 | """ |
| 1946 | |
| 1947 | lineno = 0 |
| 1948 | ateof = 0 |
| 1949 | def readline(self): |
| 1950 | if self.ateof: |
| 1951 | return "" |
| 1952 | s = file.readline(self) |
| 1953 | # Next line works too. |
| 1954 | # s = super(CountedInput, self).readline() |
| 1955 | self.lineno += 1 |
| 1956 | if s == "": |
| 1957 | self.ateof = 1 |
| 1958 | return s |
| 1959 | |
Tim Peters | 561f899 | 2001-09-13 19:36:36 +0000 | [diff] [blame] | 1960 | f = file(name=TESTFN, mode='w') |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1961 | lines = ['a\n', 'b\n', 'c\n'] |
| 1962 | try: |
| 1963 | f.writelines(lines) |
| 1964 | f.close() |
| 1965 | f = CountedInput(TESTFN) |
| 1966 | for (i, expected) in zip(range(1, 5) + [4], lines + 2 * [""]): |
| 1967 | got = f.readline() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1968 | vereq(expected, got) |
| 1969 | vereq(f.lineno, i) |
| 1970 | vereq(f.ateof, (i > len(lines))) |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1971 | f.close() |
| 1972 | finally: |
| 1973 | try: |
| 1974 | f.close() |
| 1975 | except: |
| 1976 | pass |
| 1977 | try: |
| 1978 | import os |
| 1979 | os.unlink(TESTFN) |
| 1980 | except: |
| 1981 | pass |
| 1982 | |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 1983 | def keywords(): |
| 1984 | if verbose: |
| 1985 | print "Testing keyword args to basic type constructors ..." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1986 | vereq(int(x=1), 1) |
| 1987 | vereq(float(x=2), 2.0) |
| 1988 | vereq(long(x=3), 3L) |
| 1989 | vereq(complex(imag=42, real=666), complex(666, 42)) |
| 1990 | vereq(str(object=500), '500') |
| 1991 | vereq(unicode(string='abc', errors='strict'), u'abc') |
| 1992 | vereq(tuple(sequence=range(3)), (0, 1, 2)) |
| 1993 | vereq(list(sequence=(0, 1, 2)), range(3)) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 1994 | vereq(dict(items={1: 2}), {1: 2}) |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 1995 | |
| 1996 | for constructor in (int, float, long, complex, str, unicode, |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 1997 | tuple, list, dict, file): |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 1998 | try: |
| 1999 | constructor(bogus_keyword_arg=1) |
| 2000 | except TypeError: |
| 2001 | pass |
| 2002 | else: |
| 2003 | raise TestFailed("expected TypeError from bogus keyword " |
| 2004 | "argument to %r" % constructor) |
Tim Peters | 561f899 | 2001-09-13 19:36:36 +0000 | [diff] [blame] | 2005 | |
Tim Peters | 8fa4567 | 2001-09-13 21:01:29 +0000 | [diff] [blame] | 2006 | def restricted(): |
| 2007 | import rexec |
| 2008 | if verbose: |
| 2009 | print "Testing interaction with restricted execution ..." |
| 2010 | |
| 2011 | sandbox = rexec.RExec() |
| 2012 | |
| 2013 | code1 = """f = open(%r, 'w')""" % TESTFN |
| 2014 | code2 = """f = file(%r, 'w')""" % TESTFN |
| 2015 | code3 = """\ |
| 2016 | f = open(%r) |
| 2017 | t = type(f) # a sneaky way to get the file() constructor |
| 2018 | f.close() |
| 2019 | f = t(%r, 'w') # rexec can't catch this by itself |
| 2020 | """ % (TESTFN, TESTFN) |
| 2021 | |
| 2022 | f = open(TESTFN, 'w') # Create the file so code3 can find it. |
| 2023 | f.close() |
| 2024 | |
| 2025 | try: |
| 2026 | for code in code1, code2, code3: |
| 2027 | try: |
| 2028 | sandbox.r_exec(code) |
| 2029 | except IOError, msg: |
| 2030 | if str(msg).find("restricted") >= 0: |
| 2031 | outcome = "OK" |
| 2032 | else: |
| 2033 | outcome = "got an exception, but not an expected one" |
| 2034 | else: |
| 2035 | outcome = "expected a restricted-execution exception" |
| 2036 | |
| 2037 | if outcome != "OK": |
| 2038 | raise TestFailed("%s, in %r" % (outcome, code)) |
| 2039 | |
| 2040 | finally: |
| 2041 | try: |
| 2042 | import os |
| 2043 | os.unlink(TESTFN) |
| 2044 | except: |
| 2045 | pass |
| 2046 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2047 | def str_subclass_as_dict_key(): |
| 2048 | if verbose: |
| 2049 | print "Testing a str subclass used as dict key .." |
| 2050 | |
| 2051 | class cistr(str): |
| 2052 | """Sublcass of str that computes __eq__ case-insensitively. |
| 2053 | |
| 2054 | Also computes a hash code of the string in canonical form. |
| 2055 | """ |
| 2056 | |
| 2057 | def __init__(self, value): |
| 2058 | self.canonical = value.lower() |
| 2059 | self.hashcode = hash(self.canonical) |
| 2060 | |
| 2061 | def __eq__(self, other): |
| 2062 | if not isinstance(other, cistr): |
| 2063 | other = cistr(other) |
| 2064 | return self.canonical == other.canonical |
| 2065 | |
| 2066 | def __hash__(self): |
| 2067 | return self.hashcode |
| 2068 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2069 | vereq(cistr('ABC'), 'abc') |
| 2070 | vereq('aBc', cistr('ABC')) |
| 2071 | vereq(str(cistr('ABC')), 'ABC') |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2072 | |
| 2073 | d = {cistr('one'): 1, cistr('two'): 2, cistr('tHree'): 3} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2074 | vereq(d[cistr('one')], 1) |
| 2075 | vereq(d[cistr('tWo')], 2) |
| 2076 | vereq(d[cistr('THrEE')], 3) |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2077 | verify(cistr('ONe') in d) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2078 | vereq(d.get(cistr('thrEE')), 3) |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2079 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 2080 | def classic_comparisons(): |
| 2081 | if verbose: print "Testing classic comparisons..." |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2082 | class classic: |
| 2083 | pass |
| 2084 | for base in (classic, int, object): |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 2085 | if verbose: print " (base = %s)" % base |
| 2086 | class C(base): |
| 2087 | def __init__(self, value): |
| 2088 | self.value = int(value) |
| 2089 | def __cmp__(self, other): |
| 2090 | if isinstance(other, C): |
| 2091 | return cmp(self.value, other.value) |
| 2092 | if isinstance(other, int) or isinstance(other, long): |
| 2093 | return cmp(self.value, other) |
| 2094 | return NotImplemented |
| 2095 | c1 = C(1) |
| 2096 | c2 = C(2) |
| 2097 | c3 = C(3) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2098 | vereq(c1, 1) |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 2099 | c = {1: c1, 2: c2, 3: c3} |
| 2100 | for x in 1, 2, 3: |
| 2101 | for y in 1, 2, 3: |
| 2102 | verify(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 2103 | for op in "<", "<=", "==", "!=", ">", ">=": |
| 2104 | verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op), |
| 2105 | "x=%d, y=%d" % (x, y)) |
| 2106 | verify(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 2107 | verify(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 2108 | |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2109 | def rich_comparisons(): |
| 2110 | if verbose: |
| 2111 | print "Testing rich comparisons..." |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 2112 | class Z(complex): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2113 | pass |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 2114 | z = Z(1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2115 | vereq(z, 1+0j) |
| 2116 | vereq(1+0j, z) |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 2117 | class ZZ(complex): |
| 2118 | def __eq__(self, other): |
| 2119 | try: |
| 2120 | return abs(self - other) <= 1e-6 |
| 2121 | except: |
| 2122 | return NotImplemented |
| 2123 | zz = ZZ(1.0000003) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2124 | vereq(zz, 1+0j) |
| 2125 | vereq(1+0j, zz) |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 2126 | |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2127 | class classic: |
| 2128 | pass |
| 2129 | for base in (classic, int, object, list): |
| 2130 | if verbose: print " (base = %s)" % base |
| 2131 | class C(base): |
| 2132 | def __init__(self, value): |
| 2133 | self.value = int(value) |
| 2134 | def __cmp__(self, other): |
| 2135 | raise TestFailed, "shouldn't call __cmp__" |
| 2136 | def __eq__(self, other): |
| 2137 | if isinstance(other, C): |
| 2138 | return self.value == other.value |
| 2139 | if isinstance(other, int) or isinstance(other, long): |
| 2140 | return self.value == other |
| 2141 | return NotImplemented |
| 2142 | def __ne__(self, other): |
| 2143 | if isinstance(other, C): |
| 2144 | return self.value != other.value |
| 2145 | if isinstance(other, int) or isinstance(other, long): |
| 2146 | return self.value != other |
| 2147 | return NotImplemented |
| 2148 | def __lt__(self, other): |
| 2149 | if isinstance(other, C): |
| 2150 | return self.value < other.value |
| 2151 | if isinstance(other, int) or isinstance(other, long): |
| 2152 | return self.value < other |
| 2153 | return NotImplemented |
| 2154 | def __le__(self, other): |
| 2155 | if isinstance(other, C): |
| 2156 | return self.value <= other.value |
| 2157 | if isinstance(other, int) or isinstance(other, long): |
| 2158 | return self.value <= other |
| 2159 | return NotImplemented |
| 2160 | def __gt__(self, other): |
| 2161 | if isinstance(other, C): |
| 2162 | return self.value > other.value |
| 2163 | if isinstance(other, int) or isinstance(other, long): |
| 2164 | return self.value > other |
| 2165 | return NotImplemented |
| 2166 | def __ge__(self, other): |
| 2167 | if isinstance(other, C): |
| 2168 | return self.value >= other.value |
| 2169 | if isinstance(other, int) or isinstance(other, long): |
| 2170 | return self.value >= other |
| 2171 | return NotImplemented |
| 2172 | c1 = C(1) |
| 2173 | c2 = C(2) |
| 2174 | c3 = C(3) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2175 | vereq(c1, 1) |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2176 | c = {1: c1, 2: c2, 3: c3} |
| 2177 | for x in 1, 2, 3: |
| 2178 | for y in 1, 2, 3: |
| 2179 | for op in "<", "<=", "==", "!=", ">", ">=": |
| 2180 | verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op), |
| 2181 | "x=%d, y=%d" % (x, y)) |
| 2182 | verify(eval("c[x] %s y" % op) == eval("x %s y" % op), |
| 2183 | "x=%d, y=%d" % (x, y)) |
| 2184 | verify(eval("x %s c[y]" % op) == eval("x %s y" % op), |
| 2185 | "x=%d, y=%d" % (x, y)) |
| 2186 | |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2187 | def coercions(): |
| 2188 | if verbose: print "Testing coercions..." |
| 2189 | class I(int): pass |
| 2190 | coerce(I(0), 0) |
| 2191 | coerce(0, I(0)) |
| 2192 | class L(long): pass |
| 2193 | coerce(L(0), 0) |
| 2194 | coerce(L(0), 0L) |
| 2195 | coerce(0, L(0)) |
| 2196 | coerce(0L, L(0)) |
| 2197 | class F(float): pass |
| 2198 | coerce(F(0), 0) |
| 2199 | coerce(F(0), 0L) |
| 2200 | coerce(F(0), 0.) |
| 2201 | coerce(0, F(0)) |
| 2202 | coerce(0L, F(0)) |
| 2203 | coerce(0., F(0)) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2204 | class C(complex): pass |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2205 | coerce(C(0), 0) |
| 2206 | coerce(C(0), 0L) |
| 2207 | coerce(C(0), 0.) |
| 2208 | coerce(C(0), 0j) |
| 2209 | coerce(0, C(0)) |
| 2210 | coerce(0L, C(0)) |
| 2211 | coerce(0., C(0)) |
| 2212 | coerce(0j, C(0)) |
| 2213 | |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2214 | def descrdoc(): |
| 2215 | if verbose: print "Testing descriptor doc strings..." |
| 2216 | def check(descr, what): |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2217 | vereq(descr.__doc__, what) |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2218 | check(file.closed, "flag set if the file is closed") # getset descriptor |
| 2219 | check(file.name, "file name") # member descriptor |
| 2220 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2221 | def setclass(): |
| 2222 | if verbose: print "Testing __class__ assignment..." |
| 2223 | class C(object): pass |
| 2224 | class D(object): pass |
| 2225 | class E(object): pass |
| 2226 | class F(D, E): pass |
| 2227 | for cls in C, D, E, F: |
| 2228 | for cls2 in C, D, E, F: |
| 2229 | x = cls() |
| 2230 | x.__class__ = cls2 |
| 2231 | verify(x.__class__ is cls2) |
| 2232 | x.__class__ = cls |
| 2233 | verify(x.__class__ is cls) |
| 2234 | def cant(x, C): |
| 2235 | try: |
| 2236 | x.__class__ = C |
| 2237 | except TypeError: |
| 2238 | pass |
| 2239 | else: |
| 2240 | raise TestFailed, "shouldn't allow %r.__class__ = %r" % (x, C) |
| 2241 | cant(C(), list) |
| 2242 | cant(list(), C) |
| 2243 | cant(C(), 1) |
| 2244 | cant(C(), object) |
| 2245 | cant(object(), list) |
| 2246 | cant(list(), object) |
| 2247 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 2248 | def setdict(): |
| 2249 | if verbose: print "Testing __dict__ assignment..." |
| 2250 | class C(object): pass |
| 2251 | a = C() |
| 2252 | a.__dict__ = {'b': 1} |
| 2253 | vereq(a.b, 1) |
| 2254 | def cant(x, dict): |
| 2255 | try: |
| 2256 | x.__dict__ = dict |
| 2257 | except TypeError: |
| 2258 | pass |
| 2259 | else: |
| 2260 | raise TestFailed, "shouldn't allow %r.__dict__ = %r" % (x, dict) |
| 2261 | cant(a, None) |
| 2262 | cant(a, []) |
| 2263 | cant(a, 1) |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame^] | 2264 | del a.__dict__ # Deleting __dict__ is allowed |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 2265 | # Classes don't allow __dict__ assignment |
| 2266 | cant(C, {}) |
| 2267 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2268 | def pickles(): |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2269 | if verbose: |
| 2270 | print "Testing pickling and copying new-style classes and objects..." |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2271 | import pickle, cPickle |
| 2272 | |
| 2273 | def sorteditems(d): |
| 2274 | L = d.items() |
| 2275 | L.sort() |
| 2276 | return L |
| 2277 | |
| 2278 | global C |
| 2279 | class C(object): |
| 2280 | def __init__(self, a, b): |
| 2281 | super(C, self).__init__() |
| 2282 | self.a = a |
| 2283 | self.b = b |
| 2284 | def __repr__(self): |
| 2285 | return "C(%r, %r)" % (self.a, self.b) |
| 2286 | |
| 2287 | global C1 |
| 2288 | class C1(list): |
| 2289 | def __new__(cls, a, b): |
| 2290 | return super(C1, cls).__new__(cls) |
| 2291 | def __init__(self, a, b): |
| 2292 | self.a = a |
| 2293 | self.b = b |
| 2294 | def __repr__(self): |
| 2295 | return "C1(%r, %r)<%r>" % (self.a, self.b, list(self)) |
| 2296 | |
| 2297 | global C2 |
| 2298 | class C2(int): |
| 2299 | def __new__(cls, a, b, val=0): |
| 2300 | return super(C2, cls).__new__(cls, val) |
| 2301 | def __init__(self, a, b, val=0): |
| 2302 | self.a = a |
| 2303 | self.b = b |
| 2304 | def __repr__(self): |
| 2305 | return "C2(%r, %r)<%r>" % (self.a, self.b, int(self)) |
| 2306 | |
Guido van Rossum | 90c4514 | 2001-11-24 21:07:01 +0000 | [diff] [blame] | 2307 | global C3 |
| 2308 | class C3(object): |
| 2309 | def __init__(self, foo): |
| 2310 | self.foo = foo |
| 2311 | def __getstate__(self): |
| 2312 | return self.foo |
| 2313 | def __setstate__(self, foo): |
| 2314 | self.foo = foo |
| 2315 | |
| 2316 | global C4classic, C4 |
| 2317 | class C4classic: # classic |
| 2318 | pass |
| 2319 | class C4(C4classic, object): # mixed inheritance |
| 2320 | pass |
| 2321 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2322 | for p in pickle, cPickle: |
| 2323 | for bin in 0, 1: |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2324 | if verbose: |
| 2325 | print p.__name__, ["text", "binary"][bin] |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2326 | |
| 2327 | for cls in C, C1, C2: |
| 2328 | s = p.dumps(cls, bin) |
| 2329 | cls2 = p.loads(s) |
| 2330 | verify(cls2 is cls) |
| 2331 | |
| 2332 | a = C1(1, 2); a.append(42); a.append(24) |
| 2333 | b = C2("hello", "world", 42) |
| 2334 | s = p.dumps((a, b), bin) |
| 2335 | x, y = p.loads(s) |
Guido van Rossum | 90c4514 | 2001-11-24 21:07:01 +0000 | [diff] [blame] | 2336 | vereq(x.__class__, a.__class__) |
| 2337 | vereq(sorteditems(x.__dict__), sorteditems(a.__dict__)) |
| 2338 | vereq(y.__class__, b.__class__) |
| 2339 | vereq(sorteditems(y.__dict__), sorteditems(b.__dict__)) |
| 2340 | vereq(`x`, `a`) |
| 2341 | vereq(`y`, `b`) |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2342 | if verbose: |
| 2343 | print "a = x =", a |
| 2344 | print "b = y =", b |
Guido van Rossum | 90c4514 | 2001-11-24 21:07:01 +0000 | [diff] [blame] | 2345 | # Test for __getstate__ and __setstate__ on new style class |
| 2346 | u = C3(42) |
| 2347 | s = p.dumps(u, bin) |
| 2348 | v = p.loads(s) |
| 2349 | veris(u.__class__, v.__class__) |
| 2350 | vereq(u.foo, v.foo) |
| 2351 | # Test for picklability of hybrid class |
| 2352 | u = C4() |
| 2353 | u.foo = 42 |
| 2354 | s = p.dumps(u, bin) |
| 2355 | v = p.loads(s) |
| 2356 | veris(u.__class__, v.__class__) |
| 2357 | vereq(u.foo, v.foo) |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2358 | |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2359 | # Testing copy.deepcopy() |
| 2360 | if verbose: |
| 2361 | print "deepcopy" |
| 2362 | import copy |
| 2363 | for cls in C, C1, C2: |
| 2364 | cls2 = copy.deepcopy(cls) |
| 2365 | verify(cls2 is cls) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2366 | |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2367 | a = C1(1, 2); a.append(42); a.append(24) |
| 2368 | b = C2("hello", "world", 42) |
| 2369 | x, y = copy.deepcopy((a, b)) |
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 | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2376 | if verbose: |
| 2377 | print "a = x =", a |
| 2378 | print "b = y =", b |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2379 | |
| 2380 | def copies(): |
| 2381 | if verbose: print "Testing copy.copy() and copy.deepcopy()..." |
| 2382 | import copy |
| 2383 | class C(object): |
| 2384 | pass |
| 2385 | |
| 2386 | a = C() |
| 2387 | a.foo = 12 |
| 2388 | b = copy.copy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2389 | vereq(b.__dict__, a.__dict__) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2390 | |
| 2391 | a.bar = [1,2,3] |
| 2392 | c = copy.copy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2393 | vereq(c.bar, a.bar) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2394 | verify(c.bar is a.bar) |
| 2395 | |
| 2396 | d = copy.deepcopy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2397 | vereq(d.__dict__, a.__dict__) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2398 | a.bar.append(4) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2399 | vereq(d.bar, [1,2,3]) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2400 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2401 | def binopoverride(): |
| 2402 | if verbose: print "Testing overrides of binary operations..." |
| 2403 | class I(int): |
| 2404 | def __repr__(self): |
| 2405 | return "I(%r)" % int(self) |
| 2406 | def __add__(self, other): |
| 2407 | return I(int(self) + int(other)) |
| 2408 | __radd__ = __add__ |
| 2409 | def __pow__(self, other, mod=None): |
| 2410 | if mod is None: |
| 2411 | return I(pow(int(self), int(other))) |
| 2412 | else: |
| 2413 | return I(pow(int(self), int(other), int(mod))) |
| 2414 | def __rpow__(self, other, mod=None): |
| 2415 | if mod is None: |
| 2416 | return I(pow(int(other), int(self), mod)) |
| 2417 | else: |
| 2418 | return I(pow(int(other), int(self), int(mod))) |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2419 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2420 | vereq(`I(1) + I(2)`, "I(3)") |
| 2421 | vereq(`I(1) + 2`, "I(3)") |
| 2422 | vereq(`1 + I(2)`, "I(3)") |
| 2423 | vereq(`I(2) ** I(3)`, "I(8)") |
| 2424 | vereq(`2 ** I(3)`, "I(8)") |
| 2425 | vereq(`I(2) ** 3`, "I(8)") |
| 2426 | vereq(`pow(I(2), I(3), I(5))`, "I(3)") |
| 2427 | class S(str): |
| 2428 | def __eq__(self, other): |
| 2429 | return self.lower() == other.lower() |
| 2430 | |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2431 | def subclasspropagation(): |
| 2432 | if verbose: print "Testing propagation of slot functions to subclasses..." |
| 2433 | class A(object): |
| 2434 | pass |
| 2435 | class B(A): |
| 2436 | pass |
| 2437 | class C(A): |
| 2438 | pass |
| 2439 | class D(B, C): |
| 2440 | pass |
| 2441 | d = D() |
| 2442 | vereq(hash(d), id(d)) |
| 2443 | A.__hash__ = lambda self: 42 |
| 2444 | vereq(hash(d), 42) |
| 2445 | C.__hash__ = lambda self: 314 |
| 2446 | vereq(hash(d), 314) |
| 2447 | B.__hash__ = lambda self: 144 |
| 2448 | vereq(hash(d), 144) |
| 2449 | D.__hash__ = lambda self: 100 |
| 2450 | vereq(hash(d), 100) |
| 2451 | del D.__hash__ |
| 2452 | vereq(hash(d), 144) |
| 2453 | del B.__hash__ |
| 2454 | vereq(hash(d), 314) |
| 2455 | del C.__hash__ |
| 2456 | vereq(hash(d), 42) |
| 2457 | del A.__hash__ |
| 2458 | vereq(hash(d), id(d)) |
| 2459 | d.foo = 42 |
| 2460 | d.bar = 42 |
| 2461 | vereq(d.foo, 42) |
| 2462 | vereq(d.bar, 42) |
| 2463 | def __getattribute__(self, name): |
| 2464 | if name == "foo": |
| 2465 | return 24 |
| 2466 | return object.__getattribute__(self, name) |
| 2467 | A.__getattribute__ = __getattribute__ |
| 2468 | vereq(d.foo, 24) |
| 2469 | vereq(d.bar, 42) |
| 2470 | def __getattr__(self, name): |
| 2471 | if name in ("spam", "foo", "bar"): |
| 2472 | return "hello" |
| 2473 | raise AttributeError, name |
| 2474 | B.__getattr__ = __getattr__ |
| 2475 | vereq(d.spam, "hello") |
| 2476 | vereq(d.foo, 24) |
| 2477 | vereq(d.bar, 42) |
| 2478 | del A.__getattribute__ |
| 2479 | vereq(d.foo, 42) |
| 2480 | del d.foo |
| 2481 | vereq(d.foo, "hello") |
| 2482 | vereq(d.bar, 42) |
| 2483 | del B.__getattr__ |
| 2484 | try: |
| 2485 | d.foo |
| 2486 | except AttributeError: |
| 2487 | pass |
| 2488 | else: |
| 2489 | raise TestFailed, "d.foo should be undefined now" |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 2490 | |
| 2491 | def buffer_inherit(): |
| 2492 | import binascii |
| 2493 | # SF bug [#470040] ParseTuple t# vs subclasses. |
| 2494 | if verbose: |
| 2495 | print "Testing that buffer interface is inherited ..." |
| 2496 | |
| 2497 | class MyStr(str): |
| 2498 | pass |
| 2499 | base = 'abc' |
| 2500 | m = MyStr(base) |
| 2501 | # b2a_hex uses the buffer interface to get its argument's value, via |
| 2502 | # PyArg_ParseTuple 't#' code. |
| 2503 | vereq(binascii.b2a_hex(m), binascii.b2a_hex(base)) |
| 2504 | |
| 2505 | # It's not clear that unicode will continue to support the character |
| 2506 | # buffer interface, and this test will fail if that's taken away. |
| 2507 | class MyUni(unicode): |
| 2508 | pass |
| 2509 | base = u'abc' |
| 2510 | m = MyUni(base) |
| 2511 | vereq(binascii.b2a_hex(m), binascii.b2a_hex(base)) |
| 2512 | |
| 2513 | class MyInt(int): |
| 2514 | pass |
| 2515 | m = MyInt(42) |
| 2516 | try: |
| 2517 | binascii.b2a_hex(m) |
| 2518 | raise TestFailed('subclass of int should not have a buffer interface') |
| 2519 | except TypeError: |
| 2520 | pass |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2521 | |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 2522 | def str_of_str_subclass(): |
| 2523 | import binascii |
| 2524 | import cStringIO |
| 2525 | |
| 2526 | if verbose: |
| 2527 | print "Testing __str__ defined in subclass of str ..." |
| 2528 | |
| 2529 | class octetstring(str): |
| 2530 | def __str__(self): |
| 2531 | return binascii.b2a_hex(self) |
| 2532 | def __repr__(self): |
| 2533 | return self + " repr" |
| 2534 | |
| 2535 | o = octetstring('A') |
| 2536 | vereq(type(o), octetstring) |
| 2537 | vereq(type(str(o)), str) |
| 2538 | vereq(type(repr(o)), str) |
| 2539 | vereq(ord(o), 0x41) |
| 2540 | vereq(str(o), '41') |
| 2541 | vereq(repr(o), 'A repr') |
| 2542 | vereq(o.__str__(), '41') |
| 2543 | vereq(o.__repr__(), 'A repr') |
| 2544 | |
| 2545 | capture = cStringIO.StringIO() |
| 2546 | # Calling str() or not exercises different internal paths. |
| 2547 | print >> capture, o |
| 2548 | print >> capture, str(o) |
| 2549 | vereq(capture.getvalue(), '41\n41\n') |
| 2550 | capture.close() |
| 2551 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2552 | def kwdargs(): |
| 2553 | if verbose: print "Testing keyword arguments to __init__, __call__..." |
| 2554 | def f(a): return a |
| 2555 | vereq(f.__call__(a=42), 42) |
| 2556 | a = [] |
| 2557 | list.__init__(a, sequence=[0, 1, 2]) |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2558 | vereq(a, [0, 1, 2]) |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2559 | |
Guido van Rossum | ed87ad8 | 2001-10-30 02:33:02 +0000 | [diff] [blame] | 2560 | def delhook(): |
| 2561 | if verbose: print "Testing __del__ hook..." |
| 2562 | log = [] |
| 2563 | class C(object): |
| 2564 | def __del__(self): |
| 2565 | log.append(1) |
| 2566 | c = C() |
| 2567 | vereq(log, []) |
| 2568 | del c |
| 2569 | vereq(log, [1]) |
| 2570 | |
Guido van Rossum | dbb53d9 | 2001-12-03 16:32:18 +0000 | [diff] [blame] | 2571 | def hashinherit(): |
| 2572 | if verbose: print "Testing hash of mutable subclasses..." |
| 2573 | |
| 2574 | class mydict(dict): |
| 2575 | pass |
| 2576 | d = mydict() |
| 2577 | try: |
| 2578 | hash(d) |
| 2579 | except TypeError: |
| 2580 | pass |
| 2581 | else: |
| 2582 | raise TestFailed, "hash() of dict subclass should fail" |
| 2583 | |
| 2584 | class mylist(list): |
| 2585 | pass |
| 2586 | d = mylist() |
| 2587 | try: |
| 2588 | hash(d) |
| 2589 | except TypeError: |
| 2590 | pass |
| 2591 | else: |
| 2592 | raise TestFailed, "hash() of list subclass should fail" |
| 2593 | |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 2594 | def test_main(): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2595 | class_docstrings() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2596 | lists() |
| 2597 | dicts() |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2598 | dict_constructor() |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 2599 | test_dir() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2600 | ints() |
| 2601 | longs() |
| 2602 | floats() |
| 2603 | complexes() |
| 2604 | spamlists() |
| 2605 | spamdicts() |
| 2606 | pydicts() |
| 2607 | pylists() |
| 2608 | metaclass() |
| 2609 | pymods() |
| 2610 | multi() |
| 2611 | diamond() |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 2612 | objects() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2613 | slots() |
| 2614 | dynamics() |
| 2615 | errors() |
| 2616 | classmethods() |
| 2617 | staticmethods() |
| 2618 | classic() |
| 2619 | compattr() |
| 2620 | newslot() |
| 2621 | altmro() |
| 2622 | overloading() |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 2623 | methods() |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 2624 | specials() |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 2625 | weakrefs() |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 2626 | properties() |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 2627 | supers() |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2628 | inherits() |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 2629 | keywords() |
Tim Peters | 8fa4567 | 2001-09-13 21:01:29 +0000 | [diff] [blame] | 2630 | restricted() |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2631 | str_subclass_as_dict_key() |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 2632 | classic_comparisons() |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2633 | rich_comparisons() |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2634 | coercions() |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2635 | descrdoc() |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2636 | setclass() |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 2637 | setdict() |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2638 | pickles() |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2639 | copies() |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2640 | binopoverride() |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2641 | subclasspropagation() |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 2642 | buffer_inherit() |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 2643 | str_of_str_subclass() |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2644 | kwdargs() |
Guido van Rossum | ed87ad8 | 2001-10-30 02:33:02 +0000 | [diff] [blame] | 2645 | delhook() |
Guido van Rossum | dbb53d9 | 2001-12-03 16:32:18 +0000 | [diff] [blame] | 2646 | hashinherit() |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 2647 | if verbose: print "All OK" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2648 | |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 2649 | if __name__ == "__main__": |
| 2650 | test_main() |