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