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