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 | |
Neal Norwitz | 1a99750 | 2003-01-13 20:13:12 +0000 | [diff] [blame] | 3 | from test.test_support import verify, vereq, verbose, TestFailed, TESTFN, get_original_stdout |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4 | from copy import deepcopy |
Tim Peters | 4d9b466 | 2002-04-16 01:59:17 +0000 | [diff] [blame] | 5 | import warnings |
| 6 | |
| 7 | warnings.filterwarnings("ignore", |
| 8 | r'complex divmod\(\), // and % are deprecated$', |
Guido van Rossum | 155a34d | 2002-06-03 19:45:32 +0000 | [diff] [blame] | 9 | DeprecationWarning, r'(<string>|%s)$' % __name__) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 11 | def veris(a, b): |
| 12 | if a is not b: |
| 13 | raise TestFailed, "%r is %r" % (a, b) |
| 14 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 15 | def testunop(a, res, expr="len(a)", meth="__len__"): |
| 16 | if verbose: print "checking", expr |
| 17 | dict = {'a': a} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 18 | vereq(eval(expr, dict), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 19 | t = type(a) |
| 20 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 21 | while meth not in t.__dict__: |
| 22 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 23 | vereq(m, t.__dict__[meth]) |
| 24 | vereq(m(a), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 25 | bm = getattr(a, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 26 | vereq(bm(), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 27 | |
| 28 | def testbinop(a, b, res, expr="a+b", meth="__add__"): |
| 29 | if verbose: print "checking", expr |
| 30 | dict = {'a': a, 'b': b} |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 31 | |
| 32 | # XXX Hack so this passes before 2.3 when -Qnew is specified. |
| 33 | if meth == "__div__" and 1/2 == 0.5: |
| 34 | meth = "__truediv__" |
| 35 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 36 | vereq(eval(expr, dict), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 37 | t = type(a) |
| 38 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 39 | while meth not in t.__dict__: |
| 40 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 41 | vereq(m, t.__dict__[meth]) |
| 42 | vereq(m(a, b), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 43 | bm = getattr(a, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 44 | vereq(bm(b), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 45 | |
| 46 | def testternop(a, b, c, res, expr="a[b:c]", meth="__getslice__"): |
| 47 | if verbose: print "checking", expr |
| 48 | dict = {'a': a, 'b': b, 'c': c} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 49 | vereq(eval(expr, dict), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 50 | t = type(a) |
| 51 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 52 | while meth not in t.__dict__: |
| 53 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 54 | vereq(m, t.__dict__[meth]) |
| 55 | vereq(m(a, b, c), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 56 | bm = getattr(a, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 57 | vereq(bm(b, c), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 58 | |
| 59 | def testsetop(a, b, res, stmt="a+=b", meth="__iadd__"): |
| 60 | if verbose: print "checking", stmt |
| 61 | dict = {'a': deepcopy(a), 'b': b} |
| 62 | exec stmt in dict |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 63 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 64 | t = type(a) |
| 65 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 66 | while meth not in t.__dict__: |
| 67 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 68 | vereq(m, t.__dict__[meth]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 69 | dict['a'] = deepcopy(a) |
| 70 | m(dict['a'], b) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 71 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 72 | dict['a'] = deepcopy(a) |
| 73 | bm = getattr(dict['a'], meth) |
| 74 | bm(b) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 75 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 76 | |
| 77 | def testset2op(a, b, c, res, stmt="a[b]=c", meth="__setitem__"): |
| 78 | if verbose: print "checking", stmt |
| 79 | dict = {'a': deepcopy(a), 'b': b, 'c': c} |
| 80 | exec stmt in dict |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 81 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 82 | t = type(a) |
| 83 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 84 | while meth not in t.__dict__: |
| 85 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 86 | vereq(m, t.__dict__[meth]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 87 | dict['a'] = deepcopy(a) |
| 88 | m(dict['a'], b, c) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 89 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 90 | dict['a'] = deepcopy(a) |
| 91 | bm = getattr(dict['a'], meth) |
| 92 | bm(b, c) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 93 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 94 | |
| 95 | def testset3op(a, b, c, d, res, stmt="a[b:c]=d", meth="__setslice__"): |
| 96 | if verbose: print "checking", stmt |
| 97 | dict = {'a': deepcopy(a), 'b': b, 'c': c, 'd': d} |
| 98 | exec stmt in dict |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 99 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 100 | t = type(a) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 101 | while meth not in t.__dict__: |
| 102 | t = t.__bases__[0] |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 103 | m = getattr(t, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 104 | vereq(m, t.__dict__[meth]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 105 | dict['a'] = deepcopy(a) |
| 106 | m(dict['a'], b, c, d) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 107 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 108 | dict['a'] = deepcopy(a) |
| 109 | bm = getattr(dict['a'], meth) |
| 110 | bm(b, c, d) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 111 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 112 | |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 113 | def class_docstrings(): |
| 114 | class Classic: |
| 115 | "A classic docstring." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 116 | vereq(Classic.__doc__, "A classic docstring.") |
| 117 | vereq(Classic.__dict__['__doc__'], "A classic docstring.") |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 118 | |
| 119 | class Classic2: |
| 120 | pass |
| 121 | verify(Classic2.__doc__ is None) |
| 122 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 123 | class NewStatic(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 124 | "Another docstring." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 125 | vereq(NewStatic.__doc__, "Another docstring.") |
| 126 | vereq(NewStatic.__dict__['__doc__'], "Another docstring.") |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 127 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 128 | class NewStatic2(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 129 | pass |
| 130 | verify(NewStatic2.__doc__ is None) |
| 131 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 132 | class NewDynamic(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 133 | "Another docstring." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 134 | vereq(NewDynamic.__doc__, "Another docstring.") |
| 135 | vereq(NewDynamic.__dict__['__doc__'], "Another docstring.") |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 136 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 137 | class NewDynamic2(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 138 | pass |
| 139 | verify(NewDynamic2.__doc__ is None) |
| 140 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 141 | def lists(): |
| 142 | if verbose: print "Testing list operations..." |
| 143 | testbinop([1], [2], [1,2], "a+b", "__add__") |
| 144 | testbinop([1,2,3], 2, 1, "b in a", "__contains__") |
| 145 | testbinop([1,2,3], 4, 0, "b in a", "__contains__") |
| 146 | testbinop([1,2,3], 1, 2, "a[b]", "__getitem__") |
| 147 | testternop([1,2,3], 0, 2, [1,2], "a[b:c]", "__getslice__") |
| 148 | testsetop([1], [2], [1,2], "a+=b", "__iadd__") |
| 149 | testsetop([1,2], 3, [1,2,1,2,1,2], "a*=b", "__imul__") |
| 150 | testunop([1,2,3], 3, "len(a)", "__len__") |
| 151 | testbinop([1,2], 3, [1,2,1,2,1,2], "a*b", "__mul__") |
| 152 | testbinop([1,2], 3, [1,2,1,2,1,2], "b*a", "__rmul__") |
| 153 | testset2op([1,2], 1, 3, [1,3], "a[b]=c", "__setitem__") |
| 154 | testset3op([1,2,3,4], 1, 3, [5,6], [1,5,6,4], "a[b:c]=d", "__setslice__") |
| 155 | |
| 156 | def dicts(): |
| 157 | if verbose: print "Testing dict operations..." |
| 158 | testbinop({1:2}, {2:1}, -1, "cmp(a,b)", "__cmp__") |
| 159 | testbinop({1:2,3:4}, 1, 1, "b in a", "__contains__") |
| 160 | testbinop({1:2,3:4}, 2, 0, "b in a", "__contains__") |
| 161 | testbinop({1:2,3:4}, 1, 2, "a[b]", "__getitem__") |
| 162 | d = {1:2,3:4} |
| 163 | l1 = [] |
| 164 | for i in d.keys(): l1.append(i) |
| 165 | l = [] |
| 166 | for i in iter(d): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 167 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 168 | l = [] |
| 169 | for i in d.__iter__(): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 170 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 171 | l = [] |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 172 | for i in dict.__iter__(d): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 173 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 174 | d = {1:2, 3:4} |
| 175 | testunop(d, 2, "len(a)", "__len__") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 176 | vereq(eval(repr(d), {}), d) |
| 177 | vereq(eval(d.__repr__(), {}), d) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 178 | testset2op({1:2,3:4}, 2, 3, {1:2,2:3,3:4}, "a[b]=c", "__setitem__") |
| 179 | |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 180 | def dict_constructor(): |
| 181 | if verbose: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 182 | print "Testing dict constructor ..." |
| 183 | d = dict() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 184 | vereq(d, {}) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 185 | d = dict({}) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 186 | vereq(d, {}) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 187 | d = dict({1: 2, 'a': 'b'}) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 188 | vereq(d, {1: 2, 'a': 'b'}) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 189 | vereq(d, dict(d.items())) |
Just van Rossum | a797d81 | 2002-11-23 09:45:04 +0000 | [diff] [blame] | 190 | vereq(d, dict(d.iteritems())) |
| 191 | d = dict({'one':1, 'two':2}) |
| 192 | vereq(d, dict(one=1, two=2)) |
| 193 | vereq(d, dict(**d)) |
| 194 | vereq(d, dict({"one": 1}, two=2)) |
| 195 | vereq(d, dict([("two", 2)], one=1)) |
| 196 | vereq(d, dict([("one", 100), ("two", 200)], **d)) |
| 197 | verify(d is not dict(**d)) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 198 | for badarg in 0, 0L, 0j, "0", [0], (0,): |
| 199 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 200 | dict(badarg) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 201 | except TypeError: |
| 202 | pass |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 203 | except ValueError: |
| 204 | if badarg == "0": |
| 205 | # It's a sequence, and its elements are also sequences (gotta |
| 206 | # love strings <wink>), but they aren't of length 2, so this |
| 207 | # one seemed better as a ValueError than a TypeError. |
| 208 | pass |
| 209 | else: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 210 | raise TestFailed("no TypeError from dict(%r)" % badarg) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 211 | else: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 212 | raise TestFailed("no TypeError from dict(%r)" % badarg) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 213 | |
| 214 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 215 | dict({}, {}) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 216 | except TypeError: |
| 217 | pass |
| 218 | else: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 219 | raise TestFailed("no TypeError from dict({}, {})") |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 220 | |
| 221 | class Mapping: |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 222 | # Lacks a .keys() method; will be added later. |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 223 | dict = {1:2, 3:4, 'a':1j} |
| 224 | |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 225 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 226 | dict(Mapping()) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 227 | except TypeError: |
| 228 | pass |
| 229 | else: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 230 | raise TestFailed("no TypeError from dict(incomplete mapping)") |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 231 | |
| 232 | Mapping.keys = lambda self: self.dict.keys() |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 233 | Mapping.__getitem__ = lambda self, i: self.dict[i] |
Just van Rossum | a797d81 | 2002-11-23 09:45:04 +0000 | [diff] [blame] | 234 | d = dict(Mapping()) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 235 | vereq(d, Mapping.dict) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 236 | |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 237 | # Init from sequence of iterable objects, each producing a 2-sequence. |
| 238 | class AddressBookEntry: |
| 239 | def __init__(self, first, last): |
| 240 | self.first = first |
| 241 | self.last = last |
| 242 | def __iter__(self): |
| 243 | return iter([self.first, self.last]) |
| 244 | |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 245 | d = dict([AddressBookEntry('Tim', 'Warsaw'), |
Tim Peters | fe677e2 | 2001-10-30 05:41:07 +0000 | [diff] [blame] | 246 | AddressBookEntry('Barry', 'Peters'), |
| 247 | AddressBookEntry('Tim', 'Peters'), |
| 248 | AddressBookEntry('Barry', 'Warsaw')]) |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 249 | vereq(d, {'Barry': 'Warsaw', 'Tim': 'Peters'}) |
| 250 | |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 251 | d = dict(zip(range(4), range(1, 5))) |
| 252 | vereq(d, dict([(i, i+1) for i in range(4)])) |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 253 | |
| 254 | # Bad sequence lengths. |
Tim Peters | 9fda73c | 2001-10-26 20:57:38 +0000 | [diff] [blame] | 255 | for bad in [('tooshort',)], [('too', 'long', 'by 1')]: |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 256 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 257 | dict(bad) |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 258 | except ValueError: |
| 259 | pass |
| 260 | else: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 261 | raise TestFailed("no ValueError from dict(%r)" % bad) |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 262 | |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 263 | def test_dir(): |
| 264 | if verbose: |
| 265 | print "Testing dir() ..." |
| 266 | junk = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 267 | vereq(dir(), ['junk']) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 268 | del junk |
| 269 | |
| 270 | # Just make sure these don't blow up! |
| 271 | for arg in 2, 2L, 2j, 2e0, [2], "2", u"2", (2,), {2:2}, type, test_dir: |
| 272 | dir(arg) |
| 273 | |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 274 | # Try classic classes. |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 275 | class C: |
| 276 | Cdata = 1 |
| 277 | def Cmethod(self): pass |
| 278 | |
| 279 | cstuff = ['Cdata', 'Cmethod', '__doc__', '__module__'] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 280 | vereq(dir(C), cstuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 281 | verify('im_self' in dir(C.Cmethod)) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 282 | |
| 283 | 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] | 284 | vereq(dir(c), cstuff) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 285 | |
| 286 | c.cdata = 2 |
| 287 | c.cmethod = lambda self: 0 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 288 | vereq(dir(c), cstuff + ['cdata', 'cmethod']) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 289 | verify('im_self' in dir(c.Cmethod)) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 290 | |
| 291 | class A(C): |
| 292 | Adata = 1 |
| 293 | def Amethod(self): pass |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 294 | |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 295 | astuff = ['Adata', 'Amethod'] + cstuff |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 296 | vereq(dir(A), astuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 297 | verify('im_self' in dir(A.Amethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 298 | a = A() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 299 | vereq(dir(a), astuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 300 | verify('im_self' in dir(a.Amethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 301 | a.adata = 42 |
| 302 | a.amethod = lambda self: 3 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 303 | vereq(dir(a), astuff + ['adata', 'amethod']) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 304 | |
| 305 | # The same, but with new-style classes. Since these have object as a |
| 306 | # base class, a lot more gets sucked in. |
| 307 | def interesting(strings): |
| 308 | return [s for s in strings if not s.startswith('_')] |
| 309 | |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 310 | class C(object): |
| 311 | Cdata = 1 |
| 312 | def Cmethod(self): pass |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 313 | |
| 314 | cstuff = ['Cdata', 'Cmethod'] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 315 | vereq(interesting(dir(C)), cstuff) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 316 | |
| 317 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 318 | vereq(interesting(dir(c)), cstuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 319 | verify('im_self' in dir(C.Cmethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 320 | |
| 321 | c.cdata = 2 |
| 322 | c.cmethod = lambda self: 0 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 323 | vereq(interesting(dir(c)), cstuff + ['cdata', 'cmethod']) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 324 | verify('im_self' in dir(c.Cmethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 325 | |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 326 | class A(C): |
| 327 | Adata = 1 |
| 328 | def Amethod(self): pass |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 329 | |
| 330 | astuff = ['Adata', 'Amethod'] + cstuff |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 331 | vereq(interesting(dir(A)), astuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 332 | verify('im_self' in dir(A.Amethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 333 | a = A() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 334 | vereq(interesting(dir(a)), astuff) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 335 | a.adata = 42 |
| 336 | a.amethod = lambda self: 3 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 337 | vereq(interesting(dir(a)), astuff + ['adata', 'amethod']) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 338 | verify('im_self' in dir(a.Amethod)) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 339 | |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 340 | # Try a module subclass. |
| 341 | import sys |
| 342 | class M(type(sys)): |
| 343 | pass |
Guido van Rossum | 1bdd9b0 | 2002-06-04 06:10:37 +0000 | [diff] [blame] | 344 | minstance = M("m") |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 345 | minstance.b = 2 |
| 346 | minstance.a = 1 |
Guido van Rossum | 1bdd9b0 | 2002-06-04 06:10:37 +0000 | [diff] [blame] | 347 | names = [x for x in dir(minstance) if x not in ["__name__", "__doc__"]] |
| 348 | vereq(names, ['a', 'b']) |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 349 | |
| 350 | class M2(M): |
| 351 | def getdict(self): |
| 352 | return "Not a dict!" |
| 353 | __dict__ = property(getdict) |
| 354 | |
Guido van Rossum | 1bdd9b0 | 2002-06-04 06:10:37 +0000 | [diff] [blame] | 355 | m2instance = M2("m2") |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 356 | m2instance.b = 2 |
| 357 | m2instance.a = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 358 | vereq(m2instance.__dict__, "Not a dict!") |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 359 | try: |
| 360 | dir(m2instance) |
| 361 | except TypeError: |
| 362 | pass |
| 363 | |
Tim Peters | 9e6a399 | 2001-10-30 05:45:26 +0000 | [diff] [blame] | 364 | # Two essentially featureless objects, just inheriting stuff from |
| 365 | # object. |
| 366 | vereq(dir(None), dir(Ellipsis)) |
| 367 | |
Guido van Rossum | 4402241 | 2002-05-13 18:29:46 +0000 | [diff] [blame] | 368 | # Nasty test case for proxied objects |
| 369 | class Wrapper(object): |
| 370 | def __init__(self, obj): |
| 371 | self.__obj = obj |
| 372 | def __repr__(self): |
| 373 | return "Wrapper(%s)" % repr(self.__obj) |
| 374 | def __getitem__(self, key): |
| 375 | return Wrapper(self.__obj[key]) |
| 376 | def __len__(self): |
| 377 | return len(self.__obj) |
| 378 | def __getattr__(self, name): |
| 379 | return Wrapper(getattr(self.__obj, name)) |
| 380 | |
| 381 | class C(object): |
| 382 | def __getclass(self): |
| 383 | return Wrapper(type(self)) |
| 384 | __class__ = property(__getclass) |
| 385 | |
| 386 | dir(C()) # This used to segfault |
| 387 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 388 | binops = { |
| 389 | 'add': '+', |
| 390 | 'sub': '-', |
| 391 | 'mul': '*', |
| 392 | 'div': '/', |
| 393 | 'mod': '%', |
| 394 | 'divmod': 'divmod', |
| 395 | 'pow': '**', |
| 396 | 'lshift': '<<', |
| 397 | 'rshift': '>>', |
| 398 | 'and': '&', |
| 399 | 'xor': '^', |
| 400 | 'or': '|', |
| 401 | 'cmp': 'cmp', |
| 402 | 'lt': '<', |
| 403 | 'le': '<=', |
| 404 | 'eq': '==', |
| 405 | 'ne': '!=', |
| 406 | 'gt': '>', |
| 407 | 'ge': '>=', |
| 408 | } |
| 409 | |
| 410 | for name, expr in binops.items(): |
| 411 | if expr.islower(): |
| 412 | expr = expr + "(a, b)" |
| 413 | else: |
| 414 | expr = 'a %s b' % expr |
| 415 | binops[name] = expr |
| 416 | |
| 417 | unops = { |
| 418 | 'pos': '+', |
| 419 | 'neg': '-', |
| 420 | 'abs': 'abs', |
| 421 | 'invert': '~', |
| 422 | 'int': 'int', |
| 423 | 'long': 'long', |
| 424 | 'float': 'float', |
| 425 | 'oct': 'oct', |
| 426 | 'hex': 'hex', |
| 427 | } |
| 428 | |
| 429 | for name, expr in unops.items(): |
| 430 | if expr.islower(): |
| 431 | expr = expr + "(a)" |
| 432 | else: |
| 433 | expr = '%s a' % expr |
| 434 | unops[name] = expr |
| 435 | |
| 436 | def numops(a, b, skip=[]): |
| 437 | dict = {'a': a, 'b': b} |
| 438 | for name, expr in binops.items(): |
| 439 | if name not in skip: |
| 440 | name = "__%s__" % name |
| 441 | if hasattr(a, name): |
| 442 | res = eval(expr, dict) |
| 443 | testbinop(a, b, res, expr, name) |
| 444 | for name, expr in unops.items(): |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 445 | if name not in skip: |
| 446 | name = "__%s__" % name |
| 447 | if hasattr(a, name): |
| 448 | res = eval(expr, dict) |
| 449 | testunop(a, res, expr, name) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 450 | |
| 451 | def ints(): |
| 452 | if verbose: print "Testing int operations..." |
| 453 | numops(100, 3) |
Guido van Rossum | 15d529a | 2002-03-11 00:07:13 +0000 | [diff] [blame] | 454 | # The following crashes in Python 2.2 |
| 455 | vereq((1).__nonzero__(), 1) |
| 456 | vereq((0).__nonzero__(), 0) |
Guido van Rossum | c9e9e40 | 2002-03-11 13:21:25 +0000 | [diff] [blame] | 457 | # This returns 'NotImplemented' in Python 2.2 |
| 458 | class C(int): |
| 459 | def __add__(self, other): |
| 460 | return NotImplemented |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 461 | vereq(C(5L), 5) |
Guido van Rossum | c9e9e40 | 2002-03-11 13:21:25 +0000 | [diff] [blame] | 462 | try: |
| 463 | C() + "" |
| 464 | except TypeError: |
| 465 | pass |
| 466 | else: |
Neal Norwitz | 1af5e35 | 2002-03-11 14:44:12 +0000 | [diff] [blame] | 467 | raise TestFailed, "NotImplemented should have caused TypeError" |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 468 | import sys |
| 469 | try: |
| 470 | C(sys.maxint+1) |
| 471 | except OverflowError: |
| 472 | pass |
| 473 | else: |
| 474 | raise TestFailed, "should have raised OverflowError" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 475 | |
| 476 | def longs(): |
| 477 | if verbose: print "Testing long operations..." |
| 478 | numops(100L, 3L) |
| 479 | |
| 480 | def floats(): |
| 481 | if verbose: print "Testing float operations..." |
| 482 | numops(100.0, 3.0) |
| 483 | |
| 484 | def complexes(): |
| 485 | if verbose: print "Testing complex operations..." |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 486 | 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] | 487 | class Number(complex): |
| 488 | __slots__ = ['prec'] |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 489 | def __new__(cls, *args, **kwds): |
| 490 | result = complex.__new__(cls, *args) |
| 491 | result.prec = kwds.get('prec', 12) |
| 492 | return result |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 493 | def __repr__(self): |
| 494 | prec = self.prec |
| 495 | if self.imag == 0.0: |
| 496 | return "%.*g" % (prec, self.real) |
| 497 | if self.real == 0.0: |
| 498 | return "%.*gj" % (prec, self.imag) |
| 499 | return "(%.*g+%.*gj)" % (prec, self.real, prec, self.imag) |
| 500 | __str__ = __repr__ |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 501 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 502 | a = Number(3.14, prec=6) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame^] | 503 | vereq(repr(a), "3.14") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 504 | vereq(a.prec, 6) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 505 | |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 506 | a = Number(a, prec=2) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame^] | 507 | vereq(repr(a), "3.1") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 508 | vereq(a.prec, 2) |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 509 | |
| 510 | a = Number(234.5) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame^] | 511 | vereq(repr(a), "234.5") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 512 | vereq(a.prec, 12) |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 513 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 514 | def spamlists(): |
| 515 | if verbose: print "Testing spamlist operations..." |
| 516 | import copy, xxsubtype as spam |
| 517 | def spamlist(l, memo=None): |
| 518 | import xxsubtype as spam |
| 519 | return spam.spamlist(l) |
| 520 | # This is an ugly hack: |
| 521 | copy._deepcopy_dispatch[spam.spamlist] = spamlist |
| 522 | |
| 523 | testbinop(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+b", "__add__") |
| 524 | testbinop(spamlist([1,2,3]), 2, 1, "b in a", "__contains__") |
| 525 | testbinop(spamlist([1,2,3]), 4, 0, "b in a", "__contains__") |
| 526 | testbinop(spamlist([1,2,3]), 1, 2, "a[b]", "__getitem__") |
| 527 | testternop(spamlist([1,2,3]), 0, 2, spamlist([1,2]), |
| 528 | "a[b:c]", "__getslice__") |
| 529 | testsetop(spamlist([1]), spamlist([2]), spamlist([1,2]), |
| 530 | "a+=b", "__iadd__") |
| 531 | testsetop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*=b", "__imul__") |
| 532 | testunop(spamlist([1,2,3]), 3, "len(a)", "__len__") |
| 533 | testbinop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*b", "__mul__") |
| 534 | testbinop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "b*a", "__rmul__") |
| 535 | testset2op(spamlist([1,2]), 1, 3, spamlist([1,3]), "a[b]=c", "__setitem__") |
| 536 | testset3op(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]), |
| 537 | spamlist([1,5,6,4]), "a[b:c]=d", "__setslice__") |
| 538 | # Test subclassing |
| 539 | class C(spam.spamlist): |
| 540 | def foo(self): return 1 |
| 541 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 542 | vereq(a, []) |
| 543 | vereq(a.foo(), 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 544 | a.append(100) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 545 | vereq(a, [100]) |
| 546 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 547 | a.setstate(42) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 548 | vereq(a.getstate(), 42) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 549 | |
| 550 | def spamdicts(): |
| 551 | if verbose: print "Testing spamdict operations..." |
| 552 | import copy, xxsubtype as spam |
| 553 | def spamdict(d, memo=None): |
| 554 | import xxsubtype as spam |
| 555 | sd = spam.spamdict() |
| 556 | for k, v in d.items(): sd[k] = v |
| 557 | return sd |
| 558 | # This is an ugly hack: |
| 559 | copy._deepcopy_dispatch[spam.spamdict] = spamdict |
| 560 | |
| 561 | testbinop(spamdict({1:2}), spamdict({2:1}), -1, "cmp(a,b)", "__cmp__") |
| 562 | testbinop(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__") |
| 563 | testbinop(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__") |
| 564 | testbinop(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__") |
| 565 | d = spamdict({1:2,3:4}) |
| 566 | l1 = [] |
| 567 | for i in d.keys(): l1.append(i) |
| 568 | l = [] |
| 569 | for i in iter(d): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 570 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 571 | l = [] |
| 572 | for i in d.__iter__(): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 573 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 574 | l = [] |
| 575 | for i in type(spamdict({})).__iter__(d): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 576 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 577 | straightd = {1:2, 3:4} |
| 578 | spamd = spamdict(straightd) |
| 579 | testunop(spamd, 2, "len(a)", "__len__") |
| 580 | testunop(spamd, repr(straightd), "repr(a)", "__repr__") |
| 581 | testset2op(spamdict({1:2,3:4}), 2, 3, spamdict({1:2,2:3,3:4}), |
| 582 | "a[b]=c", "__setitem__") |
| 583 | # Test subclassing |
| 584 | class C(spam.spamdict): |
| 585 | def foo(self): return 1 |
| 586 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 587 | vereq(a.items(), []) |
| 588 | vereq(a.foo(), 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 589 | a['foo'] = 'bar' |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 590 | vereq(a.items(), [('foo', 'bar')]) |
| 591 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 592 | a.setstate(100) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 593 | vereq(a.getstate(), 100) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 594 | |
| 595 | def pydicts(): |
| 596 | if verbose: print "Testing Python subclass of dict..." |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 597 | verify(issubclass(dict, dict)) |
| 598 | verify(isinstance({}, dict)) |
| 599 | d = dict() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 600 | vereq(d, {}) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 601 | verify(d.__class__ is dict) |
| 602 | verify(isinstance(d, dict)) |
| 603 | class C(dict): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 604 | state = -1 |
| 605 | def __init__(self, *a, **kw): |
| 606 | if a: |
Guido van Rossum | 90c4514 | 2001-11-24 21:07:01 +0000 | [diff] [blame] | 607 | vereq(len(a), 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 608 | self.state = a[0] |
| 609 | if kw: |
| 610 | for k, v in kw.items(): self[v] = k |
| 611 | def __getitem__(self, key): |
| 612 | return self.get(key, 0) |
| 613 | def __setitem__(self, key, value): |
Guido van Rossum | 90c4514 | 2001-11-24 21:07:01 +0000 | [diff] [blame] | 614 | verify(isinstance(key, type(0))) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 615 | dict.__setitem__(self, key, value) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 616 | def setstate(self, state): |
| 617 | self.state = state |
| 618 | def getstate(self): |
| 619 | return self.state |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 620 | verify(issubclass(C, dict)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 621 | a1 = C(12) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 622 | vereq(a1.state, 12) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 623 | a2 = C(foo=1, bar=2) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 624 | vereq(a2[1] == 'foo' and a2[2], 'bar') |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 625 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 626 | vereq(a.state, -1) |
| 627 | vereq(a.getstate(), -1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 628 | a.setstate(0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 629 | vereq(a.state, 0) |
| 630 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 631 | a.setstate(10) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 632 | vereq(a.state, 10) |
| 633 | vereq(a.getstate(), 10) |
| 634 | vereq(a[42], 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 635 | a[42] = 24 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 636 | vereq(a[42], 24) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 637 | if verbose: print "pydict stress test ..." |
| 638 | N = 50 |
| 639 | for i in range(N): |
| 640 | a[i] = C() |
| 641 | for j in range(N): |
| 642 | a[i][j] = i*j |
| 643 | for i in range(N): |
| 644 | for j in range(N): |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 645 | vereq(a[i][j], i*j) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 646 | |
| 647 | def pylists(): |
| 648 | if verbose: print "Testing Python subclass of list..." |
| 649 | class C(list): |
| 650 | def __getitem__(self, i): |
| 651 | return list.__getitem__(self, i) + 100 |
| 652 | def __getslice__(self, i, j): |
| 653 | return (i, j) |
| 654 | a = C() |
| 655 | a.extend([0,1,2]) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 656 | vereq(a[0], 100) |
| 657 | vereq(a[1], 101) |
| 658 | vereq(a[2], 102) |
| 659 | vereq(a[100:200], (100,200)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 660 | |
| 661 | def metaclass(): |
| 662 | if verbose: print "Testing __metaclass__..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 663 | class C: |
| 664 | __metaclass__ = type |
| 665 | def __init__(self): |
| 666 | self.__state = 0 |
| 667 | def getstate(self): |
| 668 | return self.__state |
| 669 | def setstate(self, state): |
| 670 | self.__state = state |
| 671 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 672 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 673 | a.setstate(10) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 674 | vereq(a.getstate(), 10) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 675 | class D: |
| 676 | class __metaclass__(type): |
| 677 | def myself(cls): return cls |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 678 | vereq(D.myself(), D) |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 679 | d = D() |
| 680 | verify(d.__class__ is D) |
| 681 | class M1(type): |
| 682 | def __new__(cls, name, bases, dict): |
| 683 | dict['__spam__'] = 1 |
| 684 | return type.__new__(cls, name, bases, dict) |
| 685 | class C: |
| 686 | __metaclass__ = M1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 687 | vereq(C.__spam__, 1) |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 688 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 689 | vereq(c.__spam__, 1) |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 690 | |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 691 | class _instance(object): |
| 692 | pass |
| 693 | class M2(object): |
| 694 | def __new__(cls, name, bases, dict): |
| 695 | self = object.__new__(cls) |
| 696 | self.name = name |
| 697 | self.bases = bases |
| 698 | self.dict = dict |
| 699 | return self |
| 700 | __new__ = staticmethod(__new__) |
| 701 | def __call__(self): |
| 702 | it = _instance() |
Guido van Rossum | 7e1ff69 | 2001-08-17 11:55:58 +0000 | [diff] [blame] | 703 | # Early binding of methods |
| 704 | for key in self.dict: |
| 705 | if key.startswith("__"): |
| 706 | continue |
| 707 | setattr(it, key, self.dict[key].__get__(it, self)) |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 708 | return it |
| 709 | class C: |
| 710 | __metaclass__ = M2 |
| 711 | def spam(self): |
| 712 | return 42 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 713 | vereq(C.name, 'C') |
| 714 | vereq(C.bases, ()) |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 715 | verify('spam' in C.dict) |
| 716 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 717 | vereq(c.spam(), 42) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 718 | |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 719 | # More metaclass examples |
| 720 | |
| 721 | class autosuper(type): |
| 722 | # Automatically add __super to the class |
| 723 | # This trick only works for dynamic classes |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 724 | def __new__(metaclass, name, bases, dict): |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 725 | cls = super(autosuper, metaclass).__new__(metaclass, |
| 726 | name, bases, dict) |
Guido van Rossum | bfa47b0 | 2001-08-31 04:35:14 +0000 | [diff] [blame] | 727 | # Name mangling for __super removes leading underscores |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 728 | while name[:1] == "_": |
| 729 | name = name[1:] |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 730 | if name: |
| 731 | name = "_%s__super" % name |
| 732 | else: |
| 733 | name = "__super" |
| 734 | setattr(cls, name, super(cls)) |
| 735 | return cls |
| 736 | class A: |
| 737 | __metaclass__ = autosuper |
| 738 | def meth(self): |
| 739 | return "A" |
| 740 | class B(A): |
| 741 | def meth(self): |
| 742 | return "B" + self.__super.meth() |
| 743 | class C(A): |
| 744 | def meth(self): |
| 745 | return "C" + self.__super.meth() |
| 746 | class D(C, B): |
| 747 | def meth(self): |
| 748 | return "D" + self.__super.meth() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 749 | vereq(D().meth(), "DCBA") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 750 | class E(B, C): |
| 751 | def meth(self): |
| 752 | return "E" + self.__super.meth() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 753 | vereq(E().meth(), "EBCA") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 754 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 755 | class autoproperty(type): |
| 756 | # Automatically create property attributes when methods |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 757 | # named _get_x and/or _set_x are found |
| 758 | def __new__(metaclass, name, bases, dict): |
| 759 | hits = {} |
| 760 | for key, val in dict.iteritems(): |
| 761 | if key.startswith("_get_"): |
| 762 | key = key[5:] |
| 763 | get, set = hits.get(key, (None, None)) |
| 764 | get = val |
| 765 | hits[key] = get, set |
| 766 | elif key.startswith("_set_"): |
| 767 | key = key[5:] |
| 768 | get, set = hits.get(key, (None, None)) |
| 769 | set = val |
| 770 | hits[key] = get, set |
| 771 | for key, (get, set) in hits.iteritems(): |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 772 | dict[key] = property(get, set) |
| 773 | return super(autoproperty, metaclass).__new__(metaclass, |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 774 | name, bases, dict) |
| 775 | class A: |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 776 | __metaclass__ = autoproperty |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 777 | def _get_x(self): |
| 778 | return -self.__x |
| 779 | def _set_x(self, x): |
| 780 | self.__x = -x |
| 781 | a = A() |
| 782 | verify(not hasattr(a, "x")) |
| 783 | a.x = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 784 | vereq(a.x, 12) |
| 785 | vereq(a._A__x, -12) |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 786 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 787 | class multimetaclass(autoproperty, autosuper): |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 788 | # Merge of multiple cooperating metaclasses |
| 789 | pass |
| 790 | class A: |
| 791 | __metaclass__ = multimetaclass |
| 792 | def _get_x(self): |
| 793 | return "A" |
| 794 | class B(A): |
| 795 | def _get_x(self): |
| 796 | return "B" + self.__super._get_x() |
| 797 | class C(A): |
| 798 | def _get_x(self): |
| 799 | return "C" + self.__super._get_x() |
| 800 | class D(C, B): |
| 801 | def _get_x(self): |
| 802 | return "D" + self.__super._get_x() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 803 | vereq(D().x, "DCBA") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 804 | |
Guido van Rossum | f76de62 | 2001-10-18 15:49:21 +0000 | [diff] [blame] | 805 | # Make sure type(x) doesn't call x.__class__.__init__ |
| 806 | class T(type): |
| 807 | counter = 0 |
| 808 | def __init__(self, *args): |
| 809 | T.counter += 1 |
| 810 | class C: |
| 811 | __metaclass__ = T |
| 812 | vereq(T.counter, 1) |
| 813 | a = C() |
| 814 | vereq(type(a), C) |
| 815 | vereq(T.counter, 1) |
| 816 | |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 817 | class C(object): pass |
| 818 | c = C() |
| 819 | try: c() |
| 820 | except TypeError: pass |
Neal Norwitz | b1295da | 2002-04-01 18:59:20 +0000 | [diff] [blame] | 821 | else: raise TestFailed, "calling object w/o call method should raise TypeError" |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 822 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 823 | def pymods(): |
| 824 | if verbose: print "Testing Python subclass of module..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 825 | log = [] |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 826 | import sys |
| 827 | MT = type(sys) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 828 | class MM(MT): |
Guido van Rossum | 1bdd9b0 | 2002-06-04 06:10:37 +0000 | [diff] [blame] | 829 | def __init__(self, name): |
| 830 | MT.__init__(self, name) |
Guido van Rossum | 867a8d2 | 2001-09-21 19:29:08 +0000 | [diff] [blame] | 831 | def __getattribute__(self, name): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 832 | log.append(("getattr", name)) |
Guido van Rossum | 867a8d2 | 2001-09-21 19:29:08 +0000 | [diff] [blame] | 833 | return MT.__getattribute__(self, name) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 834 | def __setattr__(self, name, value): |
| 835 | log.append(("setattr", name, value)) |
| 836 | MT.__setattr__(self, name, value) |
| 837 | def __delattr__(self, name): |
| 838 | log.append(("delattr", name)) |
| 839 | MT.__delattr__(self, name) |
Guido van Rossum | 1bdd9b0 | 2002-06-04 06:10:37 +0000 | [diff] [blame] | 840 | a = MM("a") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 841 | a.foo = 12 |
| 842 | x = a.foo |
| 843 | del a.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 844 | vereq(log, [("setattr", "foo", 12), |
| 845 | ("getattr", "foo"), |
| 846 | ("delattr", "foo")]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 847 | |
| 848 | def multi(): |
| 849 | if verbose: print "Testing multiple inheritance..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 850 | class C(object): |
| 851 | def __init__(self): |
| 852 | self.__state = 0 |
| 853 | def getstate(self): |
| 854 | return self.__state |
| 855 | def setstate(self, state): |
| 856 | self.__state = state |
| 857 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 858 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 859 | a.setstate(10) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 860 | vereq(a.getstate(), 10) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 861 | class D(dict, C): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 862 | def __init__(self): |
| 863 | type({}).__init__(self) |
| 864 | C.__init__(self) |
| 865 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 866 | vereq(d.keys(), []) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 867 | d["hello"] = "world" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 868 | vereq(d.items(), [("hello", "world")]) |
| 869 | vereq(d["hello"], "world") |
| 870 | vereq(d.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 871 | d.setstate(10) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 872 | vereq(d.getstate(), 10) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 873 | vereq(D.__mro__, (D, dict, C, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 874 | |
Guido van Rossum | e45763a | 2001-08-10 21:28:46 +0000 | [diff] [blame] | 875 | # SF bug #442833 |
| 876 | class Node(object): |
| 877 | def __int__(self): |
| 878 | return int(self.foo()) |
| 879 | def foo(self): |
| 880 | return "23" |
| 881 | class Frag(Node, list): |
| 882 | def foo(self): |
| 883 | return "42" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 884 | vereq(Node().__int__(), 23) |
| 885 | vereq(int(Node()), 23) |
| 886 | vereq(Frag().__int__(), 42) |
| 887 | vereq(int(Frag()), 42) |
Guido van Rossum | e45763a | 2001-08-10 21:28:46 +0000 | [diff] [blame] | 888 | |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 889 | # MI mixing classic and new-style classes. |
Tim Peters | 144b98d | 2001-11-14 23:56:45 +0000 | [diff] [blame] | 890 | |
| 891 | class A: |
| 892 | x = 1 |
| 893 | |
| 894 | class B(A): |
| 895 | pass |
| 896 | |
| 897 | class C(A): |
| 898 | x = 2 |
| 899 | |
| 900 | class D(B, C): |
| 901 | pass |
| 902 | vereq(D.x, 1) |
| 903 | |
| 904 | # Classic MRO is preserved for a classic base class. |
| 905 | class E(D, object): |
| 906 | pass |
| 907 | vereq(E.__mro__, (E, D, B, A, C, object)) |
| 908 | vereq(E.x, 1) |
| 909 | |
| 910 | # But with a mix of classic bases, their MROs are combined using |
| 911 | # new-style MRO. |
| 912 | class F(B, C, object): |
| 913 | pass |
| 914 | vereq(F.__mro__, (F, B, C, A, object)) |
| 915 | vereq(F.x, 2) |
| 916 | |
| 917 | # Try something else. |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 918 | class C: |
| 919 | def cmethod(self): |
| 920 | return "C a" |
| 921 | def all_method(self): |
| 922 | return "C b" |
| 923 | |
| 924 | class M1(C, object): |
| 925 | def m1method(self): |
| 926 | return "M1 a" |
| 927 | def all_method(self): |
| 928 | return "M1 b" |
| 929 | |
| 930 | vereq(M1.__mro__, (M1, C, object)) |
| 931 | m = M1() |
| 932 | vereq(m.cmethod(), "C a") |
| 933 | vereq(m.m1method(), "M1 a") |
| 934 | vereq(m.all_method(), "M1 b") |
| 935 | |
| 936 | class D(C): |
| 937 | def dmethod(self): |
| 938 | return "D a" |
| 939 | def all_method(self): |
| 940 | return "D b" |
| 941 | |
Guido van Rossum | 9a81892 | 2002-11-14 19:50:14 +0000 | [diff] [blame] | 942 | class M2(D, object): |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 943 | def m2method(self): |
| 944 | return "M2 a" |
| 945 | def all_method(self): |
| 946 | return "M2 b" |
| 947 | |
Guido van Rossum | 9a81892 | 2002-11-14 19:50:14 +0000 | [diff] [blame] | 948 | vereq(M2.__mro__, (M2, D, C, object)) |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 949 | m = M2() |
| 950 | vereq(m.cmethod(), "C a") |
| 951 | vereq(m.dmethod(), "D a") |
| 952 | vereq(m.m2method(), "M2 a") |
| 953 | vereq(m.all_method(), "M2 b") |
| 954 | |
Guido van Rossum | 9a81892 | 2002-11-14 19:50:14 +0000 | [diff] [blame] | 955 | class M3(M1, M2, object): |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 956 | def m3method(self): |
| 957 | return "M3 a" |
| 958 | def all_method(self): |
| 959 | return "M3 b" |
Guido van Rossum | 9a81892 | 2002-11-14 19:50:14 +0000 | [diff] [blame] | 960 | vereq(M3.__mro__, (M3, M1, M2, D, C, object)) |
Tim Peters | 144b98d | 2001-11-14 23:56:45 +0000 | [diff] [blame] | 961 | m = M3() |
| 962 | vereq(m.cmethod(), "C a") |
| 963 | vereq(m.dmethod(), "D a") |
| 964 | vereq(m.m1method(), "M1 a") |
| 965 | vereq(m.m2method(), "M2 a") |
| 966 | vereq(m.m3method(), "M3 a") |
| 967 | vereq(m.all_method(), "M3 b") |
Tim Peters | a91e964 | 2001-11-14 23:32:33 +0000 | [diff] [blame] | 968 | |
Guido van Rossum | e54616c | 2001-12-14 04:19:56 +0000 | [diff] [blame] | 969 | class Classic: |
| 970 | pass |
| 971 | try: |
| 972 | class New(Classic): |
| 973 | __metaclass__ = type |
| 974 | except TypeError: |
| 975 | pass |
| 976 | else: |
| 977 | raise TestFailed, "new class with only classic bases - shouldn't be" |
| 978 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 979 | def diamond(): |
| 980 | if verbose: print "Testing multiple inheritance special cases..." |
| 981 | class A(object): |
| 982 | def spam(self): return "A" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 983 | vereq(A().spam(), "A") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 984 | class B(A): |
| 985 | def boo(self): return "B" |
| 986 | def spam(self): return "B" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 987 | vereq(B().spam(), "B") |
| 988 | vereq(B().boo(), "B") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 989 | class C(A): |
| 990 | def boo(self): return "C" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 991 | vereq(C().spam(), "A") |
| 992 | vereq(C().boo(), "C") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 993 | class D(B, C): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 994 | vereq(D().spam(), "B") |
| 995 | vereq(D().boo(), "B") |
| 996 | vereq(D.__mro__, (D, B, C, A, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 997 | class E(C, B): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 998 | vereq(E().spam(), "B") |
| 999 | vereq(E().boo(), "C") |
| 1000 | vereq(E.__mro__, (E, C, B, A, object)) |
Guido van Rossum | 9a81892 | 2002-11-14 19:50:14 +0000 | [diff] [blame] | 1001 | # MRO order disagreement |
| 1002 | try: |
| 1003 | class F(D, E): pass |
| 1004 | except TypeError: |
| 1005 | pass |
| 1006 | else: |
| 1007 | raise TestFailed, "expected MRO order disagreement (F)" |
| 1008 | try: |
| 1009 | class G(E, D): pass |
| 1010 | except TypeError: |
| 1011 | pass |
| 1012 | else: |
| 1013 | raise TestFailed, "expected MRO order disagreement (G)" |
| 1014 | |
| 1015 | |
| 1016 | # see thread python-dev/2002-October/029035.html |
| 1017 | def ex5(): |
| 1018 | if verbose: print "Testing ex5 from C3 switch discussion..." |
| 1019 | class A(object): pass |
| 1020 | class B(object): pass |
| 1021 | class C(object): pass |
| 1022 | class X(A): pass |
| 1023 | class Y(A): pass |
| 1024 | class Z(X,B,Y,C): pass |
| 1025 | vereq(Z.__mro__, (Z, X, B, Y, A, C, object)) |
| 1026 | |
| 1027 | # see "A Monotonic Superclass Linearization for Dylan", |
| 1028 | # by Kim Barrett et al. (OOPSLA 1996) |
| 1029 | def monotonicity(): |
| 1030 | if verbose: print "Testing MRO monotonicity..." |
| 1031 | class Boat(object): pass |
| 1032 | class DayBoat(Boat): pass |
| 1033 | class WheelBoat(Boat): pass |
| 1034 | class EngineLess(DayBoat): pass |
| 1035 | class SmallMultihull(DayBoat): pass |
| 1036 | class PedalWheelBoat(EngineLess,WheelBoat): pass |
| 1037 | class SmallCatamaran(SmallMultihull): pass |
| 1038 | class Pedalo(PedalWheelBoat,SmallCatamaran): pass |
| 1039 | |
| 1040 | vereq(PedalWheelBoat.__mro__, |
| 1041 | (PedalWheelBoat, EngineLess, DayBoat, WheelBoat, Boat, |
| 1042 | object)) |
| 1043 | vereq(SmallCatamaran.__mro__, |
| 1044 | (SmallCatamaran, SmallMultihull, DayBoat, Boat, object)) |
| 1045 | |
| 1046 | vereq(Pedalo.__mro__, |
| 1047 | (Pedalo, PedalWheelBoat, EngineLess, SmallCatamaran, |
| 1048 | SmallMultihull, DayBoat, WheelBoat, Boat, object)) |
| 1049 | |
| 1050 | # see "A Monotonic Superclass Linearization for Dylan", |
| 1051 | # by Kim Barrett et al. (OOPSLA 1996) |
| 1052 | def consistency_with_epg(): |
| 1053 | if verbose: print "Testing consistentcy with EPG..." |
| 1054 | class Pane(object): pass |
| 1055 | class ScrollingMixin(object): pass |
| 1056 | class EditingMixin(object): pass |
| 1057 | class ScrollablePane(Pane,ScrollingMixin): pass |
| 1058 | class EditablePane(Pane,EditingMixin): pass |
| 1059 | class EditableScrollablePane(ScrollablePane,EditablePane): pass |
| 1060 | |
| 1061 | vereq(EditableScrollablePane.__mro__, |
| 1062 | (EditableScrollablePane, ScrollablePane, EditablePane, |
| 1063 | Pane, ScrollingMixin, EditingMixin, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1064 | |
Raymond Hettinger | f394df4 | 2003-04-06 19:13:41 +0000 | [diff] [blame] | 1065 | mro_err_msg = """Cannot create a consistent method resolution |
| 1066 | order (MRO) for bases """ |
Raymond Hettinger | 83245b5 | 2003-03-12 04:25:42 +0000 | [diff] [blame] | 1067 | |
Guido van Rossum | d32047f | 2002-11-25 21:38:52 +0000 | [diff] [blame] | 1068 | def mro_disagreement(): |
| 1069 | if verbose: print "Testing error messages for MRO disagreement..." |
| 1070 | def raises(exc, expected, callable, *args): |
| 1071 | try: |
| 1072 | callable(*args) |
| 1073 | except exc, msg: |
Guido van Rossum | a01fa26 | 2002-11-27 04:00:59 +0000 | [diff] [blame] | 1074 | if not str(msg).startswith(expected): |
Guido van Rossum | d32047f | 2002-11-25 21:38:52 +0000 | [diff] [blame] | 1075 | raise TestFailed, "Message %r, expected %r" % (str(msg), |
| 1076 | expected) |
| 1077 | else: |
| 1078 | raise TestFailed, "Expected %s" % exc |
| 1079 | class A(object): pass |
| 1080 | class B(A): pass |
| 1081 | class C(object): pass |
| 1082 | # Test some very simple errors |
| 1083 | raises(TypeError, "duplicate base class A", |
| 1084 | type, "X", (A, A), {}) |
Raymond Hettinger | 83245b5 | 2003-03-12 04:25:42 +0000 | [diff] [blame] | 1085 | raises(TypeError, mro_err_msg, |
Guido van Rossum | d32047f | 2002-11-25 21:38:52 +0000 | [diff] [blame] | 1086 | type, "X", (A, B), {}) |
Raymond Hettinger | 83245b5 | 2003-03-12 04:25:42 +0000 | [diff] [blame] | 1087 | raises(TypeError, mro_err_msg, |
Guido van Rossum | d32047f | 2002-11-25 21:38:52 +0000 | [diff] [blame] | 1088 | type, "X", (A, C, B), {}) |
| 1089 | # Test a slightly more complex error |
| 1090 | class GridLayout(object): pass |
| 1091 | class HorizontalGrid(GridLayout): pass |
| 1092 | class VerticalGrid(GridLayout): pass |
| 1093 | class HVGrid(HorizontalGrid, VerticalGrid): pass |
| 1094 | class VHGrid(VerticalGrid, HorizontalGrid): pass |
Raymond Hettinger | 83245b5 | 2003-03-12 04:25:42 +0000 | [diff] [blame] | 1095 | raises(TypeError, mro_err_msg, |
Guido van Rossum | d32047f | 2002-11-25 21:38:52 +0000 | [diff] [blame] | 1096 | type, "ConfusedGrid", (HVGrid, VHGrid), {}) |
| 1097 | |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 1098 | def objects(): |
| 1099 | if verbose: print "Testing object class..." |
| 1100 | a = object() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1101 | vereq(a.__class__, object) |
| 1102 | vereq(type(a), object) |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 1103 | b = object() |
| 1104 | verify(a is not b) |
| 1105 | verify(not hasattr(a, "foo")) |
| 1106 | try: |
| 1107 | a.foo = 12 |
Guido van Rossum | 6d94627 | 2001-08-10 19:42:38 +0000 | [diff] [blame] | 1108 | except (AttributeError, TypeError): |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 1109 | pass |
| 1110 | else: |
| 1111 | verify(0, "object() should not allow setting a foo attribute") |
| 1112 | verify(not hasattr(object(), "__dict__")) |
| 1113 | |
| 1114 | class Cdict(object): |
| 1115 | pass |
| 1116 | x = Cdict() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1117 | vereq(x.__dict__, {}) |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 1118 | x.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1119 | vereq(x.foo, 1) |
| 1120 | vereq(x.__dict__, {'foo': 1}) |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 1121 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1122 | def slots(): |
| 1123 | if verbose: print "Testing __slots__..." |
| 1124 | class C0(object): |
| 1125 | __slots__ = [] |
| 1126 | x = C0() |
| 1127 | verify(not hasattr(x, "__dict__")) |
| 1128 | verify(not hasattr(x, "foo")) |
| 1129 | |
| 1130 | class C1(object): |
| 1131 | __slots__ = ['a'] |
| 1132 | x = C1() |
| 1133 | verify(not hasattr(x, "__dict__")) |
Guido van Rossum | 6b70599 | 2001-12-04 16:23:42 +0000 | [diff] [blame] | 1134 | verify(not hasattr(x, "a")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1135 | x.a = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1136 | vereq(x.a, 1) |
Guido van Rossum | 6b70599 | 2001-12-04 16:23:42 +0000 | [diff] [blame] | 1137 | x.a = None |
| 1138 | veris(x.a, None) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1139 | del x.a |
Guido van Rossum | 6b70599 | 2001-12-04 16:23:42 +0000 | [diff] [blame] | 1140 | verify(not hasattr(x, "a")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1141 | |
| 1142 | class C3(object): |
| 1143 | __slots__ = ['a', 'b', 'c'] |
| 1144 | x = C3() |
| 1145 | verify(not hasattr(x, "__dict__")) |
Guido van Rossum | 6b70599 | 2001-12-04 16:23:42 +0000 | [diff] [blame] | 1146 | verify(not hasattr(x, 'a')) |
| 1147 | verify(not hasattr(x, 'b')) |
| 1148 | verify(not hasattr(x, 'c')) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1149 | x.a = 1 |
| 1150 | x.b = 2 |
| 1151 | x.c = 3 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1152 | vereq(x.a, 1) |
| 1153 | vereq(x.b, 2) |
| 1154 | vereq(x.c, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1155 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1156 | class C4(object): |
| 1157 | """Validate name mangling""" |
| 1158 | __slots__ = ['__a'] |
| 1159 | def __init__(self, value): |
| 1160 | self.__a = value |
| 1161 | def get(self): |
| 1162 | return self.__a |
| 1163 | x = C4(5) |
| 1164 | verify(not hasattr(x, '__dict__')) |
| 1165 | verify(not hasattr(x, '__a')) |
| 1166 | vereq(x.get(), 5) |
| 1167 | try: |
| 1168 | x.__a = 6 |
| 1169 | except AttributeError: |
| 1170 | pass |
| 1171 | else: |
| 1172 | raise TestFailed, "Double underscored names not mangled" |
| 1173 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1174 | # Make sure slot names are proper identifiers |
| 1175 | try: |
| 1176 | class C(object): |
| 1177 | __slots__ = [None] |
| 1178 | except TypeError: |
| 1179 | pass |
| 1180 | else: |
| 1181 | raise TestFailed, "[None] slots not caught" |
| 1182 | try: |
| 1183 | class C(object): |
| 1184 | __slots__ = ["foo bar"] |
| 1185 | except TypeError: |
| 1186 | pass |
| 1187 | else: |
| 1188 | raise TestFailed, "['foo bar'] slots not caught" |
| 1189 | try: |
| 1190 | class C(object): |
| 1191 | __slots__ = ["foo\0bar"] |
| 1192 | except TypeError: |
| 1193 | pass |
| 1194 | else: |
| 1195 | raise TestFailed, "['foo\\0bar'] slots not caught" |
| 1196 | try: |
| 1197 | class C(object): |
| 1198 | __slots__ = ["1"] |
| 1199 | except TypeError: |
| 1200 | pass |
| 1201 | else: |
| 1202 | raise TestFailed, "['1'] slots not caught" |
| 1203 | try: |
| 1204 | class C(object): |
| 1205 | __slots__ = [""] |
| 1206 | except TypeError: |
| 1207 | pass |
| 1208 | else: |
| 1209 | raise TestFailed, "[''] slots not caught" |
| 1210 | class C(object): |
| 1211 | __slots__ = ["a", "a_b", "_a", "A0123456789Z"] |
| 1212 | |
Guido van Rossum | 33bab01 | 2001-12-05 22:45:48 +0000 | [diff] [blame] | 1213 | # Test leaks |
| 1214 | class Counted(object): |
| 1215 | counter = 0 # counts the number of instances alive |
| 1216 | def __init__(self): |
| 1217 | Counted.counter += 1 |
| 1218 | def __del__(self): |
| 1219 | Counted.counter -= 1 |
| 1220 | class C(object): |
| 1221 | __slots__ = ['a', 'b', 'c'] |
| 1222 | x = C() |
| 1223 | x.a = Counted() |
| 1224 | x.b = Counted() |
| 1225 | x.c = Counted() |
| 1226 | vereq(Counted.counter, 3) |
| 1227 | del x |
| 1228 | vereq(Counted.counter, 0) |
| 1229 | class D(C): |
| 1230 | pass |
| 1231 | x = D() |
| 1232 | x.a = Counted() |
| 1233 | x.z = Counted() |
| 1234 | vereq(Counted.counter, 2) |
| 1235 | del x |
| 1236 | vereq(Counted.counter, 0) |
| 1237 | class E(D): |
| 1238 | __slots__ = ['e'] |
| 1239 | x = E() |
| 1240 | x.a = Counted() |
| 1241 | x.z = Counted() |
| 1242 | x.e = Counted() |
| 1243 | vereq(Counted.counter, 3) |
| 1244 | del x |
| 1245 | vereq(Counted.counter, 0) |
| 1246 | |
Guido van Rossum | 9923ffe | 2002-06-04 19:52:53 +0000 | [diff] [blame] | 1247 | # Test cyclical leaks [SF bug 519621] |
| 1248 | class F(object): |
| 1249 | __slots__ = ['a', 'b'] |
| 1250 | log = [] |
| 1251 | s = F() |
| 1252 | s.a = [Counted(), s] |
| 1253 | vereq(Counted.counter, 1) |
| 1254 | s = None |
| 1255 | import gc |
| 1256 | gc.collect() |
| 1257 | vereq(Counted.counter, 0) |
| 1258 | |
Raymond Hettinger | ab5dae3 | 2002-06-24 13:08:16 +0000 | [diff] [blame] | 1259 | # Test lookup leaks [SF bug 572567] |
| 1260 | import sys,gc |
| 1261 | class G(object): |
| 1262 | def __cmp__(self, other): |
| 1263 | return 0 |
| 1264 | g = G() |
| 1265 | orig_objects = len(gc.get_objects()) |
| 1266 | for i in xrange(10): |
| 1267 | g==g |
| 1268 | new_objects = len(gc.get_objects()) |
| 1269 | vereq(orig_objects, new_objects) |
Neal Norwitz | 98a379e | 2003-06-16 22:51:22 +0000 | [diff] [blame] | 1270 | class H(object): |
| 1271 | __slots__ = ['a', 'b'] |
| 1272 | def __init__(self): |
| 1273 | self.a = 1 |
| 1274 | self.b = 2 |
| 1275 | def __del__(self): |
| 1276 | assert self.a == 1 |
| 1277 | assert self.b == 2 |
| 1278 | |
| 1279 | save_stderr = sys.stderr |
| 1280 | sys.stderr = sys.stdout |
| 1281 | h = H() |
| 1282 | try: |
| 1283 | del h |
| 1284 | finally: |
| 1285 | sys.stderr = save_stderr |
Raymond Hettinger | ab5dae3 | 2002-06-24 13:08:16 +0000 | [diff] [blame] | 1286 | |
Guido van Rossum | 8b056da | 2002-08-13 18:26:26 +0000 | [diff] [blame] | 1287 | def slotspecials(): |
| 1288 | if verbose: print "Testing __dict__ and __weakref__ in __slots__..." |
| 1289 | |
| 1290 | class D(object): |
| 1291 | __slots__ = ["__dict__"] |
| 1292 | a = D() |
| 1293 | verify(hasattr(a, "__dict__")) |
| 1294 | verify(not hasattr(a, "__weakref__")) |
| 1295 | a.foo = 42 |
| 1296 | vereq(a.__dict__, {"foo": 42}) |
| 1297 | |
| 1298 | class W(object): |
| 1299 | __slots__ = ["__weakref__"] |
| 1300 | a = W() |
| 1301 | verify(hasattr(a, "__weakref__")) |
| 1302 | verify(not hasattr(a, "__dict__")) |
| 1303 | try: |
| 1304 | a.foo = 42 |
| 1305 | except AttributeError: |
| 1306 | pass |
| 1307 | else: |
| 1308 | raise TestFailed, "shouldn't be allowed to set a.foo" |
| 1309 | |
| 1310 | class C1(W, D): |
| 1311 | __slots__ = [] |
| 1312 | a = C1() |
| 1313 | verify(hasattr(a, "__dict__")) |
| 1314 | verify(hasattr(a, "__weakref__")) |
| 1315 | a.foo = 42 |
| 1316 | vereq(a.__dict__, {"foo": 42}) |
| 1317 | |
| 1318 | class C2(D, W): |
| 1319 | __slots__ = [] |
| 1320 | a = C2() |
| 1321 | verify(hasattr(a, "__dict__")) |
| 1322 | verify(hasattr(a, "__weakref__")) |
| 1323 | a.foo = 42 |
| 1324 | vereq(a.__dict__, {"foo": 42}) |
| 1325 | |
Guido van Rossum | 9a81892 | 2002-11-14 19:50:14 +0000 | [diff] [blame] | 1326 | # MRO order disagreement |
| 1327 | # |
| 1328 | # class C3(C1, C2): |
| 1329 | # __slots__ = [] |
| 1330 | # |
| 1331 | # class C4(C2, C1): |
| 1332 | # __slots__ = [] |
Guido van Rossum | 8b056da | 2002-08-13 18:26:26 +0000 | [diff] [blame] | 1333 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1334 | def dynamics(): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1335 | if verbose: print "Testing class attribute propagation..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1336 | class D(object): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1337 | pass |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1338 | class E(D): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1339 | pass |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1340 | class F(D): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1341 | pass |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1342 | D.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1343 | vereq(D.foo, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1344 | # Test that dynamic attributes are inherited |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1345 | vereq(E.foo, 1) |
| 1346 | vereq(F.foo, 1) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 1347 | # Test dynamic instances |
| 1348 | class C(object): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1349 | pass |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 1350 | a = C() |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1351 | verify(not hasattr(a, "foobar")) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 1352 | C.foobar = 2 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1353 | vereq(a.foobar, 2) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 1354 | C.method = lambda self: 42 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1355 | vereq(a.method(), 42) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 1356 | C.__repr__ = lambda self: "C()" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1357 | vereq(repr(a), "C()") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1358 | C.__int__ = lambda self: 100 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1359 | vereq(int(a), 100) |
| 1360 | vereq(a.foobar, 2) |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1361 | verify(not hasattr(a, "spam")) |
| 1362 | def mygetattr(self, name): |
| 1363 | if name == "spam": |
| 1364 | return "spam" |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 1365 | raise AttributeError |
| 1366 | C.__getattr__ = mygetattr |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1367 | vereq(a.spam, "spam") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1368 | a.new = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1369 | vereq(a.new, 12) |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1370 | def mysetattr(self, name, value): |
| 1371 | if name == "spam": |
| 1372 | raise AttributeError |
| 1373 | return object.__setattr__(self, name, value) |
| 1374 | C.__setattr__ = mysetattr |
| 1375 | try: |
| 1376 | a.spam = "not spam" |
| 1377 | except AttributeError: |
| 1378 | pass |
| 1379 | else: |
| 1380 | verify(0, "expected AttributeError") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1381 | vereq(a.spam, "spam") |
Guido van Rossum | 80e3675 | 2001-08-14 20:00:33 +0000 | [diff] [blame] | 1382 | class D(C): |
| 1383 | pass |
| 1384 | d = D() |
| 1385 | d.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1386 | vereq(d.foo, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1387 | |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 1388 | # Test handling of int*seq and seq*int |
| 1389 | class I(int): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1390 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1391 | vereq("a"*I(2), "aa") |
| 1392 | vereq(I(2)*"a", "aa") |
| 1393 | vereq(2*I(3), 6) |
| 1394 | vereq(I(3)*2, 6) |
| 1395 | vereq(I(3)*I(2), 6) |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 1396 | |
| 1397 | # Test handling of long*seq and seq*long |
| 1398 | class L(long): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1399 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1400 | vereq("a"*L(2L), "aa") |
| 1401 | vereq(L(2L)*"a", "aa") |
| 1402 | vereq(2*L(3), 6) |
| 1403 | vereq(L(3)*2, 6) |
| 1404 | vereq(L(3)*L(2), 6) |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 1405 | |
Guido van Rossum | 3d45d8f | 2001-09-24 18:47:40 +0000 | [diff] [blame] | 1406 | # Test comparison of classes with dynamic metaclasses |
| 1407 | class dynamicmetaclass(type): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1408 | pass |
Guido van Rossum | 3d45d8f | 2001-09-24 18:47:40 +0000 | [diff] [blame] | 1409 | class someclass: |
| 1410 | __metaclass__ = dynamicmetaclass |
| 1411 | verify(someclass != object) |
| 1412 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1413 | def errors(): |
| 1414 | if verbose: print "Testing errors..." |
| 1415 | |
| 1416 | try: |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 1417 | class C(list, dict): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1418 | pass |
| 1419 | except TypeError: |
| 1420 | pass |
| 1421 | else: |
| 1422 | verify(0, "inheritance from both list and dict should be illegal") |
| 1423 | |
| 1424 | try: |
| 1425 | class C(object, None): |
| 1426 | pass |
| 1427 | except TypeError: |
| 1428 | pass |
| 1429 | else: |
| 1430 | verify(0, "inheritance from non-type should be illegal") |
| 1431 | class Classic: |
| 1432 | pass |
| 1433 | |
| 1434 | try: |
Guido van Rossum | 8aea0cc | 2001-08-29 15:48:43 +0000 | [diff] [blame] | 1435 | class C(type(len)): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1436 | pass |
| 1437 | except TypeError: |
| 1438 | pass |
| 1439 | else: |
Guido van Rossum | 8aea0cc | 2001-08-29 15:48:43 +0000 | [diff] [blame] | 1440 | verify(0, "inheritance from CFunction should be illegal") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1441 | |
| 1442 | try: |
| 1443 | class C(object): |
| 1444 | __slots__ = 1 |
| 1445 | except TypeError: |
| 1446 | pass |
| 1447 | else: |
| 1448 | verify(0, "__slots__ = 1 should be illegal") |
| 1449 | |
| 1450 | try: |
| 1451 | class C(object): |
| 1452 | __slots__ = [1] |
| 1453 | except TypeError: |
| 1454 | pass |
| 1455 | else: |
| 1456 | verify(0, "__slots__ = [1] should be illegal") |
| 1457 | |
| 1458 | def classmethods(): |
| 1459 | if verbose: print "Testing class methods..." |
| 1460 | class C(object): |
| 1461 | def foo(*a): return a |
| 1462 | goo = classmethod(foo) |
| 1463 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1464 | vereq(C.goo(1), (C, 1)) |
| 1465 | vereq(c.goo(1), (C, 1)) |
| 1466 | vereq(c.foo(1), (c, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1467 | class D(C): |
| 1468 | pass |
| 1469 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1470 | vereq(D.goo(1), (D, 1)) |
| 1471 | vereq(d.goo(1), (D, 1)) |
| 1472 | vereq(d.foo(1), (d, 1)) |
| 1473 | vereq(D.foo(d, 1), (d, 1)) |
Guido van Rossum | 7e30548 | 2002-03-18 03:09:06 +0000 | [diff] [blame] | 1474 | # Test for a specific crash (SF bug 528132) |
| 1475 | def f(cls, arg): return (cls, arg) |
| 1476 | ff = classmethod(f) |
| 1477 | vereq(ff.__get__(0, int)(42), (int, 42)) |
| 1478 | vereq(ff.__get__(0)(42), (int, 42)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1479 | |
Guido van Rossum | 155db9a | 2002-04-02 17:53:47 +0000 | [diff] [blame] | 1480 | # Test super() with classmethods (SF bug 535444) |
| 1481 | veris(C.goo.im_self, C) |
| 1482 | veris(D.goo.im_self, D) |
| 1483 | veris(super(D,D).goo.im_self, D) |
| 1484 | veris(super(D,d).goo.im_self, D) |
| 1485 | vereq(super(D,D).goo(), (D,)) |
| 1486 | vereq(super(D,d).goo(), (D,)) |
| 1487 | |
Raymond Hettinger | be97153 | 2003-06-18 01:13:41 +0000 | [diff] [blame] | 1488 | # Verify that argument is checked for callability (SF bug 753451) |
| 1489 | try: |
| 1490 | classmethod(1).__get__(1) |
| 1491 | except TypeError: |
| 1492 | pass |
| 1493 | else: |
| 1494 | raise TestFailed, "classmethod should check for callability" |
| 1495 | |
Fred Drake | f841aa6 | 2002-03-28 15:49:54 +0000 | [diff] [blame] | 1496 | def classmethods_in_c(): |
| 1497 | if verbose: print "Testing C-based class methods..." |
| 1498 | import xxsubtype as spam |
| 1499 | a = (1, 2, 3) |
| 1500 | d = {'abc': 123} |
| 1501 | x, a1, d1 = spam.spamlist.classmeth(*a, **d) |
Tim Peters | bca1cbc | 2002-12-09 22:56:13 +0000 | [diff] [blame] | 1502 | veris(x, spam.spamlist) |
| 1503 | vereq(a, a1) |
Fred Drake | f841aa6 | 2002-03-28 15:49:54 +0000 | [diff] [blame] | 1504 | vereq(d, d1) |
| 1505 | x, a1, d1 = spam.spamlist().classmeth(*a, **d) |
Tim Peters | bca1cbc | 2002-12-09 22:56:13 +0000 | [diff] [blame] | 1506 | veris(x, spam.spamlist) |
| 1507 | vereq(a, a1) |
Fred Drake | f841aa6 | 2002-03-28 15:49:54 +0000 | [diff] [blame] | 1508 | vereq(d, d1) |
| 1509 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1510 | def staticmethods(): |
| 1511 | if verbose: print "Testing static methods..." |
| 1512 | class C(object): |
| 1513 | def foo(*a): return a |
| 1514 | goo = staticmethod(foo) |
| 1515 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1516 | vereq(C.goo(1), (1,)) |
| 1517 | vereq(c.goo(1), (1,)) |
| 1518 | vereq(c.foo(1), (c, 1,)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1519 | class D(C): |
| 1520 | pass |
| 1521 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1522 | vereq(D.goo(1), (1,)) |
| 1523 | vereq(d.goo(1), (1,)) |
| 1524 | vereq(d.foo(1), (d, 1)) |
| 1525 | vereq(D.foo(d, 1), (d, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1526 | |
Fred Drake | f841aa6 | 2002-03-28 15:49:54 +0000 | [diff] [blame] | 1527 | def staticmethods_in_c(): |
| 1528 | if verbose: print "Testing C-based static methods..." |
| 1529 | import xxsubtype as spam |
| 1530 | a = (1, 2, 3) |
| 1531 | d = {"abc": 123} |
| 1532 | x, a1, d1 = spam.spamlist.staticmeth(*a, **d) |
| 1533 | veris(x, None) |
| 1534 | vereq(a, a1) |
| 1535 | vereq(d, d1) |
| 1536 | x, a1, d2 = spam.spamlist().staticmeth(*a, **d) |
| 1537 | veris(x, None) |
| 1538 | vereq(a, a1) |
| 1539 | vereq(d, d1) |
| 1540 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1541 | def classic(): |
| 1542 | if verbose: print "Testing classic classes..." |
| 1543 | class C: |
| 1544 | def foo(*a): return a |
| 1545 | goo = classmethod(foo) |
| 1546 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1547 | vereq(C.goo(1), (C, 1)) |
| 1548 | vereq(c.goo(1), (C, 1)) |
| 1549 | vereq(c.foo(1), (c, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1550 | class D(C): |
| 1551 | pass |
| 1552 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1553 | vereq(D.goo(1), (D, 1)) |
| 1554 | vereq(d.goo(1), (D, 1)) |
| 1555 | vereq(d.foo(1), (d, 1)) |
| 1556 | vereq(D.foo(d, 1), (d, 1)) |
Guido van Rossum | 9301876 | 2001-08-17 13:40:47 +0000 | [diff] [blame] | 1557 | class E: # *not* subclassing from C |
| 1558 | foo = C.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1559 | vereq(E().foo, C.foo) # i.e., unbound |
Guido van Rossum | 84a79a8 | 2001-08-17 13:58:31 +0000 | [diff] [blame] | 1560 | verify(repr(C.foo.__get__(C())).startswith("<bound method ")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1561 | |
| 1562 | def compattr(): |
| 1563 | if verbose: print "Testing computed attributes..." |
| 1564 | class C(object): |
| 1565 | class computed_attribute(object): |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 1566 | def __init__(self, get, set=None, delete=None): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1567 | self.__get = get |
| 1568 | self.__set = set |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 1569 | self.__delete = delete |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1570 | def __get__(self, obj, type=None): |
| 1571 | return self.__get(obj) |
| 1572 | def __set__(self, obj, value): |
| 1573 | return self.__set(obj, value) |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 1574 | def __delete__(self, obj): |
| 1575 | return self.__delete(obj) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1576 | def __init__(self): |
| 1577 | self.__x = 0 |
| 1578 | def __get_x(self): |
| 1579 | x = self.__x |
| 1580 | self.__x = x+1 |
| 1581 | return x |
| 1582 | def __set_x(self, x): |
| 1583 | self.__x = x |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 1584 | def __delete_x(self): |
| 1585 | del self.__x |
| 1586 | x = computed_attribute(__get_x, __set_x, __delete_x) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1587 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1588 | vereq(a.x, 0) |
| 1589 | vereq(a.x, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1590 | a.x = 10 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1591 | vereq(a.x, 10) |
| 1592 | vereq(a.x, 11) |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 1593 | del a.x |
| 1594 | vereq(hasattr(a, 'x'), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1595 | |
| 1596 | def newslot(): |
| 1597 | if verbose: print "Testing __new__ slot override..." |
| 1598 | class C(list): |
| 1599 | def __new__(cls): |
| 1600 | self = list.__new__(cls) |
| 1601 | self.foo = 1 |
| 1602 | return self |
| 1603 | def __init__(self): |
| 1604 | self.foo = self.foo + 2 |
| 1605 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1606 | vereq(a.foo, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1607 | verify(a.__class__ is C) |
| 1608 | class D(C): |
| 1609 | pass |
| 1610 | b = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1611 | vereq(b.foo, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1612 | verify(b.__class__ is D) |
| 1613 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1614 | def altmro(): |
| 1615 | if verbose: print "Testing mro() and overriding it..." |
| 1616 | class A(object): |
| 1617 | def f(self): return "A" |
| 1618 | class B(A): |
| 1619 | pass |
| 1620 | class C(A): |
| 1621 | def f(self): return "C" |
| 1622 | class D(B, C): |
| 1623 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1624 | vereq(D.mro(), [D, B, C, A, object]) |
| 1625 | vereq(D.__mro__, (D, B, C, A, object)) |
| 1626 | vereq(D().f(), "C") |
Guido van Rossum | 9a81892 | 2002-11-14 19:50:14 +0000 | [diff] [blame] | 1627 | |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1628 | class PerverseMetaType(type): |
| 1629 | def mro(cls): |
| 1630 | L = type.mro(cls) |
| 1631 | L.reverse() |
| 1632 | return L |
Guido van Rossum | 9a81892 | 2002-11-14 19:50:14 +0000 | [diff] [blame] | 1633 | class X(D,B,C,A): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1634 | __metaclass__ = PerverseMetaType |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1635 | vereq(X.__mro__, (object, A, C, B, D, X)) |
| 1636 | vereq(X().f(), "A") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1637 | |
| 1638 | def overloading(): |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1639 | if verbose: print "Testing operator overloading..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1640 | |
| 1641 | class B(object): |
| 1642 | "Intermediate class because object doesn't have a __setattr__" |
| 1643 | |
| 1644 | class C(B): |
| 1645 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 1646 | def __getattr__(self, name): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1647 | if name == "foo": |
| 1648 | return ("getattr", name) |
| 1649 | else: |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 1650 | raise AttributeError |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1651 | def __setattr__(self, name, value): |
| 1652 | if name == "foo": |
| 1653 | self.setattr = (name, value) |
| 1654 | else: |
| 1655 | return B.__setattr__(self, name, value) |
| 1656 | def __delattr__(self, name): |
| 1657 | if name == "foo": |
| 1658 | self.delattr = name |
| 1659 | else: |
| 1660 | return B.__delattr__(self, name) |
| 1661 | |
| 1662 | def __getitem__(self, key): |
| 1663 | return ("getitem", key) |
| 1664 | def __setitem__(self, key, value): |
| 1665 | self.setitem = (key, value) |
| 1666 | def __delitem__(self, key): |
| 1667 | self.delitem = key |
| 1668 | |
| 1669 | def __getslice__(self, i, j): |
| 1670 | return ("getslice", i, j) |
| 1671 | def __setslice__(self, i, j, value): |
| 1672 | self.setslice = (i, j, value) |
| 1673 | def __delslice__(self, i, j): |
| 1674 | self.delslice = (i, j) |
| 1675 | |
| 1676 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1677 | vereq(a.foo, ("getattr", "foo")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1678 | a.foo = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1679 | vereq(a.setattr, ("foo", 12)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1680 | del a.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1681 | vereq(a.delattr, "foo") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1682 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1683 | vereq(a[12], ("getitem", 12)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1684 | a[12] = 21 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1685 | vereq(a.setitem, (12, 21)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1686 | del a[12] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1687 | vereq(a.delitem, 12) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1688 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1689 | vereq(a[0:10], ("getslice", 0, 10)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1690 | a[0:10] = "foo" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1691 | vereq(a.setslice, (0, 10, "foo")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1692 | del a[0:10] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1693 | vereq(a.delslice, (0, 10)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1694 | |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1695 | def methods(): |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1696 | if verbose: print "Testing methods..." |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1697 | class C(object): |
| 1698 | def __init__(self, x): |
| 1699 | self.x = x |
| 1700 | def foo(self): |
| 1701 | return self.x |
| 1702 | c1 = C(1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1703 | vereq(c1.foo(), 1) |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1704 | class D(C): |
| 1705 | boo = C.foo |
| 1706 | goo = c1.foo |
| 1707 | d2 = D(2) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1708 | vereq(d2.foo(), 2) |
| 1709 | vereq(d2.boo(), 2) |
| 1710 | vereq(d2.goo(), 1) |
Guido van Rossum | 9301876 | 2001-08-17 13:40:47 +0000 | [diff] [blame] | 1711 | class E(object): |
| 1712 | foo = C.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1713 | vereq(E().foo, C.foo) # i.e., unbound |
Guido van Rossum | 84a79a8 | 2001-08-17 13:58:31 +0000 | [diff] [blame] | 1714 | verify(repr(C.foo.__get__(C(1))).startswith("<bound method ")) |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1715 | |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1716 | def specials(): |
| 1717 | # Test operators like __hash__ for which a built-in default exists |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1718 | if verbose: print "Testing special operators..." |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1719 | # Test the default behavior for static classes |
| 1720 | class C(object): |
| 1721 | def __getitem__(self, i): |
| 1722 | if 0 <= i < 10: return i |
| 1723 | raise IndexError |
| 1724 | c1 = C() |
| 1725 | c2 = C() |
| 1726 | verify(not not c1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1727 | vereq(hash(c1), id(c1)) |
| 1728 | vereq(cmp(c1, c2), cmp(id(c1), id(c2))) |
| 1729 | vereq(c1, c1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1730 | verify(c1 != c2) |
| 1731 | verify(not c1 != c1) |
| 1732 | verify(not c1 == c2) |
Tim Peters | 4d2dded | 2001-08-16 19:50:51 +0000 | [diff] [blame] | 1733 | # Note that the module name appears in str/repr, and that varies |
| 1734 | # 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] | 1735 | verify(str(c1).find('C object at ') >= 0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1736 | vereq(str(c1), repr(c1)) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1737 | verify(-1 not in c1) |
| 1738 | for i in range(10): |
| 1739 | verify(i in c1) |
| 1740 | verify(10 not in c1) |
| 1741 | # Test the default behavior for dynamic classes |
| 1742 | class D(object): |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1743 | def __getitem__(self, i): |
| 1744 | if 0 <= i < 10: return i |
| 1745 | raise IndexError |
| 1746 | d1 = D() |
| 1747 | d2 = D() |
| 1748 | verify(not not d1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1749 | vereq(hash(d1), id(d1)) |
| 1750 | vereq(cmp(d1, d2), cmp(id(d1), id(d2))) |
| 1751 | vereq(d1, d1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1752 | verify(d1 != d2) |
| 1753 | verify(not d1 != d1) |
| 1754 | verify(not d1 == d2) |
Tim Peters | 4d2dded | 2001-08-16 19:50:51 +0000 | [diff] [blame] | 1755 | # Note that the module name appears in str/repr, and that varies |
| 1756 | # 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] | 1757 | verify(str(d1).find('D object at ') >= 0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1758 | vereq(str(d1), repr(d1)) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1759 | verify(-1 not in d1) |
| 1760 | for i in range(10): |
| 1761 | verify(i in d1) |
| 1762 | verify(10 not in d1) |
| 1763 | # Test overridden behavior for static classes |
| 1764 | class Proxy(object): |
| 1765 | def __init__(self, x): |
| 1766 | self.x = x |
| 1767 | def __nonzero__(self): |
| 1768 | return not not self.x |
| 1769 | def __hash__(self): |
| 1770 | return hash(self.x) |
| 1771 | def __eq__(self, other): |
| 1772 | return self.x == other |
| 1773 | def __ne__(self, other): |
| 1774 | return self.x != other |
| 1775 | def __cmp__(self, other): |
| 1776 | return cmp(self.x, other.x) |
| 1777 | def __str__(self): |
| 1778 | return "Proxy:%s" % self.x |
| 1779 | def __repr__(self): |
| 1780 | return "Proxy(%r)" % self.x |
| 1781 | def __contains__(self, value): |
| 1782 | return value in self.x |
| 1783 | p0 = Proxy(0) |
| 1784 | p1 = Proxy(1) |
| 1785 | p_1 = Proxy(-1) |
| 1786 | verify(not p0) |
| 1787 | verify(not not p1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1788 | vereq(hash(p0), hash(0)) |
| 1789 | vereq(p0, p0) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1790 | verify(p0 != p1) |
| 1791 | verify(not p0 != p0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1792 | vereq(not p0, p1) |
| 1793 | vereq(cmp(p0, p1), -1) |
| 1794 | vereq(cmp(p0, p0), 0) |
| 1795 | vereq(cmp(p0, p_1), 1) |
| 1796 | vereq(str(p0), "Proxy:0") |
| 1797 | vereq(repr(p0), "Proxy(0)") |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1798 | p10 = Proxy(range(10)) |
| 1799 | verify(-1 not in p10) |
| 1800 | for i in range(10): |
| 1801 | verify(i in p10) |
| 1802 | verify(10 not in p10) |
| 1803 | # Test overridden behavior for dynamic classes |
| 1804 | class DProxy(object): |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1805 | def __init__(self, x): |
| 1806 | self.x = x |
| 1807 | def __nonzero__(self): |
| 1808 | return not not self.x |
| 1809 | def __hash__(self): |
| 1810 | return hash(self.x) |
| 1811 | def __eq__(self, other): |
| 1812 | return self.x == other |
| 1813 | def __ne__(self, other): |
| 1814 | return self.x != other |
| 1815 | def __cmp__(self, other): |
| 1816 | return cmp(self.x, other.x) |
| 1817 | def __str__(self): |
| 1818 | return "DProxy:%s" % self.x |
| 1819 | def __repr__(self): |
| 1820 | return "DProxy(%r)" % self.x |
| 1821 | def __contains__(self, value): |
| 1822 | return value in self.x |
| 1823 | p0 = DProxy(0) |
| 1824 | p1 = DProxy(1) |
| 1825 | p_1 = DProxy(-1) |
| 1826 | verify(not p0) |
| 1827 | verify(not not p1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1828 | vereq(hash(p0), hash(0)) |
| 1829 | vereq(p0, p0) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1830 | verify(p0 != p1) |
| 1831 | verify(not p0 != p0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1832 | vereq(not p0, p1) |
| 1833 | vereq(cmp(p0, p1), -1) |
| 1834 | vereq(cmp(p0, p0), 0) |
| 1835 | vereq(cmp(p0, p_1), 1) |
| 1836 | vereq(str(p0), "DProxy:0") |
| 1837 | vereq(repr(p0), "DProxy(0)") |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1838 | p10 = DProxy(range(10)) |
| 1839 | verify(-1 not in p10) |
| 1840 | for i in range(10): |
| 1841 | verify(i in p10) |
| 1842 | verify(10 not in p10) |
Guido van Rossum | 843daa8 | 2001-09-18 20:04:26 +0000 | [diff] [blame] | 1843 | # Safety test for __cmp__ |
| 1844 | def unsafecmp(a, b): |
| 1845 | try: |
| 1846 | a.__class__.__cmp__(a, b) |
| 1847 | except TypeError: |
| 1848 | pass |
| 1849 | else: |
| 1850 | raise TestFailed, "shouldn't allow %s.__cmp__(%r, %r)" % ( |
| 1851 | a.__class__, a, b) |
| 1852 | unsafecmp(u"123", "123") |
| 1853 | unsafecmp("123", u"123") |
| 1854 | unsafecmp(1, 1.0) |
| 1855 | unsafecmp(1.0, 1) |
| 1856 | unsafecmp(1, 1L) |
| 1857 | unsafecmp(1L, 1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1858 | |
Neal Norwitz | 1a99750 | 2003-01-13 20:13:12 +0000 | [diff] [blame] | 1859 | class Letter(str): |
| 1860 | def __new__(cls, letter): |
| 1861 | if letter == 'EPS': |
| 1862 | return str.__new__(cls) |
| 1863 | return str.__new__(cls, letter) |
| 1864 | def __str__(self): |
| 1865 | if not self: |
| 1866 | return 'EPS' |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 1867 | return self |
Neal Norwitz | 1a99750 | 2003-01-13 20:13:12 +0000 | [diff] [blame] | 1868 | |
| 1869 | # sys.stdout needs to be the original to trigger the recursion bug |
| 1870 | import sys |
| 1871 | test_stdout = sys.stdout |
| 1872 | sys.stdout = get_original_stdout() |
| 1873 | try: |
| 1874 | # nothing should actually be printed, this should raise an exception |
| 1875 | print Letter('w') |
| 1876 | except RuntimeError: |
| 1877 | pass |
| 1878 | else: |
| 1879 | raise TestFailed, "expected a RuntimeError for print recursion" |
| 1880 | sys.stdout = test_stdout |
| 1881 | |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1882 | def weakrefs(): |
| 1883 | if verbose: print "Testing weak references..." |
| 1884 | import weakref |
| 1885 | class C(object): |
| 1886 | pass |
| 1887 | c = C() |
| 1888 | r = weakref.ref(c) |
| 1889 | verify(r() is c) |
| 1890 | del c |
| 1891 | verify(r() is None) |
| 1892 | del r |
| 1893 | class NoWeak(object): |
| 1894 | __slots__ = ['foo'] |
| 1895 | no = NoWeak() |
| 1896 | try: |
| 1897 | weakref.ref(no) |
| 1898 | except TypeError, msg: |
Fred Drake | 4bf018b | 2001-10-22 21:45:25 +0000 | [diff] [blame] | 1899 | verify(str(msg).find("weak reference") >= 0) |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1900 | else: |
| 1901 | verify(0, "weakref.ref(no) should be illegal") |
| 1902 | class Weak(object): |
| 1903 | __slots__ = ['foo', '__weakref__'] |
| 1904 | yes = Weak() |
| 1905 | r = weakref.ref(yes) |
| 1906 | verify(r() is yes) |
| 1907 | del yes |
| 1908 | verify(r() is None) |
| 1909 | del r |
| 1910 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1911 | def properties(): |
| 1912 | if verbose: print "Testing property..." |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1913 | class C(object): |
| 1914 | def getx(self): |
| 1915 | return self.__x |
| 1916 | def setx(self, value): |
| 1917 | self.__x = value |
| 1918 | def delx(self): |
| 1919 | del self.__x |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1920 | x = property(getx, setx, delx, doc="I'm the x property.") |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1921 | a = C() |
| 1922 | verify(not hasattr(a, "x")) |
| 1923 | a.x = 42 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1924 | vereq(a._C__x, 42) |
| 1925 | vereq(a.x, 42) |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1926 | del a.x |
| 1927 | verify(not hasattr(a, "x")) |
| 1928 | verify(not hasattr(a, "_C__x")) |
| 1929 | C.x.__set__(a, 100) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1930 | vereq(C.x.__get__(a), 100) |
Guido van Rossum | 0dbab4c5 | 2002-08-01 14:39:25 +0000 | [diff] [blame] | 1931 | C.x.__delete__(a) |
| 1932 | verify(not hasattr(a, "x")) |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1933 | |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1934 | raw = C.__dict__['x'] |
| 1935 | verify(isinstance(raw, property)) |
| 1936 | |
| 1937 | attrs = dir(raw) |
| 1938 | verify("__doc__" in attrs) |
| 1939 | verify("fget" in attrs) |
| 1940 | verify("fset" in attrs) |
| 1941 | verify("fdel" in attrs) |
| 1942 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1943 | vereq(raw.__doc__, "I'm the x property.") |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1944 | verify(raw.fget is C.__dict__['getx']) |
| 1945 | verify(raw.fset is C.__dict__['setx']) |
| 1946 | verify(raw.fdel is C.__dict__['delx']) |
| 1947 | |
| 1948 | for attr in "__doc__", "fget", "fset", "fdel": |
| 1949 | try: |
| 1950 | setattr(raw, attr, 42) |
| 1951 | except TypeError, msg: |
| 1952 | if str(msg).find('readonly') < 0: |
| 1953 | raise TestFailed("when setting readonly attr %r on a " |
| 1954 | "property, got unexpected TypeError " |
| 1955 | "msg %r" % (attr, str(msg))) |
| 1956 | else: |
| 1957 | raise TestFailed("expected TypeError from trying to set " |
| 1958 | "readonly %r attr on a property" % attr) |
| 1959 | |
Neal Norwitz | 673cd82 | 2002-10-18 16:33:13 +0000 | [diff] [blame] | 1960 | class D(object): |
| 1961 | __getitem__ = property(lambda s: 1/0) |
| 1962 | |
| 1963 | d = D() |
| 1964 | try: |
| 1965 | for i in d: |
| 1966 | str(i) |
| 1967 | except ZeroDivisionError: |
| 1968 | pass |
| 1969 | else: |
| 1970 | raise TestFailed, "expected ZeroDivisionError from bad property" |
| 1971 | |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1972 | def supers(): |
Guido van Rossum | 9881fc1 | 2001-08-24 17:07:20 +0000 | [diff] [blame] | 1973 | if verbose: print "Testing super..." |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1974 | |
| 1975 | class A(object): |
| 1976 | def meth(self, a): |
| 1977 | return "A(%r)" % a |
| 1978 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1979 | vereq(A().meth(1), "A(1)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1980 | |
| 1981 | class B(A): |
| 1982 | def __init__(self): |
| 1983 | self.__super = super(B, self) |
| 1984 | def meth(self, a): |
| 1985 | return "B(%r)" % a + self.__super.meth(a) |
| 1986 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1987 | vereq(B().meth(2), "B(2)A(2)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1988 | |
| 1989 | class C(A): |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1990 | def meth(self, a): |
| 1991 | return "C(%r)" % a + self.__super.meth(a) |
| 1992 | C._C__super = super(C) |
| 1993 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1994 | vereq(C().meth(3), "C(3)A(3)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1995 | |
| 1996 | class D(C, B): |
| 1997 | def meth(self, a): |
| 1998 | return "D(%r)" % a + super(D, self).meth(a) |
| 1999 | |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 2000 | vereq(D().meth(4), "D(4)C(4)B(4)A(4)") |
| 2001 | |
| 2002 | # Test for subclassing super |
| 2003 | |
| 2004 | class mysuper(super): |
| 2005 | def __init__(self, *args): |
| 2006 | return super(mysuper, self).__init__(*args) |
| 2007 | |
| 2008 | class E(D): |
| 2009 | def meth(self, a): |
| 2010 | return "E(%r)" % a + mysuper(E, self).meth(a) |
| 2011 | |
| 2012 | vereq(E().meth(5), "E(5)D(5)C(5)B(5)A(5)") |
| 2013 | |
| 2014 | class F(E): |
| 2015 | def meth(self, a): |
Guido van Rossum | a4541a3 | 2003-04-16 20:02:22 +0000 | [diff] [blame] | 2016 | s = self.__super # == mysuper(F, self) |
Guido van Rossum | 5b443c6 | 2001-12-03 15:38:28 +0000 | [diff] [blame] | 2017 | return "F(%r)[%s]" % (a, s.__class__.__name__) + s.meth(a) |
| 2018 | F._F__super = mysuper(F) |
| 2019 | |
| 2020 | vereq(F().meth(6), "F(6)[mysuper]E(6)D(6)C(6)B(6)A(6)") |
| 2021 | |
| 2022 | # Make sure certain errors are raised |
| 2023 | |
| 2024 | try: |
| 2025 | super(D, 42) |
| 2026 | except TypeError: |
| 2027 | pass |
| 2028 | else: |
| 2029 | raise TestFailed, "shouldn't allow super(D, 42)" |
| 2030 | |
| 2031 | try: |
| 2032 | super(D, C()) |
| 2033 | except TypeError: |
| 2034 | pass |
| 2035 | else: |
| 2036 | raise TestFailed, "shouldn't allow super(D, C())" |
| 2037 | |
| 2038 | try: |
| 2039 | super(D).__get__(12) |
| 2040 | except TypeError: |
| 2041 | pass |
| 2042 | else: |
| 2043 | raise TestFailed, "shouldn't allow super(D).__get__(12)" |
| 2044 | |
| 2045 | try: |
| 2046 | super(D).__get__(C()) |
| 2047 | except TypeError: |
| 2048 | pass |
| 2049 | else: |
| 2050 | raise TestFailed, "shouldn't allow super(D).__get__(C())" |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 2051 | |
Guido van Rossum | a4541a3 | 2003-04-16 20:02:22 +0000 | [diff] [blame] | 2052 | # Make sure data descriptors can be overridden and accessed via super |
| 2053 | # (new feature in Python 2.3) |
| 2054 | |
| 2055 | class DDbase(object): |
| 2056 | def getx(self): return 42 |
| 2057 | x = property(getx) |
| 2058 | |
| 2059 | class DDsub(DDbase): |
| 2060 | def getx(self): return "hello" |
| 2061 | x = property(getx) |
| 2062 | |
| 2063 | dd = DDsub() |
| 2064 | vereq(dd.x, "hello") |
| 2065 | vereq(super(DDsub, dd).x, 42) |
| 2066 | |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2067 | def inherits(): |
| 2068 | if verbose: print "Testing inheritance from basic types..." |
| 2069 | |
| 2070 | class hexint(int): |
| 2071 | def __repr__(self): |
| 2072 | return hex(self) |
| 2073 | def __add__(self, other): |
| 2074 | return hexint(int.__add__(self, other)) |
| 2075 | # (Note that overriding __radd__ doesn't work, |
| 2076 | # because the int type gets first dibs.) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2077 | vereq(repr(hexint(7) + 9), "0x10") |
| 2078 | vereq(repr(hexint(1000) + 7), "0x3ef") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 2079 | a = hexint(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2080 | vereq(a, 12345) |
| 2081 | vereq(int(a), 12345) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 2082 | verify(int(a).__class__ is int) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2083 | vereq(hash(a), hash(12345)) |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 2084 | verify((+a).__class__ is int) |
| 2085 | verify((a >> 0).__class__ is int) |
| 2086 | verify((a << 0).__class__ is int) |
| 2087 | verify((hexint(0) << 12).__class__ is int) |
| 2088 | verify((hexint(0) >> 12).__class__ is int) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2089 | |
| 2090 | class octlong(long): |
| 2091 | __slots__ = [] |
| 2092 | def __str__(self): |
| 2093 | s = oct(self) |
| 2094 | if s[-1] == 'L': |
| 2095 | s = s[:-1] |
| 2096 | return s |
| 2097 | def __add__(self, other): |
| 2098 | return self.__class__(super(octlong, self).__add__(other)) |
| 2099 | __radd__ = __add__ |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2100 | vereq(str(octlong(3) + 5), "010") |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2101 | # (Note that overriding __radd__ here only seems to work |
| 2102 | # because the example uses a short int left argument.) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2103 | vereq(str(5 + octlong(3000)), "05675") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 2104 | a = octlong(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2105 | vereq(a, 12345L) |
| 2106 | vereq(long(a), 12345L) |
| 2107 | vereq(hash(a), hash(12345L)) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 2108 | verify(long(a).__class__ is long) |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 2109 | verify((+a).__class__ is long) |
| 2110 | verify((-a).__class__ is long) |
| 2111 | verify((-octlong(0)).__class__ is long) |
| 2112 | verify((a >> 0).__class__ is long) |
| 2113 | verify((a << 0).__class__ is long) |
| 2114 | verify((a - 0).__class__ is long) |
| 2115 | verify((a * 1).__class__ is long) |
| 2116 | verify((a ** 1).__class__ is long) |
| 2117 | verify((a // 1).__class__ is long) |
| 2118 | verify((1 * a).__class__ is long) |
| 2119 | verify((a | 0).__class__ is long) |
| 2120 | verify((a ^ 0).__class__ is long) |
| 2121 | verify((a & -1L).__class__ is long) |
| 2122 | verify((octlong(0) << 12).__class__ is long) |
| 2123 | verify((octlong(0) >> 12).__class__ is long) |
| 2124 | verify(abs(octlong(0)).__class__ is long) |
| 2125 | |
| 2126 | # Because octlong overrides __add__, we can't check the absence of +0 |
| 2127 | # optimizations using octlong. |
| 2128 | class longclone(long): |
| 2129 | pass |
| 2130 | a = longclone(1) |
| 2131 | verify((a + 0).__class__ is long) |
| 2132 | verify((0 + a).__class__ is long) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2133 | |
Guido van Rossum | 2eb0b87 | 2002-03-01 22:24:49 +0000 | [diff] [blame] | 2134 | # Check that negative clones don't segfault |
| 2135 | a = longclone(-1) |
| 2136 | vereq(a.__dict__, {}) |
Tim Peters | 5329cdb | 2002-03-02 04:18:04 +0000 | [diff] [blame] | 2137 | vereq(long(a), -1) # verify PyNumber_Long() copies the sign bit |
Guido van Rossum | 2eb0b87 | 2002-03-01 22:24:49 +0000 | [diff] [blame] | 2138 | |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2139 | class precfloat(float): |
| 2140 | __slots__ = ['prec'] |
| 2141 | def __init__(self, value=0.0, prec=12): |
| 2142 | self.prec = int(prec) |
| 2143 | float.__init__(value) |
| 2144 | def __repr__(self): |
| 2145 | return "%.*g" % (self.prec, self) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2146 | vereq(repr(precfloat(1.1)), "1.1") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 2147 | a = precfloat(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2148 | vereq(a, 12345.0) |
| 2149 | vereq(float(a), 12345.0) |
Tim Peters | 7a50f25 | 2001-09-10 21:28:20 +0000 | [diff] [blame] | 2150 | verify(float(a).__class__ is float) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2151 | vereq(hash(a), hash(12345.0)) |
Tim Peters | 0280cf7 | 2001-09-11 21:53:35 +0000 | [diff] [blame] | 2152 | verify((+a).__class__ is float) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2153 | |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 2154 | class madcomplex(complex): |
| 2155 | def __repr__(self): |
| 2156 | return "%.17gj%+.17g" % (self.imag, self.real) |
| 2157 | a = madcomplex(-3, 4) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2158 | vereq(repr(a), "4j-3") |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 2159 | base = complex(-3, 4) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2160 | veris(base.__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2161 | vereq(a, base) |
| 2162 | vereq(complex(a), base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2163 | veris(complex(a).__class__, complex) |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 2164 | a = madcomplex(a) # just trying another form of the constructor |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2165 | vereq(repr(a), "4j-3") |
| 2166 | vereq(a, base) |
| 2167 | vereq(complex(a), base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2168 | veris(complex(a).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2169 | vereq(hash(a), hash(base)) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2170 | veris((+a).__class__, complex) |
| 2171 | veris((a + 0).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2172 | vereq(a + 0, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2173 | veris((a - 0).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2174 | vereq(a - 0, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2175 | veris((a * 1).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2176 | vereq(a * 1, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2177 | veris((a / 1).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2178 | vereq(a / 1, base) |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 2179 | |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2180 | class madtuple(tuple): |
| 2181 | _rev = None |
| 2182 | def rev(self): |
| 2183 | if self._rev is not None: |
| 2184 | return self._rev |
| 2185 | L = list(self) |
| 2186 | L.reverse() |
| 2187 | self._rev = self.__class__(L) |
| 2188 | return self._rev |
| 2189 | 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] | 2190 | vereq(a, (1,2,3,4,5,6,7,8,9,0)) |
| 2191 | vereq(a.rev(), madtuple((0,9,8,7,6,5,4,3,2,1))) |
| 2192 | 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] | 2193 | for i in range(512): |
| 2194 | t = madtuple(range(i)) |
| 2195 | u = t.rev() |
| 2196 | v = u.rev() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2197 | vereq(v, t) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 2198 | a = madtuple((1,2,3,4,5)) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2199 | vereq(tuple(a), (1,2,3,4,5)) |
Tim Peters | 4c3a0a3 | 2001-09-10 23:37:46 +0000 | [diff] [blame] | 2200 | verify(tuple(a).__class__ is tuple) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2201 | vereq(hash(a), hash((1,2,3,4,5))) |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 2202 | verify(a[:].__class__ is tuple) |
| 2203 | verify((a * 1).__class__ is tuple) |
| 2204 | verify((a * 0).__class__ is tuple) |
| 2205 | verify((a + ()).__class__ is tuple) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 2206 | a = madtuple(()) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2207 | vereq(tuple(a), ()) |
Guido van Rossum | 779ce4a | 2001-09-11 14:02:22 +0000 | [diff] [blame] | 2208 | verify(tuple(a).__class__ is tuple) |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 2209 | verify((a + a).__class__ is tuple) |
| 2210 | verify((a * 0).__class__ is tuple) |
| 2211 | verify((a * 1).__class__ is tuple) |
| 2212 | verify((a * 2).__class__ is tuple) |
| 2213 | verify(a[:].__class__ is tuple) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2214 | |
| 2215 | class madstring(str): |
| 2216 | _rev = None |
| 2217 | def rev(self): |
| 2218 | if self._rev is not None: |
| 2219 | return self._rev |
| 2220 | L = list(self) |
| 2221 | L.reverse() |
| 2222 | self._rev = self.__class__("".join(L)) |
| 2223 | return self._rev |
| 2224 | s = madstring("abcdefghijklmnopqrstuvwxyz") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2225 | vereq(s, "abcdefghijklmnopqrstuvwxyz") |
| 2226 | vereq(s.rev(), madstring("zyxwvutsrqponmlkjihgfedcba")) |
| 2227 | vereq(s.rev().rev(), madstring("abcdefghijklmnopqrstuvwxyz")) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2228 | for i in range(256): |
| 2229 | s = madstring("".join(map(chr, range(i)))) |
| 2230 | t = s.rev() |
| 2231 | u = t.rev() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2232 | vereq(u, s) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 2233 | s = madstring("12345") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2234 | vereq(str(s), "12345") |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 2235 | verify(str(s).__class__ is str) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2236 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2237 | base = "\x00" * 5 |
| 2238 | s = madstring(base) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2239 | vereq(s, base) |
| 2240 | vereq(str(s), base) |
Tim Peters | c636f56 | 2001-09-11 01:52:02 +0000 | [diff] [blame] | 2241 | verify(str(s).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2242 | vereq(hash(s), hash(base)) |
| 2243 | vereq({s: 1}[base], 1) |
| 2244 | vereq({base: 1}[s], 1) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2245 | verify((s + "").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2246 | vereq(s + "", base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2247 | verify(("" + s).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2248 | vereq("" + s, base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2249 | verify((s * 0).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2250 | vereq(s * 0, "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2251 | verify((s * 1).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2252 | vereq(s * 1, base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2253 | verify((s * 2).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2254 | vereq(s * 2, base + base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2255 | verify(s[:].__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2256 | vereq(s[:], base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2257 | verify(s[0:0].__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2258 | vereq(s[0:0], "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2259 | verify(s.strip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2260 | vereq(s.strip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2261 | verify(s.lstrip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2262 | vereq(s.lstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2263 | verify(s.rstrip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2264 | vereq(s.rstrip(), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2265 | identitytab = ''.join([chr(i) for i in range(256)]) |
| 2266 | verify(s.translate(identitytab).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2267 | vereq(s.translate(identitytab), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2268 | verify(s.translate(identitytab, "x").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2269 | vereq(s.translate(identitytab, "x"), base) |
| 2270 | vereq(s.translate(identitytab, "\x00"), "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2271 | verify(s.replace("x", "x").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2272 | vereq(s.replace("x", "x"), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2273 | verify(s.ljust(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2274 | vereq(s.ljust(len(s)), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2275 | verify(s.rjust(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2276 | vereq(s.rjust(len(s)), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2277 | verify(s.center(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2278 | vereq(s.center(len(s)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2279 | verify(s.lower().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2280 | vereq(s.lower(), base) |
Tim Peters | c636f56 | 2001-09-11 01:52:02 +0000 | [diff] [blame] | 2281 | |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 2282 | s = madstring("x y") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2283 | vereq(s, "x y") |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 2284 | verify(intern(s).__class__ is str) |
| 2285 | verify(intern(s) is intern("x y")) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2286 | vereq(intern(s), "x y") |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 2287 | |
| 2288 | i = intern("y x") |
| 2289 | s = madstring("y x") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2290 | vereq(s, i) |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 2291 | verify(intern(s).__class__ is str) |
| 2292 | verify(intern(s) is i) |
| 2293 | |
| 2294 | s = madstring(i) |
| 2295 | verify(intern(s).__class__ is str) |
| 2296 | verify(intern(s) is i) |
| 2297 | |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 2298 | class madunicode(unicode): |
| 2299 | _rev = None |
| 2300 | def rev(self): |
| 2301 | if self._rev is not None: |
| 2302 | return self._rev |
| 2303 | L = list(self) |
| 2304 | L.reverse() |
| 2305 | self._rev = self.__class__(u"".join(L)) |
| 2306 | return self._rev |
| 2307 | u = madunicode("ABCDEF") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2308 | vereq(u, u"ABCDEF") |
| 2309 | vereq(u.rev(), madunicode(u"FEDCBA")) |
| 2310 | vereq(u.rev().rev(), madunicode(u"ABCDEF")) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2311 | base = u"12345" |
| 2312 | u = madunicode(base) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2313 | vereq(unicode(u), base) |
Tim Peters | 78e0fc7 | 2001-09-11 03:07:38 +0000 | [diff] [blame] | 2314 | verify(unicode(u).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2315 | vereq(hash(u), hash(base)) |
| 2316 | vereq({u: 1}[base], 1) |
| 2317 | vereq({base: 1}[u], 1) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2318 | verify(u.strip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2319 | vereq(u.strip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2320 | verify(u.lstrip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2321 | vereq(u.lstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2322 | verify(u.rstrip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2323 | vereq(u.rstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2324 | verify(u.replace(u"x", u"x").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2325 | vereq(u.replace(u"x", u"x"), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2326 | verify(u.replace(u"xy", u"xy").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2327 | vereq(u.replace(u"xy", u"xy"), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2328 | verify(u.center(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2329 | vereq(u.center(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2330 | verify(u.ljust(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2331 | vereq(u.ljust(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2332 | verify(u.rjust(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2333 | vereq(u.rjust(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2334 | verify(u.lower().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2335 | vereq(u.lower(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2336 | verify(u.upper().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2337 | vereq(u.upper(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2338 | verify(u.capitalize().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2339 | vereq(u.capitalize(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2340 | verify(u.title().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2341 | vereq(u.title(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2342 | verify((u + u"").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2343 | vereq(u + u"", base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2344 | verify((u"" + u).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2345 | vereq(u"" + u, base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2346 | verify((u * 0).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2347 | vereq(u * 0, u"") |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2348 | verify((u * 1).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2349 | vereq(u * 1, base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2350 | verify((u * 2).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2351 | vereq(u * 2, base + base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2352 | verify(u[:].__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2353 | vereq(u[:], base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 2354 | verify(u[0:0].__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2355 | vereq(u[0:0], u"") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 2356 | |
Guido van Rossum | 12b22ff | 2001-10-09 20:36:44 +0000 | [diff] [blame] | 2357 | class sublist(list): |
| 2358 | pass |
| 2359 | a = sublist(range(5)) |
| 2360 | vereq(a, range(5)) |
| 2361 | a.append("hello") |
| 2362 | vereq(a, range(5) + ["hello"]) |
| 2363 | a[5] = 5 |
| 2364 | vereq(a, range(6)) |
| 2365 | a.extend(range(6, 20)) |
| 2366 | vereq(a, range(20)) |
| 2367 | a[-5:] = [] |
| 2368 | vereq(a, range(15)) |
| 2369 | del a[10:15] |
| 2370 | vereq(len(a), 10) |
| 2371 | vereq(a, range(10)) |
| 2372 | vereq(list(a), range(10)) |
| 2373 | vereq(a[0], 0) |
| 2374 | vereq(a[9], 9) |
| 2375 | vereq(a[-10], 0) |
| 2376 | vereq(a[-1], 9) |
| 2377 | vereq(a[:5], range(5)) |
| 2378 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 2379 | class CountedInput(file): |
| 2380 | """Counts lines read by self.readline(). |
| 2381 | |
| 2382 | self.lineno is the 0-based ordinal of the last line read, up to |
| 2383 | a maximum of one greater than the number of lines in the file. |
| 2384 | |
| 2385 | self.ateof is true if and only if the final "" line has been read, |
| 2386 | at which point self.lineno stops incrementing, and further calls |
| 2387 | to readline() continue to return "". |
| 2388 | """ |
| 2389 | |
| 2390 | lineno = 0 |
| 2391 | ateof = 0 |
| 2392 | def readline(self): |
| 2393 | if self.ateof: |
| 2394 | return "" |
| 2395 | s = file.readline(self) |
| 2396 | # Next line works too. |
| 2397 | # s = super(CountedInput, self).readline() |
| 2398 | self.lineno += 1 |
| 2399 | if s == "": |
| 2400 | self.ateof = 1 |
| 2401 | return s |
| 2402 | |
Tim Peters | 561f899 | 2001-09-13 19:36:36 +0000 | [diff] [blame] | 2403 | f = file(name=TESTFN, mode='w') |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 2404 | lines = ['a\n', 'b\n', 'c\n'] |
| 2405 | try: |
| 2406 | f.writelines(lines) |
| 2407 | f.close() |
| 2408 | f = CountedInput(TESTFN) |
| 2409 | for (i, expected) in zip(range(1, 5) + [4], lines + 2 * [""]): |
| 2410 | got = f.readline() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2411 | vereq(expected, got) |
| 2412 | vereq(f.lineno, i) |
| 2413 | vereq(f.ateof, (i > len(lines))) |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 2414 | f.close() |
| 2415 | finally: |
| 2416 | try: |
| 2417 | f.close() |
| 2418 | except: |
| 2419 | pass |
| 2420 | try: |
| 2421 | import os |
| 2422 | os.unlink(TESTFN) |
| 2423 | except: |
| 2424 | pass |
| 2425 | |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 2426 | def keywords(): |
| 2427 | if verbose: |
| 2428 | print "Testing keyword args to basic type constructors ..." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2429 | vereq(int(x=1), 1) |
| 2430 | vereq(float(x=2), 2.0) |
| 2431 | vereq(long(x=3), 3L) |
| 2432 | vereq(complex(imag=42, real=666), complex(666, 42)) |
| 2433 | vereq(str(object=500), '500') |
| 2434 | vereq(unicode(string='abc', errors='strict'), u'abc') |
| 2435 | vereq(tuple(sequence=range(3)), (0, 1, 2)) |
| 2436 | vereq(list(sequence=(0, 1, 2)), range(3)) |
Just van Rossum | a797d81 | 2002-11-23 09:45:04 +0000 | [diff] [blame] | 2437 | # note: as of Python 2.3, dict() no longer has an "items" keyword arg |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 2438 | |
| 2439 | for constructor in (int, float, long, complex, str, unicode, |
Just van Rossum | a797d81 | 2002-11-23 09:45:04 +0000 | [diff] [blame] | 2440 | tuple, list, file): |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 2441 | try: |
| 2442 | constructor(bogus_keyword_arg=1) |
| 2443 | except TypeError: |
| 2444 | pass |
| 2445 | else: |
| 2446 | raise TestFailed("expected TypeError from bogus keyword " |
| 2447 | "argument to %r" % constructor) |
Tim Peters | 561f899 | 2001-09-13 19:36:36 +0000 | [diff] [blame] | 2448 | |
Tim Peters | 8fa4567 | 2001-09-13 21:01:29 +0000 | [diff] [blame] | 2449 | def restricted(): |
Guido van Rossum | 4f37418 | 2003-01-06 16:03:43 +0000 | [diff] [blame] | 2450 | # XXX This test is disabled because rexec is not deemed safe |
| 2451 | return |
Tim Peters | 8fa4567 | 2001-09-13 21:01:29 +0000 | [diff] [blame] | 2452 | import rexec |
| 2453 | if verbose: |
| 2454 | print "Testing interaction with restricted execution ..." |
| 2455 | |
| 2456 | sandbox = rexec.RExec() |
| 2457 | |
| 2458 | code1 = """f = open(%r, 'w')""" % TESTFN |
| 2459 | code2 = """f = file(%r, 'w')""" % TESTFN |
| 2460 | code3 = """\ |
| 2461 | f = open(%r) |
| 2462 | t = type(f) # a sneaky way to get the file() constructor |
| 2463 | f.close() |
| 2464 | f = t(%r, 'w') # rexec can't catch this by itself |
| 2465 | """ % (TESTFN, TESTFN) |
| 2466 | |
| 2467 | f = open(TESTFN, 'w') # Create the file so code3 can find it. |
| 2468 | f.close() |
| 2469 | |
| 2470 | try: |
| 2471 | for code in code1, code2, code3: |
| 2472 | try: |
| 2473 | sandbox.r_exec(code) |
| 2474 | except IOError, msg: |
| 2475 | if str(msg).find("restricted") >= 0: |
| 2476 | outcome = "OK" |
| 2477 | else: |
| 2478 | outcome = "got an exception, but not an expected one" |
| 2479 | else: |
| 2480 | outcome = "expected a restricted-execution exception" |
| 2481 | |
| 2482 | if outcome != "OK": |
| 2483 | raise TestFailed("%s, in %r" % (outcome, code)) |
| 2484 | |
| 2485 | finally: |
| 2486 | try: |
| 2487 | import os |
| 2488 | os.unlink(TESTFN) |
| 2489 | except: |
| 2490 | pass |
| 2491 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2492 | def str_subclass_as_dict_key(): |
| 2493 | if verbose: |
| 2494 | print "Testing a str subclass used as dict key .." |
| 2495 | |
| 2496 | class cistr(str): |
| 2497 | """Sublcass of str that computes __eq__ case-insensitively. |
| 2498 | |
| 2499 | Also computes a hash code of the string in canonical form. |
| 2500 | """ |
| 2501 | |
| 2502 | def __init__(self, value): |
| 2503 | self.canonical = value.lower() |
| 2504 | self.hashcode = hash(self.canonical) |
| 2505 | |
| 2506 | def __eq__(self, other): |
| 2507 | if not isinstance(other, cistr): |
| 2508 | other = cistr(other) |
| 2509 | return self.canonical == other.canonical |
| 2510 | |
| 2511 | def __hash__(self): |
| 2512 | return self.hashcode |
| 2513 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2514 | vereq(cistr('ABC'), 'abc') |
| 2515 | vereq('aBc', cistr('ABC')) |
| 2516 | vereq(str(cistr('ABC')), 'ABC') |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2517 | |
| 2518 | d = {cistr('one'): 1, cistr('two'): 2, cistr('tHree'): 3} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2519 | vereq(d[cistr('one')], 1) |
| 2520 | vereq(d[cistr('tWo')], 2) |
| 2521 | vereq(d[cistr('THrEE')], 3) |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2522 | verify(cistr('ONe') in d) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2523 | vereq(d.get(cistr('thrEE')), 3) |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2524 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 2525 | def classic_comparisons(): |
| 2526 | if verbose: print "Testing classic comparisons..." |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2527 | class classic: |
| 2528 | pass |
| 2529 | for base in (classic, int, object): |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 2530 | if verbose: print " (base = %s)" % base |
| 2531 | class C(base): |
| 2532 | def __init__(self, value): |
| 2533 | self.value = int(value) |
| 2534 | def __cmp__(self, other): |
| 2535 | if isinstance(other, C): |
| 2536 | return cmp(self.value, other.value) |
| 2537 | if isinstance(other, int) or isinstance(other, long): |
| 2538 | return cmp(self.value, other) |
| 2539 | return NotImplemented |
| 2540 | c1 = C(1) |
| 2541 | c2 = C(2) |
| 2542 | c3 = C(3) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2543 | vereq(c1, 1) |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 2544 | c = {1: c1, 2: c2, 3: c3} |
| 2545 | for x in 1, 2, 3: |
| 2546 | for y in 1, 2, 3: |
| 2547 | verify(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 2548 | for op in "<", "<=", "==", "!=", ">", ">=": |
| 2549 | verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op), |
| 2550 | "x=%d, y=%d" % (x, y)) |
| 2551 | verify(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 2552 | verify(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 2553 | |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2554 | def rich_comparisons(): |
| 2555 | if verbose: |
| 2556 | print "Testing rich comparisons..." |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 2557 | class Z(complex): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2558 | pass |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 2559 | z = Z(1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2560 | vereq(z, 1+0j) |
| 2561 | vereq(1+0j, z) |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 2562 | class ZZ(complex): |
| 2563 | def __eq__(self, other): |
| 2564 | try: |
| 2565 | return abs(self - other) <= 1e-6 |
| 2566 | except: |
| 2567 | return NotImplemented |
| 2568 | zz = ZZ(1.0000003) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2569 | vereq(zz, 1+0j) |
| 2570 | vereq(1+0j, zz) |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 2571 | |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2572 | class classic: |
| 2573 | pass |
| 2574 | for base in (classic, int, object, list): |
| 2575 | if verbose: print " (base = %s)" % base |
| 2576 | class C(base): |
| 2577 | def __init__(self, value): |
| 2578 | self.value = int(value) |
| 2579 | def __cmp__(self, other): |
| 2580 | raise TestFailed, "shouldn't call __cmp__" |
| 2581 | def __eq__(self, other): |
| 2582 | if isinstance(other, C): |
| 2583 | return self.value == other.value |
| 2584 | if isinstance(other, int) or isinstance(other, long): |
| 2585 | return self.value == other |
| 2586 | return NotImplemented |
| 2587 | def __ne__(self, other): |
| 2588 | if isinstance(other, C): |
| 2589 | return self.value != other.value |
| 2590 | if isinstance(other, int) or isinstance(other, long): |
| 2591 | return self.value != other |
| 2592 | return NotImplemented |
| 2593 | def __lt__(self, other): |
| 2594 | if isinstance(other, C): |
| 2595 | return self.value < other.value |
| 2596 | if isinstance(other, int) or isinstance(other, long): |
| 2597 | return self.value < other |
| 2598 | return NotImplemented |
| 2599 | def __le__(self, other): |
| 2600 | if isinstance(other, C): |
| 2601 | return self.value <= other.value |
| 2602 | if isinstance(other, int) or isinstance(other, long): |
| 2603 | return self.value <= other |
| 2604 | return NotImplemented |
| 2605 | def __gt__(self, other): |
| 2606 | if isinstance(other, C): |
| 2607 | return self.value > other.value |
| 2608 | if isinstance(other, int) or isinstance(other, long): |
| 2609 | return self.value > other |
| 2610 | return NotImplemented |
| 2611 | def __ge__(self, other): |
| 2612 | if isinstance(other, C): |
| 2613 | return self.value >= other.value |
| 2614 | if isinstance(other, int) or isinstance(other, long): |
| 2615 | return self.value >= other |
| 2616 | return NotImplemented |
| 2617 | c1 = C(1) |
| 2618 | c2 = C(2) |
| 2619 | c3 = C(3) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2620 | vereq(c1, 1) |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2621 | c = {1: c1, 2: c2, 3: c3} |
| 2622 | for x in 1, 2, 3: |
| 2623 | for y in 1, 2, 3: |
| 2624 | for op in "<", "<=", "==", "!=", ">", ">=": |
| 2625 | verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op), |
| 2626 | "x=%d, y=%d" % (x, y)) |
| 2627 | verify(eval("c[x] %s y" % op) == eval("x %s y" % op), |
| 2628 | "x=%d, y=%d" % (x, y)) |
| 2629 | verify(eval("x %s c[y]" % op) == eval("x %s y" % op), |
| 2630 | "x=%d, y=%d" % (x, y)) |
| 2631 | |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2632 | def coercions(): |
| 2633 | if verbose: print "Testing coercions..." |
| 2634 | class I(int): pass |
| 2635 | coerce(I(0), 0) |
| 2636 | coerce(0, I(0)) |
| 2637 | class L(long): pass |
| 2638 | coerce(L(0), 0) |
| 2639 | coerce(L(0), 0L) |
| 2640 | coerce(0, L(0)) |
| 2641 | coerce(0L, L(0)) |
| 2642 | class F(float): pass |
| 2643 | coerce(F(0), 0) |
| 2644 | coerce(F(0), 0L) |
| 2645 | coerce(F(0), 0.) |
| 2646 | coerce(0, F(0)) |
| 2647 | coerce(0L, F(0)) |
| 2648 | coerce(0., F(0)) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2649 | class C(complex): pass |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2650 | coerce(C(0), 0) |
| 2651 | coerce(C(0), 0L) |
| 2652 | coerce(C(0), 0.) |
| 2653 | coerce(C(0), 0j) |
| 2654 | coerce(0, C(0)) |
| 2655 | coerce(0L, C(0)) |
| 2656 | coerce(0., C(0)) |
| 2657 | coerce(0j, C(0)) |
| 2658 | |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2659 | def descrdoc(): |
| 2660 | if verbose: print "Testing descriptor doc strings..." |
| 2661 | def check(descr, what): |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2662 | vereq(descr.__doc__, what) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2663 | check(file.closed, "True if the file is closed") # getset descriptor |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2664 | check(file.name, "file name") # member descriptor |
| 2665 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2666 | def setclass(): |
| 2667 | if verbose: print "Testing __class__ assignment..." |
| 2668 | class C(object): pass |
| 2669 | class D(object): pass |
| 2670 | class E(object): pass |
| 2671 | class F(D, E): pass |
| 2672 | for cls in C, D, E, F: |
| 2673 | for cls2 in C, D, E, F: |
| 2674 | x = cls() |
| 2675 | x.__class__ = cls2 |
| 2676 | verify(x.__class__ is cls2) |
| 2677 | x.__class__ = cls |
| 2678 | verify(x.__class__ is cls) |
| 2679 | def cant(x, C): |
| 2680 | try: |
| 2681 | x.__class__ = C |
| 2682 | except TypeError: |
| 2683 | pass |
| 2684 | else: |
| 2685 | raise TestFailed, "shouldn't allow %r.__class__ = %r" % (x, C) |
Guido van Rossum | b6b8942 | 2002-04-15 01:03:30 +0000 | [diff] [blame] | 2686 | try: |
| 2687 | delattr(x, "__class__") |
| 2688 | except TypeError: |
| 2689 | pass |
| 2690 | else: |
| 2691 | raise TestFailed, "shouldn't allow del %r.__class__" % x |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2692 | cant(C(), list) |
| 2693 | cant(list(), C) |
| 2694 | cant(C(), 1) |
| 2695 | cant(C(), object) |
| 2696 | cant(object(), list) |
| 2697 | cant(list(), object) |
Guido van Rossum | 40af889 | 2002-08-10 05:42:07 +0000 | [diff] [blame] | 2698 | class Int(int): __slots__ = [] |
| 2699 | cant(2, Int) |
| 2700 | cant(Int(), int) |
| 2701 | cant(True, int) |
| 2702 | cant(2, bool) |
Neal Norwitz | 78ce6b1 | 2002-12-24 15:26:42 +0000 | [diff] [blame] | 2703 | o = object() |
| 2704 | cant(o, type(1)) |
| 2705 | cant(o, type(None)) |
| 2706 | del o |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2707 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 2708 | def setdict(): |
| 2709 | if verbose: print "Testing __dict__ assignment..." |
| 2710 | class C(object): pass |
| 2711 | a = C() |
| 2712 | a.__dict__ = {'b': 1} |
| 2713 | vereq(a.b, 1) |
| 2714 | def cant(x, dict): |
| 2715 | try: |
| 2716 | x.__dict__ = dict |
| 2717 | except TypeError: |
| 2718 | pass |
| 2719 | else: |
| 2720 | raise TestFailed, "shouldn't allow %r.__dict__ = %r" % (x, dict) |
| 2721 | cant(a, None) |
| 2722 | cant(a, []) |
| 2723 | cant(a, 1) |
Guido van Rossum | d331cb5 | 2001-12-05 19:46:42 +0000 | [diff] [blame] | 2724 | del a.__dict__ # Deleting __dict__ is allowed |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 2725 | # Classes don't allow __dict__ assignment |
| 2726 | cant(C, {}) |
| 2727 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2728 | def pickles(): |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2729 | if verbose: |
| 2730 | print "Testing pickling and copying new-style classes and objects..." |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2731 | import pickle, cPickle |
| 2732 | |
| 2733 | def sorteditems(d): |
| 2734 | L = d.items() |
| 2735 | L.sort() |
| 2736 | return L |
| 2737 | |
| 2738 | global C |
| 2739 | class C(object): |
| 2740 | def __init__(self, a, b): |
| 2741 | super(C, self).__init__() |
| 2742 | self.a = a |
| 2743 | self.b = b |
| 2744 | def __repr__(self): |
| 2745 | return "C(%r, %r)" % (self.a, self.b) |
| 2746 | |
| 2747 | global C1 |
| 2748 | class C1(list): |
| 2749 | def __new__(cls, a, b): |
| 2750 | return super(C1, cls).__new__(cls) |
Guido van Rossum | f631859 | 2003-02-07 14:59:13 +0000 | [diff] [blame] | 2751 | def __getnewargs__(self): |
| 2752 | return (self.a, self.b) |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2753 | def __init__(self, a, b): |
| 2754 | self.a = a |
| 2755 | self.b = b |
| 2756 | def __repr__(self): |
| 2757 | return "C1(%r, %r)<%r>" % (self.a, self.b, list(self)) |
| 2758 | |
| 2759 | global C2 |
| 2760 | class C2(int): |
| 2761 | def __new__(cls, a, b, val=0): |
| 2762 | return super(C2, cls).__new__(cls, val) |
Guido van Rossum | f631859 | 2003-02-07 14:59:13 +0000 | [diff] [blame] | 2763 | def __getnewargs__(self): |
| 2764 | return (self.a, self.b, int(self)) |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2765 | def __init__(self, a, b, val=0): |
| 2766 | self.a = a |
| 2767 | self.b = b |
| 2768 | def __repr__(self): |
| 2769 | return "C2(%r, %r)<%r>" % (self.a, self.b, int(self)) |
| 2770 | |
Guido van Rossum | 90c4514 | 2001-11-24 21:07:01 +0000 | [diff] [blame] | 2771 | global C3 |
| 2772 | class C3(object): |
| 2773 | def __init__(self, foo): |
| 2774 | self.foo = foo |
| 2775 | def __getstate__(self): |
| 2776 | return self.foo |
| 2777 | def __setstate__(self, foo): |
| 2778 | self.foo = foo |
| 2779 | |
| 2780 | global C4classic, C4 |
| 2781 | class C4classic: # classic |
| 2782 | pass |
| 2783 | class C4(C4classic, object): # mixed inheritance |
| 2784 | pass |
| 2785 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2786 | for p in pickle, cPickle: |
| 2787 | for bin in 0, 1: |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2788 | if verbose: |
| 2789 | print p.__name__, ["text", "binary"][bin] |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2790 | |
| 2791 | for cls in C, C1, C2: |
| 2792 | s = p.dumps(cls, bin) |
| 2793 | cls2 = p.loads(s) |
| 2794 | verify(cls2 is cls) |
| 2795 | |
| 2796 | a = C1(1, 2); a.append(42); a.append(24) |
| 2797 | b = C2("hello", "world", 42) |
| 2798 | s = p.dumps((a, b), bin) |
| 2799 | x, y = p.loads(s) |
Guido van Rossum | 90c4514 | 2001-11-24 21:07:01 +0000 | [diff] [blame] | 2800 | vereq(x.__class__, a.__class__) |
| 2801 | vereq(sorteditems(x.__dict__), sorteditems(a.__dict__)) |
| 2802 | vereq(y.__class__, b.__class__) |
| 2803 | vereq(sorteditems(y.__dict__), sorteditems(b.__dict__)) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame^] | 2804 | vereq(repr(x), repr(a)) |
| 2805 | vereq(repr(y), repr(b)) |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2806 | if verbose: |
| 2807 | print "a = x =", a |
| 2808 | print "b = y =", b |
Guido van Rossum | 90c4514 | 2001-11-24 21:07:01 +0000 | [diff] [blame] | 2809 | # Test for __getstate__ and __setstate__ on new style class |
| 2810 | u = C3(42) |
| 2811 | s = p.dumps(u, bin) |
| 2812 | v = p.loads(s) |
| 2813 | veris(u.__class__, v.__class__) |
| 2814 | vereq(u.foo, v.foo) |
| 2815 | # Test for picklability of hybrid class |
| 2816 | u = C4() |
| 2817 | u.foo = 42 |
| 2818 | s = p.dumps(u, bin) |
| 2819 | v = p.loads(s) |
| 2820 | veris(u.__class__, v.__class__) |
| 2821 | vereq(u.foo, v.foo) |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2822 | |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2823 | # Testing copy.deepcopy() |
| 2824 | if verbose: |
| 2825 | print "deepcopy" |
| 2826 | import copy |
| 2827 | for cls in C, C1, C2: |
| 2828 | cls2 = copy.deepcopy(cls) |
| 2829 | verify(cls2 is cls) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2830 | |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2831 | a = C1(1, 2); a.append(42); a.append(24) |
| 2832 | b = C2("hello", "world", 42) |
| 2833 | x, y = copy.deepcopy((a, b)) |
Guido van Rossum | 90c4514 | 2001-11-24 21:07:01 +0000 | [diff] [blame] | 2834 | vereq(x.__class__, a.__class__) |
| 2835 | vereq(sorteditems(x.__dict__), sorteditems(a.__dict__)) |
| 2836 | vereq(y.__class__, b.__class__) |
| 2837 | vereq(sorteditems(y.__dict__), sorteditems(b.__dict__)) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame^] | 2838 | vereq(repr(x), repr(a)) |
| 2839 | vereq(repr(y), repr(b)) |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2840 | if verbose: |
| 2841 | print "a = x =", a |
| 2842 | print "b = y =", b |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2843 | |
Guido van Rossum | 8c84255 | 2002-03-14 23:05:54 +0000 | [diff] [blame] | 2844 | def pickleslots(): |
| 2845 | if verbose: print "Testing pickling of classes with __slots__ ..." |
| 2846 | import pickle, cPickle |
| 2847 | # Pickling of classes with __slots__ but without __getstate__ should fail |
| 2848 | global B, C, D, E |
| 2849 | class B(object): |
| 2850 | pass |
| 2851 | for base in [object, B]: |
| 2852 | class C(base): |
| 2853 | __slots__ = ['a'] |
| 2854 | class D(C): |
| 2855 | pass |
| 2856 | try: |
| 2857 | pickle.dumps(C()) |
| 2858 | except TypeError: |
| 2859 | pass |
| 2860 | else: |
| 2861 | raise TestFailed, "should fail: pickle C instance - %s" % base |
| 2862 | try: |
| 2863 | cPickle.dumps(C()) |
| 2864 | except TypeError: |
| 2865 | pass |
| 2866 | else: |
| 2867 | raise TestFailed, "should fail: cPickle C instance - %s" % base |
| 2868 | try: |
| 2869 | pickle.dumps(C()) |
| 2870 | except TypeError: |
| 2871 | pass |
| 2872 | else: |
| 2873 | raise TestFailed, "should fail: pickle D instance - %s" % base |
| 2874 | try: |
| 2875 | cPickle.dumps(D()) |
| 2876 | except TypeError: |
| 2877 | pass |
| 2878 | else: |
| 2879 | raise TestFailed, "should fail: cPickle D instance - %s" % base |
Guido van Rossum | 3f50cdc | 2003-02-10 21:31:27 +0000 | [diff] [blame] | 2880 | # Give C a nice generic __getstate__ and __setstate__ |
Guido van Rossum | 8c84255 | 2002-03-14 23:05:54 +0000 | [diff] [blame] | 2881 | class C(base): |
| 2882 | __slots__ = ['a'] |
| 2883 | def __getstate__(self): |
| 2884 | try: |
| 2885 | d = self.__dict__.copy() |
| 2886 | except AttributeError: |
| 2887 | d = {} |
Guido van Rossum | 3f50cdc | 2003-02-10 21:31:27 +0000 | [diff] [blame] | 2888 | for cls in self.__class__.__mro__: |
| 2889 | for sn in cls.__dict__.get('__slots__', ()): |
| 2890 | try: |
| 2891 | d[sn] = getattr(self, sn) |
| 2892 | except AttributeError: |
| 2893 | pass |
Guido van Rossum | 8c84255 | 2002-03-14 23:05:54 +0000 | [diff] [blame] | 2894 | return d |
| 2895 | def __setstate__(self, d): |
| 2896 | for k, v in d.items(): |
| 2897 | setattr(self, k, v) |
| 2898 | class D(C): |
| 2899 | pass |
| 2900 | # Now it should work |
| 2901 | x = C() |
| 2902 | y = pickle.loads(pickle.dumps(x)) |
| 2903 | vereq(hasattr(y, 'a'), 0) |
| 2904 | y = cPickle.loads(cPickle.dumps(x)) |
| 2905 | vereq(hasattr(y, 'a'), 0) |
| 2906 | x.a = 42 |
| 2907 | y = pickle.loads(pickle.dumps(x)) |
| 2908 | vereq(y.a, 42) |
| 2909 | y = cPickle.loads(cPickle.dumps(x)) |
| 2910 | vereq(y.a, 42) |
| 2911 | x = D() |
| 2912 | x.a = 42 |
| 2913 | x.b = 100 |
| 2914 | y = pickle.loads(pickle.dumps(x)) |
| 2915 | vereq(y.a + y.b, 142) |
| 2916 | y = cPickle.loads(cPickle.dumps(x)) |
| 2917 | vereq(y.a + y.b, 142) |
Guido van Rossum | 3f50cdc | 2003-02-10 21:31:27 +0000 | [diff] [blame] | 2918 | # A subclass that adds a slot should also work |
Guido van Rossum | 8c84255 | 2002-03-14 23:05:54 +0000 | [diff] [blame] | 2919 | class E(C): |
| 2920 | __slots__ = ['b'] |
Guido van Rossum | 3f50cdc | 2003-02-10 21:31:27 +0000 | [diff] [blame] | 2921 | x = E() |
| 2922 | x.a = 42 |
| 2923 | x.b = "foo" |
| 2924 | y = pickle.loads(pickle.dumps(x)) |
| 2925 | vereq(y.a, x.a) |
| 2926 | vereq(y.b, x.b) |
| 2927 | y = cPickle.loads(cPickle.dumps(x)) |
| 2928 | vereq(y.a, x.a) |
| 2929 | vereq(y.b, x.b) |
Guido van Rossum | 8c84255 | 2002-03-14 23:05:54 +0000 | [diff] [blame] | 2930 | |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2931 | def copies(): |
| 2932 | if verbose: print "Testing copy.copy() and copy.deepcopy()..." |
| 2933 | import copy |
| 2934 | class C(object): |
| 2935 | pass |
| 2936 | |
| 2937 | a = C() |
| 2938 | a.foo = 12 |
| 2939 | b = copy.copy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2940 | vereq(b.__dict__, a.__dict__) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2941 | |
| 2942 | a.bar = [1,2,3] |
| 2943 | c = copy.copy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2944 | vereq(c.bar, a.bar) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2945 | verify(c.bar is a.bar) |
| 2946 | |
| 2947 | d = copy.deepcopy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2948 | vereq(d.__dict__, a.__dict__) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2949 | a.bar.append(4) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2950 | vereq(d.bar, [1,2,3]) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2951 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2952 | def binopoverride(): |
| 2953 | if verbose: print "Testing overrides of binary operations..." |
| 2954 | class I(int): |
| 2955 | def __repr__(self): |
| 2956 | return "I(%r)" % int(self) |
| 2957 | def __add__(self, other): |
| 2958 | return I(int(self) + int(other)) |
| 2959 | __radd__ = __add__ |
| 2960 | def __pow__(self, other, mod=None): |
| 2961 | if mod is None: |
| 2962 | return I(pow(int(self), int(other))) |
| 2963 | else: |
| 2964 | return I(pow(int(self), int(other), int(mod))) |
| 2965 | def __rpow__(self, other, mod=None): |
| 2966 | if mod is None: |
| 2967 | return I(pow(int(other), int(self), mod)) |
| 2968 | else: |
| 2969 | return I(pow(int(other), int(self), int(mod))) |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2970 | |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame^] | 2971 | vereq(repr(I(1) + I(2)), "I(3)") |
| 2972 | vereq(repr(I(1) + 2), "I(3)") |
| 2973 | vereq(repr(1 + I(2)), "I(3)") |
| 2974 | vereq(repr(I(2) ** I(3)), "I(8)") |
| 2975 | vereq(repr(2 ** I(3)), "I(8)") |
| 2976 | vereq(repr(I(2) ** 3), "I(8)") |
| 2977 | vereq(repr(pow(I(2), I(3), I(5))), "I(3)") |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2978 | class S(str): |
| 2979 | def __eq__(self, other): |
| 2980 | return self.lower() == other.lower() |
| 2981 | |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2982 | def subclasspropagation(): |
| 2983 | if verbose: print "Testing propagation of slot functions to subclasses..." |
| 2984 | class A(object): |
| 2985 | pass |
| 2986 | class B(A): |
| 2987 | pass |
| 2988 | class C(A): |
| 2989 | pass |
| 2990 | class D(B, C): |
| 2991 | pass |
| 2992 | d = D() |
| 2993 | vereq(hash(d), id(d)) |
| 2994 | A.__hash__ = lambda self: 42 |
| 2995 | vereq(hash(d), 42) |
| 2996 | C.__hash__ = lambda self: 314 |
| 2997 | vereq(hash(d), 314) |
| 2998 | B.__hash__ = lambda self: 144 |
| 2999 | vereq(hash(d), 144) |
| 3000 | D.__hash__ = lambda self: 100 |
| 3001 | vereq(hash(d), 100) |
| 3002 | del D.__hash__ |
| 3003 | vereq(hash(d), 144) |
| 3004 | del B.__hash__ |
| 3005 | vereq(hash(d), 314) |
| 3006 | del C.__hash__ |
| 3007 | vereq(hash(d), 42) |
| 3008 | del A.__hash__ |
| 3009 | vereq(hash(d), id(d)) |
| 3010 | d.foo = 42 |
| 3011 | d.bar = 42 |
| 3012 | vereq(d.foo, 42) |
| 3013 | vereq(d.bar, 42) |
| 3014 | def __getattribute__(self, name): |
| 3015 | if name == "foo": |
| 3016 | return 24 |
| 3017 | return object.__getattribute__(self, name) |
| 3018 | A.__getattribute__ = __getattribute__ |
| 3019 | vereq(d.foo, 24) |
| 3020 | vereq(d.bar, 42) |
| 3021 | def __getattr__(self, name): |
| 3022 | if name in ("spam", "foo", "bar"): |
| 3023 | return "hello" |
| 3024 | raise AttributeError, name |
| 3025 | B.__getattr__ = __getattr__ |
| 3026 | vereq(d.spam, "hello") |
| 3027 | vereq(d.foo, 24) |
| 3028 | vereq(d.bar, 42) |
| 3029 | del A.__getattribute__ |
| 3030 | vereq(d.foo, 42) |
| 3031 | del d.foo |
| 3032 | vereq(d.foo, "hello") |
| 3033 | vereq(d.bar, 42) |
| 3034 | del B.__getattr__ |
| 3035 | try: |
| 3036 | d.foo |
| 3037 | except AttributeError: |
| 3038 | pass |
| 3039 | else: |
| 3040 | raise TestFailed, "d.foo should be undefined now" |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3041 | |
Guido van Rossum | e7f3e24 | 2002-06-14 02:35:45 +0000 | [diff] [blame] | 3042 | # Test a nasty bug in recurse_down_subclasses() |
| 3043 | import gc |
| 3044 | class A(object): |
| 3045 | pass |
| 3046 | class B(A): |
| 3047 | pass |
| 3048 | del B |
| 3049 | gc.collect() |
| 3050 | A.__setitem__ = lambda *a: None # crash |
| 3051 | |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 3052 | def buffer_inherit(): |
| 3053 | import binascii |
| 3054 | # SF bug [#470040] ParseTuple t# vs subclasses. |
| 3055 | if verbose: |
| 3056 | print "Testing that buffer interface is inherited ..." |
| 3057 | |
| 3058 | class MyStr(str): |
| 3059 | pass |
| 3060 | base = 'abc' |
| 3061 | m = MyStr(base) |
| 3062 | # b2a_hex uses the buffer interface to get its argument's value, via |
| 3063 | # PyArg_ParseTuple 't#' code. |
| 3064 | vereq(binascii.b2a_hex(m), binascii.b2a_hex(base)) |
| 3065 | |
| 3066 | # It's not clear that unicode will continue to support the character |
| 3067 | # buffer interface, and this test will fail if that's taken away. |
| 3068 | class MyUni(unicode): |
| 3069 | pass |
| 3070 | base = u'abc' |
| 3071 | m = MyUni(base) |
| 3072 | vereq(binascii.b2a_hex(m), binascii.b2a_hex(base)) |
| 3073 | |
| 3074 | class MyInt(int): |
| 3075 | pass |
| 3076 | m = MyInt(42) |
| 3077 | try: |
| 3078 | binascii.b2a_hex(m) |
| 3079 | raise TestFailed('subclass of int should not have a buffer interface') |
| 3080 | except TypeError: |
| 3081 | pass |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 3082 | |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 3083 | def str_of_str_subclass(): |
| 3084 | import binascii |
| 3085 | import cStringIO |
| 3086 | |
| 3087 | if verbose: |
| 3088 | print "Testing __str__ defined in subclass of str ..." |
| 3089 | |
| 3090 | class octetstring(str): |
| 3091 | def __str__(self): |
| 3092 | return binascii.b2a_hex(self) |
| 3093 | def __repr__(self): |
| 3094 | return self + " repr" |
| 3095 | |
| 3096 | o = octetstring('A') |
| 3097 | vereq(type(o), octetstring) |
| 3098 | vereq(type(str(o)), str) |
| 3099 | vereq(type(repr(o)), str) |
| 3100 | vereq(ord(o), 0x41) |
| 3101 | vereq(str(o), '41') |
| 3102 | vereq(repr(o), 'A repr') |
| 3103 | vereq(o.__str__(), '41') |
| 3104 | vereq(o.__repr__(), 'A repr') |
| 3105 | |
| 3106 | capture = cStringIO.StringIO() |
| 3107 | # Calling str() or not exercises different internal paths. |
| 3108 | print >> capture, o |
| 3109 | print >> capture, str(o) |
| 3110 | vereq(capture.getvalue(), '41\n41\n') |
| 3111 | capture.close() |
| 3112 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 3113 | def kwdargs(): |
| 3114 | if verbose: print "Testing keyword arguments to __init__, __call__..." |
| 3115 | def f(a): return a |
| 3116 | vereq(f.__call__(a=42), 42) |
| 3117 | a = [] |
| 3118 | list.__init__(a, sequence=[0, 1, 2]) |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 3119 | vereq(a, [0, 1, 2]) |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 3120 | |
Guido van Rossum | ed87ad8 | 2001-10-30 02:33:02 +0000 | [diff] [blame] | 3121 | def delhook(): |
| 3122 | if verbose: print "Testing __del__ hook..." |
| 3123 | log = [] |
| 3124 | class C(object): |
| 3125 | def __del__(self): |
| 3126 | log.append(1) |
| 3127 | c = C() |
| 3128 | vereq(log, []) |
| 3129 | del c |
| 3130 | vereq(log, [1]) |
| 3131 | |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 3132 | class D(object): pass |
| 3133 | d = D() |
| 3134 | try: del d[0] |
| 3135 | except TypeError: pass |
| 3136 | else: raise TestFailed, "invalid del() didn't raise TypeError" |
| 3137 | |
Guido van Rossum | dbb53d9 | 2001-12-03 16:32:18 +0000 | [diff] [blame] | 3138 | def hashinherit(): |
| 3139 | if verbose: print "Testing hash of mutable subclasses..." |
| 3140 | |
| 3141 | class mydict(dict): |
| 3142 | pass |
| 3143 | d = mydict() |
| 3144 | try: |
| 3145 | hash(d) |
| 3146 | except TypeError: |
| 3147 | pass |
| 3148 | else: |
| 3149 | raise TestFailed, "hash() of dict subclass should fail" |
| 3150 | |
| 3151 | class mylist(list): |
| 3152 | pass |
| 3153 | d = mylist() |
| 3154 | try: |
| 3155 | hash(d) |
| 3156 | except TypeError: |
| 3157 | pass |
| 3158 | else: |
| 3159 | raise TestFailed, "hash() of list subclass should fail" |
| 3160 | |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 3161 | def strops(): |
| 3162 | try: 'a' + 5 |
| 3163 | except TypeError: pass |
| 3164 | else: raise TestFailed, "'' + 5 doesn't raise TypeError" |
| 3165 | |
| 3166 | try: ''.split('') |
| 3167 | except ValueError: pass |
| 3168 | else: raise TestFailed, "''.split('') doesn't raise ValueError" |
| 3169 | |
| 3170 | try: ''.join([0]) |
| 3171 | except TypeError: pass |
| 3172 | else: raise TestFailed, "''.join([0]) doesn't raise TypeError" |
| 3173 | |
| 3174 | try: ''.rindex('5') |
| 3175 | except ValueError: pass |
| 3176 | else: raise TestFailed, "''.rindex('5') doesn't raise ValueError" |
| 3177 | |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 3178 | try: '%(n)s' % None |
| 3179 | except TypeError: pass |
| 3180 | else: raise TestFailed, "'%(n)s' % None doesn't raise TypeError" |
| 3181 | |
| 3182 | try: '%(n' % {} |
| 3183 | except ValueError: pass |
| 3184 | else: raise TestFailed, "'%(n' % {} '' doesn't raise ValueError" |
| 3185 | |
| 3186 | try: '%*s' % ('abc') |
| 3187 | except TypeError: pass |
| 3188 | else: raise TestFailed, "'%*s' % ('abc') doesn't raise TypeError" |
| 3189 | |
| 3190 | try: '%*.*s' % ('abc', 5) |
| 3191 | except TypeError: pass |
| 3192 | else: raise TestFailed, "'%*.*s' % ('abc', 5) doesn't raise TypeError" |
| 3193 | |
| 3194 | try: '%s' % (1, 2) |
| 3195 | except TypeError: pass |
| 3196 | else: raise TestFailed, "'%s' % (1, 2) doesn't raise TypeError" |
| 3197 | |
| 3198 | try: '%' % None |
| 3199 | except ValueError: pass |
| 3200 | else: raise TestFailed, "'%' % None doesn't raise ValueError" |
| 3201 | |
| 3202 | vereq('534253'.isdigit(), 1) |
| 3203 | vereq('534253x'.isdigit(), 0) |
| 3204 | vereq('%c' % 5, '\x05') |
| 3205 | vereq('%c' % '5', '5') |
| 3206 | |
Guido van Rossum | 2764a3a | 2001-12-28 21:39:03 +0000 | [diff] [blame] | 3207 | def deepcopyrecursive(): |
| 3208 | if verbose: print "Testing deepcopy of recursive objects..." |
| 3209 | class Node: |
| 3210 | pass |
| 3211 | a = Node() |
| 3212 | b = Node() |
| 3213 | a.b = b |
| 3214 | b.a = a |
| 3215 | z = deepcopy(a) # This blew up before |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 3216 | |
Guido van Rossum | d703567 | 2002-03-12 20:43:31 +0000 | [diff] [blame] | 3217 | def modules(): |
| 3218 | if verbose: print "Testing uninitialized module objects..." |
| 3219 | from types import ModuleType as M |
| 3220 | m = M.__new__(M) |
| 3221 | str(m) |
| 3222 | vereq(hasattr(m, "__name__"), 0) |
| 3223 | vereq(hasattr(m, "__file__"), 0) |
| 3224 | vereq(hasattr(m, "foo"), 0) |
| 3225 | vereq(m.__dict__, None) |
| 3226 | m.foo = 1 |
| 3227 | vereq(m.__dict__, {"foo": 1}) |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 3228 | |
Walter Dörwald | dbd2d25 | 2002-03-25 18:36:32 +0000 | [diff] [blame] | 3229 | def dictproxyiterkeys(): |
| 3230 | class C(object): |
| 3231 | def meth(self): |
| 3232 | pass |
| 3233 | if verbose: print "Testing dict-proxy iterkeys..." |
| 3234 | keys = [ key for key in C.__dict__.iterkeys() ] |
| 3235 | keys.sort() |
| 3236 | vereq(keys, ['__dict__', '__doc__', '__module__', '__weakref__', 'meth']) |
| 3237 | |
| 3238 | def dictproxyitervalues(): |
| 3239 | class C(object): |
| 3240 | def meth(self): |
| 3241 | pass |
| 3242 | if verbose: print "Testing dict-proxy itervalues..." |
| 3243 | values = [ values for values in C.__dict__.itervalues() ] |
| 3244 | vereq(len(values), 5) |
| 3245 | |
| 3246 | def dictproxyiteritems(): |
| 3247 | class C(object): |
| 3248 | def meth(self): |
| 3249 | pass |
| 3250 | if verbose: print "Testing dict-proxy iteritems..." |
| 3251 | keys = [ key for (key, value) in C.__dict__.iteritems() ] |
| 3252 | keys.sort() |
| 3253 | vereq(keys, ['__dict__', '__doc__', '__module__', '__weakref__', 'meth']) |
| 3254 | |
Guido van Rossum | 8ace1ab | 2002-04-06 01:05:01 +0000 | [diff] [blame] | 3255 | def funnynew(): |
| 3256 | if verbose: print "Testing __new__ returning something unexpected..." |
| 3257 | class C(object): |
| 3258 | def __new__(cls, arg): |
| 3259 | if isinstance(arg, str): return [1, 2, 3] |
| 3260 | elif isinstance(arg, int): return object.__new__(D) |
| 3261 | else: return object.__new__(cls) |
| 3262 | class D(C): |
| 3263 | def __init__(self, arg): |
| 3264 | self.foo = arg |
| 3265 | vereq(C("1"), [1, 2, 3]) |
| 3266 | vereq(D("1"), [1, 2, 3]) |
| 3267 | d = D(None) |
| 3268 | veris(d.foo, None) |
| 3269 | d = C(1) |
| 3270 | vereq(isinstance(d, D), True) |
| 3271 | vereq(d.foo, 1) |
| 3272 | d = D(1) |
| 3273 | vereq(isinstance(d, D), True) |
| 3274 | vereq(d.foo, 1) |
| 3275 | |
Guido van Rossum | e8fc640 | 2002-04-16 16:44:51 +0000 | [diff] [blame] | 3276 | def imulbug(): |
| 3277 | # SF bug 544647 |
| 3278 | if verbose: print "Testing for __imul__ problems..." |
| 3279 | class C(object): |
| 3280 | def __imul__(self, other): |
| 3281 | return (self, other) |
| 3282 | x = C() |
| 3283 | y = x |
| 3284 | y *= 1.0 |
| 3285 | vereq(y, (x, 1.0)) |
| 3286 | y = x |
| 3287 | y *= 2 |
| 3288 | vereq(y, (x, 2)) |
| 3289 | y = x |
| 3290 | y *= 3L |
| 3291 | vereq(y, (x, 3L)) |
| 3292 | y = x |
| 3293 | y *= 1L<<100 |
| 3294 | vereq(y, (x, 1L<<100)) |
| 3295 | y = x |
| 3296 | y *= None |
| 3297 | vereq(y, (x, None)) |
| 3298 | y = x |
| 3299 | y *= "foo" |
| 3300 | vereq(y, (x, "foo")) |
| 3301 | |
Guido van Rossum | d99b3e7 | 2002-04-18 00:27:33 +0000 | [diff] [blame] | 3302 | def docdescriptor(): |
| 3303 | # SF bug 542984 |
| 3304 | if verbose: print "Testing __doc__ descriptor..." |
| 3305 | class DocDescr(object): |
| 3306 | def __get__(self, object, otype): |
| 3307 | if object: |
| 3308 | object = object.__class__.__name__ + ' instance' |
| 3309 | if otype: |
| 3310 | otype = otype.__name__ |
| 3311 | return 'object=%s; type=%s' % (object, otype) |
| 3312 | class OldClass: |
| 3313 | __doc__ = DocDescr() |
| 3314 | class NewClass(object): |
| 3315 | __doc__ = DocDescr() |
| 3316 | vereq(OldClass.__doc__, 'object=None; type=OldClass') |
| 3317 | vereq(OldClass().__doc__, 'object=OldClass instance; type=OldClass') |
| 3318 | vereq(NewClass.__doc__, 'object=None; type=NewClass') |
| 3319 | vereq(NewClass().__doc__, 'object=NewClass instance; type=NewClass') |
| 3320 | |
Tim Peters | afb2c80 | 2002-04-18 18:06:20 +0000 | [diff] [blame] | 3321 | def string_exceptions(): |
| 3322 | if verbose: |
| 3323 | print "Testing string exceptions ..." |
| 3324 | |
| 3325 | # Ensure builtin strings work OK as exceptions. |
| 3326 | astring = "An exception string." |
| 3327 | try: |
| 3328 | raise astring |
| 3329 | except astring: |
| 3330 | pass |
| 3331 | else: |
| 3332 | raise TestFailed, "builtin string not usable as exception" |
| 3333 | |
| 3334 | # Ensure string subclass instances do not. |
| 3335 | class MyStr(str): |
| 3336 | pass |
| 3337 | |
| 3338 | newstring = MyStr("oops -- shouldn't work") |
| 3339 | try: |
| 3340 | raise newstring |
| 3341 | except TypeError: |
| 3342 | pass |
| 3343 | except: |
| 3344 | raise TestFailed, "string subclass allowed as exception" |
| 3345 | |
Guido van Rossum | a48cb8f | 2002-06-06 17:53:03 +0000 | [diff] [blame] | 3346 | def copy_setstate(): |
| 3347 | if verbose: |
| 3348 | print "Testing that copy.*copy() correctly uses __setstate__..." |
| 3349 | import copy |
| 3350 | class C(object): |
| 3351 | def __init__(self, foo=None): |
| 3352 | self.foo = foo |
| 3353 | self.__foo = foo |
| 3354 | def setfoo(self, foo=None): |
| 3355 | self.foo = foo |
| 3356 | def getfoo(self): |
| 3357 | return self.__foo |
| 3358 | def __getstate__(self): |
| 3359 | return [self.foo] |
| 3360 | def __setstate__(self, lst): |
| 3361 | assert len(lst) == 1 |
| 3362 | self.__foo = self.foo = lst[0] |
| 3363 | a = C(42) |
| 3364 | a.setfoo(24) |
| 3365 | vereq(a.foo, 24) |
| 3366 | vereq(a.getfoo(), 42) |
| 3367 | b = copy.copy(a) |
| 3368 | vereq(b.foo, 24) |
| 3369 | vereq(b.getfoo(), 24) |
| 3370 | b = copy.deepcopy(a) |
| 3371 | vereq(b.foo, 24) |
| 3372 | vereq(b.getfoo(), 24) |
| 3373 | |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 3374 | def slices(): |
| 3375 | if verbose: |
| 3376 | print "Testing cases with slices and overridden __getitem__ ..." |
| 3377 | # Strings |
| 3378 | vereq("hello"[:4], "hell") |
| 3379 | vereq("hello"[slice(4)], "hell") |
| 3380 | vereq(str.__getitem__("hello", slice(4)), "hell") |
| 3381 | class S(str): |
| 3382 | def __getitem__(self, x): |
| 3383 | return str.__getitem__(self, x) |
| 3384 | vereq(S("hello")[:4], "hell") |
| 3385 | vereq(S("hello")[slice(4)], "hell") |
| 3386 | vereq(S("hello").__getitem__(slice(4)), "hell") |
| 3387 | # Tuples |
| 3388 | vereq((1,2,3)[:2], (1,2)) |
| 3389 | vereq((1,2,3)[slice(2)], (1,2)) |
| 3390 | vereq(tuple.__getitem__((1,2,3), slice(2)), (1,2)) |
| 3391 | class T(tuple): |
| 3392 | def __getitem__(self, x): |
| 3393 | return tuple.__getitem__(self, x) |
| 3394 | vereq(T((1,2,3))[:2], (1,2)) |
| 3395 | vereq(T((1,2,3))[slice(2)], (1,2)) |
| 3396 | vereq(T((1,2,3)).__getitem__(slice(2)), (1,2)) |
| 3397 | # Lists |
| 3398 | vereq([1,2,3][:2], [1,2]) |
| 3399 | vereq([1,2,3][slice(2)], [1,2]) |
| 3400 | vereq(list.__getitem__([1,2,3], slice(2)), [1,2]) |
| 3401 | class L(list): |
| 3402 | def __getitem__(self, x): |
| 3403 | return list.__getitem__(self, x) |
| 3404 | vereq(L([1,2,3])[:2], [1,2]) |
| 3405 | vereq(L([1,2,3])[slice(2)], [1,2]) |
| 3406 | vereq(L([1,2,3]).__getitem__(slice(2)), [1,2]) |
| 3407 | # Now do lists and __setitem__ |
| 3408 | a = L([1,2,3]) |
| 3409 | a[slice(1, 3)] = [3,2] |
| 3410 | vereq(a, [1,3,2]) |
| 3411 | a[slice(0, 2, 1)] = [3,1] |
| 3412 | vereq(a, [3,1,2]) |
| 3413 | a.__setitem__(slice(1, 3), [2,1]) |
| 3414 | vereq(a, [3,2,1]) |
| 3415 | a.__setitem__(slice(0, 2, 1), [2,3]) |
| 3416 | vereq(a, [2,3,1]) |
| 3417 | |
Tim Peters | 2484aae | 2002-07-11 06:56:07 +0000 | [diff] [blame] | 3418 | def subtype_resurrection(): |
| 3419 | if verbose: |
Tim Peters | 45228ca | 2002-07-11 07:09:42 +0000 | [diff] [blame] | 3420 | print "Testing resurrection of new-style instance..." |
Tim Peters | 2484aae | 2002-07-11 06:56:07 +0000 | [diff] [blame] | 3421 | |
| 3422 | class C(object): |
| 3423 | container = [] |
| 3424 | |
| 3425 | def __del__(self): |
| 3426 | # resurrect the instance |
| 3427 | C.container.append(self) |
| 3428 | |
| 3429 | c = C() |
| 3430 | c.attr = 42 |
Tim Peters | 14cb1e1 | 2002-07-11 18:26:21 +0000 | [diff] [blame] | 3431 | # The most interesting thing here is whether this blows up, due to flawed |
Tim Peters | 45228ca | 2002-07-11 07:09:42 +0000 | [diff] [blame] | 3432 | # GC tracking logic in typeobject.c's call_finalizer() (a 2.2.1 bug). |
Tim Peters | 2484aae | 2002-07-11 06:56:07 +0000 | [diff] [blame] | 3433 | del c |
Tim Peters | 14cb1e1 | 2002-07-11 18:26:21 +0000 | [diff] [blame] | 3434 | |
| 3435 | # If that didn't blow up, it's also interesting to see whether clearing |
| 3436 | # the last container slot works: that will attempt to delete c again, |
| 3437 | # which will cause c to get appended back to the container again "during" |
| 3438 | # the del. |
| 3439 | del C.container[-1] |
| 3440 | vereq(len(C.container), 1) |
Tim Peters | 2484aae | 2002-07-11 06:56:07 +0000 | [diff] [blame] | 3441 | vereq(C.container[-1].attr, 42) |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 3442 | |
Tim Peters | 14cb1e1 | 2002-07-11 18:26:21 +0000 | [diff] [blame] | 3443 | # Make c mortal again, so that the test framework with -l doesn't report |
| 3444 | # it as a leak. |
| 3445 | del C.__del__ |
| 3446 | |
Guido van Rossum | 2d70246 | 2002-08-06 21:28:28 +0000 | [diff] [blame] | 3447 | def slottrash(): |
| 3448 | # Deallocating deeply nested slotted trash caused stack overflows |
| 3449 | if verbose: |
| 3450 | print "Testing slot trash..." |
| 3451 | class trash(object): |
| 3452 | __slots__ = ['x'] |
| 3453 | def __init__(self, x): |
| 3454 | self.x = x |
| 3455 | o = None |
| 3456 | for i in xrange(50000): |
| 3457 | o = trash(o) |
| 3458 | del o |
| 3459 | |
Neal Norwitz | f9dd0f1 | 2002-08-13 17:16:49 +0000 | [diff] [blame] | 3460 | def slotmultipleinheritance(): |
| 3461 | # SF bug 575229, multiple inheritance w/ slots dumps core |
| 3462 | class A(object): |
| 3463 | __slots__=() |
| 3464 | class B(object): |
| 3465 | pass |
| 3466 | class C(A,B) : |
| 3467 | __slots__=() |
Guido van Rossum | 8b056da | 2002-08-13 18:26:26 +0000 | [diff] [blame] | 3468 | vereq(C.__basicsize__, B.__basicsize__) |
| 3469 | verify(hasattr(C, '__dict__')) |
| 3470 | verify(hasattr(C, '__weakref__')) |
| 3471 | C().x = 2 |
Neal Norwitz | f9dd0f1 | 2002-08-13 17:16:49 +0000 | [diff] [blame] | 3472 | |
Guido van Rossum | 0f5f0b8 | 2002-08-09 16:11:37 +0000 | [diff] [blame] | 3473 | def testrmul(): |
| 3474 | # SF patch 592646 |
| 3475 | if verbose: |
| 3476 | print "Testing correct invocation of __rmul__..." |
| 3477 | class C(object): |
| 3478 | def __mul__(self, other): |
| 3479 | return "mul" |
| 3480 | def __rmul__(self, other): |
| 3481 | return "rmul" |
| 3482 | a = C() |
| 3483 | vereq(a*2, "mul") |
| 3484 | vereq(a*2.2, "mul") |
| 3485 | vereq(2*a, "rmul") |
| 3486 | vereq(2.2*a, "rmul") |
| 3487 | |
Guido van Rossum | 6e5680f | 2002-10-15 01:01:53 +0000 | [diff] [blame] | 3488 | def testipow(): |
| 3489 | # [SF bug 620179] |
| 3490 | if verbose: |
| 3491 | print "Testing correct invocation of __ipow__..." |
| 3492 | class C(object): |
| 3493 | def __ipow__(self, other): |
| 3494 | pass |
| 3495 | a = C() |
| 3496 | a **= 2 |
| 3497 | |
Guido van Rossum | 9fc8a29 | 2002-05-24 21:40:08 +0000 | [diff] [blame] | 3498 | def do_this_first(): |
| 3499 | if verbose: |
| 3500 | print "Testing SF bug 551412 ..." |
| 3501 | # This dumps core when SF bug 551412 isn't fixed -- |
| 3502 | # but only when test_descr.py is run separately. |
| 3503 | # (That can't be helped -- as soon as PyType_Ready() |
| 3504 | # is called for PyLong_Type, the bug is gone.) |
| 3505 | class UserLong(object): |
| 3506 | def __pow__(self, *args): |
| 3507 | pass |
| 3508 | try: |
| 3509 | pow(0L, UserLong(), 0L) |
| 3510 | except: |
| 3511 | pass |
| 3512 | |
Guido van Rossum | a96b0df | 2002-06-18 16:49:45 +0000 | [diff] [blame] | 3513 | if verbose: |
| 3514 | print "Testing SF bug 570483..." |
| 3515 | # Another segfault only when run early |
| 3516 | # (before PyType_Ready(tuple) is called) |
| 3517 | type.mro(tuple) |
| 3518 | |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 3519 | def test_mutable_bases(): |
Guido van Rossum | 2720b0d | 2003-01-06 21:26:44 +0000 | [diff] [blame] | 3520 | if verbose: |
| 3521 | print "Testing mutable bases..." |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3522 | # stuff that should work: |
| 3523 | class C(object): |
| 3524 | pass |
| 3525 | class C2(object): |
| 3526 | def __getattribute__(self, attr): |
| 3527 | if attr == 'a': |
| 3528 | return 2 |
| 3529 | else: |
Tim Peters | 6578dc9 | 2002-12-24 18:31:27 +0000 | [diff] [blame] | 3530 | return super(C2, self).__getattribute__(attr) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3531 | def meth(self): |
| 3532 | return 1 |
| 3533 | class D(C): |
| 3534 | pass |
| 3535 | class E(D): |
| 3536 | pass |
| 3537 | d = D() |
| 3538 | e = E() |
Michael W. Hudson | caf17be | 2002-11-27 10:24:44 +0000 | [diff] [blame] | 3539 | D.__bases__ = (C,) |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3540 | D.__bases__ = (C2,) |
| 3541 | vereq(d.meth(), 1) |
| 3542 | vereq(e.meth(), 1) |
| 3543 | vereq(d.a, 2) |
| 3544 | vereq(e.a, 2) |
| 3545 | vereq(C2.__subclasses__(), [D]) |
| 3546 | |
| 3547 | # stuff that shouldn't: |
| 3548 | class L(list): |
| 3549 | pass |
| 3550 | |
| 3551 | try: |
| 3552 | L.__bases__ = (dict,) |
| 3553 | except TypeError: |
| 3554 | pass |
| 3555 | else: |
| 3556 | raise TestFailed, "shouldn't turn list subclass into dict subclass" |
| 3557 | |
| 3558 | try: |
| 3559 | list.__bases__ = (dict,) |
| 3560 | except TypeError: |
| 3561 | pass |
| 3562 | else: |
| 3563 | raise TestFailed, "shouldn't be able to assign to list.__bases__" |
| 3564 | |
| 3565 | try: |
| 3566 | del D.__bases__ |
| 3567 | except TypeError: |
| 3568 | pass |
| 3569 | else: |
| 3570 | raise TestFailed, "shouldn't be able to delete .__bases__" |
| 3571 | |
| 3572 | try: |
Guido van Rossum | 3bbc0ee | 2002-12-13 17:49:38 +0000 | [diff] [blame] | 3573 | D.__bases__ = () |
| 3574 | except TypeError, msg: |
| 3575 | if str(msg) == "a new-style class can't have only classic bases": |
| 3576 | raise TestFailed, "wrong error message for .__bases__ = ()" |
| 3577 | else: |
| 3578 | raise TestFailed, "shouldn't be able to set .__bases__ to ()" |
| 3579 | |
| 3580 | try: |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3581 | D.__bases__ = (D,) |
| 3582 | except TypeError: |
| 3583 | pass |
| 3584 | else: |
| 3585 | # actually, we'll have crashed by here... |
| 3586 | raise TestFailed, "shouldn't be able to create inheritance cycles" |
| 3587 | |
Michael W. Hudson | caf17be | 2002-11-27 10:24:44 +0000 | [diff] [blame] | 3588 | try: |
Michael W. Hudson | e723e45 | 2003-08-07 14:58:10 +0000 | [diff] [blame] | 3589 | D.__bases__ = (C, C) |
| 3590 | except TypeError: |
| 3591 | pass |
| 3592 | else: |
| 3593 | raise TestFailed, "didn't detect repeated base classes" |
| 3594 | |
| 3595 | try: |
Michael W. Hudson | caf17be | 2002-11-27 10:24:44 +0000 | [diff] [blame] | 3596 | D.__bases__ = (E,) |
| 3597 | except TypeError: |
| 3598 | pass |
| 3599 | else: |
| 3600 | raise TestFailed, "shouldn't be able to create inheritance cycles" |
| 3601 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3602 | # let's throw a classic class into the mix: |
| 3603 | class Classic: |
| 3604 | def meth2(self): |
| 3605 | return 3 |
| 3606 | |
| 3607 | D.__bases__ = (C, Classic) |
| 3608 | |
| 3609 | vereq(d.meth2(), 3) |
| 3610 | vereq(e.meth2(), 3) |
| 3611 | try: |
| 3612 | d.a |
| 3613 | except AttributeError: |
| 3614 | pass |
| 3615 | else: |
| 3616 | raise TestFailed, "attribute should have vanished" |
| 3617 | |
| 3618 | try: |
| 3619 | D.__bases__ = (Classic,) |
| 3620 | except TypeError: |
| 3621 | pass |
| 3622 | else: |
| 3623 | raise TestFailed, "new-style class must have a new-style base" |
| 3624 | |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 3625 | def test_mutable_bases_with_failing_mro(): |
Guido van Rossum | 2720b0d | 2003-01-06 21:26:44 +0000 | [diff] [blame] | 3626 | if verbose: |
| 3627 | print "Testing mutable bases with failing mro..." |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 3628 | class WorkOnce(type): |
| 3629 | def __new__(self, name, bases, ns): |
| 3630 | self.flag = 0 |
| 3631 | return super(WorkOnce, self).__new__(WorkOnce, name, bases, ns) |
| 3632 | def mro(self): |
| 3633 | if self.flag > 0: |
| 3634 | raise RuntimeError, "bozo" |
| 3635 | else: |
| 3636 | self.flag += 1 |
| 3637 | return type.mro(self) |
| 3638 | |
| 3639 | class WorkAlways(type): |
| 3640 | def mro(self): |
| 3641 | # this is here to make sure that .mro()s aren't called |
| 3642 | # with an exception set (which was possible at one point). |
| 3643 | # An error message will be printed in a debug build. |
| 3644 | # What's a good way to test for this? |
| 3645 | return type.mro(self) |
| 3646 | |
| 3647 | class C(object): |
| 3648 | pass |
| 3649 | |
| 3650 | class C2(object): |
| 3651 | pass |
| 3652 | |
| 3653 | class D(C): |
| 3654 | pass |
| 3655 | |
| 3656 | class E(D): |
| 3657 | pass |
| 3658 | |
| 3659 | class F(D): |
| 3660 | __metaclass__ = WorkOnce |
| 3661 | |
| 3662 | class G(D): |
| 3663 | __metaclass__ = WorkAlways |
| 3664 | |
| 3665 | # Immediate subclasses have their mro's adjusted in alphabetical |
| 3666 | # order, so E's will get adjusted before adjusting F's fails. We |
| 3667 | # check here that E's gets restored. |
Tim Peters | 6578dc9 | 2002-12-24 18:31:27 +0000 | [diff] [blame] | 3668 | |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 3669 | E_mro_before = E.__mro__ |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 3670 | D_mro_before = D.__mro__ |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 3671 | |
| 3672 | try: |
| 3673 | D.__bases__ = (C2,) |
| 3674 | except RuntimeError: |
| 3675 | vereq(E.__mro__, E_mro_before) |
Michael W. Hudson | 7e7c00d | 2002-11-27 15:40:09 +0000 | [diff] [blame] | 3676 | vereq(D.__mro__, D_mro_before) |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 3677 | else: |
| 3678 | raise TestFailed, "exception not propagated" |
| 3679 | |
| 3680 | def test_mutable_bases_catch_mro_conflict(): |
Guido van Rossum | 2720b0d | 2003-01-06 21:26:44 +0000 | [diff] [blame] | 3681 | if verbose: |
| 3682 | print "Testing mutable bases catch mro conflict..." |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 3683 | class A(object): |
| 3684 | pass |
| 3685 | |
| 3686 | class B(object): |
| 3687 | pass |
| 3688 | |
| 3689 | class C(A, B): |
| 3690 | pass |
| 3691 | |
| 3692 | class D(A, B): |
| 3693 | pass |
| 3694 | |
| 3695 | class E(C, D): |
| 3696 | pass |
| 3697 | |
| 3698 | try: |
| 3699 | C.__bases__ = (B, A) |
| 3700 | except TypeError: |
| 3701 | pass |
| 3702 | else: |
| 3703 | raise TestFailed, "didn't catch MRO conflict" |
Tim Peters | 6578dc9 | 2002-12-24 18:31:27 +0000 | [diff] [blame] | 3704 | |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3705 | def mutable_names(): |
Guido van Rossum | 2720b0d | 2003-01-06 21:26:44 +0000 | [diff] [blame] | 3706 | if verbose: |
| 3707 | print "Testing mutable names..." |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3708 | class C(object): |
| 3709 | pass |
| 3710 | |
Michael W. Hudson | ade8c8b2 | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 3711 | # C.__module__ could be 'test_descr' or '__main__' |
| 3712 | mod = C.__module__ |
Tim Peters | 6578dc9 | 2002-12-24 18:31:27 +0000 | [diff] [blame] | 3713 | |
Michael W. Hudson | ade8c8b2 | 2002-11-27 16:29:26 +0000 | [diff] [blame] | 3714 | C.__name__ = 'D' |
| 3715 | vereq((C.__module__, C.__name__), (mod, 'D')) |
| 3716 | |
| 3717 | C.__name__ = 'D.E' |
| 3718 | vereq((C.__module__, C.__name__), (mod, 'D.E')) |
Tim Peters | 6578dc9 | 2002-12-24 18:31:27 +0000 | [diff] [blame] | 3719 | |
Guido van Rossum | 613f24f | 2003-01-06 23:00:59 +0000 | [diff] [blame] | 3720 | def subclass_right_op(): |
| 3721 | if verbose: |
| 3722 | print "Testing correct dispatch of subclass overloading __r<op>__..." |
| 3723 | |
| 3724 | # This code tests various cases where right-dispatch of a subclass |
| 3725 | # should be preferred over left-dispatch of a base class. |
| 3726 | |
| 3727 | # Case 1: subclass of int; this tests code in abstract.c::binary_op1() |
| 3728 | |
| 3729 | class B(int): |
Guido van Rossum | f389c77 | 2003-02-27 20:04:19 +0000 | [diff] [blame] | 3730 | def __floordiv__(self, other): |
| 3731 | return "B.__floordiv__" |
| 3732 | def __rfloordiv__(self, other): |
| 3733 | return "B.__rfloordiv__" |
Guido van Rossum | 613f24f | 2003-01-06 23:00:59 +0000 | [diff] [blame] | 3734 | |
Guido van Rossum | f389c77 | 2003-02-27 20:04:19 +0000 | [diff] [blame] | 3735 | vereq(B(1) // 1, "B.__floordiv__") |
| 3736 | vereq(1 // B(1), "B.__rfloordiv__") |
Guido van Rossum | 613f24f | 2003-01-06 23:00:59 +0000 | [diff] [blame] | 3737 | |
| 3738 | # Case 2: subclass of object; this is just the baseline for case 3 |
| 3739 | |
| 3740 | class C(object): |
Guido van Rossum | f389c77 | 2003-02-27 20:04:19 +0000 | [diff] [blame] | 3741 | def __floordiv__(self, other): |
| 3742 | return "C.__floordiv__" |
| 3743 | def __rfloordiv__(self, other): |
| 3744 | return "C.__rfloordiv__" |
Guido van Rossum | 613f24f | 2003-01-06 23:00:59 +0000 | [diff] [blame] | 3745 | |
Guido van Rossum | f389c77 | 2003-02-27 20:04:19 +0000 | [diff] [blame] | 3746 | vereq(C() // 1, "C.__floordiv__") |
| 3747 | vereq(1 // C(), "C.__rfloordiv__") |
Guido van Rossum | 613f24f | 2003-01-06 23:00:59 +0000 | [diff] [blame] | 3748 | |
| 3749 | # Case 3: subclass of new-style class; here it gets interesting |
| 3750 | |
| 3751 | class D(C): |
Guido van Rossum | f389c77 | 2003-02-27 20:04:19 +0000 | [diff] [blame] | 3752 | def __floordiv__(self, other): |
| 3753 | return "D.__floordiv__" |
| 3754 | def __rfloordiv__(self, other): |
| 3755 | return "D.__rfloordiv__" |
Guido van Rossum | 613f24f | 2003-01-06 23:00:59 +0000 | [diff] [blame] | 3756 | |
Guido van Rossum | f389c77 | 2003-02-27 20:04:19 +0000 | [diff] [blame] | 3757 | vereq(D() // C(), "D.__floordiv__") |
| 3758 | vereq(C() // D(), "D.__rfloordiv__") |
Guido van Rossum | 613f24f | 2003-01-06 23:00:59 +0000 | [diff] [blame] | 3759 | |
| 3760 | # Case 4: this didn't work right in 2.2.2 and 2.3a1 |
| 3761 | |
| 3762 | class E(C): |
| 3763 | pass |
| 3764 | |
Guido van Rossum | f389c77 | 2003-02-27 20:04:19 +0000 | [diff] [blame] | 3765 | vereq(E.__rfloordiv__, C.__rfloordiv__) |
Guido van Rossum | 613f24f | 2003-01-06 23:00:59 +0000 | [diff] [blame] | 3766 | |
Guido van Rossum | f389c77 | 2003-02-27 20:04:19 +0000 | [diff] [blame] | 3767 | vereq(E() // 1, "C.__floordiv__") |
| 3768 | vereq(1 // E(), "C.__rfloordiv__") |
| 3769 | vereq(E() // C(), "C.__floordiv__") |
| 3770 | vereq(C() // E(), "C.__floordiv__") # This one would fail |
Guido van Rossum | 613f24f | 2003-01-06 23:00:59 +0000 | [diff] [blame] | 3771 | |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 3772 | def dict_type_with_metaclass(): |
| 3773 | if verbose: |
| 3774 | print "Testing type of __dict__ when __metaclass__ set..." |
| 3775 | |
| 3776 | class B(object): |
| 3777 | pass |
| 3778 | class M(type): |
| 3779 | pass |
| 3780 | class C: |
| 3781 | # In 2.3a1, C.__dict__ was a real dict rather than a dict proxy |
| 3782 | __metaclass__ = M |
| 3783 | veris(type(C.__dict__), type(B.__dict__)) |
| 3784 | |
Guido van Rossum | b6e5a0c | 2003-02-11 18:44:42 +0000 | [diff] [blame] | 3785 | def meth_class_get(): |
| 3786 | # Full coverage of descrobject.c::classmethod_get() |
Guido van Rossum | 03bc7d3 | 2003-02-12 03:32:58 +0000 | [diff] [blame] | 3787 | if verbose: |
| 3788 | print "Testing __get__ method of METH_CLASS C methods..." |
Guido van Rossum | b6e5a0c | 2003-02-11 18:44:42 +0000 | [diff] [blame] | 3789 | # Baseline |
| 3790 | arg = [1, 2, 3] |
| 3791 | res = {1: None, 2: None, 3: None} |
| 3792 | vereq(dict.fromkeys(arg), res) |
| 3793 | vereq({}.fromkeys(arg), res) |
| 3794 | # Now get the descriptor |
| 3795 | descr = dict.__dict__["fromkeys"] |
| 3796 | # More baseline using the descriptor directly |
| 3797 | vereq(descr.__get__(None, dict)(arg), res) |
| 3798 | vereq(descr.__get__({})(arg), res) |
| 3799 | # Now check various error cases |
| 3800 | try: |
| 3801 | descr.__get__(None, None) |
| 3802 | except TypeError: |
| 3803 | pass |
| 3804 | else: |
| 3805 | raise TestFailed, "shouldn't have allowed descr.__get__(None, None)" |
| 3806 | try: |
| 3807 | descr.__get__(42) |
| 3808 | except TypeError: |
| 3809 | pass |
| 3810 | else: |
| 3811 | raise TestFailed, "shouldn't have allowed descr.__get__(42)" |
| 3812 | try: |
| 3813 | descr.__get__(None, 42) |
| 3814 | except TypeError: |
| 3815 | pass |
| 3816 | else: |
| 3817 | raise TestFailed, "shouldn't have allowed descr.__get__(None, 42)" |
| 3818 | try: |
| 3819 | descr.__get__(None, int) |
| 3820 | except TypeError: |
| 3821 | pass |
| 3822 | else: |
| 3823 | raise TestFailed, "shouldn't have allowed descr.__get__(None, int)" |
| 3824 | |
Guido van Rossum | 03bc7d3 | 2003-02-12 03:32:58 +0000 | [diff] [blame] | 3825 | def isinst_isclass(): |
| 3826 | if verbose: |
| 3827 | print "Testing proxy isinstance() and isclass()..." |
| 3828 | class Proxy(object): |
| 3829 | def __init__(self, obj): |
| 3830 | self.__obj = obj |
| 3831 | def __getattribute__(self, name): |
| 3832 | if name.startswith("_Proxy__"): |
| 3833 | return object.__getattribute__(self, name) |
| 3834 | else: |
| 3835 | return getattr(self.__obj, name) |
| 3836 | # Test with a classic class |
| 3837 | class C: |
| 3838 | pass |
| 3839 | a = C() |
| 3840 | pa = Proxy(a) |
| 3841 | verify(isinstance(a, C)) # Baseline |
| 3842 | verify(isinstance(pa, C)) # Test |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 3843 | # Test with a classic subclass |
| 3844 | class D(C): |
| 3845 | pass |
| 3846 | a = D() |
| 3847 | pa = Proxy(a) |
| 3848 | verify(isinstance(a, C)) # Baseline |
| 3849 | verify(isinstance(pa, C)) # Test |
Guido van Rossum | 03bc7d3 | 2003-02-12 03:32:58 +0000 | [diff] [blame] | 3850 | # Test with a new-style class |
| 3851 | class C(object): |
| 3852 | pass |
| 3853 | a = C() |
| 3854 | pa = Proxy(a) |
| 3855 | verify(isinstance(a, C)) # Baseline |
| 3856 | verify(isinstance(pa, C)) # Test |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 3857 | # Test with a new-style subclass |
| 3858 | class D(C): |
| 3859 | pass |
| 3860 | a = D() |
| 3861 | pa = Proxy(a) |
| 3862 | verify(isinstance(a, C)) # Baseline |
| 3863 | verify(isinstance(pa, C)) # Test |
| 3864 | |
| 3865 | def proxysuper(): |
| 3866 | if verbose: |
| 3867 | print "Testing super() for a proxy object..." |
| 3868 | class Proxy(object): |
| 3869 | def __init__(self, obj): |
| 3870 | self.__obj = obj |
| 3871 | def __getattribute__(self, name): |
| 3872 | if name.startswith("_Proxy__"): |
| 3873 | return object.__getattribute__(self, name) |
| 3874 | else: |
| 3875 | return getattr(self.__obj, name) |
| 3876 | |
| 3877 | class B(object): |
| 3878 | def f(self): |
| 3879 | return "B.f" |
| 3880 | |
| 3881 | class C(B): |
| 3882 | def f(self): |
| 3883 | return super(C, self).f() + "->C.f" |
| 3884 | |
| 3885 | obj = C() |
| 3886 | p = Proxy(obj) |
| 3887 | vereq(C.__dict__["f"](p), "B.f->C.f") |
Guido van Rossum | 03bc7d3 | 2003-02-12 03:32:58 +0000 | [diff] [blame] | 3888 | |
Guido van Rossum | 52b2705 | 2003-04-15 20:05:10 +0000 | [diff] [blame] | 3889 | def carloverre(): |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 3890 | if verbose: |
Guido van Rossum | 52b2705 | 2003-04-15 20:05:10 +0000 | [diff] [blame] | 3891 | print "Testing prohibition of Carlo Verre's hack..." |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 3892 | try: |
| 3893 | object.__setattr__(str, "foo", 42) |
| 3894 | except TypeError: |
| 3895 | pass |
| 3896 | else: |
Guido van Rossum | 52b2705 | 2003-04-15 20:05:10 +0000 | [diff] [blame] | 3897 | raise TestFailed, "Carlo Verre __setattr__ suceeded!" |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 3898 | try: |
| 3899 | object.__delattr__(str, "lower") |
| 3900 | except TypeError: |
| 3901 | pass |
| 3902 | else: |
Guido van Rossum | 52b2705 | 2003-04-15 20:05:10 +0000 | [diff] [blame] | 3903 | raise TestFailed, "Carlo Verre __delattr__ succeeded!" |
Guido van Rossum | 4dcdb78 | 2003-04-14 21:46:03 +0000 | [diff] [blame] | 3904 | |
Guido van Rossum | aabe0b3 | 2003-05-29 14:26:57 +0000 | [diff] [blame] | 3905 | def weakref_segfault(): |
| 3906 | # SF 742911 |
| 3907 | if verbose: |
| 3908 | print "Testing weakref segfault..." |
| 3909 | |
| 3910 | import weakref |
| 3911 | |
| 3912 | class Provoker: |
| 3913 | def __init__(self, referrent): |
| 3914 | self.ref = weakref.ref(referrent) |
| 3915 | |
| 3916 | def __del__(self): |
| 3917 | x = self.ref() |
Guido van Rossum | aabe0b3 | 2003-05-29 14:26:57 +0000 | [diff] [blame] | 3918 | |
| 3919 | class Oops(object): |
| 3920 | pass |
| 3921 | |
| 3922 | o = Oops() |
| 3923 | o.whatever = Provoker(o) |
| 3924 | del o |
| 3925 | |
Raymond Hettinger | 2b6220d | 2003-06-29 15:44:07 +0000 | [diff] [blame] | 3926 | # Fix SF #762455, segfault when sys.stdout is changed in getattr |
| 3927 | def filefault(): |
| 3928 | if verbose: |
| 3929 | print "Testing sys.stdout is changed in getattr..." |
| 3930 | import sys |
| 3931 | class StdoutGuard: |
| 3932 | def __getattr__(self, attr): |
| 3933 | sys.stdout = sys.__stdout__ |
| 3934 | raise RuntimeError("Premature access to sys.stdout.%s" % attr) |
| 3935 | sys.stdout = StdoutGuard() |
| 3936 | try: |
| 3937 | print "Oops!" |
| 3938 | except RuntimeError: |
| 3939 | pass |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 3940 | |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 3941 | def vicious_descriptor_nonsense(): |
| 3942 | # A potential segfault spotted by Thomas Wouters in mail to |
| 3943 | # python-dev 2003-04-17, turned into an example & fixed by Michael |
| 3944 | # Hudson just less than four months later... |
| 3945 | if verbose: |
| 3946 | print "Testing vicious_descriptor_nonsense..." |
| 3947 | |
| 3948 | class Evil(object): |
| 3949 | def __hash__(self): |
| 3950 | return hash('attr') |
| 3951 | def __eq__(self, other): |
| 3952 | del C.attr |
| 3953 | return 0 |
| 3954 | |
| 3955 | class Descr(object): |
| 3956 | def __get__(self, ob, type=None): |
| 3957 | return 1 |
| 3958 | |
| 3959 | class C(object): |
| 3960 | attr = Descr() |
| 3961 | |
| 3962 | c = C() |
| 3963 | c.__dict__[Evil()] = 0 |
| 3964 | |
| 3965 | vereq(c.attr, 1) |
| 3966 | # this makes a crash more likely: |
| 3967 | import gc; gc.collect() |
| 3968 | vereq(hasattr(c, 'attr'), False) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 3969 | |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 3970 | |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 3971 | def test_main(): |
Guido van Rossum | aabe0b3 | 2003-05-29 14:26:57 +0000 | [diff] [blame] | 3972 | weakref_segfault() # Must be first, somehow |
Guido van Rossum | 9fc8a29 | 2002-05-24 21:40:08 +0000 | [diff] [blame] | 3973 | do_this_first() |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 3974 | class_docstrings() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3975 | lists() |
| 3976 | dicts() |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 3977 | dict_constructor() |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 3978 | test_dir() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3979 | ints() |
| 3980 | longs() |
| 3981 | floats() |
| 3982 | complexes() |
| 3983 | spamlists() |
| 3984 | spamdicts() |
| 3985 | pydicts() |
| 3986 | pylists() |
| 3987 | metaclass() |
| 3988 | pymods() |
| 3989 | multi() |
Guido van Rossum | d32047f | 2002-11-25 21:38:52 +0000 | [diff] [blame] | 3990 | mro_disagreement() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3991 | diamond() |
Guido van Rossum | 9a81892 | 2002-11-14 19:50:14 +0000 | [diff] [blame] | 3992 | ex5() |
| 3993 | monotonicity() |
| 3994 | consistency_with_epg() |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 3995 | objects() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3996 | slots() |
Guido van Rossum | 8b056da | 2002-08-13 18:26:26 +0000 | [diff] [blame] | 3997 | slotspecials() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3998 | dynamics() |
| 3999 | errors() |
| 4000 | classmethods() |
Fred Drake | f841aa6 | 2002-03-28 15:49:54 +0000 | [diff] [blame] | 4001 | classmethods_in_c() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4002 | staticmethods() |
Fred Drake | f841aa6 | 2002-03-28 15:49:54 +0000 | [diff] [blame] | 4003 | staticmethods_in_c() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4004 | classic() |
| 4005 | compattr() |
| 4006 | newslot() |
| 4007 | altmro() |
| 4008 | overloading() |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 4009 | methods() |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 4010 | specials() |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 4011 | weakrefs() |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 4012 | properties() |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 4013 | supers() |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 4014 | inherits() |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 4015 | keywords() |
Tim Peters | 8fa4567 | 2001-09-13 21:01:29 +0000 | [diff] [blame] | 4016 | restricted() |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 4017 | str_subclass_as_dict_key() |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 4018 | classic_comparisons() |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 4019 | rich_comparisons() |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 4020 | coercions() |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 4021 | descrdoc() |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 4022 | setclass() |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 4023 | setdict() |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 4024 | pickles() |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 4025 | copies() |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 4026 | binopoverride() |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 4027 | subclasspropagation() |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 4028 | buffer_inherit() |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 4029 | str_of_str_subclass() |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 4030 | kwdargs() |
Guido van Rossum | ed87ad8 | 2001-10-30 02:33:02 +0000 | [diff] [blame] | 4031 | delhook() |
Guido van Rossum | dbb53d9 | 2001-12-03 16:32:18 +0000 | [diff] [blame] | 4032 | hashinherit() |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 4033 | strops() |
Guido van Rossum | 2764a3a | 2001-12-28 21:39:03 +0000 | [diff] [blame] | 4034 | deepcopyrecursive() |
Guido van Rossum | d703567 | 2002-03-12 20:43:31 +0000 | [diff] [blame] | 4035 | modules() |
Walter Dörwald | dbd2d25 | 2002-03-25 18:36:32 +0000 | [diff] [blame] | 4036 | dictproxyiterkeys() |
| 4037 | dictproxyitervalues() |
| 4038 | dictproxyiteritems() |
Guido van Rossum | 8c84255 | 2002-03-14 23:05:54 +0000 | [diff] [blame] | 4039 | pickleslots() |
Guido van Rossum | 8ace1ab | 2002-04-06 01:05:01 +0000 | [diff] [blame] | 4040 | funnynew() |
Guido van Rossum | e8fc640 | 2002-04-16 16:44:51 +0000 | [diff] [blame] | 4041 | imulbug() |
Guido van Rossum | d99b3e7 | 2002-04-18 00:27:33 +0000 | [diff] [blame] | 4042 | docdescriptor() |
Tim Peters | afb2c80 | 2002-04-18 18:06:20 +0000 | [diff] [blame] | 4043 | string_exceptions() |
Guido van Rossum | a48cb8f | 2002-06-06 17:53:03 +0000 | [diff] [blame] | 4044 | copy_setstate() |
Guido van Rossum | 09638c1 | 2002-06-13 19:17:46 +0000 | [diff] [blame] | 4045 | slices() |
Tim Peters | 2484aae | 2002-07-11 06:56:07 +0000 | [diff] [blame] | 4046 | subtype_resurrection() |
Guido van Rossum | 2d70246 | 2002-08-06 21:28:28 +0000 | [diff] [blame] | 4047 | slottrash() |
Neal Norwitz | f9dd0f1 | 2002-08-13 17:16:49 +0000 | [diff] [blame] | 4048 | slotmultipleinheritance() |
Guido van Rossum | 0f5f0b8 | 2002-08-09 16:11:37 +0000 | [diff] [blame] | 4049 | testrmul() |
Guido van Rossum | 6e5680f | 2002-10-15 01:01:53 +0000 | [diff] [blame] | 4050 | testipow() |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 4051 | test_mutable_bases() |
| 4052 | test_mutable_bases_with_failing_mro() |
| 4053 | test_mutable_bases_catch_mro_conflict() |
Michael W. Hudson | 98bbc49 | 2002-11-26 14:47:27 +0000 | [diff] [blame] | 4054 | mutable_names() |
Guido van Rossum | 613f24f | 2003-01-06 23:00:59 +0000 | [diff] [blame] | 4055 | subclass_right_op() |
Guido van Rossum | 373c741 | 2003-01-07 13:41:37 +0000 | [diff] [blame] | 4056 | dict_type_with_metaclass() |
Guido van Rossum | b6e5a0c | 2003-02-11 18:44:42 +0000 | [diff] [blame] | 4057 | meth_class_get() |
Guido van Rossum | 03bc7d3 | 2003-02-12 03:32:58 +0000 | [diff] [blame] | 4058 | isinst_isclass() |
Guido van Rossum | a89d10e | 2003-02-12 03:58:38 +0000 | [diff] [blame] | 4059 | proxysuper() |
Guido van Rossum | 52b2705 | 2003-04-15 20:05:10 +0000 | [diff] [blame] | 4060 | carloverre() |
Raymond Hettinger | 2b6220d | 2003-06-29 15:44:07 +0000 | [diff] [blame] | 4061 | filefault() |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 4062 | vicious_descriptor_nonsense() |
Michael W. Hudson | 586da8f | 2002-11-27 15:20:19 +0000 | [diff] [blame] | 4063 | |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 4064 | if verbose: print "All OK" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4065 | |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 4066 | if __name__ == "__main__": |
| 4067 | test_main() |