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