Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1 | "Test the functionality of Python classes implementing operators." |
| 2 | |
Barry Warsaw | 408b6d3 | 2002-07-30 23:27:12 +0000 | [diff] [blame] | 3 | from test.test_support import TestFailed |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 4 | |
| 5 | testmeths = [ |
| 6 | |
| 7 | # Binary operations |
| 8 | "add", |
| 9 | "radd", |
| 10 | "sub", |
| 11 | "rsub", |
| 12 | "mul", |
| 13 | "rmul", |
| 14 | "div", |
| 15 | "rdiv", |
| 16 | "mod", |
| 17 | "rmod", |
| 18 | "divmod", |
| 19 | "rdivmod", |
| 20 | "pow", |
| 21 | "rpow", |
| 22 | "rshift", |
| 23 | "rrshift", |
| 24 | "lshift", |
| 25 | "rlshift", |
| 26 | "and", |
| 27 | "rand", |
| 28 | "or", |
| 29 | "ror", |
| 30 | "xor", |
| 31 | "rxor", |
| 32 | |
| 33 | # List/dict operations |
| 34 | "contains", |
| 35 | "getitem", |
| 36 | "getslice", |
| 37 | "setitem", |
| 38 | "setslice", |
| 39 | "delitem", |
| 40 | "delslice", |
| 41 | |
| 42 | # Unary operations |
| 43 | "neg", |
| 44 | "pos", |
| 45 | "abs", |
| 46 | "int", |
| 47 | "long", |
| 48 | "float", |
| 49 | "oct", |
| 50 | "hex", |
| 51 | |
| 52 | # generic operations |
| 53 | "init", |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 54 | ] |
| 55 | |
| 56 | # These need to return something other than None |
| 57 | # "coerce", |
| 58 | # "hash", |
| 59 | # "str", |
| 60 | # "repr", |
| 61 | |
| 62 | # These are separate because they can influence the test of other methods. |
| 63 | # "getattr", |
| 64 | # "setattr", |
| 65 | # "delattr", |
| 66 | |
| 67 | class AllTests: |
| 68 | def __coerce__(self, *args): |
| 69 | print "__coerce__:", args |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 70 | return (self,) + args |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 71 | |
| 72 | def __hash__(self, *args): |
| 73 | print "__hash__:", args |
Trent Mick | d68d0a6 | 2000-10-04 17:50:59 +0000 | [diff] [blame] | 74 | return hash(id(self)) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 75 | |
| 76 | def __str__(self, *args): |
| 77 | print "__str__:", args |
| 78 | return "AllTests" |
| 79 | |
| 80 | def __repr__(self, *args): |
| 81 | print "__repr__:", args |
| 82 | return "AllTests" |
| 83 | |
| 84 | def __cmp__(self, *args): |
| 85 | print "__cmp__:", args |
| 86 | return 0 |
| 87 | |
Barry Warsaw | 07d8d64 | 2001-08-20 20:29:07 +0000 | [diff] [blame] | 88 | def __del__(self, *args): |
| 89 | print "__del__:", args |
| 90 | |
Tim Peters | 0170521 | 2001-12-11 19:28:47 +0000 | [diff] [blame] | 91 | # Synthesize AllTests methods from the names in testmeths. |
| 92 | |
| 93 | method_template = """\ |
| 94 | def __%(method)s__(self, *args): |
| 95 | print "__%(method)s__:", args |
| 96 | """ |
| 97 | |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 98 | for method in testmeths: |
Tim Peters | 0170521 | 2001-12-11 19:28:47 +0000 | [diff] [blame] | 99 | exec method_template % locals() in AllTests.__dict__ |
| 100 | |
| 101 | del method, method_template |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 102 | |
| 103 | # this also tests __init__ of course. |
| 104 | testme = AllTests() |
| 105 | |
| 106 | # Binary operations |
| 107 | |
| 108 | testme + 1 |
| 109 | 1 + testme |
| 110 | |
| 111 | testme - 1 |
| 112 | 1 - testme |
| 113 | |
| 114 | testme * 1 |
| 115 | 1 * testme |
| 116 | |
Tim Peters | 0170521 | 2001-12-11 19:28:47 +0000 | [diff] [blame] | 117 | if 1/2 == 0: |
| 118 | testme / 1 |
| 119 | 1 / testme |
| 120 | else: |
| 121 | # True division is in effect, so "/" doesn't map to __div__ etc; but |
| 122 | # the canned expected-output file requires that __div__ etc get called. |
| 123 | testme.__coerce__(1) |
| 124 | testme.__div__(1) |
| 125 | testme.__coerce__(1) |
| 126 | testme.__rdiv__(1) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 127 | |
| 128 | testme % 1 |
| 129 | 1 % testme |
| 130 | |
| 131 | divmod(testme,1) |
| 132 | divmod(1, testme) |
| 133 | |
| 134 | testme ** 1 |
| 135 | 1 ** testme |
| 136 | |
| 137 | testme >> 1 |
| 138 | 1 >> testme |
| 139 | |
| 140 | testme << 1 |
| 141 | 1 << testme |
| 142 | |
| 143 | testme & 1 |
| 144 | 1 & testme |
| 145 | |
| 146 | testme | 1 |
| 147 | 1 | testme |
| 148 | |
| 149 | testme ^ 1 |
| 150 | 1 ^ testme |
| 151 | |
| 152 | |
| 153 | # List/dict operations |
| 154 | |
| 155 | 1 in testme |
| 156 | |
| 157 | testme[1] |
| 158 | testme[1] = 1 |
| 159 | del testme[1] |
| 160 | |
| 161 | testme[:42] |
| 162 | testme[:42] = "The Answer" |
| 163 | del testme[:42] |
| 164 | |
| 165 | testme[2:1024:10] |
| 166 | testme[2:1024:10] = "A lot" |
| 167 | del testme[2:1024:10] |
| 168 | |
| 169 | testme[:42, ..., :24:, 24, 100] |
| 170 | testme[:42, ..., :24:, 24, 100] = "Strange" |
| 171 | del testme[:42, ..., :24:, 24, 100] |
| 172 | |
| 173 | |
| 174 | # Now remove the slice hooks to see if converting normal slices to slice |
| 175 | # object works. |
| 176 | |
| 177 | del AllTests.__getslice__ |
| 178 | del AllTests.__setslice__ |
| 179 | del AllTests.__delslice__ |
| 180 | |
Barry Warsaw | 07d8d64 | 2001-08-20 20:29:07 +0000 | [diff] [blame] | 181 | import sys |
| 182 | if sys.platform[:4] != 'java': |
| 183 | testme[:42] |
| 184 | testme[:42] = "The Answer" |
| 185 | del testme[:42] |
| 186 | else: |
| 187 | # This works under Jython, but the actual slice values are |
| 188 | # different. |
| 189 | print "__getitem__: (slice(0, 42, None),)" |
| 190 | print "__setitem__: (slice(0, 42, None), 'The Answer')" |
| 191 | print "__delitem__: (slice(0, 42, None),)" |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 192 | |
| 193 | # Unary operations |
| 194 | |
| 195 | -testme |
| 196 | +testme |
| 197 | abs(testme) |
Barry Warsaw | 07d8d64 | 2001-08-20 20:29:07 +0000 | [diff] [blame] | 198 | if sys.platform[:4] != 'java': |
| 199 | int(testme) |
| 200 | long(testme) |
| 201 | float(testme) |
| 202 | oct(testme) |
| 203 | hex(testme) |
| 204 | else: |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 205 | # Jython enforced that these methods return |
Barry Warsaw | 07d8d64 | 2001-08-20 20:29:07 +0000 | [diff] [blame] | 206 | # a value of the expected type. |
| 207 | print "__int__: ()" |
| 208 | print "__long__: ()" |
| 209 | print "__float__: ()" |
| 210 | print "__oct__: ()" |
| 211 | print "__hex__: ()" |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 212 | |
| 213 | |
| 214 | # And the rest... |
| 215 | |
| 216 | hash(testme) |
| 217 | repr(testme) |
| 218 | str(testme) |
| 219 | |
| 220 | testme == 1 |
| 221 | testme < 1 |
| 222 | testme > 1 |
| 223 | testme <> 1 |
| 224 | testme != 1 |
| 225 | 1 == testme |
| 226 | 1 < testme |
| 227 | 1 > testme |
| 228 | 1 <> testme |
| 229 | 1 != testme |
| 230 | |
| 231 | # This test has to be last (duh.) |
| 232 | |
| 233 | del testme |
Barry Warsaw | 07d8d64 | 2001-08-20 20:29:07 +0000 | [diff] [blame] | 234 | if sys.platform[:4] == 'java': |
| 235 | import java |
| 236 | java.lang.System.gc() |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 237 | |
| 238 | # Interfering tests |
| 239 | |
| 240 | class ExtraTests: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 241 | def __getattr__(self, *args): |
| 242 | print "__getattr__:", args |
| 243 | return "SomeVal" |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 244 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 245 | def __setattr__(self, *args): |
| 246 | print "__setattr__:", args |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 247 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 248 | def __delattr__(self, *args): |
| 249 | print "__delattr__:", args |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 250 | |
| 251 | testme = ExtraTests() |
| 252 | testme.spam |
| 253 | testme.eggs = "spam, spam, spam and ham" |
| 254 | del testme.cardinal |
Guido van Rossum | 2312024 | 2001-01-18 23:47:15 +0000 | [diff] [blame] | 255 | |
| 256 | |
| 257 | # Test correct errors from hash() on objects with comparisons but no __hash__ |
| 258 | |
| 259 | class C0: |
| 260 | pass |
| 261 | |
| 262 | hash(C0()) # This should work; the next two should raise TypeError |
| 263 | |
| 264 | class C1: |
| 265 | def __cmp__(self, other): return 0 |
| 266 | |
| 267 | try: hash(C1()) |
| 268 | except TypeError: pass |
| 269 | else: raise TestFailed, "hash(C1()) should raise an exception" |
| 270 | |
| 271 | class C2: |
| 272 | def __eq__(self, other): return 1 |
| 273 | |
| 274 | try: hash(C2()) |
| 275 | except TypeError: pass |
| 276 | else: raise TestFailed, "hash(C2()) should raise an exception" |
Guido van Rossum | 16b93b3 | 2002-06-13 21:32:51 +0000 | [diff] [blame] | 277 | |
| 278 | |
| 279 | # Test for SF bug 532646 |
| 280 | |
| 281 | class A: |
| 282 | pass |
| 283 | A.__call__ = A() |
| 284 | a = A() |
| 285 | try: |
| 286 | a() # This should not segfault |
| 287 | except RuntimeError: |
| 288 | pass |
| 289 | else: |
| 290 | raise TestFailed, "how could this not have overflowed the stack?" |
Guido van Rossum | 2c9590f | 2002-10-29 19:08:29 +0000 | [diff] [blame] | 291 | |
| 292 | |
| 293 | # Tests for exceptions raised in instance_getattr2(). |
| 294 | |
| 295 | def booh(self): |
| 296 | raise AttributeError, "booh" |
| 297 | |
| 298 | class A: |
| 299 | a = property(booh) |
| 300 | try: |
| 301 | A().a # Raised AttributeError: A instance has no attribute 'a' |
| 302 | except AttributeError, x: |
| 303 | if str(x) is not "booh": |
| 304 | print "attribute error for A().a got masked:", str(x) |
| 305 | |
| 306 | class E: |
| 307 | __eq__ = property(booh) |
| 308 | E() == E() # In debug mode, caused a C-level assert() to fail |
| 309 | |
| 310 | class I: |
| 311 | __init__ = property(booh) |
| 312 | try: |
| 313 | I() # In debug mode, printed XXX undetected error and raises AttributeError |
| 314 | except AttributeError, x: |
| 315 | pass |
| 316 | else: |
| 317 | print "attribute error for I.__init__ got masked" |