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 | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 3 | from test_support import verify, verbose, TestFailed, TESTFN |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4 | from copy import deepcopy |
| 5 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 6 | def vereq(a, b): |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 7 | if not (a == b): |
| 8 | raise TestFailed, "%r == %r" % (a, b) |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 10 | def veris(a, b): |
| 11 | if a is not b: |
| 12 | raise TestFailed, "%r is %r" % (a, b) |
| 13 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 14 | def testunop(a, res, expr="len(a)", meth="__len__"): |
| 15 | if verbose: print "checking", expr |
| 16 | dict = {'a': a} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 17 | vereq(eval(expr, dict), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 18 | t = type(a) |
| 19 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 20 | while meth not in t.__dict__: |
| 21 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 22 | vereq(m, t.__dict__[meth]) |
| 23 | vereq(m(a), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 24 | bm = getattr(a, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 25 | vereq(bm(), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 26 | |
| 27 | def testbinop(a, b, res, expr="a+b", meth="__add__"): |
| 28 | if verbose: print "checking", expr |
| 29 | dict = {'a': a, 'b': b} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 30 | vereq(eval(expr, dict), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 31 | t = type(a) |
| 32 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 33 | while meth not in t.__dict__: |
| 34 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 35 | vereq(m, t.__dict__[meth]) |
| 36 | vereq(m(a, b), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 37 | bm = getattr(a, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 38 | vereq(bm(b), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 39 | |
| 40 | def testternop(a, b, c, res, expr="a[b:c]", meth="__getslice__"): |
| 41 | if verbose: print "checking", expr |
| 42 | dict = {'a': a, 'b': b, 'c': c} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 43 | vereq(eval(expr, dict), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 44 | t = type(a) |
| 45 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 46 | while meth not in t.__dict__: |
| 47 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 48 | vereq(m, t.__dict__[meth]) |
| 49 | vereq(m(a, b, c), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 50 | bm = getattr(a, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 51 | vereq(bm(b, c), res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 52 | |
| 53 | def testsetop(a, b, res, stmt="a+=b", meth="__iadd__"): |
| 54 | if verbose: print "checking", stmt |
| 55 | dict = {'a': deepcopy(a), 'b': b} |
| 56 | exec stmt in dict |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 57 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 58 | t = type(a) |
| 59 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 60 | while meth not in t.__dict__: |
| 61 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 62 | vereq(m, t.__dict__[meth]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 63 | dict['a'] = deepcopy(a) |
| 64 | m(dict['a'], b) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 65 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 66 | dict['a'] = deepcopy(a) |
| 67 | bm = getattr(dict['a'], meth) |
| 68 | bm(b) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 69 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 70 | |
| 71 | def testset2op(a, b, c, res, stmt="a[b]=c", meth="__setitem__"): |
| 72 | if verbose: print "checking", stmt |
| 73 | dict = {'a': deepcopy(a), 'b': b, 'c': c} |
| 74 | exec stmt in dict |
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 | t = type(a) |
| 77 | m = getattr(t, meth) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 78 | while meth not in t.__dict__: |
| 79 | t = t.__bases__[0] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 80 | vereq(m, t.__dict__[meth]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 81 | dict['a'] = deepcopy(a) |
| 82 | m(dict['a'], b, c) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 83 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 84 | dict['a'] = deepcopy(a) |
| 85 | bm = getattr(dict['a'], meth) |
| 86 | bm(b, c) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 87 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 88 | |
| 89 | def testset3op(a, b, c, d, res, stmt="a[b:c]=d", meth="__setslice__"): |
| 90 | if verbose: print "checking", stmt |
| 91 | dict = {'a': deepcopy(a), 'b': b, 'c': c, 'd': d} |
| 92 | exec stmt in dict |
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 | t = type(a) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 95 | while meth not in t.__dict__: |
| 96 | t = t.__bases__[0] |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 97 | m = getattr(t, meth) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 98 | vereq(m, t.__dict__[meth]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 99 | dict['a'] = deepcopy(a) |
| 100 | m(dict['a'], b, c, d) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 101 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 102 | dict['a'] = deepcopy(a) |
| 103 | bm = getattr(dict['a'], meth) |
| 104 | bm(b, c, d) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 105 | vereq(dict['a'], res) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 106 | |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 107 | def class_docstrings(): |
| 108 | class Classic: |
| 109 | "A classic docstring." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 110 | vereq(Classic.__doc__, "A classic docstring.") |
| 111 | vereq(Classic.__dict__['__doc__'], "A classic docstring.") |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 112 | |
| 113 | class Classic2: |
| 114 | pass |
| 115 | verify(Classic2.__doc__ is None) |
| 116 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 117 | class NewStatic(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 118 | "Another docstring." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 119 | vereq(NewStatic.__doc__, "Another docstring.") |
| 120 | vereq(NewStatic.__dict__['__doc__'], "Another docstring.") |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 121 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 122 | class NewStatic2(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 123 | pass |
| 124 | verify(NewStatic2.__doc__ is None) |
| 125 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 126 | class NewDynamic(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 127 | "Another docstring." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 128 | vereq(NewDynamic.__doc__, "Another docstring.") |
| 129 | vereq(NewDynamic.__dict__['__doc__'], "Another docstring.") |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 130 | |
Tim Peters | 4fb1fe8 | 2001-10-04 05:48:13 +0000 | [diff] [blame] | 131 | class NewDynamic2(object): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 132 | pass |
| 133 | verify(NewDynamic2.__doc__ is None) |
| 134 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 135 | def lists(): |
| 136 | if verbose: print "Testing list operations..." |
| 137 | testbinop([1], [2], [1,2], "a+b", "__add__") |
| 138 | testbinop([1,2,3], 2, 1, "b in a", "__contains__") |
| 139 | testbinop([1,2,3], 4, 0, "b in a", "__contains__") |
| 140 | testbinop([1,2,3], 1, 2, "a[b]", "__getitem__") |
| 141 | testternop([1,2,3], 0, 2, [1,2], "a[b:c]", "__getslice__") |
| 142 | testsetop([1], [2], [1,2], "a+=b", "__iadd__") |
| 143 | testsetop([1,2], 3, [1,2,1,2,1,2], "a*=b", "__imul__") |
| 144 | testunop([1,2,3], 3, "len(a)", "__len__") |
| 145 | testbinop([1,2], 3, [1,2,1,2,1,2], "a*b", "__mul__") |
| 146 | testbinop([1,2], 3, [1,2,1,2,1,2], "b*a", "__rmul__") |
| 147 | testset2op([1,2], 1, 3, [1,3], "a[b]=c", "__setitem__") |
| 148 | testset3op([1,2,3,4], 1, 3, [5,6], [1,5,6,4], "a[b:c]=d", "__setslice__") |
| 149 | |
| 150 | def dicts(): |
| 151 | if verbose: print "Testing dict operations..." |
| 152 | testbinop({1:2}, {2:1}, -1, "cmp(a,b)", "__cmp__") |
| 153 | testbinop({1:2,3:4}, 1, 1, "b in a", "__contains__") |
| 154 | testbinop({1:2,3:4}, 2, 0, "b in a", "__contains__") |
| 155 | testbinop({1:2,3:4}, 1, 2, "a[b]", "__getitem__") |
| 156 | d = {1:2,3:4} |
| 157 | l1 = [] |
| 158 | for i in d.keys(): l1.append(i) |
| 159 | l = [] |
| 160 | for i in iter(d): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 161 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 162 | l = [] |
| 163 | for i in d.__iter__(): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 164 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 165 | l = [] |
| 166 | for i in dictionary.__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 | d = {1:2, 3:4} |
| 169 | testunop(d, 2, "len(a)", "__len__") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 170 | vereq(eval(repr(d), {}), d) |
| 171 | vereq(eval(d.__repr__(), {}), d) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 172 | testset2op({1:2,3:4}, 2, 3, {1:2,2:3,3:4}, "a[b]=c", "__setitem__") |
| 173 | |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 174 | def dict_constructor(): |
| 175 | if verbose: |
| 176 | print "Testing dictionary constructor ..." |
| 177 | d = dictionary() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 178 | vereq(d, {}) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 179 | d = dictionary({}) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 180 | vereq(d, {}) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 181 | d = dictionary(mapping={}) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 182 | vereq(d, {}) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 183 | d = dictionary({1: 2, 'a': 'b'}) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 184 | vereq(d, {1: 2, 'a': 'b'}) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 185 | for badarg in 0, 0L, 0j, "0", [0], (0,): |
| 186 | try: |
| 187 | dictionary(badarg) |
| 188 | except TypeError: |
| 189 | pass |
| 190 | else: |
| 191 | raise TestFailed("no TypeError from dictionary(%r)" % badarg) |
| 192 | try: |
| 193 | dictionary(senseless={}) |
| 194 | except TypeError: |
| 195 | pass |
| 196 | else: |
| 197 | raise TestFailed("no TypeError from dictionary(senseless={}") |
| 198 | |
| 199 | try: |
| 200 | dictionary({}, {}) |
| 201 | except TypeError: |
| 202 | pass |
| 203 | else: |
| 204 | raise TestFailed("no TypeError from dictionary({}, {})") |
| 205 | |
| 206 | class Mapping: |
| 207 | dict = {1:2, 3:4, 'a':1j} |
| 208 | |
| 209 | def __getitem__(self, i): |
| 210 | return self.dict[i] |
| 211 | |
| 212 | try: |
| 213 | dictionary(Mapping()) |
| 214 | except TypeError: |
| 215 | pass |
| 216 | else: |
| 217 | raise TestFailed("no TypeError from dictionary(incomplete mapping)") |
| 218 | |
| 219 | Mapping.keys = lambda self: self.dict.keys() |
| 220 | d = dictionary(mapping=Mapping()) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 221 | vereq(d, Mapping.dict) |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 222 | |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 223 | def test_dir(): |
| 224 | if verbose: |
| 225 | print "Testing dir() ..." |
| 226 | junk = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 227 | vereq(dir(), ['junk']) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 228 | del junk |
| 229 | |
| 230 | # Just make sure these don't blow up! |
| 231 | for arg in 2, 2L, 2j, 2e0, [2], "2", u"2", (2,), {2:2}, type, test_dir: |
| 232 | dir(arg) |
| 233 | |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 234 | # Try classic classes. |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 235 | class C: |
| 236 | Cdata = 1 |
| 237 | def Cmethod(self): pass |
| 238 | |
| 239 | cstuff = ['Cdata', 'Cmethod', '__doc__', '__module__'] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 240 | vereq(dir(C), cstuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 241 | verify('im_self' in dir(C.Cmethod)) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 242 | |
| 243 | 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] | 244 | vereq(dir(c), cstuff) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 245 | |
| 246 | c.cdata = 2 |
| 247 | c.cmethod = lambda self: 0 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 248 | vereq(dir(c), cstuff + ['cdata', 'cmethod']) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 249 | verify('im_self' in dir(c.Cmethod)) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 250 | |
| 251 | class A(C): |
| 252 | Adata = 1 |
| 253 | def Amethod(self): pass |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 254 | |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 255 | astuff = ['Adata', 'Amethod'] + cstuff |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 256 | vereq(dir(A), astuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 257 | verify('im_self' in dir(A.Amethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 258 | a = A() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 259 | vereq(dir(a), astuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 260 | verify('im_self' in dir(a.Amethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 261 | a.adata = 42 |
| 262 | a.amethod = lambda self: 3 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 263 | vereq(dir(a), astuff + ['adata', 'amethod']) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 264 | |
| 265 | # The same, but with new-style classes. Since these have object as a |
| 266 | # base class, a lot more gets sucked in. |
| 267 | def interesting(strings): |
| 268 | return [s for s in strings if not s.startswith('_')] |
| 269 | |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 270 | class C(object): |
| 271 | Cdata = 1 |
| 272 | def Cmethod(self): pass |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 273 | |
| 274 | cstuff = ['Cdata', 'Cmethod'] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 275 | vereq(interesting(dir(C)), cstuff) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 276 | |
| 277 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 278 | vereq(interesting(dir(c)), cstuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 279 | verify('im_self' in dir(C.Cmethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 280 | |
| 281 | c.cdata = 2 |
| 282 | c.cmethod = lambda self: 0 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 283 | vereq(interesting(dir(c)), cstuff + ['cdata', 'cmethod']) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 284 | verify('im_self' in dir(c.Cmethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 285 | |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 286 | class A(C): |
| 287 | Adata = 1 |
| 288 | def Amethod(self): pass |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 289 | |
| 290 | astuff = ['Adata', 'Amethod'] + cstuff |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 291 | vereq(interesting(dir(A)), astuff) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 292 | verify('im_self' in dir(A.Amethod)) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 293 | a = A() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 294 | vereq(interesting(dir(a)), astuff) |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 295 | a.adata = 42 |
| 296 | a.amethod = lambda self: 3 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 297 | vereq(interesting(dir(a)), astuff + ['adata', 'amethod']) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 298 | verify('im_self' in dir(a.Amethod)) |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 299 | |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 300 | # Try a module subclass. |
| 301 | import sys |
| 302 | class M(type(sys)): |
| 303 | pass |
| 304 | minstance = M() |
| 305 | minstance.b = 2 |
| 306 | minstance.a = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 307 | vereq(dir(minstance), ['a', 'b']) |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 308 | |
| 309 | class M2(M): |
| 310 | def getdict(self): |
| 311 | return "Not a dict!" |
| 312 | __dict__ = property(getdict) |
| 313 | |
| 314 | m2instance = M2() |
| 315 | m2instance.b = 2 |
| 316 | m2instance.a = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 317 | vereq(m2instance.__dict__, "Not a dict!") |
Tim Peters | caaff8d | 2001-09-10 23:12:14 +0000 | [diff] [blame] | 318 | try: |
| 319 | dir(m2instance) |
| 320 | except TypeError: |
| 321 | pass |
| 322 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 323 | binops = { |
| 324 | 'add': '+', |
| 325 | 'sub': '-', |
| 326 | 'mul': '*', |
| 327 | 'div': '/', |
| 328 | 'mod': '%', |
| 329 | 'divmod': 'divmod', |
| 330 | 'pow': '**', |
| 331 | 'lshift': '<<', |
| 332 | 'rshift': '>>', |
| 333 | 'and': '&', |
| 334 | 'xor': '^', |
| 335 | 'or': '|', |
| 336 | 'cmp': 'cmp', |
| 337 | 'lt': '<', |
| 338 | 'le': '<=', |
| 339 | 'eq': '==', |
| 340 | 'ne': '!=', |
| 341 | 'gt': '>', |
| 342 | 'ge': '>=', |
| 343 | } |
| 344 | |
| 345 | for name, expr in binops.items(): |
| 346 | if expr.islower(): |
| 347 | expr = expr + "(a, b)" |
| 348 | else: |
| 349 | expr = 'a %s b' % expr |
| 350 | binops[name] = expr |
| 351 | |
| 352 | unops = { |
| 353 | 'pos': '+', |
| 354 | 'neg': '-', |
| 355 | 'abs': 'abs', |
| 356 | 'invert': '~', |
| 357 | 'int': 'int', |
| 358 | 'long': 'long', |
| 359 | 'float': 'float', |
| 360 | 'oct': 'oct', |
| 361 | 'hex': 'hex', |
| 362 | } |
| 363 | |
| 364 | for name, expr in unops.items(): |
| 365 | if expr.islower(): |
| 366 | expr = expr + "(a)" |
| 367 | else: |
| 368 | expr = '%s a' % expr |
| 369 | unops[name] = expr |
| 370 | |
| 371 | def numops(a, b, skip=[]): |
| 372 | dict = {'a': a, 'b': b} |
| 373 | for name, expr in binops.items(): |
| 374 | if name not in skip: |
| 375 | name = "__%s__" % name |
| 376 | if hasattr(a, name): |
| 377 | res = eval(expr, dict) |
| 378 | testbinop(a, b, res, expr, name) |
| 379 | for name, expr in unops.items(): |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 380 | if name not in skip: |
| 381 | name = "__%s__" % name |
| 382 | if hasattr(a, name): |
| 383 | res = eval(expr, dict) |
| 384 | testunop(a, res, expr, name) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 385 | |
| 386 | def ints(): |
| 387 | if verbose: print "Testing int operations..." |
| 388 | numops(100, 3) |
| 389 | |
| 390 | def longs(): |
| 391 | if verbose: print "Testing long operations..." |
| 392 | numops(100L, 3L) |
| 393 | |
| 394 | def floats(): |
| 395 | if verbose: print "Testing float operations..." |
| 396 | numops(100.0, 3.0) |
| 397 | |
| 398 | def complexes(): |
| 399 | if verbose: print "Testing complex operations..." |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 400 | 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] | 401 | class Number(complex): |
| 402 | __slots__ = ['prec'] |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 403 | def __new__(cls, *args, **kwds): |
| 404 | result = complex.__new__(cls, *args) |
| 405 | result.prec = kwds.get('prec', 12) |
| 406 | return result |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 407 | def __repr__(self): |
| 408 | prec = self.prec |
| 409 | if self.imag == 0.0: |
| 410 | return "%.*g" % (prec, self.real) |
| 411 | if self.real == 0.0: |
| 412 | return "%.*gj" % (prec, self.imag) |
| 413 | return "(%.*g+%.*gj)" % (prec, self.real, prec, self.imag) |
| 414 | __str__ = __repr__ |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 415 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 416 | a = Number(3.14, prec=6) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 417 | vereq(`a`, "3.14") |
| 418 | vereq(a.prec, 6) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 419 | |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 420 | a = Number(a, prec=2) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 421 | vereq(`a`, "3.1") |
| 422 | vereq(a.prec, 2) |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 423 | |
| 424 | a = Number(234.5) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 425 | vereq(`a`, "234.5") |
| 426 | vereq(a.prec, 12) |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 427 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 428 | def spamlists(): |
| 429 | if verbose: print "Testing spamlist operations..." |
| 430 | import copy, xxsubtype as spam |
| 431 | def spamlist(l, memo=None): |
| 432 | import xxsubtype as spam |
| 433 | return spam.spamlist(l) |
| 434 | # This is an ugly hack: |
| 435 | copy._deepcopy_dispatch[spam.spamlist] = spamlist |
| 436 | |
| 437 | testbinop(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+b", "__add__") |
| 438 | testbinop(spamlist([1,2,3]), 2, 1, "b in a", "__contains__") |
| 439 | testbinop(spamlist([1,2,3]), 4, 0, "b in a", "__contains__") |
| 440 | testbinop(spamlist([1,2,3]), 1, 2, "a[b]", "__getitem__") |
| 441 | testternop(spamlist([1,2,3]), 0, 2, spamlist([1,2]), |
| 442 | "a[b:c]", "__getslice__") |
| 443 | testsetop(spamlist([1]), spamlist([2]), spamlist([1,2]), |
| 444 | "a+=b", "__iadd__") |
| 445 | testsetop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*=b", "__imul__") |
| 446 | testunop(spamlist([1,2,3]), 3, "len(a)", "__len__") |
| 447 | testbinop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*b", "__mul__") |
| 448 | testbinop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "b*a", "__rmul__") |
| 449 | testset2op(spamlist([1,2]), 1, 3, spamlist([1,3]), "a[b]=c", "__setitem__") |
| 450 | testset3op(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]), |
| 451 | spamlist([1,5,6,4]), "a[b:c]=d", "__setslice__") |
| 452 | # Test subclassing |
| 453 | class C(spam.spamlist): |
| 454 | def foo(self): return 1 |
| 455 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 456 | vereq(a, []) |
| 457 | vereq(a.foo(), 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 458 | a.append(100) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 459 | vereq(a, [100]) |
| 460 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 461 | a.setstate(42) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 462 | vereq(a.getstate(), 42) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 463 | |
| 464 | def spamdicts(): |
| 465 | if verbose: print "Testing spamdict operations..." |
| 466 | import copy, xxsubtype as spam |
| 467 | def spamdict(d, memo=None): |
| 468 | import xxsubtype as spam |
| 469 | sd = spam.spamdict() |
| 470 | for k, v in d.items(): sd[k] = v |
| 471 | return sd |
| 472 | # This is an ugly hack: |
| 473 | copy._deepcopy_dispatch[spam.spamdict] = spamdict |
| 474 | |
| 475 | testbinop(spamdict({1:2}), spamdict({2:1}), -1, "cmp(a,b)", "__cmp__") |
| 476 | testbinop(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__") |
| 477 | testbinop(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__") |
| 478 | testbinop(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__") |
| 479 | d = spamdict({1:2,3:4}) |
| 480 | l1 = [] |
| 481 | for i in d.keys(): l1.append(i) |
| 482 | l = [] |
| 483 | for i in iter(d): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 484 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 485 | l = [] |
| 486 | for i in d.__iter__(): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 487 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 488 | l = [] |
| 489 | for i in type(spamdict({})).__iter__(d): l.append(i) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 490 | vereq(l, l1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 491 | straightd = {1:2, 3:4} |
| 492 | spamd = spamdict(straightd) |
| 493 | testunop(spamd, 2, "len(a)", "__len__") |
| 494 | testunop(spamd, repr(straightd), "repr(a)", "__repr__") |
| 495 | testset2op(spamdict({1:2,3:4}), 2, 3, spamdict({1:2,2:3,3:4}), |
| 496 | "a[b]=c", "__setitem__") |
| 497 | # Test subclassing |
| 498 | class C(spam.spamdict): |
| 499 | def foo(self): return 1 |
| 500 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 501 | vereq(a.items(), []) |
| 502 | vereq(a.foo(), 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 503 | a['foo'] = 'bar' |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 504 | vereq(a.items(), [('foo', 'bar')]) |
| 505 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 506 | a.setstate(100) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 507 | vereq(a.getstate(), 100) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 508 | |
| 509 | def pydicts(): |
| 510 | if verbose: print "Testing Python subclass of dict..." |
| 511 | verify(issubclass(dictionary, dictionary)) |
| 512 | verify(isinstance({}, dictionary)) |
| 513 | d = dictionary() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 514 | vereq(d, {}) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 515 | verify(d.__class__ is dictionary) |
| 516 | verify(isinstance(d, dictionary)) |
| 517 | class C(dictionary): |
| 518 | state = -1 |
| 519 | def __init__(self, *a, **kw): |
| 520 | if a: |
| 521 | assert len(a) == 1 |
| 522 | self.state = a[0] |
| 523 | if kw: |
| 524 | for k, v in kw.items(): self[v] = k |
| 525 | def __getitem__(self, key): |
| 526 | return self.get(key, 0) |
| 527 | def __setitem__(self, key, value): |
| 528 | assert isinstance(key, type(0)) |
| 529 | dictionary.__setitem__(self, key, value) |
| 530 | def setstate(self, state): |
| 531 | self.state = state |
| 532 | def getstate(self): |
| 533 | return self.state |
| 534 | verify(issubclass(C, dictionary)) |
| 535 | a1 = C(12) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 536 | vereq(a1.state, 12) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 537 | a2 = C(foo=1, bar=2) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 538 | vereq(a2[1] == 'foo' and a2[2], 'bar') |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 539 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 540 | vereq(a.state, -1) |
| 541 | vereq(a.getstate(), -1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 542 | a.setstate(0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 543 | vereq(a.state, 0) |
| 544 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 545 | a.setstate(10) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 546 | vereq(a.state, 10) |
| 547 | vereq(a.getstate(), 10) |
| 548 | vereq(a[42], 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 549 | a[42] = 24 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 550 | vereq(a[42], 24) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 551 | if verbose: print "pydict stress test ..." |
| 552 | N = 50 |
| 553 | for i in range(N): |
| 554 | a[i] = C() |
| 555 | for j in range(N): |
| 556 | a[i][j] = i*j |
| 557 | for i in range(N): |
| 558 | for j in range(N): |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 559 | vereq(a[i][j], i*j) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 560 | |
| 561 | def pylists(): |
| 562 | if verbose: print "Testing Python subclass of list..." |
| 563 | class C(list): |
| 564 | def __getitem__(self, i): |
| 565 | return list.__getitem__(self, i) + 100 |
| 566 | def __getslice__(self, i, j): |
| 567 | return (i, j) |
| 568 | a = C() |
| 569 | a.extend([0,1,2]) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 570 | vereq(a[0], 100) |
| 571 | vereq(a[1], 101) |
| 572 | vereq(a[2], 102) |
| 573 | vereq(a[100:200], (100,200)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 574 | |
| 575 | def metaclass(): |
| 576 | if verbose: print "Testing __metaclass__..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 577 | class C: |
| 578 | __metaclass__ = type |
| 579 | def __init__(self): |
| 580 | self.__state = 0 |
| 581 | def getstate(self): |
| 582 | return self.__state |
| 583 | def setstate(self, state): |
| 584 | self.__state = state |
| 585 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 586 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 587 | a.setstate(10) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 588 | vereq(a.getstate(), 10) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 589 | class D: |
| 590 | class __metaclass__(type): |
| 591 | def myself(cls): return cls |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 592 | vereq(D.myself(), D) |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 593 | d = D() |
| 594 | verify(d.__class__ is D) |
| 595 | class M1(type): |
| 596 | def __new__(cls, name, bases, dict): |
| 597 | dict['__spam__'] = 1 |
| 598 | return type.__new__(cls, name, bases, dict) |
| 599 | class C: |
| 600 | __metaclass__ = M1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 601 | vereq(C.__spam__, 1) |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 602 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 603 | vereq(c.__spam__, 1) |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 604 | |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 605 | class _instance(object): |
| 606 | pass |
| 607 | class M2(object): |
| 608 | def __new__(cls, name, bases, dict): |
| 609 | self = object.__new__(cls) |
| 610 | self.name = name |
| 611 | self.bases = bases |
| 612 | self.dict = dict |
| 613 | return self |
| 614 | __new__ = staticmethod(__new__) |
| 615 | def __call__(self): |
| 616 | it = _instance() |
Guido van Rossum | 7e1ff69 | 2001-08-17 11:55:58 +0000 | [diff] [blame] | 617 | # Early binding of methods |
| 618 | for key in self.dict: |
| 619 | if key.startswith("__"): |
| 620 | continue |
| 621 | setattr(it, key, self.dict[key].__get__(it, self)) |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 622 | return it |
| 623 | class C: |
| 624 | __metaclass__ = M2 |
| 625 | def spam(self): |
| 626 | return 42 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 627 | vereq(C.name, 'C') |
| 628 | vereq(C.bases, ()) |
Guido van Rossum | 309b566 | 2001-08-17 11:43:17 +0000 | [diff] [blame] | 629 | verify('spam' in C.dict) |
| 630 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 631 | vereq(c.spam(), 42) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 632 | |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 633 | # More metaclass examples |
| 634 | |
| 635 | class autosuper(type): |
| 636 | # Automatically add __super to the class |
| 637 | # This trick only works for dynamic classes |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 638 | def __new__(metaclass, name, bases, dict): |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 639 | cls = super(autosuper, metaclass).__new__(metaclass, |
| 640 | name, bases, dict) |
Guido van Rossum | bfa47b0 | 2001-08-31 04:35:14 +0000 | [diff] [blame] | 641 | # Name mangling for __super removes leading underscores |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 642 | while name[:1] == "_": |
| 643 | name = name[1:] |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 644 | if name: |
| 645 | name = "_%s__super" % name |
| 646 | else: |
| 647 | name = "__super" |
| 648 | setattr(cls, name, super(cls)) |
| 649 | return cls |
| 650 | class A: |
| 651 | __metaclass__ = autosuper |
| 652 | def meth(self): |
| 653 | return "A" |
| 654 | class B(A): |
| 655 | def meth(self): |
| 656 | return "B" + self.__super.meth() |
| 657 | class C(A): |
| 658 | def meth(self): |
| 659 | return "C" + self.__super.meth() |
| 660 | class D(C, B): |
| 661 | def meth(self): |
| 662 | return "D" + self.__super.meth() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 663 | vereq(D().meth(), "DCBA") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 664 | class E(B, C): |
| 665 | def meth(self): |
| 666 | return "E" + self.__super.meth() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 667 | vereq(E().meth(), "EBCA") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 668 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 669 | class autoproperty(type): |
| 670 | # Automatically create property attributes when methods |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 671 | # named _get_x and/or _set_x are found |
| 672 | def __new__(metaclass, name, bases, dict): |
| 673 | hits = {} |
| 674 | for key, val in dict.iteritems(): |
| 675 | if key.startswith("_get_"): |
| 676 | key = key[5:] |
| 677 | get, set = hits.get(key, (None, None)) |
| 678 | get = val |
| 679 | hits[key] = get, set |
| 680 | elif key.startswith("_set_"): |
| 681 | key = key[5:] |
| 682 | get, set = hits.get(key, (None, None)) |
| 683 | set = val |
| 684 | hits[key] = get, set |
| 685 | for key, (get, set) in hits.iteritems(): |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 686 | dict[key] = property(get, set) |
| 687 | return super(autoproperty, metaclass).__new__(metaclass, |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 688 | name, bases, dict) |
| 689 | class A: |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 690 | __metaclass__ = autoproperty |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 691 | def _get_x(self): |
| 692 | return -self.__x |
| 693 | def _set_x(self, x): |
| 694 | self.__x = -x |
| 695 | a = A() |
| 696 | verify(not hasattr(a, "x")) |
| 697 | a.x = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 698 | vereq(a.x, 12) |
| 699 | vereq(a._A__x, -12) |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 700 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 701 | class multimetaclass(autoproperty, autosuper): |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 702 | # Merge of multiple cooperating metaclasses |
| 703 | pass |
| 704 | class A: |
| 705 | __metaclass__ = multimetaclass |
| 706 | def _get_x(self): |
| 707 | return "A" |
| 708 | class B(A): |
| 709 | def _get_x(self): |
| 710 | return "B" + self.__super._get_x() |
| 711 | class C(A): |
| 712 | def _get_x(self): |
| 713 | return "C" + self.__super._get_x() |
| 714 | class D(C, B): |
| 715 | def _get_x(self): |
| 716 | return "D" + self.__super._get_x() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 717 | vereq(D().x, "DCBA") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 718 | |
Guido van Rossum | f76de62 | 2001-10-18 15:49:21 +0000 | [diff] [blame] | 719 | # Make sure type(x) doesn't call x.__class__.__init__ |
| 720 | class T(type): |
| 721 | counter = 0 |
| 722 | def __init__(self, *args): |
| 723 | T.counter += 1 |
| 724 | class C: |
| 725 | __metaclass__ = T |
| 726 | vereq(T.counter, 1) |
| 727 | a = C() |
| 728 | vereq(type(a), C) |
| 729 | vereq(T.counter, 1) |
| 730 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 731 | def pymods(): |
| 732 | if verbose: print "Testing Python subclass of module..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 733 | log = [] |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 734 | import sys |
| 735 | MT = type(sys) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 736 | class MM(MT): |
| 737 | def __init__(self): |
| 738 | MT.__init__(self) |
Guido van Rossum | 867a8d2 | 2001-09-21 19:29:08 +0000 | [diff] [blame] | 739 | def __getattribute__(self, name): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 740 | log.append(("getattr", name)) |
Guido van Rossum | 867a8d2 | 2001-09-21 19:29:08 +0000 | [diff] [blame] | 741 | return MT.__getattribute__(self, name) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 742 | def __setattr__(self, name, value): |
| 743 | log.append(("setattr", name, value)) |
| 744 | MT.__setattr__(self, name, value) |
| 745 | def __delattr__(self, name): |
| 746 | log.append(("delattr", name)) |
| 747 | MT.__delattr__(self, name) |
| 748 | a = MM() |
| 749 | a.foo = 12 |
| 750 | x = a.foo |
| 751 | del a.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 752 | vereq(log, [("setattr", "foo", 12), |
| 753 | ("getattr", "foo"), |
| 754 | ("delattr", "foo")]) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 755 | |
| 756 | def multi(): |
| 757 | if verbose: print "Testing multiple inheritance..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 758 | class C(object): |
| 759 | def __init__(self): |
| 760 | self.__state = 0 |
| 761 | def getstate(self): |
| 762 | return self.__state |
| 763 | def setstate(self, state): |
| 764 | self.__state = state |
| 765 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 766 | vereq(a.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 767 | a.setstate(10) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 768 | vereq(a.getstate(), 10) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 769 | class D(dictionary, C): |
| 770 | def __init__(self): |
| 771 | type({}).__init__(self) |
| 772 | C.__init__(self) |
| 773 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 774 | vereq(d.keys(), []) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 775 | d["hello"] = "world" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 776 | vereq(d.items(), [("hello", "world")]) |
| 777 | vereq(d["hello"], "world") |
| 778 | vereq(d.getstate(), 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 779 | d.setstate(10) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 780 | vereq(d.getstate(), 10) |
| 781 | vereq(D.__mro__, (D, dictionary, C, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 782 | |
Guido van Rossum | e45763a | 2001-08-10 21:28:46 +0000 | [diff] [blame] | 783 | # SF bug #442833 |
| 784 | class Node(object): |
| 785 | def __int__(self): |
| 786 | return int(self.foo()) |
| 787 | def foo(self): |
| 788 | return "23" |
| 789 | class Frag(Node, list): |
| 790 | def foo(self): |
| 791 | return "42" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 792 | vereq(Node().__int__(), 23) |
| 793 | vereq(int(Node()), 23) |
| 794 | vereq(Frag().__int__(), 42) |
| 795 | vereq(int(Frag()), 42) |
Guido van Rossum | e45763a | 2001-08-10 21:28:46 +0000 | [diff] [blame] | 796 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 797 | def diamond(): |
| 798 | if verbose: print "Testing multiple inheritance special cases..." |
| 799 | class A(object): |
| 800 | def spam(self): return "A" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 801 | vereq(A().spam(), "A") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 802 | class B(A): |
| 803 | def boo(self): return "B" |
| 804 | def spam(self): return "B" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 805 | vereq(B().spam(), "B") |
| 806 | vereq(B().boo(), "B") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 807 | class C(A): |
| 808 | def boo(self): return "C" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 809 | vereq(C().spam(), "A") |
| 810 | vereq(C().boo(), "C") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 811 | class D(B, C): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 812 | vereq(D().spam(), "B") |
| 813 | vereq(D().boo(), "B") |
| 814 | vereq(D.__mro__, (D, B, C, A, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 815 | class E(C, B): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 816 | vereq(E().spam(), "B") |
| 817 | vereq(E().boo(), "C") |
| 818 | vereq(E.__mro__, (E, C, B, A, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 819 | class F(D, E): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 820 | vereq(F().spam(), "B") |
| 821 | vereq(F().boo(), "B") |
| 822 | vereq(F.__mro__, (F, D, E, B, C, A, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 823 | class G(E, D): pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 824 | vereq(G().spam(), "B") |
| 825 | vereq(G().boo(), "C") |
| 826 | vereq(G.__mro__, (G, E, D, C, B, A, object)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 827 | |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 828 | def objects(): |
| 829 | if verbose: print "Testing object class..." |
| 830 | a = object() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 831 | vereq(a.__class__, object) |
| 832 | vereq(type(a), object) |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 833 | b = object() |
| 834 | verify(a is not b) |
| 835 | verify(not hasattr(a, "foo")) |
| 836 | try: |
| 837 | a.foo = 12 |
Guido van Rossum | 6d94627 | 2001-08-10 19:42:38 +0000 | [diff] [blame] | 838 | except (AttributeError, TypeError): |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 839 | pass |
| 840 | else: |
| 841 | verify(0, "object() should not allow setting a foo attribute") |
| 842 | verify(not hasattr(object(), "__dict__")) |
| 843 | |
| 844 | class Cdict(object): |
| 845 | pass |
| 846 | x = Cdict() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 847 | vereq(x.__dict__, {}) |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 848 | x.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 849 | vereq(x.foo, 1) |
| 850 | vereq(x.__dict__, {'foo': 1}) |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 851 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 852 | def slots(): |
| 853 | if verbose: print "Testing __slots__..." |
| 854 | class C0(object): |
| 855 | __slots__ = [] |
| 856 | x = C0() |
| 857 | verify(not hasattr(x, "__dict__")) |
| 858 | verify(not hasattr(x, "foo")) |
| 859 | |
| 860 | class C1(object): |
| 861 | __slots__ = ['a'] |
| 862 | x = C1() |
| 863 | verify(not hasattr(x, "__dict__")) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 864 | vereq(x.a, None) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 865 | x.a = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 866 | vereq(x.a, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 867 | del x.a |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 868 | vereq(x.a, None) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 869 | |
| 870 | class C3(object): |
| 871 | __slots__ = ['a', 'b', 'c'] |
| 872 | x = C3() |
| 873 | verify(not hasattr(x, "__dict__")) |
| 874 | verify(x.a is None) |
| 875 | verify(x.b is None) |
| 876 | verify(x.c is None) |
| 877 | x.a = 1 |
| 878 | x.b = 2 |
| 879 | x.c = 3 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 880 | vereq(x.a, 1) |
| 881 | vereq(x.b, 2) |
| 882 | vereq(x.c, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 883 | |
| 884 | def dynamics(): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 885 | if verbose: print "Testing class attribute propagation..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 886 | class D(object): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 887 | pass |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 888 | class E(D): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 889 | pass |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 890 | class F(D): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 891 | pass |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 892 | D.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 893 | vereq(D.foo, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 894 | # Test that dynamic attributes are inherited |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 895 | vereq(E.foo, 1) |
| 896 | vereq(F.foo, 1) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 897 | # Test dynamic instances |
| 898 | class C(object): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 899 | pass |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 900 | a = C() |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 901 | verify(not hasattr(a, "foobar")) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 902 | C.foobar = 2 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 903 | vereq(a.foobar, 2) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 904 | C.method = lambda self: 42 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 905 | vereq(a.method(), 42) |
Guido van Rossum | 9d4fe42 | 2001-08-12 03:38:18 +0000 | [diff] [blame] | 906 | C.__repr__ = lambda self: "C()" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 907 | vereq(repr(a), "C()") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 908 | C.__int__ = lambda self: 100 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 909 | vereq(int(a), 100) |
| 910 | vereq(a.foobar, 2) |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 911 | verify(not hasattr(a, "spam")) |
| 912 | def mygetattr(self, name): |
| 913 | if name == "spam": |
| 914 | return "spam" |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 915 | raise AttributeError |
| 916 | C.__getattr__ = mygetattr |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 917 | vereq(a.spam, "spam") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 918 | a.new = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 919 | vereq(a.new, 12) |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 920 | def mysetattr(self, name, value): |
| 921 | if name == "spam": |
| 922 | raise AttributeError |
| 923 | return object.__setattr__(self, name, value) |
| 924 | C.__setattr__ = mysetattr |
| 925 | try: |
| 926 | a.spam = "not spam" |
| 927 | except AttributeError: |
| 928 | pass |
| 929 | else: |
| 930 | verify(0, "expected AttributeError") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 931 | vereq(a.spam, "spam") |
Guido van Rossum | 80e3675 | 2001-08-14 20:00:33 +0000 | [diff] [blame] | 932 | class D(C): |
| 933 | pass |
| 934 | d = D() |
| 935 | d.foo = 1 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 936 | vereq(d.foo, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 937 | |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 938 | # Test handling of int*seq and seq*int |
| 939 | class I(int): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 940 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 941 | vereq("a"*I(2), "aa") |
| 942 | vereq(I(2)*"a", "aa") |
| 943 | vereq(2*I(3), 6) |
| 944 | vereq(I(3)*2, 6) |
| 945 | vereq(I(3)*I(2), 6) |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 946 | |
| 947 | # Test handling of long*seq and seq*long |
| 948 | class L(long): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 949 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 950 | vereq("a"*L(2L), "aa") |
| 951 | vereq(L(2L)*"a", "aa") |
| 952 | vereq(2*L(3), 6) |
| 953 | vereq(L(3)*2, 6) |
| 954 | vereq(L(3)*L(2), 6) |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 955 | |
Guido van Rossum | 3d45d8f | 2001-09-24 18:47:40 +0000 | [diff] [blame] | 956 | # Test comparison of classes with dynamic metaclasses |
| 957 | class dynamicmetaclass(type): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 958 | pass |
Guido van Rossum | 3d45d8f | 2001-09-24 18:47:40 +0000 | [diff] [blame] | 959 | class someclass: |
| 960 | __metaclass__ = dynamicmetaclass |
| 961 | verify(someclass != object) |
| 962 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 963 | def errors(): |
| 964 | if verbose: print "Testing errors..." |
| 965 | |
| 966 | try: |
| 967 | class C(list, dictionary): |
| 968 | pass |
| 969 | except TypeError: |
| 970 | pass |
| 971 | else: |
| 972 | verify(0, "inheritance from both list and dict should be illegal") |
| 973 | |
| 974 | try: |
| 975 | class C(object, None): |
| 976 | pass |
| 977 | except TypeError: |
| 978 | pass |
| 979 | else: |
| 980 | verify(0, "inheritance from non-type should be illegal") |
| 981 | class Classic: |
| 982 | pass |
| 983 | |
| 984 | try: |
| 985 | class C(object, Classic): |
| 986 | pass |
| 987 | except TypeError: |
| 988 | pass |
| 989 | else: |
| 990 | verify(0, "inheritance from object and Classic should be illegal") |
| 991 | |
| 992 | try: |
Guido van Rossum | 8aea0cc | 2001-08-29 15:48:43 +0000 | [diff] [blame] | 993 | class C(type(len)): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 994 | pass |
| 995 | except TypeError: |
| 996 | pass |
| 997 | else: |
Guido van Rossum | 8aea0cc | 2001-08-29 15:48:43 +0000 | [diff] [blame] | 998 | verify(0, "inheritance from CFunction should be illegal") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 999 | |
| 1000 | try: |
| 1001 | class C(object): |
| 1002 | __slots__ = 1 |
| 1003 | except TypeError: |
| 1004 | pass |
| 1005 | else: |
| 1006 | verify(0, "__slots__ = 1 should be illegal") |
| 1007 | |
| 1008 | try: |
| 1009 | class C(object): |
| 1010 | __slots__ = [1] |
| 1011 | except TypeError: |
| 1012 | pass |
| 1013 | else: |
| 1014 | verify(0, "__slots__ = [1] should be illegal") |
| 1015 | |
| 1016 | def classmethods(): |
| 1017 | if verbose: print "Testing class methods..." |
| 1018 | class C(object): |
| 1019 | def foo(*a): return a |
| 1020 | goo = classmethod(foo) |
| 1021 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1022 | vereq(C.goo(1), (C, 1)) |
| 1023 | vereq(c.goo(1), (C, 1)) |
| 1024 | vereq(c.foo(1), (c, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1025 | class D(C): |
| 1026 | pass |
| 1027 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1028 | vereq(D.goo(1), (D, 1)) |
| 1029 | vereq(d.goo(1), (D, 1)) |
| 1030 | vereq(d.foo(1), (d, 1)) |
| 1031 | vereq(D.foo(d, 1), (d, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1032 | |
| 1033 | def staticmethods(): |
| 1034 | if verbose: print "Testing static methods..." |
| 1035 | class C(object): |
| 1036 | def foo(*a): return a |
| 1037 | goo = staticmethod(foo) |
| 1038 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1039 | vereq(C.goo(1), (1,)) |
| 1040 | vereq(c.goo(1), (1,)) |
| 1041 | vereq(c.foo(1), (c, 1,)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1042 | class D(C): |
| 1043 | pass |
| 1044 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1045 | vereq(D.goo(1), (1,)) |
| 1046 | vereq(d.goo(1), (1,)) |
| 1047 | vereq(d.foo(1), (d, 1)) |
| 1048 | vereq(D.foo(d, 1), (d, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1049 | |
| 1050 | def classic(): |
| 1051 | if verbose: print "Testing classic classes..." |
| 1052 | class C: |
| 1053 | def foo(*a): return a |
| 1054 | goo = classmethod(foo) |
| 1055 | c = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1056 | vereq(C.goo(1), (C, 1)) |
| 1057 | vereq(c.goo(1), (C, 1)) |
| 1058 | vereq(c.foo(1), (c, 1)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1059 | class D(C): |
| 1060 | pass |
| 1061 | d = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1062 | vereq(D.goo(1), (D, 1)) |
| 1063 | vereq(d.goo(1), (D, 1)) |
| 1064 | vereq(d.foo(1), (d, 1)) |
| 1065 | vereq(D.foo(d, 1), (d, 1)) |
Guido van Rossum | 9301876 | 2001-08-17 13:40:47 +0000 | [diff] [blame] | 1066 | class E: # *not* subclassing from C |
| 1067 | foo = C.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1068 | vereq(E().foo, C.foo) # i.e., unbound |
Guido van Rossum | 84a79a8 | 2001-08-17 13:58:31 +0000 | [diff] [blame] | 1069 | verify(repr(C.foo.__get__(C())).startswith("<bound method ")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1070 | |
| 1071 | def compattr(): |
| 1072 | if verbose: print "Testing computed attributes..." |
| 1073 | class C(object): |
| 1074 | class computed_attribute(object): |
| 1075 | def __init__(self, get, set=None): |
| 1076 | self.__get = get |
| 1077 | self.__set = set |
| 1078 | def __get__(self, obj, type=None): |
| 1079 | return self.__get(obj) |
| 1080 | def __set__(self, obj, value): |
| 1081 | return self.__set(obj, value) |
| 1082 | def __init__(self): |
| 1083 | self.__x = 0 |
| 1084 | def __get_x(self): |
| 1085 | x = self.__x |
| 1086 | self.__x = x+1 |
| 1087 | return x |
| 1088 | def __set_x(self, x): |
| 1089 | self.__x = x |
| 1090 | x = computed_attribute(__get_x, __set_x) |
| 1091 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1092 | vereq(a.x, 0) |
| 1093 | vereq(a.x, 1) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1094 | a.x = 10 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1095 | vereq(a.x, 10) |
| 1096 | vereq(a.x, 11) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1097 | |
| 1098 | def newslot(): |
| 1099 | if verbose: print "Testing __new__ slot override..." |
| 1100 | class C(list): |
| 1101 | def __new__(cls): |
| 1102 | self = list.__new__(cls) |
| 1103 | self.foo = 1 |
| 1104 | return self |
| 1105 | def __init__(self): |
| 1106 | self.foo = self.foo + 2 |
| 1107 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1108 | vereq(a.foo, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1109 | verify(a.__class__ is C) |
| 1110 | class D(C): |
| 1111 | pass |
| 1112 | b = D() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1113 | vereq(b.foo, 3) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1114 | verify(b.__class__ is D) |
| 1115 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1116 | def altmro(): |
| 1117 | if verbose: print "Testing mro() and overriding it..." |
| 1118 | class A(object): |
| 1119 | def f(self): return "A" |
| 1120 | class B(A): |
| 1121 | pass |
| 1122 | class C(A): |
| 1123 | def f(self): return "C" |
| 1124 | class D(B, C): |
| 1125 | pass |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1126 | vereq(D.mro(), [D, B, C, A, object]) |
| 1127 | vereq(D.__mro__, (D, B, C, A, object)) |
| 1128 | vereq(D().f(), "C") |
Guido van Rossum | d307740 | 2001-08-12 05:24:18 +0000 | [diff] [blame] | 1129 | class PerverseMetaType(type): |
| 1130 | def mro(cls): |
| 1131 | L = type.mro(cls) |
| 1132 | L.reverse() |
| 1133 | return L |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1134 | class X(A,B,C,D): |
| 1135 | __metaclass__ = PerverseMetaType |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1136 | vereq(X.__mro__, (object, A, C, B, D, X)) |
| 1137 | vereq(X().f(), "A") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1138 | |
| 1139 | def overloading(): |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1140 | if verbose: print "Testing operator overloading..." |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1141 | |
| 1142 | class B(object): |
| 1143 | "Intermediate class because object doesn't have a __setattr__" |
| 1144 | |
| 1145 | class C(B): |
| 1146 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 1147 | def __getattr__(self, name): |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1148 | if name == "foo": |
| 1149 | return ("getattr", name) |
| 1150 | else: |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 1151 | raise AttributeError |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1152 | def __setattr__(self, name, value): |
| 1153 | if name == "foo": |
| 1154 | self.setattr = (name, value) |
| 1155 | else: |
| 1156 | return B.__setattr__(self, name, value) |
| 1157 | def __delattr__(self, name): |
| 1158 | if name == "foo": |
| 1159 | self.delattr = name |
| 1160 | else: |
| 1161 | return B.__delattr__(self, name) |
| 1162 | |
| 1163 | def __getitem__(self, key): |
| 1164 | return ("getitem", key) |
| 1165 | def __setitem__(self, key, value): |
| 1166 | self.setitem = (key, value) |
| 1167 | def __delitem__(self, key): |
| 1168 | self.delitem = key |
| 1169 | |
| 1170 | def __getslice__(self, i, j): |
| 1171 | return ("getslice", i, j) |
| 1172 | def __setslice__(self, i, j, value): |
| 1173 | self.setslice = (i, j, value) |
| 1174 | def __delslice__(self, i, j): |
| 1175 | self.delslice = (i, j) |
| 1176 | |
| 1177 | a = C() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1178 | vereq(a.foo, ("getattr", "foo")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1179 | a.foo = 12 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1180 | vereq(a.setattr, ("foo", 12)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1181 | del a.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1182 | vereq(a.delattr, "foo") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1183 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1184 | vereq(a[12], ("getitem", 12)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1185 | a[12] = 21 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1186 | vereq(a.setitem, (12, 21)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1187 | del a[12] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1188 | vereq(a.delitem, 12) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1189 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1190 | vereq(a[0:10], ("getslice", 0, 10)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1191 | a[0:10] = "foo" |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1192 | vereq(a.setslice, (0, 10, "foo")) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1193 | del a[0:10] |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1194 | vereq(a.delslice, (0, 10)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1195 | |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1196 | def methods(): |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1197 | if verbose: print "Testing methods..." |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1198 | class C(object): |
| 1199 | def __init__(self, x): |
| 1200 | self.x = x |
| 1201 | def foo(self): |
| 1202 | return self.x |
| 1203 | c1 = C(1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1204 | vereq(c1.foo(), 1) |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1205 | class D(C): |
| 1206 | boo = C.foo |
| 1207 | goo = c1.foo |
| 1208 | d2 = D(2) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1209 | vereq(d2.foo(), 2) |
| 1210 | vereq(d2.boo(), 2) |
| 1211 | vereq(d2.goo(), 1) |
Guido van Rossum | 9301876 | 2001-08-17 13:40:47 +0000 | [diff] [blame] | 1212 | class E(object): |
| 1213 | foo = C.foo |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1214 | vereq(E().foo, C.foo) # i.e., unbound |
Guido van Rossum | 84a79a8 | 2001-08-17 13:58:31 +0000 | [diff] [blame] | 1215 | verify(repr(C.foo.__get__(C(1))).startswith("<bound method ")) |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 1216 | |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1217 | def specials(): |
| 1218 | # Test operators like __hash__ for which a built-in default exists |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1219 | if verbose: print "Testing special operators..." |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1220 | # Test the default behavior for static classes |
| 1221 | class C(object): |
| 1222 | def __getitem__(self, i): |
| 1223 | if 0 <= i < 10: return i |
| 1224 | raise IndexError |
| 1225 | c1 = C() |
| 1226 | c2 = C() |
| 1227 | verify(not not c1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1228 | vereq(hash(c1), id(c1)) |
| 1229 | vereq(cmp(c1, c2), cmp(id(c1), id(c2))) |
| 1230 | vereq(c1, c1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1231 | verify(c1 != c2) |
| 1232 | verify(not c1 != c1) |
| 1233 | verify(not c1 == c2) |
Tim Peters | 4d2dded | 2001-08-16 19:50:51 +0000 | [diff] [blame] | 1234 | # Note that the module name appears in str/repr, and that varies |
| 1235 | # 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] | 1236 | verify(str(c1).find('C object at ') >= 0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1237 | vereq(str(c1), repr(c1)) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1238 | verify(-1 not in c1) |
| 1239 | for i in range(10): |
| 1240 | verify(i in c1) |
| 1241 | verify(10 not in c1) |
| 1242 | # Test the default behavior for dynamic classes |
| 1243 | class D(object): |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1244 | def __getitem__(self, i): |
| 1245 | if 0 <= i < 10: return i |
| 1246 | raise IndexError |
| 1247 | d1 = D() |
| 1248 | d2 = D() |
| 1249 | verify(not not d1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1250 | vereq(hash(d1), id(d1)) |
| 1251 | vereq(cmp(d1, d2), cmp(id(d1), id(d2))) |
| 1252 | vereq(d1, d1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1253 | verify(d1 != d2) |
| 1254 | verify(not d1 != d1) |
| 1255 | verify(not d1 == d2) |
Tim Peters | 4d2dded | 2001-08-16 19:50:51 +0000 | [diff] [blame] | 1256 | # Note that the module name appears in str/repr, and that varies |
| 1257 | # 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] | 1258 | verify(str(d1).find('D object at ') >= 0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1259 | vereq(str(d1), repr(d1)) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1260 | verify(-1 not in d1) |
| 1261 | for i in range(10): |
| 1262 | verify(i in d1) |
| 1263 | verify(10 not in d1) |
| 1264 | # Test overridden behavior for static classes |
| 1265 | class Proxy(object): |
| 1266 | def __init__(self, x): |
| 1267 | self.x = x |
| 1268 | def __nonzero__(self): |
| 1269 | return not not self.x |
| 1270 | def __hash__(self): |
| 1271 | return hash(self.x) |
| 1272 | def __eq__(self, other): |
| 1273 | return self.x == other |
| 1274 | def __ne__(self, other): |
| 1275 | return self.x != other |
| 1276 | def __cmp__(self, other): |
| 1277 | return cmp(self.x, other.x) |
| 1278 | def __str__(self): |
| 1279 | return "Proxy:%s" % self.x |
| 1280 | def __repr__(self): |
| 1281 | return "Proxy(%r)" % self.x |
| 1282 | def __contains__(self, value): |
| 1283 | return value in self.x |
| 1284 | p0 = Proxy(0) |
| 1285 | p1 = Proxy(1) |
| 1286 | p_1 = Proxy(-1) |
| 1287 | verify(not p0) |
| 1288 | verify(not not p1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1289 | vereq(hash(p0), hash(0)) |
| 1290 | vereq(p0, p0) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1291 | verify(p0 != p1) |
| 1292 | verify(not p0 != p0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1293 | vereq(not p0, p1) |
| 1294 | vereq(cmp(p0, p1), -1) |
| 1295 | vereq(cmp(p0, p0), 0) |
| 1296 | vereq(cmp(p0, p_1), 1) |
| 1297 | vereq(str(p0), "Proxy:0") |
| 1298 | vereq(repr(p0), "Proxy(0)") |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1299 | p10 = Proxy(range(10)) |
| 1300 | verify(-1 not in p10) |
| 1301 | for i in range(10): |
| 1302 | verify(i in p10) |
| 1303 | verify(10 not in p10) |
| 1304 | # Test overridden behavior for dynamic classes |
| 1305 | class DProxy(object): |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1306 | def __init__(self, x): |
| 1307 | self.x = x |
| 1308 | def __nonzero__(self): |
| 1309 | return not not self.x |
| 1310 | def __hash__(self): |
| 1311 | return hash(self.x) |
| 1312 | def __eq__(self, other): |
| 1313 | return self.x == other |
| 1314 | def __ne__(self, other): |
| 1315 | return self.x != other |
| 1316 | def __cmp__(self, other): |
| 1317 | return cmp(self.x, other.x) |
| 1318 | def __str__(self): |
| 1319 | return "DProxy:%s" % self.x |
| 1320 | def __repr__(self): |
| 1321 | return "DProxy(%r)" % self.x |
| 1322 | def __contains__(self, value): |
| 1323 | return value in self.x |
| 1324 | p0 = DProxy(0) |
| 1325 | p1 = DProxy(1) |
| 1326 | p_1 = DProxy(-1) |
| 1327 | verify(not p0) |
| 1328 | verify(not not p1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1329 | vereq(hash(p0), hash(0)) |
| 1330 | vereq(p0, p0) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1331 | verify(p0 != p1) |
| 1332 | verify(not p0 != p0) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1333 | vereq(not p0, p1) |
| 1334 | vereq(cmp(p0, p1), -1) |
| 1335 | vereq(cmp(p0, p0), 0) |
| 1336 | vereq(cmp(p0, p_1), 1) |
| 1337 | vereq(str(p0), "DProxy:0") |
| 1338 | vereq(repr(p0), "DProxy(0)") |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1339 | p10 = DProxy(range(10)) |
| 1340 | verify(-1 not in p10) |
| 1341 | for i in range(10): |
| 1342 | verify(i in p10) |
| 1343 | verify(10 not in p10) |
Guido van Rossum | 843daa8 | 2001-09-18 20:04:26 +0000 | [diff] [blame] | 1344 | # Safety test for __cmp__ |
| 1345 | def unsafecmp(a, b): |
| 1346 | try: |
| 1347 | a.__class__.__cmp__(a, b) |
| 1348 | except TypeError: |
| 1349 | pass |
| 1350 | else: |
| 1351 | raise TestFailed, "shouldn't allow %s.__cmp__(%r, %r)" % ( |
| 1352 | a.__class__, a, b) |
| 1353 | unsafecmp(u"123", "123") |
| 1354 | unsafecmp("123", u"123") |
| 1355 | unsafecmp(1, 1.0) |
| 1356 | unsafecmp(1.0, 1) |
| 1357 | unsafecmp(1, 1L) |
| 1358 | unsafecmp(1L, 1) |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 1359 | |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1360 | def weakrefs(): |
| 1361 | if verbose: print "Testing weak references..." |
| 1362 | import weakref |
| 1363 | class C(object): |
| 1364 | pass |
| 1365 | c = C() |
| 1366 | r = weakref.ref(c) |
| 1367 | verify(r() is c) |
| 1368 | del c |
| 1369 | verify(r() is None) |
| 1370 | del r |
| 1371 | class NoWeak(object): |
| 1372 | __slots__ = ['foo'] |
| 1373 | no = NoWeak() |
| 1374 | try: |
| 1375 | weakref.ref(no) |
| 1376 | except TypeError, msg: |
Fred Drake | 4bf018b | 2001-10-22 21:45:25 +0000 | [diff] [blame^] | 1377 | verify(str(msg).find("weak reference") >= 0) |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 1378 | else: |
| 1379 | verify(0, "weakref.ref(no) should be illegal") |
| 1380 | class Weak(object): |
| 1381 | __slots__ = ['foo', '__weakref__'] |
| 1382 | yes = Weak() |
| 1383 | r = weakref.ref(yes) |
| 1384 | verify(r() is yes) |
| 1385 | del yes |
| 1386 | verify(r() is None) |
| 1387 | del r |
| 1388 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1389 | def properties(): |
| 1390 | if verbose: print "Testing property..." |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1391 | class C(object): |
| 1392 | def getx(self): |
| 1393 | return self.__x |
| 1394 | def setx(self, value): |
| 1395 | self.__x = value |
| 1396 | def delx(self): |
| 1397 | del self.__x |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1398 | x = property(getx, setx, delx, doc="I'm the x property.") |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1399 | a = C() |
| 1400 | verify(not hasattr(a, "x")) |
| 1401 | a.x = 42 |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1402 | vereq(a._C__x, 42) |
| 1403 | vereq(a.x, 42) |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1404 | del a.x |
| 1405 | verify(not hasattr(a, "x")) |
| 1406 | verify(not hasattr(a, "_C__x")) |
| 1407 | C.x.__set__(a, 100) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1408 | vereq(C.x.__get__(a), 100) |
Guido van Rossum | 76f0cb8 | 2001-08-24 15:24:24 +0000 | [diff] [blame] | 1409 | ## C.x.__set__(a) |
| 1410 | ## verify(not hasattr(a, "x")) |
| 1411 | |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1412 | raw = C.__dict__['x'] |
| 1413 | verify(isinstance(raw, property)) |
| 1414 | |
| 1415 | attrs = dir(raw) |
| 1416 | verify("__doc__" in attrs) |
| 1417 | verify("fget" in attrs) |
| 1418 | verify("fset" in attrs) |
| 1419 | verify("fdel" in attrs) |
| 1420 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1421 | vereq(raw.__doc__, "I'm the x property.") |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1422 | verify(raw.fget is C.__dict__['getx']) |
| 1423 | verify(raw.fset is C.__dict__['setx']) |
| 1424 | verify(raw.fdel is C.__dict__['delx']) |
| 1425 | |
| 1426 | for attr in "__doc__", "fget", "fset", "fdel": |
| 1427 | try: |
| 1428 | setattr(raw, attr, 42) |
| 1429 | except TypeError, msg: |
| 1430 | if str(msg).find('readonly') < 0: |
| 1431 | raise TestFailed("when setting readonly attr %r on a " |
| 1432 | "property, got unexpected TypeError " |
| 1433 | "msg %r" % (attr, str(msg))) |
| 1434 | else: |
| 1435 | raise TestFailed("expected TypeError from trying to set " |
| 1436 | "readonly %r attr on a property" % attr) |
| 1437 | |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1438 | def supers(): |
Guido van Rossum | 9881fc1 | 2001-08-24 17:07:20 +0000 | [diff] [blame] | 1439 | if verbose: print "Testing super..." |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1440 | |
| 1441 | class A(object): |
| 1442 | def meth(self, a): |
| 1443 | return "A(%r)" % a |
| 1444 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1445 | vereq(A().meth(1), "A(1)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1446 | |
| 1447 | class B(A): |
| 1448 | def __init__(self): |
| 1449 | self.__super = super(B, self) |
| 1450 | def meth(self, a): |
| 1451 | return "B(%r)" % a + self.__super.meth(a) |
| 1452 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1453 | vereq(B().meth(2), "B(2)A(2)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1454 | |
| 1455 | class C(A): |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1456 | def meth(self, a): |
| 1457 | return "C(%r)" % a + self.__super.meth(a) |
| 1458 | C._C__super = super(C) |
| 1459 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1460 | vereq(C().meth(3), "C(3)A(3)") |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 1461 | |
| 1462 | class D(C, B): |
| 1463 | def meth(self, a): |
| 1464 | return "D(%r)" % a + super(D, self).meth(a) |
| 1465 | |
| 1466 | verify (D().meth(4) == "D(4)C(4)B(4)A(4)") |
| 1467 | |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1468 | def inherits(): |
| 1469 | if verbose: print "Testing inheritance from basic types..." |
| 1470 | |
| 1471 | class hexint(int): |
| 1472 | def __repr__(self): |
| 1473 | return hex(self) |
| 1474 | def __add__(self, other): |
| 1475 | return hexint(int.__add__(self, other)) |
| 1476 | # (Note that overriding __radd__ doesn't work, |
| 1477 | # because the int type gets first dibs.) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1478 | vereq(repr(hexint(7) + 9), "0x10") |
| 1479 | vereq(repr(hexint(1000) + 7), "0x3ef") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1480 | a = hexint(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1481 | vereq(a, 12345) |
| 1482 | vereq(int(a), 12345) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1483 | verify(int(a).__class__ is int) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1484 | vereq(hash(a), hash(12345)) |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 1485 | verify((+a).__class__ is int) |
| 1486 | verify((a >> 0).__class__ is int) |
| 1487 | verify((a << 0).__class__ is int) |
| 1488 | verify((hexint(0) << 12).__class__ is int) |
| 1489 | verify((hexint(0) >> 12).__class__ is int) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1490 | |
| 1491 | class octlong(long): |
| 1492 | __slots__ = [] |
| 1493 | def __str__(self): |
| 1494 | s = oct(self) |
| 1495 | if s[-1] == 'L': |
| 1496 | s = s[:-1] |
| 1497 | return s |
| 1498 | def __add__(self, other): |
| 1499 | return self.__class__(super(octlong, self).__add__(other)) |
| 1500 | __radd__ = __add__ |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1501 | vereq(str(octlong(3) + 5), "010") |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1502 | # (Note that overriding __radd__ here only seems to work |
| 1503 | # because the example uses a short int left argument.) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1504 | vereq(str(5 + octlong(3000)), "05675") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1505 | a = octlong(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1506 | vereq(a, 12345L) |
| 1507 | vereq(long(a), 12345L) |
| 1508 | vereq(hash(a), hash(12345L)) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1509 | verify(long(a).__class__ is long) |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 1510 | verify((+a).__class__ is long) |
| 1511 | verify((-a).__class__ is long) |
| 1512 | verify((-octlong(0)).__class__ is long) |
| 1513 | verify((a >> 0).__class__ is long) |
| 1514 | verify((a << 0).__class__ is long) |
| 1515 | verify((a - 0).__class__ is long) |
| 1516 | verify((a * 1).__class__ is long) |
| 1517 | verify((a ** 1).__class__ is long) |
| 1518 | verify((a // 1).__class__ is long) |
| 1519 | verify((1 * a).__class__ is long) |
| 1520 | verify((a | 0).__class__ is long) |
| 1521 | verify((a ^ 0).__class__ is long) |
| 1522 | verify((a & -1L).__class__ is long) |
| 1523 | verify((octlong(0) << 12).__class__ is long) |
| 1524 | verify((octlong(0) >> 12).__class__ is long) |
| 1525 | verify(abs(octlong(0)).__class__ is long) |
| 1526 | |
| 1527 | # Because octlong overrides __add__, we can't check the absence of +0 |
| 1528 | # optimizations using octlong. |
| 1529 | class longclone(long): |
| 1530 | pass |
| 1531 | a = longclone(1) |
| 1532 | verify((a + 0).__class__ is long) |
| 1533 | verify((0 + a).__class__ is long) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1534 | |
| 1535 | class precfloat(float): |
| 1536 | __slots__ = ['prec'] |
| 1537 | def __init__(self, value=0.0, prec=12): |
| 1538 | self.prec = int(prec) |
| 1539 | float.__init__(value) |
| 1540 | def __repr__(self): |
| 1541 | return "%.*g" % (self.prec, self) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1542 | vereq(repr(precfloat(1.1)), "1.1") |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1543 | a = precfloat(12345) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1544 | vereq(a, 12345.0) |
| 1545 | vereq(float(a), 12345.0) |
Tim Peters | 7a50f25 | 2001-09-10 21:28:20 +0000 | [diff] [blame] | 1546 | verify(float(a).__class__ is float) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1547 | vereq(hash(a), hash(12345.0)) |
Tim Peters | 0280cf7 | 2001-09-11 21:53:35 +0000 | [diff] [blame] | 1548 | verify((+a).__class__ is float) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1549 | |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1550 | class madcomplex(complex): |
| 1551 | def __repr__(self): |
| 1552 | return "%.17gj%+.17g" % (self.imag, self.real) |
| 1553 | a = madcomplex(-3, 4) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1554 | vereq(repr(a), "4j-3") |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1555 | base = complex(-3, 4) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1556 | veris(base.__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1557 | vereq(a, base) |
| 1558 | vereq(complex(a), base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1559 | veris(complex(a).__class__, complex) |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1560 | a = madcomplex(a) # just trying another form of the constructor |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1561 | vereq(repr(a), "4j-3") |
| 1562 | vereq(a, base) |
| 1563 | vereq(complex(a), base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1564 | veris(complex(a).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1565 | vereq(hash(a), hash(base)) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1566 | veris((+a).__class__, complex) |
| 1567 | veris((a + 0).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1568 | vereq(a + 0, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1569 | veris((a - 0).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1570 | vereq(a - 0, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1571 | veris((a * 1).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1572 | vereq(a * 1, base) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 1573 | veris((a / 1).__class__, complex) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1574 | vereq(a / 1, base) |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1575 | |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1576 | class madtuple(tuple): |
| 1577 | _rev = None |
| 1578 | def rev(self): |
| 1579 | if self._rev is not None: |
| 1580 | return self._rev |
| 1581 | L = list(self) |
| 1582 | L.reverse() |
| 1583 | self._rev = self.__class__(L) |
| 1584 | return self._rev |
| 1585 | 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] | 1586 | vereq(a, (1,2,3,4,5,6,7,8,9,0)) |
| 1587 | vereq(a.rev(), madtuple((0,9,8,7,6,5,4,3,2,1))) |
| 1588 | 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] | 1589 | for i in range(512): |
| 1590 | t = madtuple(range(i)) |
| 1591 | u = t.rev() |
| 1592 | v = u.rev() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1593 | vereq(v, t) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1594 | a = madtuple((1,2,3,4,5)) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1595 | vereq(tuple(a), (1,2,3,4,5)) |
Tim Peters | 4c3a0a3 | 2001-09-10 23:37:46 +0000 | [diff] [blame] | 1596 | verify(tuple(a).__class__ is tuple) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1597 | vereq(hash(a), hash((1,2,3,4,5))) |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 1598 | verify(a[:].__class__ is tuple) |
| 1599 | verify((a * 1).__class__ is tuple) |
| 1600 | verify((a * 0).__class__ is tuple) |
| 1601 | verify((a + ()).__class__ is tuple) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1602 | a = madtuple(()) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1603 | vereq(tuple(a), ()) |
Guido van Rossum | 779ce4a | 2001-09-11 14:02:22 +0000 | [diff] [blame] | 1604 | verify(tuple(a).__class__ is tuple) |
Tim Peters | 7b07a41 | 2001-09-11 19:48:03 +0000 | [diff] [blame] | 1605 | verify((a + a).__class__ is tuple) |
| 1606 | verify((a * 0).__class__ is tuple) |
| 1607 | verify((a * 1).__class__ is tuple) |
| 1608 | verify((a * 2).__class__ is tuple) |
| 1609 | verify(a[:].__class__ is tuple) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1610 | |
| 1611 | class madstring(str): |
| 1612 | _rev = None |
| 1613 | def rev(self): |
| 1614 | if self._rev is not None: |
| 1615 | return self._rev |
| 1616 | L = list(self) |
| 1617 | L.reverse() |
| 1618 | self._rev = self.__class__("".join(L)) |
| 1619 | return self._rev |
| 1620 | s = madstring("abcdefghijklmnopqrstuvwxyz") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1621 | vereq(s, "abcdefghijklmnopqrstuvwxyz") |
| 1622 | vereq(s.rev(), madstring("zyxwvutsrqponmlkjihgfedcba")) |
| 1623 | vereq(s.rev().rev(), madstring("abcdefghijklmnopqrstuvwxyz")) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1624 | for i in range(256): |
| 1625 | s = madstring("".join(map(chr, range(i)))) |
| 1626 | t = s.rev() |
| 1627 | u = t.rev() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1628 | vereq(u, s) |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 1629 | s = madstring("12345") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1630 | vereq(str(s), "12345") |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 1631 | verify(str(s).__class__ is str) |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 1632 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1633 | base = "\x00" * 5 |
| 1634 | s = madstring(base) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1635 | vereq(s, base) |
| 1636 | vereq(str(s), base) |
Tim Peters | c636f56 | 2001-09-11 01:52:02 +0000 | [diff] [blame] | 1637 | verify(str(s).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1638 | vereq(hash(s), hash(base)) |
| 1639 | vereq({s: 1}[base], 1) |
| 1640 | vereq({base: 1}[s], 1) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1641 | verify((s + "").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1642 | vereq(s + "", base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1643 | verify(("" + s).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1644 | vereq("" + s, base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1645 | verify((s * 0).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1646 | vereq(s * 0, "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1647 | verify((s * 1).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1648 | vereq(s * 1, base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1649 | verify((s * 2).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1650 | vereq(s * 2, base + base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1651 | verify(s[:].__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1652 | vereq(s[:], base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1653 | verify(s[0:0].__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1654 | vereq(s[0:0], "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1655 | verify(s.strip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1656 | vereq(s.strip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1657 | verify(s.lstrip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1658 | vereq(s.lstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1659 | verify(s.rstrip().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1660 | vereq(s.rstrip(), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1661 | identitytab = ''.join([chr(i) for i in range(256)]) |
| 1662 | verify(s.translate(identitytab).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1663 | vereq(s.translate(identitytab), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1664 | verify(s.translate(identitytab, "x").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1665 | vereq(s.translate(identitytab, "x"), base) |
| 1666 | vereq(s.translate(identitytab, "\x00"), "") |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1667 | verify(s.replace("x", "x").__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1668 | vereq(s.replace("x", "x"), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1669 | verify(s.ljust(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1670 | vereq(s.ljust(len(s)), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1671 | verify(s.rjust(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1672 | vereq(s.rjust(len(s)), base) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1673 | verify(s.center(len(s)).__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1674 | vereq(s.center(len(s)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1675 | verify(s.lower().__class__ is str) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1676 | vereq(s.lower(), base) |
Tim Peters | c636f56 | 2001-09-11 01:52:02 +0000 | [diff] [blame] | 1677 | |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1678 | s = madstring("x y") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1679 | vereq(s, "x y") |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1680 | verify(intern(s).__class__ is str) |
| 1681 | verify(intern(s) is intern("x y")) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1682 | vereq(intern(s), "x y") |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1683 | |
| 1684 | i = intern("y x") |
| 1685 | s = madstring("y x") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1686 | vereq(s, i) |
Tim Peters | 111f609 | 2001-09-12 07:54:51 +0000 | [diff] [blame] | 1687 | verify(intern(s).__class__ is str) |
| 1688 | verify(intern(s) is i) |
| 1689 | |
| 1690 | s = madstring(i) |
| 1691 | verify(intern(s).__class__ is str) |
| 1692 | verify(intern(s) is i) |
| 1693 | |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 1694 | class madunicode(unicode): |
| 1695 | _rev = None |
| 1696 | def rev(self): |
| 1697 | if self._rev is not None: |
| 1698 | return self._rev |
| 1699 | L = list(self) |
| 1700 | L.reverse() |
| 1701 | self._rev = self.__class__(u"".join(L)) |
| 1702 | return self._rev |
| 1703 | u = madunicode("ABCDEF") |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1704 | vereq(u, u"ABCDEF") |
| 1705 | vereq(u.rev(), madunicode(u"FEDCBA")) |
| 1706 | vereq(u.rev().rev(), madunicode(u"ABCDEF")) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1707 | base = u"12345" |
| 1708 | u = madunicode(base) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1709 | vereq(unicode(u), base) |
Tim Peters | 78e0fc7 | 2001-09-11 03:07:38 +0000 | [diff] [blame] | 1710 | verify(unicode(u).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1711 | vereq(hash(u), hash(base)) |
| 1712 | vereq({u: 1}[base], 1) |
| 1713 | vereq({base: 1}[u], 1) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1714 | verify(u.strip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1715 | vereq(u.strip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1716 | verify(u.lstrip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1717 | vereq(u.lstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1718 | verify(u.rstrip().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1719 | vereq(u.rstrip(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1720 | verify(u.replace(u"x", u"x").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1721 | vereq(u.replace(u"x", u"x"), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1722 | verify(u.replace(u"xy", u"xy").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1723 | vereq(u.replace(u"xy", u"xy"), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1724 | verify(u.center(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1725 | vereq(u.center(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1726 | verify(u.ljust(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1727 | vereq(u.ljust(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1728 | verify(u.rjust(len(u)).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1729 | vereq(u.rjust(len(u)), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1730 | verify(u.lower().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1731 | vereq(u.lower(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1732 | verify(u.upper().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1733 | vereq(u.upper(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1734 | verify(u.capitalize().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1735 | vereq(u.capitalize(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1736 | verify(u.title().__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1737 | vereq(u.title(), base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1738 | verify((u + u"").__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1739 | vereq(u + u"", base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1740 | verify((u"" + u).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1741 | vereq(u"" + u, base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1742 | verify((u * 0).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1743 | vereq(u * 0, u"") |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1744 | verify((u * 1).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1745 | vereq(u * 1, base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1746 | verify((u * 2).__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1747 | vereq(u * 2, base + base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1748 | verify(u[:].__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1749 | vereq(u[:], base) |
Tim Peters | 7a29bd5 | 2001-09-12 03:03:31 +0000 | [diff] [blame] | 1750 | verify(u[0:0].__class__ is unicode) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1751 | vereq(u[0:0], u"") |
Guido van Rossum | 91ee798 | 2001-08-30 20:52:40 +0000 | [diff] [blame] | 1752 | |
Guido van Rossum | 12b22ff | 2001-10-09 20:36:44 +0000 | [diff] [blame] | 1753 | class sublist(list): |
| 1754 | pass |
| 1755 | a = sublist(range(5)) |
| 1756 | vereq(a, range(5)) |
| 1757 | a.append("hello") |
| 1758 | vereq(a, range(5) + ["hello"]) |
| 1759 | a[5] = 5 |
| 1760 | vereq(a, range(6)) |
| 1761 | a.extend(range(6, 20)) |
| 1762 | vereq(a, range(20)) |
| 1763 | a[-5:] = [] |
| 1764 | vereq(a, range(15)) |
| 1765 | del a[10:15] |
| 1766 | vereq(len(a), 10) |
| 1767 | vereq(a, range(10)) |
| 1768 | vereq(list(a), range(10)) |
| 1769 | vereq(a[0], 0) |
| 1770 | vereq(a[9], 9) |
| 1771 | vereq(a[-10], 0) |
| 1772 | vereq(a[-1], 9) |
| 1773 | vereq(a[:5], range(5)) |
| 1774 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1775 | class CountedInput(file): |
| 1776 | """Counts lines read by self.readline(). |
| 1777 | |
| 1778 | self.lineno is the 0-based ordinal of the last line read, up to |
| 1779 | a maximum of one greater than the number of lines in the file. |
| 1780 | |
| 1781 | self.ateof is true if and only if the final "" line has been read, |
| 1782 | at which point self.lineno stops incrementing, and further calls |
| 1783 | to readline() continue to return "". |
| 1784 | """ |
| 1785 | |
| 1786 | lineno = 0 |
| 1787 | ateof = 0 |
| 1788 | def readline(self): |
| 1789 | if self.ateof: |
| 1790 | return "" |
| 1791 | s = file.readline(self) |
| 1792 | # Next line works too. |
| 1793 | # s = super(CountedInput, self).readline() |
| 1794 | self.lineno += 1 |
| 1795 | if s == "": |
| 1796 | self.ateof = 1 |
| 1797 | return s |
| 1798 | |
Tim Peters | 561f899 | 2001-09-13 19:36:36 +0000 | [diff] [blame] | 1799 | f = file(name=TESTFN, mode='w') |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1800 | lines = ['a\n', 'b\n', 'c\n'] |
| 1801 | try: |
| 1802 | f.writelines(lines) |
| 1803 | f.close() |
| 1804 | f = CountedInput(TESTFN) |
| 1805 | for (i, expected) in zip(range(1, 5) + [4], lines + 2 * [""]): |
| 1806 | got = f.readline() |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1807 | vereq(expected, got) |
| 1808 | vereq(f.lineno, i) |
| 1809 | vereq(f.ateof, (i > len(lines))) |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1810 | f.close() |
| 1811 | finally: |
| 1812 | try: |
| 1813 | f.close() |
| 1814 | except: |
| 1815 | pass |
| 1816 | try: |
| 1817 | import os |
| 1818 | os.unlink(TESTFN) |
| 1819 | except: |
| 1820 | pass |
| 1821 | |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 1822 | def keywords(): |
| 1823 | if verbose: |
| 1824 | print "Testing keyword args to basic type constructors ..." |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1825 | vereq(int(x=1), 1) |
| 1826 | vereq(float(x=2), 2.0) |
| 1827 | vereq(long(x=3), 3L) |
| 1828 | vereq(complex(imag=42, real=666), complex(666, 42)) |
| 1829 | vereq(str(object=500), '500') |
| 1830 | vereq(unicode(string='abc', errors='strict'), u'abc') |
| 1831 | vereq(tuple(sequence=range(3)), (0, 1, 2)) |
| 1832 | vereq(list(sequence=(0, 1, 2)), range(3)) |
| 1833 | vereq(dictionary(mapping={1: 2}), {1: 2}) |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 1834 | |
| 1835 | for constructor in (int, float, long, complex, str, unicode, |
| 1836 | tuple, list, dictionary, file): |
| 1837 | try: |
| 1838 | constructor(bogus_keyword_arg=1) |
| 1839 | except TypeError: |
| 1840 | pass |
| 1841 | else: |
| 1842 | raise TestFailed("expected TypeError from bogus keyword " |
| 1843 | "argument to %r" % constructor) |
Tim Peters | 561f899 | 2001-09-13 19:36:36 +0000 | [diff] [blame] | 1844 | |
Tim Peters | 8fa4567 | 2001-09-13 21:01:29 +0000 | [diff] [blame] | 1845 | def restricted(): |
| 1846 | import rexec |
| 1847 | if verbose: |
| 1848 | print "Testing interaction with restricted execution ..." |
| 1849 | |
| 1850 | sandbox = rexec.RExec() |
| 1851 | |
| 1852 | code1 = """f = open(%r, 'w')""" % TESTFN |
| 1853 | code2 = """f = file(%r, 'w')""" % TESTFN |
| 1854 | code3 = """\ |
| 1855 | f = open(%r) |
| 1856 | t = type(f) # a sneaky way to get the file() constructor |
| 1857 | f.close() |
| 1858 | f = t(%r, 'w') # rexec can't catch this by itself |
| 1859 | """ % (TESTFN, TESTFN) |
| 1860 | |
| 1861 | f = open(TESTFN, 'w') # Create the file so code3 can find it. |
| 1862 | f.close() |
| 1863 | |
| 1864 | try: |
| 1865 | for code in code1, code2, code3: |
| 1866 | try: |
| 1867 | sandbox.r_exec(code) |
| 1868 | except IOError, msg: |
| 1869 | if str(msg).find("restricted") >= 0: |
| 1870 | outcome = "OK" |
| 1871 | else: |
| 1872 | outcome = "got an exception, but not an expected one" |
| 1873 | else: |
| 1874 | outcome = "expected a restricted-execution exception" |
| 1875 | |
| 1876 | if outcome != "OK": |
| 1877 | raise TestFailed("%s, in %r" % (outcome, code)) |
| 1878 | |
| 1879 | finally: |
| 1880 | try: |
| 1881 | import os |
| 1882 | os.unlink(TESTFN) |
| 1883 | except: |
| 1884 | pass |
| 1885 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1886 | def str_subclass_as_dict_key(): |
| 1887 | if verbose: |
| 1888 | print "Testing a str subclass used as dict key .." |
| 1889 | |
| 1890 | class cistr(str): |
| 1891 | """Sublcass of str that computes __eq__ case-insensitively. |
| 1892 | |
| 1893 | Also computes a hash code of the string in canonical form. |
| 1894 | """ |
| 1895 | |
| 1896 | def __init__(self, value): |
| 1897 | self.canonical = value.lower() |
| 1898 | self.hashcode = hash(self.canonical) |
| 1899 | |
| 1900 | def __eq__(self, other): |
| 1901 | if not isinstance(other, cistr): |
| 1902 | other = cistr(other) |
| 1903 | return self.canonical == other.canonical |
| 1904 | |
| 1905 | def __hash__(self): |
| 1906 | return self.hashcode |
| 1907 | |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1908 | vereq(cistr('ABC'), 'abc') |
| 1909 | vereq('aBc', cistr('ABC')) |
| 1910 | vereq(str(cistr('ABC')), 'ABC') |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1911 | |
| 1912 | d = {cistr('one'): 1, cistr('two'): 2, cistr('tHree'): 3} |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1913 | vereq(d[cistr('one')], 1) |
| 1914 | vereq(d[cistr('tWo')], 2) |
| 1915 | vereq(d[cistr('THrEE')], 3) |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1916 | verify(cistr('ONe') in d) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1917 | vereq(d.get(cistr('thrEE')), 3) |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1918 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 1919 | def classic_comparisons(): |
| 1920 | if verbose: print "Testing classic comparisons..." |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 1921 | class classic: |
| 1922 | pass |
| 1923 | for base in (classic, int, object): |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 1924 | if verbose: print " (base = %s)" % base |
| 1925 | class C(base): |
| 1926 | def __init__(self, value): |
| 1927 | self.value = int(value) |
| 1928 | def __cmp__(self, other): |
| 1929 | if isinstance(other, C): |
| 1930 | return cmp(self.value, other.value) |
| 1931 | if isinstance(other, int) or isinstance(other, long): |
| 1932 | return cmp(self.value, other) |
| 1933 | return NotImplemented |
| 1934 | c1 = C(1) |
| 1935 | c2 = C(2) |
| 1936 | c3 = C(3) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1937 | vereq(c1, 1) |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 1938 | c = {1: c1, 2: c2, 3: c3} |
| 1939 | for x in 1, 2, 3: |
| 1940 | for y in 1, 2, 3: |
| 1941 | verify(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 1942 | for op in "<", "<=", "==", "!=", ">", ">=": |
| 1943 | verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op), |
| 1944 | "x=%d, y=%d" % (x, y)) |
| 1945 | verify(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 1946 | verify(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) |
| 1947 | |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 1948 | def rich_comparisons(): |
| 1949 | if verbose: |
| 1950 | print "Testing rich comparisons..." |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 1951 | class Z(complex): |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1952 | pass |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 1953 | z = Z(1) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1954 | vereq(z, 1+0j) |
| 1955 | vereq(1+0j, z) |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 1956 | class ZZ(complex): |
| 1957 | def __eq__(self, other): |
| 1958 | try: |
| 1959 | return abs(self - other) <= 1e-6 |
| 1960 | except: |
| 1961 | return NotImplemented |
| 1962 | zz = ZZ(1.0000003) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 1963 | vereq(zz, 1+0j) |
| 1964 | vereq(1+0j, zz) |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1965 | |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 1966 | class classic: |
| 1967 | pass |
| 1968 | for base in (classic, int, object, list): |
| 1969 | if verbose: print " (base = %s)" % base |
| 1970 | class C(base): |
| 1971 | def __init__(self, value): |
| 1972 | self.value = int(value) |
| 1973 | def __cmp__(self, other): |
| 1974 | raise TestFailed, "shouldn't call __cmp__" |
| 1975 | def __eq__(self, other): |
| 1976 | if isinstance(other, C): |
| 1977 | return self.value == other.value |
| 1978 | if isinstance(other, int) or isinstance(other, long): |
| 1979 | return self.value == other |
| 1980 | return NotImplemented |
| 1981 | def __ne__(self, other): |
| 1982 | if isinstance(other, C): |
| 1983 | return self.value != other.value |
| 1984 | if isinstance(other, int) or isinstance(other, long): |
| 1985 | return self.value != other |
| 1986 | return NotImplemented |
| 1987 | def __lt__(self, other): |
| 1988 | if isinstance(other, C): |
| 1989 | return self.value < other.value |
| 1990 | if isinstance(other, int) or isinstance(other, long): |
| 1991 | return self.value < other |
| 1992 | return NotImplemented |
| 1993 | def __le__(self, other): |
| 1994 | if isinstance(other, C): |
| 1995 | return self.value <= other.value |
| 1996 | if isinstance(other, int) or isinstance(other, long): |
| 1997 | return self.value <= other |
| 1998 | return NotImplemented |
| 1999 | def __gt__(self, other): |
| 2000 | if isinstance(other, C): |
| 2001 | return self.value > other.value |
| 2002 | if isinstance(other, int) or isinstance(other, long): |
| 2003 | return self.value > other |
| 2004 | return NotImplemented |
| 2005 | def __ge__(self, other): |
| 2006 | if isinstance(other, C): |
| 2007 | return self.value >= other.value |
| 2008 | if isinstance(other, int) or isinstance(other, long): |
| 2009 | return self.value >= other |
| 2010 | return NotImplemented |
| 2011 | c1 = C(1) |
| 2012 | c2 = C(2) |
| 2013 | c3 = C(3) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2014 | vereq(c1, 1) |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2015 | c = {1: c1, 2: c2, 3: c3} |
| 2016 | for x in 1, 2, 3: |
| 2017 | for y in 1, 2, 3: |
| 2018 | for op in "<", "<=", "==", "!=", ">", ">=": |
| 2019 | verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op), |
| 2020 | "x=%d, y=%d" % (x, y)) |
| 2021 | verify(eval("c[x] %s y" % op) == eval("x %s y" % op), |
| 2022 | "x=%d, y=%d" % (x, y)) |
| 2023 | verify(eval("x %s c[y]" % op) == eval("x %s y" % op), |
| 2024 | "x=%d, y=%d" % (x, y)) |
| 2025 | |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2026 | def coercions(): |
| 2027 | if verbose: print "Testing coercions..." |
| 2028 | class I(int): pass |
| 2029 | coerce(I(0), 0) |
| 2030 | coerce(0, I(0)) |
| 2031 | class L(long): pass |
| 2032 | coerce(L(0), 0) |
| 2033 | coerce(L(0), 0L) |
| 2034 | coerce(0, L(0)) |
| 2035 | coerce(0L, L(0)) |
| 2036 | class F(float): pass |
| 2037 | coerce(F(0), 0) |
| 2038 | coerce(F(0), 0L) |
| 2039 | coerce(F(0), 0.) |
| 2040 | coerce(0, F(0)) |
| 2041 | coerce(0L, F(0)) |
| 2042 | coerce(0., F(0)) |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 2043 | class C(complex): pass |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2044 | coerce(C(0), 0) |
| 2045 | coerce(C(0), 0L) |
| 2046 | coerce(C(0), 0.) |
| 2047 | coerce(C(0), 0j) |
| 2048 | coerce(0, C(0)) |
| 2049 | coerce(0L, C(0)) |
| 2050 | coerce(0., C(0)) |
| 2051 | coerce(0j, C(0)) |
| 2052 | |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2053 | def descrdoc(): |
| 2054 | if verbose: print "Testing descriptor doc strings..." |
| 2055 | def check(descr, what): |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2056 | vereq(descr.__doc__, what) |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2057 | check(file.closed, "flag set if the file is closed") # getset descriptor |
| 2058 | check(file.name, "file name") # member descriptor |
| 2059 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2060 | def setclass(): |
| 2061 | if verbose: print "Testing __class__ assignment..." |
| 2062 | class C(object): pass |
| 2063 | class D(object): pass |
| 2064 | class E(object): pass |
| 2065 | class F(D, E): pass |
| 2066 | for cls in C, D, E, F: |
| 2067 | for cls2 in C, D, E, F: |
| 2068 | x = cls() |
| 2069 | x.__class__ = cls2 |
| 2070 | verify(x.__class__ is cls2) |
| 2071 | x.__class__ = cls |
| 2072 | verify(x.__class__ is cls) |
| 2073 | def cant(x, C): |
| 2074 | try: |
| 2075 | x.__class__ = C |
| 2076 | except TypeError: |
| 2077 | pass |
| 2078 | else: |
| 2079 | raise TestFailed, "shouldn't allow %r.__class__ = %r" % (x, C) |
| 2080 | cant(C(), list) |
| 2081 | cant(list(), C) |
| 2082 | cant(C(), 1) |
| 2083 | cant(C(), object) |
| 2084 | cant(object(), list) |
| 2085 | cant(list(), object) |
| 2086 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2087 | def pickles(): |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2088 | if verbose: |
| 2089 | print "Testing pickling and copying new-style classes and objects..." |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2090 | import pickle, cPickle |
| 2091 | |
| 2092 | def sorteditems(d): |
| 2093 | L = d.items() |
| 2094 | L.sort() |
| 2095 | return L |
| 2096 | |
| 2097 | global C |
| 2098 | class C(object): |
| 2099 | def __init__(self, a, b): |
| 2100 | super(C, self).__init__() |
| 2101 | self.a = a |
| 2102 | self.b = b |
| 2103 | def __repr__(self): |
| 2104 | return "C(%r, %r)" % (self.a, self.b) |
| 2105 | |
| 2106 | global C1 |
| 2107 | class C1(list): |
| 2108 | def __new__(cls, a, b): |
| 2109 | return super(C1, cls).__new__(cls) |
| 2110 | def __init__(self, a, b): |
| 2111 | self.a = a |
| 2112 | self.b = b |
| 2113 | def __repr__(self): |
| 2114 | return "C1(%r, %r)<%r>" % (self.a, self.b, list(self)) |
| 2115 | |
| 2116 | global C2 |
| 2117 | class C2(int): |
| 2118 | def __new__(cls, a, b, val=0): |
| 2119 | return super(C2, cls).__new__(cls, val) |
| 2120 | def __init__(self, a, b, val=0): |
| 2121 | self.a = a |
| 2122 | self.b = b |
| 2123 | def __repr__(self): |
| 2124 | return "C2(%r, %r)<%r>" % (self.a, self.b, int(self)) |
| 2125 | |
| 2126 | for p in pickle, cPickle: |
| 2127 | for bin in 0, 1: |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2128 | if verbose: |
| 2129 | print p.__name__, ["text", "binary"][bin] |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2130 | |
| 2131 | for cls in C, C1, C2: |
| 2132 | s = p.dumps(cls, bin) |
| 2133 | cls2 = p.loads(s) |
| 2134 | verify(cls2 is cls) |
| 2135 | |
| 2136 | a = C1(1, 2); a.append(42); a.append(24) |
| 2137 | b = C2("hello", "world", 42) |
| 2138 | s = p.dumps((a, b), bin) |
| 2139 | x, y = p.loads(s) |
| 2140 | assert x.__class__ == a.__class__ |
| 2141 | assert sorteditems(x.__dict__) == sorteditems(a.__dict__) |
| 2142 | assert y.__class__ == b.__class__ |
| 2143 | assert sorteditems(y.__dict__) == sorteditems(b.__dict__) |
| 2144 | assert `x` == `a` |
| 2145 | assert `y` == `b` |
| 2146 | if verbose: |
| 2147 | print "a = x =", a |
| 2148 | print "b = y =", b |
| 2149 | |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2150 | # Testing copy.deepcopy() |
| 2151 | if verbose: |
| 2152 | print "deepcopy" |
| 2153 | import copy |
| 2154 | for cls in C, C1, C2: |
| 2155 | cls2 = copy.deepcopy(cls) |
| 2156 | verify(cls2 is cls) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2157 | |
Guido van Rossum | fe1fd0e | 2001-10-02 19:58:32 +0000 | [diff] [blame] | 2158 | a = C1(1, 2); a.append(42); a.append(24) |
| 2159 | b = C2("hello", "world", 42) |
| 2160 | x, y = copy.deepcopy((a, b)) |
| 2161 | assert x.__class__ == a.__class__ |
| 2162 | assert sorteditems(x.__dict__) == sorteditems(a.__dict__) |
| 2163 | assert y.__class__ == b.__class__ |
| 2164 | assert sorteditems(y.__dict__) == sorteditems(b.__dict__) |
| 2165 | assert `x` == `a` |
| 2166 | assert `y` == `b` |
| 2167 | if verbose: |
| 2168 | print "a = x =", a |
| 2169 | print "b = y =", b |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2170 | |
| 2171 | def copies(): |
| 2172 | if verbose: print "Testing copy.copy() and copy.deepcopy()..." |
| 2173 | import copy |
| 2174 | class C(object): |
| 2175 | pass |
| 2176 | |
| 2177 | a = C() |
| 2178 | a.foo = 12 |
| 2179 | b = copy.copy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2180 | vereq(b.__dict__, a.__dict__) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2181 | |
| 2182 | a.bar = [1,2,3] |
| 2183 | c = copy.copy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2184 | vereq(c.bar, a.bar) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2185 | verify(c.bar is a.bar) |
| 2186 | |
| 2187 | d = copy.deepcopy(a) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2188 | vereq(d.__dict__, a.__dict__) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2189 | a.bar.append(4) |
Guido van Rossum | 4570455 | 2001-10-08 16:35:45 +0000 | [diff] [blame] | 2190 | vereq(d.bar, [1,2,3]) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2191 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2192 | def binopoverride(): |
| 2193 | if verbose: print "Testing overrides of binary operations..." |
| 2194 | class I(int): |
| 2195 | def __repr__(self): |
| 2196 | return "I(%r)" % int(self) |
| 2197 | def __add__(self, other): |
| 2198 | return I(int(self) + int(other)) |
| 2199 | __radd__ = __add__ |
| 2200 | def __pow__(self, other, mod=None): |
| 2201 | if mod is None: |
| 2202 | return I(pow(int(self), int(other))) |
| 2203 | else: |
| 2204 | return I(pow(int(self), int(other), int(mod))) |
| 2205 | def __rpow__(self, other, mod=None): |
| 2206 | if mod is None: |
| 2207 | return I(pow(int(other), int(self), mod)) |
| 2208 | else: |
| 2209 | return I(pow(int(other), int(self), int(mod))) |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2210 | |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2211 | vereq(`I(1) + I(2)`, "I(3)") |
| 2212 | vereq(`I(1) + 2`, "I(3)") |
| 2213 | vereq(`1 + I(2)`, "I(3)") |
| 2214 | vereq(`I(2) ** I(3)`, "I(8)") |
| 2215 | vereq(`2 ** I(3)`, "I(8)") |
| 2216 | vereq(`I(2) ** 3`, "I(8)") |
| 2217 | vereq(`pow(I(2), I(3), I(5))`, "I(3)") |
| 2218 | class S(str): |
| 2219 | def __eq__(self, other): |
| 2220 | return self.lower() == other.lower() |
| 2221 | |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2222 | def subclasspropagation(): |
| 2223 | if verbose: print "Testing propagation of slot functions to subclasses..." |
| 2224 | class A(object): |
| 2225 | pass |
| 2226 | class B(A): |
| 2227 | pass |
| 2228 | class C(A): |
| 2229 | pass |
| 2230 | class D(B, C): |
| 2231 | pass |
| 2232 | d = D() |
| 2233 | vereq(hash(d), id(d)) |
| 2234 | A.__hash__ = lambda self: 42 |
| 2235 | vereq(hash(d), 42) |
| 2236 | C.__hash__ = lambda self: 314 |
| 2237 | vereq(hash(d), 314) |
| 2238 | B.__hash__ = lambda self: 144 |
| 2239 | vereq(hash(d), 144) |
| 2240 | D.__hash__ = lambda self: 100 |
| 2241 | vereq(hash(d), 100) |
| 2242 | del D.__hash__ |
| 2243 | vereq(hash(d), 144) |
| 2244 | del B.__hash__ |
| 2245 | vereq(hash(d), 314) |
| 2246 | del C.__hash__ |
| 2247 | vereq(hash(d), 42) |
| 2248 | del A.__hash__ |
| 2249 | vereq(hash(d), id(d)) |
| 2250 | d.foo = 42 |
| 2251 | d.bar = 42 |
| 2252 | vereq(d.foo, 42) |
| 2253 | vereq(d.bar, 42) |
| 2254 | def __getattribute__(self, name): |
| 2255 | if name == "foo": |
| 2256 | return 24 |
| 2257 | return object.__getattribute__(self, name) |
| 2258 | A.__getattribute__ = __getattribute__ |
| 2259 | vereq(d.foo, 24) |
| 2260 | vereq(d.bar, 42) |
| 2261 | def __getattr__(self, name): |
| 2262 | if name in ("spam", "foo", "bar"): |
| 2263 | return "hello" |
| 2264 | raise AttributeError, name |
| 2265 | B.__getattr__ = __getattr__ |
| 2266 | vereq(d.spam, "hello") |
| 2267 | vereq(d.foo, 24) |
| 2268 | vereq(d.bar, 42) |
| 2269 | del A.__getattribute__ |
| 2270 | vereq(d.foo, 42) |
| 2271 | del d.foo |
| 2272 | vereq(d.foo, "hello") |
| 2273 | vereq(d.bar, 42) |
| 2274 | del B.__getattr__ |
| 2275 | try: |
| 2276 | d.foo |
| 2277 | except AttributeError: |
| 2278 | pass |
| 2279 | else: |
| 2280 | raise TestFailed, "d.foo should be undefined now" |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 2281 | |
| 2282 | def buffer_inherit(): |
| 2283 | import binascii |
| 2284 | # SF bug [#470040] ParseTuple t# vs subclasses. |
| 2285 | if verbose: |
| 2286 | print "Testing that buffer interface is inherited ..." |
| 2287 | |
| 2288 | class MyStr(str): |
| 2289 | pass |
| 2290 | base = 'abc' |
| 2291 | m = MyStr(base) |
| 2292 | # b2a_hex uses the buffer interface to get its argument's value, via |
| 2293 | # PyArg_ParseTuple 't#' code. |
| 2294 | vereq(binascii.b2a_hex(m), binascii.b2a_hex(base)) |
| 2295 | |
| 2296 | # It's not clear that unicode will continue to support the character |
| 2297 | # buffer interface, and this test will fail if that's taken away. |
| 2298 | class MyUni(unicode): |
| 2299 | pass |
| 2300 | base = u'abc' |
| 2301 | m = MyUni(base) |
| 2302 | vereq(binascii.b2a_hex(m), binascii.b2a_hex(base)) |
| 2303 | |
| 2304 | class MyInt(int): |
| 2305 | pass |
| 2306 | m = MyInt(42) |
| 2307 | try: |
| 2308 | binascii.b2a_hex(m) |
| 2309 | raise TestFailed('subclass of int should not have a buffer interface') |
| 2310 | except TypeError: |
| 2311 | pass |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2312 | |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 2313 | def str_of_str_subclass(): |
| 2314 | import binascii |
| 2315 | import cStringIO |
| 2316 | |
| 2317 | if verbose: |
| 2318 | print "Testing __str__ defined in subclass of str ..." |
| 2319 | |
| 2320 | class octetstring(str): |
| 2321 | def __str__(self): |
| 2322 | return binascii.b2a_hex(self) |
| 2323 | def __repr__(self): |
| 2324 | return self + " repr" |
| 2325 | |
| 2326 | o = octetstring('A') |
| 2327 | vereq(type(o), octetstring) |
| 2328 | vereq(type(str(o)), str) |
| 2329 | vereq(type(repr(o)), str) |
| 2330 | vereq(ord(o), 0x41) |
| 2331 | vereq(str(o), '41') |
| 2332 | vereq(repr(o), 'A repr') |
| 2333 | vereq(o.__str__(), '41') |
| 2334 | vereq(o.__repr__(), 'A repr') |
| 2335 | |
| 2336 | capture = cStringIO.StringIO() |
| 2337 | # Calling str() or not exercises different internal paths. |
| 2338 | print >> capture, o |
| 2339 | print >> capture, str(o) |
| 2340 | vereq(capture.getvalue(), '41\n41\n') |
| 2341 | capture.close() |
| 2342 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2343 | def kwdargs(): |
| 2344 | if verbose: print "Testing keyword arguments to __init__, __call__..." |
| 2345 | def f(a): return a |
| 2346 | vereq(f.__call__(a=42), 42) |
| 2347 | a = [] |
| 2348 | list.__init__(a, sequence=[0, 1, 2]) |
| 2349 | vereq(a, [0, 1, 2]) |
| 2350 | |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 2351 | def test_main(): |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 2352 | class_docstrings() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2353 | lists() |
| 2354 | dicts() |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2355 | dict_constructor() |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 2356 | test_dir() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2357 | ints() |
| 2358 | longs() |
| 2359 | floats() |
| 2360 | complexes() |
| 2361 | spamlists() |
| 2362 | spamdicts() |
| 2363 | pydicts() |
| 2364 | pylists() |
| 2365 | metaclass() |
| 2366 | pymods() |
| 2367 | multi() |
| 2368 | diamond() |
Guido van Rossum | 3720261 | 2001-08-09 19:45:21 +0000 | [diff] [blame] | 2369 | objects() |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2370 | slots() |
| 2371 | dynamics() |
| 2372 | errors() |
| 2373 | classmethods() |
| 2374 | staticmethods() |
| 2375 | classic() |
| 2376 | compattr() |
| 2377 | newslot() |
| 2378 | altmro() |
| 2379 | overloading() |
Guido van Rossum | b5a136b | 2001-08-15 17:51:17 +0000 | [diff] [blame] | 2380 | methods() |
Guido van Rossum | a4ff6ab | 2001-08-15 23:57:59 +0000 | [diff] [blame] | 2381 | specials() |
Guido van Rossum | 65d5d7f | 2001-08-17 21:27:53 +0000 | [diff] [blame] | 2382 | weakrefs() |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 2383 | properties() |
Guido van Rossum | c4a1880 | 2001-08-24 16:55:27 +0000 | [diff] [blame] | 2384 | supers() |
Guido van Rossum | caa9f43 | 2001-08-30 20:06:08 +0000 | [diff] [blame] | 2385 | inherits() |
Tim Peters | 808b94e | 2001-09-13 19:33:07 +0000 | [diff] [blame] | 2386 | keywords() |
Tim Peters | 8fa4567 | 2001-09-13 21:01:29 +0000 | [diff] [blame] | 2387 | restricted() |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2388 | str_subclass_as_dict_key() |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 2389 | classic_comparisons() |
Guido van Rossum | 0639f59 | 2001-09-18 21:06:04 +0000 | [diff] [blame] | 2390 | rich_comparisons() |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2391 | coercions() |
Guido van Rossum | 8b9cc7e | 2001-09-20 21:49:53 +0000 | [diff] [blame] | 2392 | descrdoc() |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 2393 | setclass() |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 2394 | pickles() |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 2395 | copies() |
Guido van Rossum | 4bb1e36 | 2001-09-28 23:49:48 +0000 | [diff] [blame] | 2396 | binopoverride() |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 2397 | subclasspropagation() |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 2398 | buffer_inherit() |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 2399 | str_of_str_subclass() |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2400 | kwdargs() |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 2401 | if verbose: print "All OK" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2402 | |
Guido van Rossum | a56b42b | 2001-09-20 21:39:07 +0000 | [diff] [blame] | 2403 | if __name__ == "__main__": |
| 2404 | test_main() |