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