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