Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 1 | # Test enhancements related to descriptors and new-style classes |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 3 | from test_support import verify, verbose, TestFailed, TESTFN |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4 | from copy import deepcopy |
| 5 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 6 | def vereq(a, b): |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 7 | if not (a == b): |
| 8 | raise TestFailed, "%r == %r" % (a, b) |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 10 | def veris(a, b): |
| 11 | if a is not b: |
| 12 | raise TestFailed, "%r is %r" % (a, b) |
| 13 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14 | def testunop(a, res, expr="len(a)", meth="__len__"): |
| 15 | if verbose: print "checking", expr |
| 16 | dict = {'a': a} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 17 | vereq(eval(expr, dict), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 18 | t = type(a) |
| 19 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 20 | while meth not in t.__dict__: |
| 21 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 22 | vereq(m, t.__dict__[meth]) |
| 23 | vereq(m(a), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 24 | bm = getattr(a, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 25 | vereq(bm(), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 26 | |
| 27 | def testbinop(a, b, res, expr="a+b", meth="__add__"): |
| 28 | if verbose: print "checking", expr |
| 29 | dict = {'a': a, 'b': b} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 30 | vereq(eval(expr, dict), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 31 | t = type(a) |
| 32 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 33 | while meth not in t.__dict__: |
| 34 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 35 | vereq(m, t.__dict__[meth]) |
| 36 | vereq(m(a, b), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 37 | bm = getattr(a, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 38 | vereq(bm(b), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 39 | |
| 40 | def testternop(a, b, c, res, expr="a[b:c]", meth="__getslice__"): |
| 41 | if verbose: print "checking", expr |
| 42 | dict = {'a': a, 'b': b, 'c': c} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 43 | vereq(eval(expr, dict), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 44 | t = type(a) |
| 45 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 46 | while meth not in t.__dict__: |
| 47 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 48 | vereq(m, t.__dict__[meth]) |
| 49 | vereq(m(a, b, c), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 50 | bm = getattr(a, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 51 | vereq(bm(b, c), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 52 | |
| 53 | def testsetop(a, b, res, stmt="a+=b", meth="__iadd__"): |
| 54 | if verbose: print "checking", stmt |
| 55 | dict = {'a': deepcopy(a), 'b': b} |
| 56 | exec stmt in dict |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 57 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 58 | t = type(a) |
| 59 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 60 | while meth not in t.__dict__: |
| 61 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 62 | vereq(m, t.__dict__[meth]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 63 | dict['a'] = deepcopy(a) |
| 64 | m(dict['a'], 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 | dict['a'] = deepcopy(a) |
| 67 | bm = getattr(dict['a'], meth) |
| 68 | bm(b) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 69 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 70 | |
| 71 | def testset2op(a, b, c, res, stmt="a[b]=c", meth="__setitem__"): |
| 72 | if verbose: print "checking", stmt |
| 73 | dict = {'a': deepcopy(a), 'b': b, 'c': c} |
| 74 | exec stmt in dict |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 75 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 76 | t = type(a) |
| 77 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 78 | while meth not in t.__dict__: |
| 79 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 80 | vereq(m, t.__dict__[meth]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 81 | dict['a'] = deepcopy(a) |
| 82 | m(dict['a'], 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 | dict['a'] = deepcopy(a) |
| 85 | bm = getattr(dict['a'], meth) |
| 86 | bm(b, c) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 87 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 88 | |
| 89 | def testset3op(a, b, c, d, res, stmt="a[b:c]=d", meth="__setslice__"): |
| 90 | if verbose: print "checking", stmt |
| 91 | dict = {'a': deepcopy(a), 'b': b, 'c': c, 'd': d} |
| 92 | exec stmt in dict |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 93 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 94 | t = type(a) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 95 | while meth not in t.__dict__: |
| 96 | t = t.__bases__[0] |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 97 | m = getattr(t, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 98 | vereq(m, t.__dict__[meth]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 99 | dict['a'] = deepcopy(a) |
| 100 | m(dict['a'], 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 | dict['a'] = deepcopy(a) |
| 103 | bm = getattr(dict['a'], meth) |
| 104 | bm(b, c, d) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 105 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 106 | |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 107 | def class_docstrings(): |
| 108 | class Classic: |
| 109 | "A classic docstring." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 110 | vereq(Classic.__doc__, "A classic docstring.") |
| 111 | vereq(Classic.__dict__['__doc__'], "A classic docstring.") |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 112 | |
| 113 | class Classic2: |
| 114 | pass |
| 115 | verify(Classic2.__doc__ is None) |
| 116 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 117 | class NewStatic(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 118 | "Another docstring." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 119 | vereq(NewStatic.__doc__, "Another docstring.") |
| 120 | vereq(NewStatic.__dict__['__doc__'], "Another docstring.") |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 121 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 122 | class NewStatic2(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 123 | pass |
| 124 | verify(NewStatic2.__doc__ is None) |
| 125 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 126 | class NewDynamic(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 127 | "Another docstring." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 128 | vereq(NewDynamic.__doc__, "Another docstring.") |
| 129 | vereq(NewDynamic.__dict__['__doc__'], "Another docstring.") |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 130 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 131 | class NewDynamic2(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 132 | pass |
| 133 | verify(NewDynamic2.__doc__ is None) |
| 134 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 135 | def lists(): |
| 136 | if verbose: print "Testing list operations..." |
| 137 | testbinop([1], [2], [1,2], "a+b", "__add__") |
| 138 | testbinop([1,2,3], 2, 1, "b in a", "__contains__") |
| 139 | testbinop([1,2,3], 4, 0, "b in a", "__contains__") |
| 140 | testbinop([1,2,3], 1, 2, "a[b]", "__getitem__") |
| 141 | testternop([1,2,3], 0, 2, [1,2], "a[b:c]", "__getslice__") |
| 142 | testsetop([1], [2], [1,2], "a+=b", "__iadd__") |
| 143 | testsetop([1,2], 3, [1,2,1,2,1,2], "a*=b", "__imul__") |
| 144 | testunop([1,2,3], 3, "len(a)", "__len__") |
| 145 | testbinop([1,2], 3, [1,2,1,2,1,2], "a*b", "__mul__") |
| 146 | testbinop([1,2], 3, [1,2,1,2,1,2], "b*a", "__rmul__") |
| 147 | testset2op([1,2], 1, 3, [1,3], "a[b]=c", "__setitem__") |
| 148 | testset3op([1,2,3,4], 1, 3, [5,6], [1,5,6,4], "a[b:c]=d", "__setslice__") |
| 149 | |
| 150 | def dicts(): |
| 151 | if verbose: print "Testing dict operations..." |
| 152 | testbinop({1:2}, {2:1}, -1, "cmp(a,b)", "__cmp__") |
| 153 | testbinop({1:2,3:4}, 1, 1, "b in a", "__contains__") |
| 154 | testbinop({1:2,3:4}, 2, 0, "b in a", "__contains__") |
| 155 | testbinop({1:2,3:4}, 1, 2, "a[b]", "__getitem__") |
| 156 | d = {1:2,3:4} |
| 157 | l1 = [] |
| 158 | for i in d.keys(): l1.append(i) |
| 159 | l = [] |
| 160 | for i in iter(d): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 161 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 162 | l = [] |
| 163 | for i in d.__iter__(): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 164 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 165 | l = [] |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 166 | for i in dict.__iter__(d): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 167 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 168 | d = {1:2, 3:4} |
| 169 | testunop(d, 2, "len(a)", "__len__") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 170 | vereq(eval(repr(d), {}), d) |
| 171 | vereq(eval(d.__repr__(), {}), d) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 172 | testset2op({1:2,3:4}, 2, 3, {1:2,2:3,3:4}, "a[b]=c", "__setitem__") |
| 173 | |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 174 | def dict_constructor(): |
| 175 | if verbose: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 176 | print "Testing dict constructor ..." |
| 177 | d = dict() |
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({}) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 180 | vereq(d, {}) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 181 | d = dict(items={}) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 182 | vereq(d, {}) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 183 | d = dict({1: 2, 'a': 'b'}) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 184 | vereq(d, {1: 2, 'a': 'b'}) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 185 | vereq(d, dict(d.items())) |
| 186 | vereq(d, dict(items=d.iteritems())) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 187 | for badarg in 0, 0L, 0j, "0", [0], (0,): |
| 188 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 189 | dict(badarg) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 190 | except TypeError: |
| 191 | pass |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 192 | except ValueError: |
| 193 | if badarg == "0": |
| 194 | # It's a sequence, and its elements are also sequences (gotta |
| 195 | # love strings <wink>), but they aren't of length 2, so this |
| 196 | # one seemed better as a ValueError than a TypeError. |
| 197 | pass |
| 198 | else: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 199 | raise TestFailed("no TypeError from dict(%r)" % badarg) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 200 | else: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 201 | raise TestFailed("no TypeError from dict(%r)" % badarg) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 202 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 203 | dict(senseless={}) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 204 | except TypeError: |
| 205 | pass |
| 206 | else: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 207 | raise TestFailed("no TypeError from dict(senseless={})") |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 208 | |
| 209 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 210 | dict({}, {}) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 211 | except TypeError: |
| 212 | pass |
| 213 | else: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 214 | raise TestFailed("no TypeError from dict({}, {})") |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 215 | |
| 216 | class Mapping: |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 217 | # Lacks a .keys() method; will be added later. |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 218 | dict = {1:2, 3:4, 'a':1j} |
| 219 | |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 220 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 221 | dict(Mapping()) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 222 | except TypeError: |
| 223 | pass |
| 224 | else: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 225 | raise TestFailed("no TypeError from dict(incomplete mapping)") |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 226 | |
| 227 | Mapping.keys = lambda self: self.dict.keys() |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 228 | Mapping.__getitem__ = lambda self, i: self.dict[i] |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 229 | d = dict(items=Mapping()) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 230 | vereq(d, Mapping.dict) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 231 | |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 232 | # Init from sequence of iterable objects, each producing a 2-sequence. |
| 233 | class AddressBookEntry: |
| 234 | def __init__(self, first, last): |
| 235 | self.first = first |
| 236 | self.last = last |
| 237 | def __iter__(self): |
| 238 | return iter([self.first, self.last]) |
| 239 | |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 240 | d = dict([AddressBookEntry('Tim', 'Warsaw'), |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 241 | AddressBookEntry('Barry', 'Peters'), |
| 242 | AddressBookEntry('Tim', 'Peters'), |
| 243 | AddressBookEntry('Barry', 'Warsaw')]) |
| 244 | vereq(d, {'Barry': 'Warsaw', 'Tim': 'Peters'}) |
| 245 | |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 246 | d = dict(zip(range(4), range(1, 5))) |
| 247 | vereq(d, dict([(i, i+1) for i in range(4)])) |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 248 | |
| 249 | # Bad sequence lengths. |
Tim Peters | 9fda73c | 2001-10-26 20:57:38 +0000 | [diff] [blame] | 250 | for bad in [('tooshort',)], [('too', 'long', 'by 1')]: |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 251 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 252 | dict(bad) |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 253 | except ValueError: |
| 254 | pass |
| 255 | else: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 256 | raise TestFailed("no ValueError from dict(%r)" % bad) |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 257 | |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 258 | def test_dir(): |
| 259 | if verbose: |
| 260 | print "Testing dir() ..." |
| 261 | junk = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 262 | vereq(dir(), ['junk']) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 263 | del junk |
| 264 | |
| 265 | # Just make sure these don't blow up! |
| 266 | for arg in 2, 2L, 2j, 2e0, [2], "2", u"2", (2,), {2:2}, type, test_dir: |
| 267 | dir(arg) |
| 268 | |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 269 | # Try classic classes. |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 270 | class C: |
| 271 | Cdata = 1 |
| 272 | def Cmethod(self): pass |
| 273 | |
| 274 | cstuff = ['Cdata', 'Cmethod', '__doc__', '__module__'] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 275 | vereq(dir(C), cstuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 276 | verify('im_self' in dir(C.Cmethod)) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 277 | |
| 278 | 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] | 279 | vereq(dir(c), cstuff) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 280 | |
| 281 | c.cdata = 2 |
| 282 | c.cmethod = lambda self: 0 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 283 | vereq(dir(c), cstuff + ['cdata', 'cmethod']) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 284 | verify('im_self' in dir(c.Cmethod)) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 285 | |
| 286 | class A(C): |
| 287 | Adata = 1 |
| 288 | def Amethod(self): pass |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 289 | |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 290 | astuff = ['Adata', 'Amethod'] + cstuff |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 291 | vereq(dir(A), astuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 292 | verify('im_self' in dir(A.Amethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 293 | a = A() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 294 | vereq(dir(a), astuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 295 | verify('im_self' in dir(a.Amethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 296 | a.adata = 42 |
| 297 | a.amethod = lambda self: 3 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 298 | vereq(dir(a), astuff + ['adata', 'amethod']) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 299 | |
| 300 | # The same, but with new-style classes. Since these have object as a |
| 301 | # base class, a lot more gets sucked in. |
| 302 | def interesting(strings): |
| 303 | return [s for s in strings if not s.startswith('_')] |
| 304 | |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 305 | class C(object): |
| 306 | Cdata = 1 |
| 307 | def Cmethod(self): pass |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 308 | |
| 309 | cstuff = ['Cdata', 'Cmethod'] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 310 | vereq(interesting(dir(C)), cstuff) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 311 | |
| 312 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 313 | vereq(interesting(dir(c)), cstuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 314 | verify('im_self' in dir(C.Cmethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 315 | |
| 316 | c.cdata = 2 |
| 317 | c.cmethod = lambda self: 0 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 318 | vereq(interesting(dir(c)), cstuff + ['cdata', 'cmethod']) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 319 | verify('im_self' in dir(c.Cmethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 320 | |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 321 | class A(C): |
| 322 | Adata = 1 |
| 323 | def Amethod(self): pass |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 324 | |
| 325 | astuff = ['Adata', 'Amethod'] + cstuff |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 326 | vereq(interesting(dir(A)), astuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 327 | verify('im_self' in dir(A.Amethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 328 | a = A() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 329 | vereq(interesting(dir(a)), astuff) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 330 | a.adata = 42 |
| 331 | a.amethod = lambda self: 3 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 332 | vereq(interesting(dir(a)), astuff + ['adata', 'amethod']) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 333 | verify('im_self' in dir(a.Amethod)) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 334 | |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 335 | # Try a module subclass. |
| 336 | import sys |
| 337 | class M(type(sys)): |
| 338 | pass |
| 339 | minstance = M() |
| 340 | minstance.b = 2 |
| 341 | minstance.a = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 342 | vereq(dir(minstance), ['a', 'b']) |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 343 | |
| 344 | class M2(M): |
| 345 | def getdict(self): |
| 346 | return "Not a dict!" |
| 347 | __dict__ = property(getdict) |
| 348 | |
| 349 | m2instance = M2() |
| 350 | m2instance.b = 2 |
| 351 | m2instance.a = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 352 | vereq(m2instance.__dict__, "Not a dict!") |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 353 | try: |
| 354 | dir(m2instance) |
| 355 | except TypeError: |
| 356 | pass |
| 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: |
| 556 | assert len(a) == 1 |
| 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): |
| 563 | assert 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 | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 832 | def diamond(): |
| 833 | if verbose: print "Testing multiple inheritance special cases..." |
| 834 | class A(object): |
| 835 | def spam(self): return "A" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 836 | vereq(A().spam(), "A") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 837 | class B(A): |
| 838 | def boo(self): return "B" |
| 839 | def spam(self): return "B" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 840 | vereq(B().spam(), "B") |
| 841 | vereq(B().boo(), "B") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 842 | class C(A): |
| 843 | def boo(self): return "C" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 844 | vereq(C().spam(), "A") |
| 845 | vereq(C().boo(), "C") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 846 | class D(B, C): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 847 | vereq(D().spam(), "B") |
| 848 | vereq(D().boo(), "B") |
| 849 | vereq(D.__mro__, (D, B, C, A, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 850 | class E(C, B): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 851 | vereq(E().spam(), "B") |
| 852 | vereq(E().boo(), "C") |
| 853 | vereq(E.__mro__, (E, C, B, A, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 854 | class F(D, E): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 855 | vereq(F().spam(), "B") |
| 856 | vereq(F().boo(), "B") |
| 857 | vereq(F.__mro__, (F, D, E, B, C, A, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 858 | class G(E, D): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 859 | vereq(G().spam(), "B") |
| 860 | vereq(G().boo(), "C") |
| 861 | vereq(G.__mro__, (G, E, D, C, B, A, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 862 | |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 863 | def objects(): |
| 864 | if verbose: print "Testing object class..." |
| 865 | a = object() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 866 | vereq(a.__class__, object) |
| 867 | vereq(type(a), object) |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 868 | b = object() |
| 869 | verify(a is not b) |
| 870 | verify(not hasattr(a, "foo")) |
| 871 | try: |
| 872 | a.foo = 12 |
Guido van Rossum | 6d94627 | 2001-08-10 19:42:38 +0000 | [diff] [blame] | 873 | except (AttributeError, TypeError): |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 874 | pass |
| 875 | else: |
| 876 | verify(0, "object() should not allow setting a foo attribute") |
| 877 | verify(not hasattr(object(), "__dict__")) |
| 878 | |
| 879 | class Cdict(object): |
| 880 | pass |
| 881 | x = Cdict() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 882 | vereq(x.__dict__, {}) |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 883 | x.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 884 | vereq(x.foo, 1) |
| 885 | vereq(x.__dict__, {'foo': 1}) |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 886 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 887 | def slots(): |
| 888 | if verbose: print "Testing __slots__..." |
| 889 | class C0(object): |
| 890 | __slots__ = [] |
| 891 | x = C0() |
| 892 | verify(not hasattr(x, "__dict__")) |
| 893 | verify(not hasattr(x, "foo")) |
| 894 | |
| 895 | class C1(object): |
| 896 | __slots__ = ['a'] |
| 897 | x = C1() |
| 898 | verify(not hasattr(x, "__dict__")) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 899 | vereq(x.a, None) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 900 | x.a = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 901 | vereq(x.a, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 902 | del x.a |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 903 | vereq(x.a, None) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 904 | |
| 905 | class C3(object): |
| 906 | __slots__ = ['a', 'b', 'c'] |
| 907 | x = C3() |
| 908 | verify(not hasattr(x, "__dict__")) |
| 909 | verify(x.a is None) |
| 910 | verify(x.b is None) |
| 911 | verify(x.c is None) |
| 912 | x.a = 1 |
| 913 | x.b = 2 |
| 914 | x.c = 3 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 915 | vereq(x.a, 1) |
| 916 | vereq(x.b, 2) |
| 917 | vereq(x.c, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 918 | |
| 919 | def dynamics(): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 920 | if verbose: print "Testing class attribute propagation..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 921 | class D(object): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 922 | pass |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 923 | class E(D): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 924 | pass |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 925 | class F(D): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 926 | pass |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 927 | D.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 928 | vereq(D.foo, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 929 | # Test that dynamic attributes are inherited |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 930 | vereq(E.foo, 1) |
| 931 | vereq(F.foo, 1) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 932 | # Test dynamic instances |
| 933 | class C(object): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 934 | pass |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 935 | a = C() |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 936 | verify(not hasattr(a, "foobar")) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 937 | C.foobar = 2 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 938 | vereq(a.foobar, 2) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 939 | C.method = lambda self: 42 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 940 | vereq(a.method(), 42) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 941 | C.__repr__ = lambda self: "C()" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 942 | vereq(repr(a), "C()") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 943 | C.__int__ = lambda self: 100 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 944 | vereq(int(a), 100) |
| 945 | vereq(a.foobar, 2) |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 946 | verify(not hasattr(a, "spam")) |
| 947 | def mygetattr(self, name): |
| 948 | if name == "spam": |
| 949 | return "spam" |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 950 | raise AttributeError |
| 951 | C.__getattr__ = mygetattr |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 952 | vereq(a.spam, "spam") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 953 | a.new = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 954 | vereq(a.new, 12) |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 955 | def mysetattr(self, name, value): |
| 956 | if name == "spam": |
| 957 | raise AttributeError |
| 958 | return object.__setattr__(self, name, value) |
| 959 | C.__setattr__ = mysetattr |
| 960 | try: |
| 961 | a.spam = "not spam" |
| 962 | except AttributeError: |
| 963 | pass |
| 964 | else: |
| 965 | verify(0, "expected AttributeError") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 966 | vereq(a.spam, "spam") |
Guido van Rossum | 80e3675 | 2001-08-14 20:00:33 +0000 | [diff] [blame] | 967 | class D(C): |
| 968 | pass |
| 969 | d = D() |
| 970 | d.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 971 | vereq(d.foo, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 972 | |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 973 | # Test handling of int*seq and seq*int |
| 974 | class I(int): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 975 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 976 | vereq("a"*I(2), "aa") |
| 977 | vereq(I(2)*"a", "aa") |
| 978 | vereq(2*I(3), 6) |
| 979 | vereq(I(3)*2, 6) |
| 980 | vereq(I(3)*I(2), 6) |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 981 | |
| 982 | # Test handling of long*seq and seq*long |
| 983 | class L(long): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 984 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 985 | vereq("a"*L(2L), "aa") |
| 986 | vereq(L(2L)*"a", "aa") |
| 987 | vereq(2*L(3), 6) |
| 988 | vereq(L(3)*2, 6) |
| 989 | vereq(L(3)*L(2), 6) |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 990 | |
Guido van Rossum | 3d45d8f | 2001-09-24 18:47:40 +0000 | [diff] [blame] | 991 | # Test comparison of classes with dynamic metaclasses |
| 992 | class dynamicmetaclass(type): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 993 | pass |
Guido van Rossum | 3d45d8f | 2001-09-24 18:47:40 +0000 | [diff] [blame] | 994 | class someclass: |
| 995 | __metaclass__ = dynamicmetaclass |
| 996 | verify(someclass != object) |
| 997 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 998 | def errors(): |
| 999 | if verbose: print "Testing errors..." |
| 1000 | |
| 1001 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 1002 | class C(list, dict): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1003 | pass |
| 1004 | except TypeError: |
| 1005 | pass |
| 1006 | else: |
| 1007 | verify(0, "inheritance from both list and dict should be illegal") |
| 1008 | |
| 1009 | try: |
| 1010 | class C(object, None): |
| 1011 | pass |
| 1012 | except TypeError: |
| 1013 | pass |
| 1014 | else: |
| 1015 | verify(0, "inheritance from non-type should be illegal") |
| 1016 | class Classic: |
| 1017 | pass |
| 1018 | |
| 1019 | try: |
| 1020 | class C(object, Classic): |
| 1021 | pass |
| 1022 | except TypeError: |
| 1023 | pass |
| 1024 | else: |
| 1025 | verify(0, "inheritance from object and Classic should be illegal") |
| 1026 | |
| 1027 | try: |
Guido van Rossum | 8aea0cc | 2001-08-29 15:48:43 +0000 | [diff] [blame] | 1028 | class C(type(len)): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1029 | pass |
| 1030 | except TypeError: |
| 1031 | pass |
| 1032 | else: |
Guido van Rossum | 8aea0cc | 2001-08-29 15:48:43 +0000 | [diff] [blame] | 1033 | verify(0, "inheritance from CFunction should be illegal") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1034 | |
| 1035 | try: |
| 1036 | class C(object): |
| 1037 | __slots__ = 1 |
| 1038 | except TypeError: |
| 1039 | pass |
| 1040 | else: |
| 1041 | verify(0, "__slots__ = 1 should be illegal") |
| 1042 | |
| 1043 | try: |
| 1044 | class C(object): |
| 1045 | __slots__ = [1] |
| 1046 | except TypeError: |
| 1047 | pass |
| 1048 | else: |
| 1049 | verify(0, "__slots__ = [1] should be illegal") |
| 1050 | |
| 1051 | def classmethods(): |
| 1052 | if verbose: print "Testing class methods..." |
| 1053 | class C(object): |
| 1054 | def foo(*a): return a |
| 1055 | goo = classmethod(foo) |
| 1056 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1057 | vereq(C.goo(1), (C, 1)) |
| 1058 | vereq(c.goo(1), (C, 1)) |
| 1059 | vereq(c.foo(1), (c, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1060 | class D(C): |
| 1061 | pass |
| 1062 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1063 | vereq(D.goo(1), (D, 1)) |
| 1064 | vereq(d.goo(1), (D, 1)) |
| 1065 | vereq(d.foo(1), (d, 1)) |
| 1066 | vereq(D.foo(d, 1), (d, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1067 | |
| 1068 | def staticmethods(): |
| 1069 | if verbose: print "Testing static methods..." |
| 1070 | class C(object): |
| 1071 | def foo(*a): return a |
| 1072 | goo = staticmethod(foo) |
| 1073 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1074 | vereq(C.goo(1), (1,)) |
| 1075 | vereq(c.goo(1), (1,)) |
| 1076 | vereq(c.foo(1), (c, 1,)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1077 | class D(C): |
| 1078 | pass |
| 1079 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1080 | vereq(D.goo(1), (1,)) |
| 1081 | vereq(d.goo(1), (1,)) |
| 1082 | vereq(d.foo(1), (d, 1)) |
| 1083 | vereq(D.foo(d, 1), (d, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1084 | |
| 1085 | def classic(): |
| 1086 | if verbose: print "Testing classic classes..." |
| 1087 | class C: |
| 1088 | def foo(*a): return a |
| 1089 | goo = classmethod(foo) |
| 1090 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1091 | vereq(C.goo(1), (C, 1)) |
| 1092 | vereq(c.goo(1), (C, 1)) |
| 1093 | vereq(c.foo(1), (c, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1094 | class D(C): |
| 1095 | pass |
| 1096 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1097 | vereq(D.goo(1), (D, 1)) |
| 1098 | vereq(d.goo(1), (D, 1)) |
| 1099 | vereq(d.foo(1), (d, 1)) |
| 1100 | vereq(D.foo(d, 1), (d, 1)) |
Guido van Rossum | 9301876 | 2001-08-17 13:40:47 +0000 | [diff] [blame] | 1101 | class E: # *not* subclassing from C |
| 1102 | foo = C.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1103 | vereq(E().foo, C.foo) # i.e., unbound |
Guido van Rossum | 84a79a8 | 2001-08-17 13:58:31 +0000 | [diff] [blame] | 1104 | verify(repr(C.foo.__get__(C())).startswith("<bound method ")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1105 | |
| 1106 | def compattr(): |
| 1107 | if verbose: print "Testing computed attributes..." |
| 1108 | class C(object): |
| 1109 | class computed_attribute(object): |
| 1110 | def __init__(self, get, set=None): |
| 1111 | self.__get = get |
| 1112 | self.__set = set |
| 1113 | def __get__(self, obj, type=None): |
| 1114 | return self.__get(obj) |
| 1115 | def __set__(self, obj, value): |
| 1116 | return self.__set(obj, value) |
| 1117 | def __init__(self): |
| 1118 | self.__x = 0 |
| 1119 | def __get_x(self): |
| 1120 | x = self.__x |
| 1121 | self.__x = x+1 |
| 1122 | return x |
| 1123 | def __set_x(self, x): |
| 1124 | self.__x = x |
| 1125 | x = computed_attribute(__get_x, __set_x) |
| 1126 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1127 | vereq(a.x, 0) |
| 1128 | vereq(a.x, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1129 | a.x = 10 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1130 | vereq(a.x, 10) |
| 1131 | vereq(a.x, 11) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1132 | |
| 1133 | def newslot(): |
| 1134 | if verbose: print "Testing __new__ slot override..." |
| 1135 | class C(list): |
| 1136 | def __new__(cls): |
| 1137 | self = list.__new__(cls) |
| 1138 | self.foo = 1 |
| 1139 | return self |
| 1140 | def __init__(self): |
| 1141 | self.foo = self.foo + 2 |
| 1142 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1143 | vereq(a.foo, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1144 | verify(a.__class__ is C) |
| 1145 | class D(C): |
| 1146 | pass |
| 1147 | b = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1148 | vereq(b.foo, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1149 | verify(b.__class__ is D) |
| 1150 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1151 | def altmro(): |
| 1152 | if verbose: print "Testing mro() and overriding it..." |
| 1153 | class A(object): |
| 1154 | def f(self): return "A" |
| 1155 | class B(A): |
| 1156 | pass |
| 1157 | class C(A): |
| 1158 | def f(self): return "C" |
| 1159 | class D(B, C): |
| 1160 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1161 | vereq(D.mro(), [D, B, C, A, object]) |
| 1162 | vereq(D.__mro__, (D, B, C, A, object)) |
| 1163 | vereq(D().f(), "C") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1164 | class PerverseMetaType(type): |
| 1165 | def mro(cls): |
| 1166 | L = type.mro(cls) |
| 1167 | L.reverse() |
| 1168 | return L |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1169 | class X(A,B,C,D): |
| 1170 | __metaclass__ = PerverseMetaType |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1171 | vereq(X.__mro__, (object, A, C, B, D, X)) |
| 1172 | vereq(X().f(), "A") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1173 | |
| 1174 | def overloading(): |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1175 | if verbose: print "Testing operator overloading..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1176 | |
| 1177 | class B(object): |
| 1178 | "Intermediate class because object doesn't have a __setattr__" |
| 1179 | |
| 1180 | class C(B): |
| 1181 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 1182 | def __getattr__(self, name): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1183 | if name == "foo": |
| 1184 | return ("getattr", name) |
| 1185 | else: |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 1186 | raise AttributeError |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1187 | def __setattr__(self, name, value): |
| 1188 | if name == "foo": |
| 1189 | self.setattr = (name, value) |
| 1190 | else: |
| 1191 | return B.__setattr__(self, name, value) |
| 1192 | def __delattr__(self, name): |
| 1193 | if name == "foo": |
| 1194 | self.delattr = name |
| 1195 | else: |
| 1196 | return B.__delattr__(self, name) |
| 1197 | |
| 1198 | def __getitem__(self, key): |
| 1199 | return ("getitem", key) |
| 1200 | def __setitem__(self, key, value): |
| 1201 | self.setitem = (key, value) |
| 1202 | def __delitem__(self, key): |
| 1203 | self.delitem = key |
| 1204 | |
| 1205 | def __getslice__(self, i, j): |
| 1206 | return ("getslice", i, j) |
| 1207 | def __setslice__(self, i, j, value): |
| 1208 | self.setslice = (i, j, value) |
| 1209 | def __delslice__(self, i, j): |
| 1210 | self.delslice = (i, j) |
| 1211 | |
| 1212 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1213 | vereq(a.foo, ("getattr", "foo")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1214 | a.foo = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1215 | vereq(a.setattr, ("foo", 12)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1216 | del a.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1217 | vereq(a.delattr, "foo") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1218 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1219 | vereq(a[12], ("getitem", 12)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1220 | a[12] = 21 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1221 | vereq(a.setitem, (12, 21)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1222 | del a[12] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1223 | vereq(a.delitem, 12) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1224 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1225 | vereq(a[0:10], ("getslice", 0, 10)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1226 | a[0:10] = "foo" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1227 | vereq(a.setslice, (0, 10, "foo")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1228 | del a[0:10] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1229 | vereq(a.delslice, (0, 10)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1230 | |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1231 | def methods(): |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1232 | if verbose: print "Testing methods..." |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1233 | class C(object): |
| 1234 | def __init__(self, x): |
| 1235 | self.x = x |
| 1236 | def foo(self): |
| 1237 | return self.x |
| 1238 | c1 = C(1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1239 | vereq(c1.foo(), 1) |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1240 | class D(C): |
| 1241 | boo = C.foo |
| 1242 | goo = c1.foo |
| 1243 | d2 = D(2) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1244 | vereq(d2.foo(), 2) |
| 1245 | vereq(d2.boo(), 2) |
| 1246 | vereq(d2.goo(), 1) |
Guido van Rossum | 9301876 | 2001-08-17 13:40:47 +0000 | [diff] [blame] | 1247 | class E(object): |
| 1248 | foo = C.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1249 | vereq(E().foo, C.foo) # i.e., unbound |
Guido van Rossum | 84a79a8 | 2001-08-17 13:58:31 +0000 | [diff] [blame] | 1250 | verify(repr(C.foo.__get__(C(1))).startswith("<bound method ")) |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1251 | |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1252 | def specials(): |
| 1253 | # Test operators like __hash__ for which a built-in default exists |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1254 | if verbose: print "Testing special operators..." |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1255 | # Test the default behavior for static classes |
| 1256 | class C(object): |
| 1257 | def __getitem__(self, i): |
| 1258 | if 0 <= i < 10: return i |
| 1259 | raise IndexError |
| 1260 | c1 = C() |
| 1261 | c2 = C() |
| 1262 | verify(not not c1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1263 | vereq(hash(c1), id(c1)) |
| 1264 | vereq(cmp(c1, c2), cmp(id(c1), id(c2))) |
| 1265 | vereq(c1, c1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1266 | verify(c1 != c2) |
| 1267 | verify(not c1 != c1) |
| 1268 | verify(not c1 == c2) |
Tim Peters | 4d2dded | 2001-08-16 19:50:51 +0000 | [diff] [blame] | 1269 | # Note that the module name appears in str/repr, and that varies |
| 1270 | # 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] | 1271 | verify(str(c1).find('C object at ') >= 0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1272 | vereq(str(c1), repr(c1)) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1273 | verify(-1 not in c1) |
| 1274 | for i in range(10): |
| 1275 | verify(i in c1) |
| 1276 | verify(10 not in c1) |
| 1277 | # Test the default behavior for dynamic classes |
| 1278 | class D(object): |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1279 | def __getitem__(self, i): |
| 1280 | if 0 <= i < 10: return i |
| 1281 | raise IndexError |
| 1282 | d1 = D() |
| 1283 | d2 = D() |
| 1284 | verify(not not d1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1285 | vereq(hash(d1), id(d1)) |
| 1286 | vereq(cmp(d1, d2), cmp(id(d1), id(d2))) |
| 1287 | vereq(d1, d1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1288 | verify(d1 != d2) |
| 1289 | verify(not d1 != d1) |
| 1290 | verify(not d1 == d2) |
Tim Peters | 4d2dded | 2001-08-16 19:50:51 +0000 | [diff] [blame] | 1291 | # Note that the module name appears in str/repr, and that varies |
| 1292 | # 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] | 1293 | verify(str(d1).find('D object at ') >= 0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1294 | vereq(str(d1), repr(d1)) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1295 | verify(-1 not in d1) |
| 1296 | for i in range(10): |
| 1297 | verify(i in d1) |
| 1298 | verify(10 not in d1) |
| 1299 | # Test overridden behavior for static classes |
| 1300 | class Proxy(object): |
| 1301 | def __init__(self, x): |
| 1302 | self.x = x |
| 1303 | def __nonzero__(self): |
| 1304 | return not not self.x |
| 1305 | def __hash__(self): |
| 1306 | return hash(self.x) |
| 1307 | def __eq__(self, other): |
| 1308 | return self.x == other |
| 1309 | def __ne__(self, other): |
| 1310 | return self.x != other |
| 1311 | def __cmp__(self, other): |
| 1312 | return cmp(self.x, other.x) |
| 1313 | def __str__(self): |
| 1314 | return "Proxy:%s" % self.x |
| 1315 | def __repr__(self): |
| 1316 | return "Proxy(%r)" % self.x |
| 1317 | def __contains__(self, value): |
| 1318 | return value in self.x |
| 1319 | p0 = Proxy(0) |
| 1320 | p1 = Proxy(1) |
| 1321 | p_1 = Proxy(-1) |
| 1322 | verify(not p0) |
| 1323 | verify(not not p1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1324 | vereq(hash(p0), hash(0)) |
| 1325 | vereq(p0, p0) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1326 | verify(p0 != p1) |
| 1327 | verify(not p0 != p0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1328 | vereq(not p0, p1) |
| 1329 | vereq(cmp(p0, p1), -1) |
| 1330 | vereq(cmp(p0, p0), 0) |
| 1331 | vereq(cmp(p0, p_1), 1) |
| 1332 | vereq(str(p0), "Proxy:0") |
| 1333 | vereq(repr(p0), "Proxy(0)") |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1334 | p10 = Proxy(range(10)) |
| 1335 | verify(-1 not in p10) |
| 1336 | for i in range(10): |
| 1337 | verify(i in p10) |
| 1338 | verify(10 not in p10) |
| 1339 | # Test overridden behavior for dynamic classes |
| 1340 | class DProxy(object): |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1341 | def __init__(self, x): |
| 1342 | self.x = x |
| 1343 | def __nonzero__(self): |
| 1344 | return not not self.x |
| 1345 | def __hash__(self): |
| 1346 | return hash(self.x) |
| 1347 | def __eq__(self, other): |
| 1348 | return self.x == other |
| 1349 | def __ne__(self, other): |
| 1350 | return self.x != other |
| 1351 | def __cmp__(self, other): |
| 1352 | return cmp(self.x, other.x) |
| 1353 | def __str__(self): |
| 1354 | return "DProxy:%s" % self.x |
| 1355 | def __repr__(self): |
| 1356 | return "DProxy(%r)" % self.x |
| 1357 | def __contains__(self, value): |
| 1358 | return value in self.x |
| 1359 | p0 = DProxy(0) |
| 1360 | p1 = DProxy(1) |
| 1361 | p_1 = DProxy(-1) |
| 1362 | verify(not p0) |
| 1363 | verify(not not p1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1364 | vereq(hash(p0), hash(0)) |
| 1365 | vereq(p0, p0) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1366 | verify(p0 != p1) |
| 1367 | verify(not p0 != p0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1368 | vereq(not p0, p1) |
| 1369 | vereq(cmp(p0, p1), -1) |
| 1370 | vereq(cmp(p0, p0), 0) |
| 1371 | vereq(cmp(p0, p_1), 1) |
| 1372 | vereq(str(p0), "DProxy:0") |
| 1373 | vereq(repr(p0), "DProxy(0)") |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1374 | p10 = DProxy(range(10)) |
| 1375 | verify(-1 not in p10) |
| 1376 | for i in range(10): |
| 1377 | verify(i in p10) |
| 1378 | verify(10 not in p10) |
Guido van Rossum | 843daa8 | 2001-09-18 20:04:26 +0000 | [diff] [blame] | 1379 | # Safety test for __cmp__ |
| 1380 | def unsafecmp(a, b): |
| 1381 | try: |
| 1382 | a.__class__.__cmp__(a, b) |
| 1383 | except TypeError: |
| 1384 | pass |
| 1385 | else: |
| 1386 | raise TestFailed, "shouldn't allow %s.__cmp__(%r, %r)" % ( |
| 1387 | a.__class__, a, b) |
| 1388 | unsafecmp(u"123", "123") |
| 1389 | unsafecmp("123", u"123") |
| 1390 | unsafecmp(1, 1.0) |
| 1391 | unsafecmp(1.0, 1) |
| 1392 | unsafecmp(1, 1L) |
| 1393 | unsafecmp(1L, 1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1394 | |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1395 | def weakrefs(): |
| 1396 | if verbose: print "Testing weak references..." |
| 1397 | import weakref |
| 1398 | class C(object): |
| 1399 | pass |
| 1400 | c = C() |
| 1401 | r = weakref.ref(c) |
| 1402 | verify(r() is c) |
| 1403 | del c |
| 1404 | verify(r() is None) |
| 1405 | del r |
| 1406 | class NoWeak(object): |
| 1407 | __slots__ = ['foo'] |
| 1408 | no = NoWeak() |
| 1409 | try: |
| 1410 | weakref.ref(no) |
| 1411 | except TypeError, msg: |
Fred Drake | 4bf018b | 2001-10-22 21:45:25 +0000 | [diff] [blame] | 1412 | verify(str(msg).find("weak reference") >= 0) |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1413 | else: |
| 1414 | verify(0, "weakref.ref(no) should be illegal") |
| 1415 | class Weak(object): |
| 1416 | __slots__ = ['foo', '__weakref__'] |
| 1417 | yes = Weak() |
| 1418 | r = weakref.ref(yes) |
| 1419 | verify(r() is yes) |
| 1420 | del yes |
| 1421 | verify(r() is None) |
| 1422 | del r |
| 1423 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1424 | def properties(): |
| 1425 | if verbose: print "Testing property..." |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1426 | class C(object): |
| 1427 | def getx(self): |
| 1428 | return self.__x |
| 1429 | def setx(self, value): |
| 1430 | self.__x = value |
| 1431 | def delx(self): |
| 1432 | del self.__x |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1433 | x = property(getx, setx, delx, doc="I'm the x property.") |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1434 | a = C() |
| 1435 | verify(not hasattr(a, "x")) |
| 1436 | a.x = 42 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1437 | vereq(a._C__x, 42) |
| 1438 | vereq(a.x, 42) |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1439 | del a.x |
| 1440 | verify(not hasattr(a, "x")) |
| 1441 | verify(not hasattr(a, "_C__x")) |
| 1442 | C.x.__set__(a, 100) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1443 | vereq(C.x.__get__(a), 100) |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1444 | ## C.x.__set__(a) |
| 1445 | ## verify(not hasattr(a, "x")) |
| 1446 | |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1447 | raw = C.__dict__['x'] |
| 1448 | verify(isinstance(raw, property)) |
| 1449 | |
| 1450 | attrs = dir(raw) |
| 1451 | verify("__doc__" in attrs) |
| 1452 | verify("fget" in attrs) |
| 1453 | verify("fset" in attrs) |
| 1454 | verify("fdel" in attrs) |
| 1455 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1456 | vereq(raw.__doc__, "I'm the x property.") |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1457 | verify(raw.fget is C.__dict__['getx']) |
| 1458 | verify(raw.fset is C.__dict__['setx']) |
| 1459 | verify(raw.fdel is C.__dict__['delx']) |
| 1460 | |
| 1461 | for attr in "__doc__", "fget", "fset", "fdel": |
| 1462 | try: |
| 1463 | setattr(raw, attr, 42) |
| 1464 | except TypeError, msg: |
| 1465 | if str(msg).find('readonly') < 0: |
| 1466 | raise TestFailed("when setting readonly attr %r on a " |
| 1467 | "property, got unexpected TypeError " |
| 1468 | "msg %r" % (attr, str(msg))) |
| 1469 | else: |
| 1470 | raise TestFailed("expected TypeError from trying to set " |
| 1471 | "readonly %r attr on a property" % attr) |
| 1472 | |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1473 | def supers(): |
Guido van Rossum | 9881fc1 | 2001-08-24 17:07:20 +0000 | [diff] [blame] | 1474 | if verbose: print "Testing super..." |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1475 | |
| 1476 | class A(object): |
| 1477 | def meth(self, a): |
| 1478 | return "A(%r)" % a |
| 1479 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1480 | vereq(A().meth(1), "A(1)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1481 | |
| 1482 | class B(A): |
| 1483 | def __init__(self): |
| 1484 | self.__super = super(B, self) |
| 1485 | def meth(self, a): |
| 1486 | return "B(%r)" % a + self.__super.meth(a) |
| 1487 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1488 | vereq(B().meth(2), "B(2)A(2)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1489 | |
| 1490 | class C(A): |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1491 | def meth(self, a): |
| 1492 | return "C(%r)" % a + self.__super.meth(a) |
| 1493 | C._C__super = super(C) |
| 1494 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1495 | vereq(C().meth(3), "C(3)A(3)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1496 | |
| 1497 | class D(C, B): |
| 1498 | def meth(self, a): |
| 1499 | return "D(%r)" % a + super(D, self).meth(a) |
| 1500 | |
| 1501 | verify (D().meth(4) == "D(4)C(4)B(4)A(4)") |
| 1502 | |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1503 | def inherits(): |
| 1504 | if verbose: print "Testing inheritance from basic types..." |
| 1505 | |
| 1506 | class hexint(int): |
| 1507 | def __repr__(self): |
| 1508 | return hex(self) |
| 1509 | def __add__(self, other): |
| 1510 | return hexint(int.__add__(self, other)) |
| 1511 | # (Note that overriding __radd__ doesn't work, |
| 1512 | # because the int type gets first dibs.) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1513 | vereq(repr(hexint(7) + 9), "0x10") |
| 1514 | vereq(repr(hexint(1000) + 7), "0x3ef") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1515 | a = hexint(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1516 | vereq(a, 12345) |
| 1517 | vereq(int(a), 12345) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1518 | verify(int(a).__class__ is int) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1519 | vereq(hash(a), hash(12345)) |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 1520 | verify((+a).__class__ is int) |
| 1521 | verify((a >> 0).__class__ is int) |
| 1522 | verify((a << 0).__class__ is int) |
| 1523 | verify((hexint(0) << 12).__class__ is int) |
| 1524 | verify((hexint(0) >> 12).__class__ is int) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1525 | |
| 1526 | class octlong(long): |
| 1527 | __slots__ = [] |
| 1528 | def __str__(self): |
| 1529 | s = oct(self) |
| 1530 | if s[-1] == 'L': |
| 1531 | s = s[:-1] |
| 1532 | return s |
| 1533 | def __add__(self, other): |
| 1534 | return self.__class__(super(octlong, self).__add__(other)) |
| 1535 | __radd__ = __add__ |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1536 | vereq(str(octlong(3) + 5), "010") |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1537 | # (Note that overriding __radd__ here only seems to work |
| 1538 | # because the example uses a short int left argument.) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1539 | vereq(str(5 + octlong(3000)), "05675") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1540 | a = octlong(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1541 | vereq(a, 12345L) |
| 1542 | vereq(long(a), 12345L) |
| 1543 | vereq(hash(a), hash(12345L)) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1544 | verify(long(a).__class__ is long) |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 1545 | verify((+a).__class__ is long) |
| 1546 | verify((-a).__class__ is long) |
| 1547 | verify((-octlong(0)).__class__ is long) |
| 1548 | verify((a >> 0).__class__ is long) |
| 1549 | verify((a << 0).__class__ is long) |
| 1550 | verify((a - 0).__class__ is long) |
| 1551 | verify((a * 1).__class__ is long) |
| 1552 | verify((a ** 1).__class__ is long) |
| 1553 | verify((a // 1).__class__ is long) |
| 1554 | verify((1 * a).__class__ is long) |
| 1555 | verify((a | 0).__class__ is long) |
| 1556 | verify((a ^ 0).__class__ is long) |
| 1557 | verify((a & -1L).__class__ is long) |
| 1558 | verify((octlong(0) << 12).__class__ is long) |
| 1559 | verify((octlong(0) >> 12).__class__ is long) |
| 1560 | verify(abs(octlong(0)).__class__ is long) |
| 1561 | |
| 1562 | # Because octlong overrides __add__, we can't check the absence of +0 |
| 1563 | # optimizations using octlong. |
| 1564 | class longclone(long): |
| 1565 | pass |
| 1566 | a = longclone(1) |
| 1567 | verify((a + 0).__class__ is long) |
| 1568 | verify((0 + a).__class__ is long) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1569 | |
| 1570 | class precfloat(float): |
| 1571 | __slots__ = ['prec'] |
| 1572 | def __init__(self, value=0.0, prec=12): |
| 1573 | self.prec = int(prec) |
| 1574 | float.__init__(value) |
| 1575 | def __repr__(self): |
| 1576 | return "%.*g" % (self.prec, self) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1577 | vereq(repr(precfloat(1.1)), "1.1") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1578 | a = precfloat(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1579 | vereq(a, 12345.0) |
| 1580 | vereq(float(a), 12345.0) |
Tim Peters | 7a50f25 | 2001-09-10 21:28:20 +0000 | [diff] [blame] | 1581 | verify(float(a).__class__ is float) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1582 | vereq(hash(a), hash(12345.0)) |
Tim Peters | 0280cf7 | 2001-09-11 21:53:35 +0000 | [diff] [blame] | 1583 | verify((+a).__class__ is float) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1584 | |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1585 | class madcomplex(complex): |
| 1586 | def __repr__(self): |
| 1587 | return "%.17gj%+.17g" % (self.imag, self.real) |
| 1588 | a = madcomplex(-3, 4) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1589 | vereq(repr(a), "4j-3") |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1590 | base = complex(-3, 4) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1591 | veris(base.__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1592 | vereq(a, base) |
| 1593 | vereq(complex(a), base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1594 | veris(complex(a).__class__, complex) |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1595 | a = madcomplex(a) # just trying another form of the constructor |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1596 | vereq(repr(a), "4j-3") |
| 1597 | vereq(a, base) |
| 1598 | vereq(complex(a), base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1599 | veris(complex(a).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1600 | vereq(hash(a), hash(base)) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1601 | veris((+a).__class__, complex) |
| 1602 | veris((a + 0).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1603 | vereq(a + 0, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1604 | veris((a - 0).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1605 | vereq(a - 0, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1606 | veris((a * 1).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1607 | vereq(a * 1, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1608 | veris((a / 1).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1609 | vereq(a / 1, base) |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1610 | |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1611 | class madtuple(tuple): |
| 1612 | _rev = None |
| 1613 | def rev(self): |
| 1614 | if self._rev is not None: |
| 1615 | return self._rev |
| 1616 | L = list(self) |
| 1617 | L.reverse() |
| 1618 | self._rev = self.__class__(L) |
| 1619 | return self._rev |
| 1620 | 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] | 1621 | vereq(a, (1,2,3,4,5,6,7,8,9,0)) |
| 1622 | vereq(a.rev(), madtuple((0,9,8,7,6,5,4,3,2,1))) |
| 1623 | 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] | 1624 | for i in range(512): |
| 1625 | t = madtuple(range(i)) |
| 1626 | u = t.rev() |
| 1627 | v = u.rev() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1628 | vereq(v, t) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1629 | a = madtuple((1,2,3,4,5)) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1630 | vereq(tuple(a), (1,2,3,4,5)) |
Tim Peters | 4c3a0a3 | 2001-09-10 23:37:46 +0000 | [diff] [blame] | 1631 | verify(tuple(a).__class__ is tuple) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1632 | vereq(hash(a), hash((1,2,3,4,5))) |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 1633 | verify(a[:].__class__ is tuple) |
| 1634 | verify((a * 1).__class__ is tuple) |
| 1635 | verify((a * 0).__class__ is tuple) |
| 1636 | verify((a + ()).__class__ is tuple) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1637 | a = madtuple(()) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1638 | vereq(tuple(a), ()) |
Guido van Rossum | 779ce4a | 2001-09-11 14:02:22 +0000 | [diff] [blame] | 1639 | verify(tuple(a).__class__ is tuple) |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 1640 | verify((a + a).__class__ is tuple) |
| 1641 | verify((a * 0).__class__ is tuple) |
| 1642 | verify((a * 1).__class__ is tuple) |
| 1643 | verify((a * 2).__class__ is tuple) |
| 1644 | verify(a[:].__class__ is tuple) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1645 | |
| 1646 | class madstring(str): |
| 1647 | _rev = None |
| 1648 | def rev(self): |
| 1649 | if self._rev is not None: |
| 1650 | return self._rev |
| 1651 | L = list(self) |
| 1652 | L.reverse() |
| 1653 | self._rev = self.__class__("".join(L)) |
| 1654 | return self._rev |
| 1655 | s = madstring("abcdefghijklmnopqrstuvwxyz") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1656 | vereq(s, "abcdefghijklmnopqrstuvwxyz") |
| 1657 | vereq(s.rev(), madstring("zyxwvutsrqponmlkjihgfedcba")) |
| 1658 | vereq(s.rev().rev(), madstring("abcdefghijklmnopqrstuvwxyz")) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1659 | for i in range(256): |
| 1660 | s = madstring("".join(map(chr, range(i)))) |
| 1661 | t = s.rev() |
| 1662 | u = t.rev() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1663 | vereq(u, s) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1664 | s = madstring("12345") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1665 | vereq(str(s), "12345") |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 1666 | verify(str(s).__class__ is str) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1667 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1668 | base = "\x00" * 5 |
| 1669 | s = madstring(base) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1670 | vereq(s, base) |
| 1671 | vereq(str(s), base) |
Tim Peters | c636f56 | 2001-09-11 01:52:02 +0000 | [diff] [blame] | 1672 | verify(str(s).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1673 | vereq(hash(s), hash(base)) |
| 1674 | vereq({s: 1}[base], 1) |
| 1675 | vereq({base: 1}[s], 1) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1676 | verify((s + "").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1677 | vereq(s + "", base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1678 | verify(("" + s).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1679 | vereq("" + s, base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1680 | verify((s * 0).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1681 | vereq(s * 0, "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1682 | verify((s * 1).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1683 | vereq(s * 1, base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1684 | verify((s * 2).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1685 | vereq(s * 2, base + base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1686 | verify(s[:].__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1687 | vereq(s[:], base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1688 | verify(s[0:0].__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1689 | vereq(s[0:0], "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1690 | verify(s.strip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1691 | vereq(s.strip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1692 | verify(s.lstrip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1693 | vereq(s.lstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1694 | verify(s.rstrip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1695 | vereq(s.rstrip(), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1696 | identitytab = ''.join([chr(i) for i in range(256)]) |
| 1697 | verify(s.translate(identitytab).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1698 | vereq(s.translate(identitytab), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1699 | verify(s.translate(identitytab, "x").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1700 | vereq(s.translate(identitytab, "x"), base) |
| 1701 | vereq(s.translate(identitytab, "\x00"), "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1702 | verify(s.replace("x", "x").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1703 | vereq(s.replace("x", "x"), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1704 | verify(s.ljust(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1705 | vereq(s.ljust(len(s)), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1706 | verify(s.rjust(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1707 | vereq(s.rjust(len(s)), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1708 | verify(s.center(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1709 | vereq(s.center(len(s)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1710 | verify(s.lower().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1711 | vereq(s.lower(), base) |
Tim Peters | c636f56 | 2001-09-11 01:52:02 +0000 | [diff] [blame] | 1712 | |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1713 | s = madstring("x y") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1714 | vereq(s, "x y") |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1715 | verify(intern(s).__class__ is str) |
| 1716 | verify(intern(s) is intern("x y")) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1717 | vereq(intern(s), "x y") |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1718 | |
| 1719 | i = intern("y x") |
| 1720 | s = madstring("y x") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1721 | vereq(s, i) |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1722 | verify(intern(s).__class__ is str) |
| 1723 | verify(intern(s) is i) |
| 1724 | |
| 1725 | s = madstring(i) |
| 1726 | verify(intern(s).__class__ is str) |
| 1727 | verify(intern(s) is i) |
| 1728 | |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 1729 | class madunicode(unicode): |
| 1730 | _rev = None |
| 1731 | def rev(self): |
| 1732 | if self._rev is not None: |
| 1733 | return self._rev |
| 1734 | L = list(self) |
| 1735 | L.reverse() |
| 1736 | self._rev = self.__class__(u"".join(L)) |
| 1737 | return self._rev |
| 1738 | u = madunicode("ABCDEF") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1739 | vereq(u, u"ABCDEF") |
| 1740 | vereq(u.rev(), madunicode(u"FEDCBA")) |
| 1741 | vereq(u.rev().rev(), madunicode(u"ABCDEF")) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1742 | base = u"12345" |
| 1743 | u = madunicode(base) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1744 | vereq(unicode(u), base) |
Tim Peters | 78e0fc7 | 2001-09-11 03:07:38 +0000 | [diff] [blame] | 1745 | verify(unicode(u).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1746 | vereq(hash(u), hash(base)) |
| 1747 | vereq({u: 1}[base], 1) |
| 1748 | vereq({base: 1}[u], 1) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1749 | verify(u.strip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1750 | vereq(u.strip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1751 | verify(u.lstrip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1752 | vereq(u.lstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1753 | verify(u.rstrip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1754 | vereq(u.rstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1755 | verify(u.replace(u"x", u"x").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1756 | vereq(u.replace(u"x", u"x"), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1757 | verify(u.replace(u"xy", u"xy").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1758 | vereq(u.replace(u"xy", u"xy"), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1759 | verify(u.center(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1760 | vereq(u.center(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1761 | verify(u.ljust(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1762 | vereq(u.ljust(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1763 | verify(u.rjust(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1764 | vereq(u.rjust(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1765 | verify(u.lower().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1766 | vereq(u.lower(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1767 | verify(u.upper().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1768 | vereq(u.upper(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1769 | verify(u.capitalize().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1770 | vereq(u.capitalize(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1771 | verify(u.title().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1772 | vereq(u.title(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1773 | verify((u + u"").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1774 | vereq(u + u"", base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1775 | verify((u"" + u).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1776 | vereq(u"" + u, base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1777 | verify((u * 0).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1778 | vereq(u * 0, u"") |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1779 | verify((u * 1).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1780 | vereq(u * 1, base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1781 | verify((u * 2).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1782 | vereq(u * 2, base + base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1783 | verify(u[:].__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1784 | vereq(u[:], base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1785 | verify(u[0:0].__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1786 | vereq(u[0:0], u"") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 1787 | |
Guido van Rossum | 12b22ff | 2001-10-09 20:36:44 +0000 | [diff] [blame] | 1788 | class sublist(list): |
| 1789 | pass |
| 1790 | a = sublist(range(5)) |
| 1791 | vereq(a, range(5)) |
| 1792 | a.append("hello") |
| 1793 | vereq(a, range(5) + ["hello"]) |
| 1794 | a[5] = 5 |
| 1795 | vereq(a, range(6)) |
| 1796 | a.extend(range(6, 20)) |
| 1797 | vereq(a, range(20)) |
| 1798 | a[-5:] = [] |
| 1799 | vereq(a, range(15)) |
| 1800 | del a[10:15] |
| 1801 | vereq(len(a), 10) |
| 1802 | vereq(a, range(10)) |
| 1803 | vereq(list(a), range(10)) |
| 1804 | vereq(a[0], 0) |
| 1805 | vereq(a[9], 9) |
| 1806 | vereq(a[-10], 0) |
| 1807 | vereq(a[-1], 9) |
| 1808 | vereq(a[:5], range(5)) |
| 1809 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1810 | class CountedInput(file): |
| 1811 | """Counts lines read by self.readline(). |
| 1812 | |
| 1813 | self.lineno is the 0-based ordinal of the last line read, up to |
| 1814 | a maximum of one greater than the number of lines in the file. |
| 1815 | |
| 1816 | self.ateof is true if and only if the final "" line has been read, |
| 1817 | at which point self.lineno stops incrementing, and further calls |
| 1818 | to readline() continue to return "". |
| 1819 | """ |
| 1820 | |
| 1821 | lineno = 0 |
| 1822 | ateof = 0 |
| 1823 | def readline(self): |
| 1824 | if self.ateof: |
| 1825 | return "" |
| 1826 | s = file.readline(self) |
| 1827 | # Next line works too. |
| 1828 | # s = super(CountedInput, self).readline() |
| 1829 | self.lineno += 1 |
| 1830 | if s == "": |
| 1831 | self.ateof = 1 |
| 1832 | return s |
| 1833 | |
Tim Peters | 561f899 | 2001-09-13 19:36:36 +0000 | [diff] [blame] | 1834 | f = file(name=TESTFN, mode='w') |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1835 | lines = ['a\n', 'b\n', 'c\n'] |
| 1836 | try: |
| 1837 | f.writelines(lines) |
| 1838 | f.close() |
| 1839 | f = CountedInput(TESTFN) |
| 1840 | for (i, expected) in zip(range(1, 5) + [4], lines + 2 * [""]): |
| 1841 | got = f.readline() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1842 | vereq(expected, got) |
| 1843 | vereq(f.lineno, i) |
| 1844 | vereq(f.ateof, (i > len(lines))) |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1845 | f.close() |
| 1846 | finally: |
| 1847 | try: |
| 1848 | f.close() |
| 1849 | except: |
| 1850 | pass |
| 1851 | try: |
| 1852 | import os |
| 1853 | os.unlink(TESTFN) |
| 1854 | except: |
| 1855 | pass |
| 1856 | |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 1857 | def keywords(): |
| 1858 | if verbose: |
| 1859 | print "Testing keyword args to basic type constructors ..." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1860 | vereq(int(x=1), 1) |
| 1861 | vereq(float(x=2), 2.0) |
| 1862 | vereq(long(x=3), 3L) |
| 1863 | vereq(complex(imag=42, real=666), complex(666, 42)) |
| 1864 | vereq(str(object=500), '500') |
| 1865 | vereq(unicode(string='abc', errors='strict'), u'abc') |
| 1866 | vereq(tuple(sequence=range(3)), (0, 1, 2)) |
| 1867 | vereq(list(sequence=(0, 1, 2)), range(3)) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 1868 | vereq(dict(items={1: 2}), {1: 2}) |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 1869 | |
| 1870 | for constructor in (int, float, long, complex, str, unicode, |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame^] | 1871 | tuple, list, dict, file): |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 1872 | try: |
| 1873 | constructor(bogus_keyword_arg=1) |
| 1874 | except TypeError: |
| 1875 | pass |
| 1876 | else: |
| 1877 | raise TestFailed("expected TypeError from bogus keyword " |
| 1878 | "argument to %r" % constructor) |
Tim Peters | 561f899 | 2001-09-13 19:36:36 +0000 | [diff] [blame] | 1879 | |
Tim Peters | 8fa4567 | 2001-09-13 21:01:29 +0000 | [diff] [blame] | 1880 | def restricted(): |
| 1881 | import rexec |
| 1882 | if verbose: |
| 1883 | print "Testing interaction with restricted execution ..." |
| 1884 | |
| 1885 | sandbox = rexec.RExec() |
| 1886 | |
| 1887 | code1 = """f = open(%r, 'w')""" % TESTFN |
| 1888 | code2 = """f = file(%r, 'w')""" % TESTFN |
| 1889 | code3 = """\ |
| 1890 | f = open(%r) |
| 1891 | t = type(f) # a sneaky way to get the file() constructor |
| 1892 | f.close() |
| 1893 | f = t(%r, 'w') # rexec can't catch this by itself |
| 1894 | """ % (TESTFN, TESTFN) |
| 1895 | |
| 1896 | f = open(TESTFN, 'w') # Create the file so code3 can find it. |
| 1897 | f.close() |
| 1898 | |
| 1899 | try: |
| 1900 | for code in code1, code2, code3: |
| 1901 | try: |
| 1902 | sandbox.r_exec(code) |
| 1903 | except IOError, msg: |
| 1904 | if str(msg).find("restricted") >= 0: |
| 1905 | outcome = "OK" |
| 1906 | else: |
| 1907 | outcome = "got an exception, but not an expected one" |
| 1908 | else: |
| 1909 | outcome = "expected a restricted-execution exception" |
| 1910 | |
| 1911 | if outcome != "OK": |
| 1912 | raise TestFailed("%s, in %r" % (outcome, code)) |
| 1913 | |
| 1914 | finally: |
| 1915 | try: |
| 1916 | import os |
| 1917 | os.unlink(TESTFN) |
| 1918 | except: |
| 1919 | pass |
| 1920 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1921 | def str_subclass_as_dict_key(): |
| 1922 | if verbose: |
| 1923 | print "Testing a str subclass used as dict key .." |
| 1924 | |
| 1925 | class cistr(str): |
| 1926 | """Sublcass of str that computes __eq__ case-insensitively. |
| 1927 | |
| 1928 | Also computes a hash code of the string in canonical form. |
| 1929 | """ |
| 1930 | |
| 1931 | def __init__(self, value): |
| 1932 | self.canonical = value.lower() |
| 1933 | self.hashcode = hash(self.canonical) |
| 1934 | |
| 1935 | def __eq__(self, other): |
| 1936 | if not isinstance(other, cistr): |
| 1937 | other = cistr(other) |
| 1938 | return self.canonical == other.canonical |
| 1939 | |
| 1940 | def __hash__(self): |
| 1941 | return self.hashcode |
| 1942 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1943 | vereq(cistr('ABC'), 'abc') |
| 1944 | vereq('aBc', cistr('ABC')) |
| 1945 | vereq(str(cistr('ABC')), 'ABC') |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1946 | |
| 1947 | d = {cistr('one'): 1, cistr('two'): 2, cistr('tHree'): 3} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1948 | vereq(d[cistr('one')], 1) |
| 1949 | vereq(d[cistr('tWo')], 2) |
| 1950 | vereq(d[cistr('THrEE')], 3) |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1951 | verify(cistr('ONe') in d) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1952 | vereq(d.get(cistr('thrEE')), 3) |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1953 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 1954 | def classic_comparisons(): |
| 1955 | if verbose: print "Testing classic comparisons..." |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 1956 | class classic: |
| 1957 | pass |
| 1958 | for base in (classic, int, object): |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 1959 | if verbose: print " (base = %s)" % base |
| 1960 | class C(base): |
| 1961 | def __init__(self, value): |
| 1962 | self.value = int(value) |
| 1963 | def __cmp__(self, other): |
| 1964 | if isinstance(other, C): |
| 1965 | return cmp(self.value, other.value) |
| 1966 | if isinstance(other, int) or isinstance(other, long): |
| 1967 | return cmp(self.value, other) |
| 1968 | return NotImplemented |
| 1969 | c1 = C(1) |
| 1970 | c2 = C(2) |
| 1971 | c3 = C(3) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1972 | vereq(c1, 1) |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 1973 | c = {1: c1, 2: c2, 3: c3} |
| 1974 | for x in 1, 2, 3: |
| 1975 | for y in 1, 2, 3: |
| 1976 | verify(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 1977 | for op in "<", "<=", "==", "!=", ">", ">=": |
| 1978 | verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op), |
| 1979 | "x=%d, y=%d" % (x, y)) |
| 1980 | verify(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 1981 | verify(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 1982 | |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 1983 | def rich_comparisons(): |
| 1984 | if verbose: |
| 1985 | print "Testing rich comparisons..." |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 1986 | class Z(complex): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1987 | pass |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 1988 | z = Z(1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1989 | vereq(z, 1+0j) |
| 1990 | vereq(1+0j, z) |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 1991 | class ZZ(complex): |
| 1992 | def __eq__(self, other): |
| 1993 | try: |
| 1994 | return abs(self - other) <= 1e-6 |
| 1995 | except: |
| 1996 | return NotImplemented |
| 1997 | zz = ZZ(1.0000003) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1998 | vereq(zz, 1+0j) |
| 1999 | vereq(1+0j, zz) |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 2000 | |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2001 | class classic: |
| 2002 | pass |
| 2003 | for base in (classic, int, object, list): |
| 2004 | if verbose: print " (base = %s)" % base |
| 2005 | class C(base): |
| 2006 | def __init__(self, value): |
| 2007 | self.value = int(value) |
| 2008 | def __cmp__(self, other): |
| 2009 | raise TestFailed, "shouldn't call __cmp__" |
| 2010 | def __eq__(self, other): |
| 2011 | if isinstance(other, C): |
| 2012 | return self.value == other.value |
| 2013 | if isinstance(other, int) or isinstance(other, long): |
| 2014 | return self.value == other |
| 2015 | return NotImplemented |
| 2016 | def __ne__(self, other): |
| 2017 | if isinstance(other, C): |
| 2018 | return self.value != other.value |
| 2019 | if isinstance(other, int) or isinstance(other, long): |
| 2020 | return self.value != other |
| 2021 | return NotImplemented |
| 2022 | def __lt__(self, other): |
| 2023 | if isinstance(other, C): |
| 2024 | return self.value < other.value |
| 2025 | if isinstance(other, int) or isinstance(other, long): |
| 2026 | return self.value < other |
| 2027 | return NotImplemented |
| 2028 | def __le__(self, other): |
| 2029 | if isinstance(other, C): |
| 2030 | return self.value <= other.value |
| 2031 | if isinstance(other, int) or isinstance(other, long): |
| 2032 | return self.value <= other |
| 2033 | return NotImplemented |
| 2034 | def __gt__(self, other): |
| 2035 | if isinstance(other, C): |
| 2036 | return self.value > other.value |
| 2037 | if isinstance(other, int) or isinstance(other, long): |
| 2038 | return self.value > other |
| 2039 | return NotImplemented |
| 2040 | def __ge__(self, other): |
| 2041 | if isinstance(other, C): |
| 2042 | return self.value >= other.value |
| 2043 | if isinstance(other, int) or isinstance(other, long): |
| 2044 | return self.value >= other |
| 2045 | return NotImplemented |
| 2046 | c1 = C(1) |
| 2047 | c2 = C(2) |
| 2048 | c3 = C(3) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2049 | vereq(c1, 1) |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2050 | c = {1: c1, 2: c2, 3: c3} |
| 2051 | for x in 1, 2, 3: |
| 2052 | for y in 1, 2, 3: |
| 2053 | for op in "<", "<=", "==", "!=", ">", ">=": |
| 2054 | verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op), |
| 2055 | "x=%d, y=%d" % (x, y)) |
| 2056 | verify(eval("c[x] %s y" % op) == eval("x %s y" % op), |
| 2057 | "x=%d, y=%d" % (x, y)) |
| 2058 | verify(eval("x %s c[y]" % op) == eval("x %s y" % op), |
| 2059 | "x=%d, y=%d" % (x, y)) |
| 2060 | |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2061 | def coercions(): |
| 2062 | if verbose: print "Testing coercions..." |
| 2063 | class I(int): pass |
| 2064 | coerce(I(0), 0) |
| 2065 | coerce(0, I(0)) |
| 2066 | class L(long): pass |
| 2067 | coerce(L(0), 0) |
| 2068 | coerce(L(0), 0L) |
| 2069 | coerce(0, L(0)) |
| 2070 | coerce(0L, L(0)) |
| 2071 | class F(float): pass |
| 2072 | coerce(F(0), 0) |
| 2073 | coerce(F(0), 0L) |
| 2074 | coerce(F(0), 0.) |
| 2075 | coerce(0, F(0)) |
| 2076 | coerce(0L, F(0)) |
| 2077 | coerce(0., F(0)) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2078 | class C(complex): pass |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2079 | coerce(C(0), 0) |
| 2080 | coerce(C(0), 0L) |
| 2081 | coerce(C(0), 0.) |
| 2082 | coerce(C(0), 0j) |
| 2083 | coerce(0, C(0)) |
| 2084 | coerce(0L, C(0)) |
| 2085 | coerce(0., C(0)) |
| 2086 | coerce(0j, C(0)) |
| 2087 | |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2088 | def descrdoc(): |
| 2089 | if verbose: print "Testing descriptor doc strings..." |
| 2090 | def check(descr, what): |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2091 | vereq(descr.__doc__, what) |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2092 | check(file.closed, "flag set if the file is closed") # getset descriptor |
| 2093 | check(file.name, "file name") # member descriptor |
| 2094 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2095 | def setclass(): |
| 2096 | if verbose: print "Testing __class__ assignment..." |
| 2097 | class C(object): pass |
| 2098 | class D(object): pass |
| 2099 | class E(object): pass |
| 2100 | class F(D, E): pass |
| 2101 | for cls in C, D, E, F: |
| 2102 | for cls2 in C, D, E, F: |
| 2103 | x = cls() |
| 2104 | x.__class__ = cls2 |
| 2105 | verify(x.__class__ is cls2) |
| 2106 | x.__class__ = cls |
| 2107 | verify(x.__class__ is cls) |
| 2108 | def cant(x, C): |
| 2109 | try: |
| 2110 | x.__class__ = C |
| 2111 | except TypeError: |
| 2112 | pass |
| 2113 | else: |
| 2114 | raise TestFailed, "shouldn't allow %r.__class__ = %r" % (x, C) |
| 2115 | cant(C(), list) |
| 2116 | cant(list(), C) |
| 2117 | cant(C(), 1) |
| 2118 | cant(C(), object) |
| 2119 | cant(object(), list) |
| 2120 | cant(list(), object) |
| 2121 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 2122 | def setdict(): |
| 2123 | if verbose: print "Testing __dict__ assignment..." |
| 2124 | class C(object): pass |
| 2125 | a = C() |
| 2126 | a.__dict__ = {'b': 1} |
| 2127 | vereq(a.b, 1) |
| 2128 | def cant(x, dict): |
| 2129 | try: |
| 2130 | x.__dict__ = dict |
| 2131 | except TypeError: |
| 2132 | pass |
| 2133 | else: |
| 2134 | raise TestFailed, "shouldn't allow %r.__dict__ = %r" % (x, dict) |
| 2135 | cant(a, None) |
| 2136 | cant(a, []) |
| 2137 | cant(a, 1) |
| 2138 | try: |
| 2139 | del a.__dict__ |
| 2140 | except TypeError: |
| 2141 | pass |
| 2142 | else: |
| 2143 | raise TestFailed, "shouldn't allow del %r.__dict__" % (a) |
| 2144 | # Classes don't allow __dict__ assignment |
| 2145 | cant(C, {}) |
| 2146 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2147 | def pickles(): |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2148 | if verbose: |
| 2149 | print "Testing pickling and copying new-style classes and objects..." |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2150 | import pickle, cPickle |
| 2151 | |
| 2152 | def sorteditems(d): |
| 2153 | L = d.items() |
| 2154 | L.sort() |
| 2155 | return L |
| 2156 | |
| 2157 | global C |
| 2158 | class C(object): |
| 2159 | def __init__(self, a, b): |
| 2160 | super(C, self).__init__() |
| 2161 | self.a = a |
| 2162 | self.b = b |
| 2163 | def __repr__(self): |
| 2164 | return "C(%r, %r)" % (self.a, self.b) |
| 2165 | |
| 2166 | global C1 |
| 2167 | class C1(list): |
| 2168 | def __new__(cls, a, b): |
| 2169 | return super(C1, cls).__new__(cls) |
| 2170 | def __init__(self, a, b): |
| 2171 | self.a = a |
| 2172 | self.b = b |
| 2173 | def __repr__(self): |
| 2174 | return "C1(%r, %r)<%r>" % (self.a, self.b, list(self)) |
| 2175 | |
| 2176 | global C2 |
| 2177 | class C2(int): |
| 2178 | def __new__(cls, a, b, val=0): |
| 2179 | return super(C2, cls).__new__(cls, val) |
| 2180 | def __init__(self, a, b, val=0): |
| 2181 | self.a = a |
| 2182 | self.b = b |
| 2183 | def __repr__(self): |
| 2184 | return "C2(%r, %r)<%r>" % (self.a, self.b, int(self)) |
| 2185 | |
| 2186 | for p in pickle, cPickle: |
| 2187 | for bin in 0, 1: |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2188 | if verbose: |
| 2189 | print p.__name__, ["text", "binary"][bin] |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2190 | |
| 2191 | for cls in C, C1, C2: |
| 2192 | s = p.dumps(cls, bin) |
| 2193 | cls2 = p.loads(s) |
| 2194 | verify(cls2 is cls) |
| 2195 | |
| 2196 | a = C1(1, 2); a.append(42); a.append(24) |
| 2197 | b = C2("hello", "world", 42) |
| 2198 | s = p.dumps((a, b), bin) |
| 2199 | x, y = p.loads(s) |
| 2200 | assert x.__class__ == a.__class__ |
| 2201 | assert sorteditems(x.__dict__) == sorteditems(a.__dict__) |
| 2202 | assert y.__class__ == b.__class__ |
| 2203 | assert sorteditems(y.__dict__) == sorteditems(b.__dict__) |
| 2204 | assert `x` == `a` |
| 2205 | assert `y` == `b` |
| 2206 | if verbose: |
| 2207 | print "a = x =", a |
| 2208 | print "b = y =", b |
| 2209 | |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2210 | # Testing copy.deepcopy() |
| 2211 | if verbose: |
| 2212 | print "deepcopy" |
| 2213 | import copy |
| 2214 | for cls in C, C1, C2: |
| 2215 | cls2 = copy.deepcopy(cls) |
| 2216 | verify(cls2 is cls) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2217 | |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2218 | a = C1(1, 2); a.append(42); a.append(24) |
| 2219 | b = C2("hello", "world", 42) |
| 2220 | x, y = copy.deepcopy((a, b)) |
| 2221 | assert x.__class__ == a.__class__ |
| 2222 | assert sorteditems(x.__dict__) == sorteditems(a.__dict__) |
| 2223 | assert y.__class__ == b.__class__ |
| 2224 | assert sorteditems(y.__dict__) == sorteditems(b.__dict__) |
| 2225 | assert `x` == `a` |
| 2226 | assert `y` == `b` |
| 2227 | if verbose: |
| 2228 | print "a = x =", a |
| 2229 | print "b = y =", b |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2230 | |
| 2231 | def copies(): |
| 2232 | if verbose: print "Testing copy.copy() and copy.deepcopy()..." |
| 2233 | import copy |
| 2234 | class C(object): |
| 2235 | pass |
| 2236 | |
| 2237 | a = C() |
| 2238 | a.foo = 12 |
| 2239 | b = copy.copy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2240 | vereq(b.__dict__, a.__dict__) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2241 | |
| 2242 | a.bar = [1,2,3] |
| 2243 | c = copy.copy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2244 | vereq(c.bar, a.bar) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2245 | verify(c.bar is a.bar) |
| 2246 | |
| 2247 | d = copy.deepcopy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2248 | vereq(d.__dict__, a.__dict__) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2249 | a.bar.append(4) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2250 | vereq(d.bar, [1,2,3]) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2251 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2252 | def binopoverride(): |
| 2253 | if verbose: print "Testing overrides of binary operations..." |
| 2254 | class I(int): |
| 2255 | def __repr__(self): |
| 2256 | return "I(%r)" % int(self) |
| 2257 | def __add__(self, other): |
| 2258 | return I(int(self) + int(other)) |
| 2259 | __radd__ = __add__ |
| 2260 | def __pow__(self, other, mod=None): |
| 2261 | if mod is None: |
| 2262 | return I(pow(int(self), int(other))) |
| 2263 | else: |
| 2264 | return I(pow(int(self), int(other), int(mod))) |
| 2265 | def __rpow__(self, other, mod=None): |
| 2266 | if mod is None: |
| 2267 | return I(pow(int(other), int(self), mod)) |
| 2268 | else: |
| 2269 | return I(pow(int(other), int(self), int(mod))) |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2270 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2271 | vereq(`I(1) + I(2)`, "I(3)") |
| 2272 | vereq(`I(1) + 2`, "I(3)") |
| 2273 | vereq(`1 + I(2)`, "I(3)") |
| 2274 | vereq(`I(2) ** I(3)`, "I(8)") |
| 2275 | vereq(`2 ** I(3)`, "I(8)") |
| 2276 | vereq(`I(2) ** 3`, "I(8)") |
| 2277 | vereq(`pow(I(2), I(3), I(5))`, "I(3)") |
| 2278 | class S(str): |
| 2279 | def __eq__(self, other): |
| 2280 | return self.lower() == other.lower() |
| 2281 | |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2282 | def subclasspropagation(): |
| 2283 | if verbose: print "Testing propagation of slot functions to subclasses..." |
| 2284 | class A(object): |
| 2285 | pass |
| 2286 | class B(A): |
| 2287 | pass |
| 2288 | class C(A): |
| 2289 | pass |
| 2290 | class D(B, C): |
| 2291 | pass |
| 2292 | d = D() |
| 2293 | vereq(hash(d), id(d)) |
| 2294 | A.__hash__ = lambda self: 42 |
| 2295 | vereq(hash(d), 42) |
| 2296 | C.__hash__ = lambda self: 314 |
| 2297 | vereq(hash(d), 314) |
| 2298 | B.__hash__ = lambda self: 144 |
| 2299 | vereq(hash(d), 144) |
| 2300 | D.__hash__ = lambda self: 100 |
| 2301 | vereq(hash(d), 100) |
| 2302 | del D.__hash__ |
| 2303 | vereq(hash(d), 144) |
| 2304 | del B.__hash__ |
| 2305 | vereq(hash(d), 314) |
| 2306 | del C.__hash__ |
| 2307 | vereq(hash(d), 42) |
| 2308 | del A.__hash__ |
| 2309 | vereq(hash(d), id(d)) |
| 2310 | d.foo = 42 |
| 2311 | d.bar = 42 |
| 2312 | vereq(d.foo, 42) |
| 2313 | vereq(d.bar, 42) |
| 2314 | def __getattribute__(self, name): |
| 2315 | if name == "foo": |
| 2316 | return 24 |
| 2317 | return object.__getattribute__(self, name) |
| 2318 | A.__getattribute__ = __getattribute__ |
| 2319 | vereq(d.foo, 24) |
| 2320 | vereq(d.bar, 42) |
| 2321 | def __getattr__(self, name): |
| 2322 | if name in ("spam", "foo", "bar"): |
| 2323 | return "hello" |
| 2324 | raise AttributeError, name |
| 2325 | B.__getattr__ = __getattr__ |
| 2326 | vereq(d.spam, "hello") |
| 2327 | vereq(d.foo, 24) |
| 2328 | vereq(d.bar, 42) |
| 2329 | del A.__getattribute__ |
| 2330 | vereq(d.foo, 42) |
| 2331 | del d.foo |
| 2332 | vereq(d.foo, "hello") |
| 2333 | vereq(d.bar, 42) |
| 2334 | del B.__getattr__ |
| 2335 | try: |
| 2336 | d.foo |
| 2337 | except AttributeError: |
| 2338 | pass |
| 2339 | else: |
| 2340 | raise TestFailed, "d.foo should be undefined now" |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 2341 | |
| 2342 | def buffer_inherit(): |
| 2343 | import binascii |
| 2344 | # SF bug [#470040] ParseTuple t# vs subclasses. |
| 2345 | if verbose: |
| 2346 | print "Testing that buffer interface is inherited ..." |
| 2347 | |
| 2348 | class MyStr(str): |
| 2349 | pass |
| 2350 | base = 'abc' |
| 2351 | m = MyStr(base) |
| 2352 | # b2a_hex uses the buffer interface to get its argument's value, via |
| 2353 | # PyArg_ParseTuple 't#' code. |
| 2354 | vereq(binascii.b2a_hex(m), binascii.b2a_hex(base)) |
| 2355 | |
| 2356 | # It's not clear that unicode will continue to support the character |
| 2357 | # buffer interface, and this test will fail if that's taken away. |
| 2358 | class MyUni(unicode): |
| 2359 | pass |
| 2360 | base = u'abc' |
| 2361 | m = MyUni(base) |
| 2362 | vereq(binascii.b2a_hex(m), binascii.b2a_hex(base)) |
| 2363 | |
| 2364 | class MyInt(int): |
| 2365 | pass |
| 2366 | m = MyInt(42) |
| 2367 | try: |
| 2368 | binascii.b2a_hex(m) |
| 2369 | raise TestFailed('subclass of int should not have a buffer interface') |
| 2370 | except TypeError: |
| 2371 | pass |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2372 | |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 2373 | def str_of_str_subclass(): |
| 2374 | import binascii |
| 2375 | import cStringIO |
| 2376 | |
| 2377 | if verbose: |
| 2378 | print "Testing __str__ defined in subclass of str ..." |
| 2379 | |
| 2380 | class octetstring(str): |
| 2381 | def __str__(self): |
| 2382 | return binascii.b2a_hex(self) |
| 2383 | def __repr__(self): |
| 2384 | return self + " repr" |
| 2385 | |
| 2386 | o = octetstring('A') |
| 2387 | vereq(type(o), octetstring) |
| 2388 | vereq(type(str(o)), str) |
| 2389 | vereq(type(repr(o)), str) |
| 2390 | vereq(ord(o), 0x41) |
| 2391 | vereq(str(o), '41') |
| 2392 | vereq(repr(o), 'A repr') |
| 2393 | vereq(o.__str__(), '41') |
| 2394 | vereq(o.__repr__(), 'A repr') |
| 2395 | |
| 2396 | capture = cStringIO.StringIO() |
| 2397 | # Calling str() or not exercises different internal paths. |
| 2398 | print >> capture, o |
| 2399 | print >> capture, str(o) |
| 2400 | vereq(capture.getvalue(), '41\n41\n') |
| 2401 | capture.close() |
| 2402 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2403 | def kwdargs(): |
| 2404 | if verbose: print "Testing keyword arguments to __init__, __call__..." |
| 2405 | def f(a): return a |
| 2406 | vereq(f.__call__(a=42), 42) |
| 2407 | a = [] |
| 2408 | list.__init__(a, sequence=[0, 1, 2]) |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2409 | vereq(a, [0, 1, 2]) |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2410 | |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 2411 | def test_main(): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2412 | class_docstrings() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2413 | lists() |
| 2414 | dicts() |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2415 | dict_constructor() |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 2416 | test_dir() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2417 | ints() |
| 2418 | longs() |
| 2419 | floats() |
| 2420 | complexes() |
| 2421 | spamlists() |
| 2422 | spamdicts() |
| 2423 | pydicts() |
| 2424 | pylists() |
| 2425 | metaclass() |
| 2426 | pymods() |
| 2427 | multi() |
| 2428 | diamond() |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 2429 | objects() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2430 | slots() |
| 2431 | dynamics() |
| 2432 | errors() |
| 2433 | classmethods() |
| 2434 | staticmethods() |
| 2435 | classic() |
| 2436 | compattr() |
| 2437 | newslot() |
| 2438 | altmro() |
| 2439 | overloading() |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 2440 | methods() |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 2441 | specials() |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 2442 | weakrefs() |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 2443 | properties() |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 2444 | supers() |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2445 | inherits() |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 2446 | keywords() |
Tim Peters | 8fa4567 | 2001-09-13 21:01:29 +0000 | [diff] [blame] | 2447 | restricted() |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2448 | str_subclass_as_dict_key() |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 2449 | classic_comparisons() |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2450 | rich_comparisons() |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2451 | coercions() |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2452 | descrdoc() |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2453 | setclass() |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 2454 | setdict() |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2455 | pickles() |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2456 | copies() |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2457 | binopoverride() |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2458 | subclasspropagation() |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 2459 | buffer_inherit() |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 2460 | str_of_str_subclass() |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2461 | kwdargs() |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 2462 | if verbose: print "All OK" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2463 | |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 2464 | if __name__ == "__main__": |
| 2465 | test_main() |