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