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