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