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", |
Neal Norwitz | bcc0db8 | 2006-03-24 08:14:36 +0000 | [diff] [blame] | 14 | "truediv", |
| 15 | "rtruediv", |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 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", |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 46 | |
| 47 | # generic operations |
| 48 | "init", |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 49 | ] |
| 50 | |
| 51 | # These need to return something other than None |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 52 | # "hash", |
| 53 | # "str", |
| 54 | # "repr", |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 55 | # "int", |
| 56 | # "long", |
| 57 | # "float", |
| 58 | # "oct", |
| 59 | # "hex", |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 60 | |
| 61 | # These are separate because they can influence the test of other methods. |
| 62 | # "getattr", |
| 63 | # "setattr", |
| 64 | # "delattr", |
| 65 | |
| 66 | class AllTests: |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 67 | def __hash__(self, *args): |
| 68 | print "__hash__:", args |
Trent Mick | d68d0a6 | 2000-10-04 17:50:59 +0000 | [diff] [blame] | 69 | return hash(id(self)) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 70 | |
| 71 | def __str__(self, *args): |
| 72 | print "__str__:", args |
| 73 | return "AllTests" |
| 74 | |
| 75 | def __repr__(self, *args): |
| 76 | print "__repr__:", args |
| 77 | return "AllTests" |
| 78 | |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 79 | def __int__(self, *args): |
| 80 | print "__int__:", args |
| 81 | return 1 |
| 82 | |
| 83 | def __float__(self, *args): |
| 84 | print "__float__:", args |
| 85 | return 1.0 |
| 86 | |
| 87 | def __long__(self, *args): |
| 88 | print "__long__:", args |
| 89 | return 1L |
| 90 | |
| 91 | def __oct__(self, *args): |
| 92 | print "__oct__:", args |
| 93 | return '01' |
| 94 | |
| 95 | def __hex__(self, *args): |
| 96 | print "__hex__:", args |
| 97 | return '0x1' |
| 98 | |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 99 | def __cmp__(self, *args): |
| 100 | print "__cmp__:", args |
| 101 | return 0 |
| 102 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 103 | def __eq__(self, *args): |
| 104 | print "__eq__:", args |
| 105 | return True |
| 106 | |
| 107 | def __ne__(self, *args): |
| 108 | print "__ne__:", args |
| 109 | return False |
| 110 | |
| 111 | def __lt__(self, *args): |
| 112 | print "__lt__:", args |
| 113 | return False |
| 114 | |
| 115 | def __le__(self, *args): |
| 116 | print "__le__:", args |
| 117 | return True |
| 118 | |
| 119 | def __gt__(self, *args): |
| 120 | print "__gt__:", args |
| 121 | return False |
| 122 | |
| 123 | def __ge__(self, *args): |
| 124 | print "__ge__:", args |
| 125 | return True |
| 126 | |
Barry Warsaw | 07d8d64 | 2001-08-20 20:29:07 +0000 | [diff] [blame] | 127 | def __del__(self, *args): |
| 128 | print "__del__:", args |
| 129 | |
Tim Peters | 0170521 | 2001-12-11 19:28:47 +0000 | [diff] [blame] | 130 | # Synthesize AllTests methods from the names in testmeths. |
| 131 | |
| 132 | method_template = """\ |
| 133 | def __%(method)s__(self, *args): |
| 134 | print "__%(method)s__:", args |
| 135 | """ |
| 136 | |
Thomas Wouters | 4cdada9 | 2006-04-15 09:19:16 +0000 | [diff] [blame] | 137 | d = {} |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 138 | for method in testmeths: |
Thomas Wouters | 4cdada9 | 2006-04-15 09:19:16 +0000 | [diff] [blame] | 139 | exec method_template % locals() in d |
| 140 | for k in d: |
| 141 | setattr(AllTests, k, d[k]) |
| 142 | del d, k |
Tim Peters | 0170521 | 2001-12-11 19:28:47 +0000 | [diff] [blame] | 143 | del method, method_template |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 144 | |
| 145 | # this also tests __init__ of course. |
| 146 | testme = AllTests() |
| 147 | |
| 148 | # Binary operations |
| 149 | |
| 150 | testme + 1 |
| 151 | 1 + testme |
| 152 | |
| 153 | testme - 1 |
| 154 | 1 - testme |
| 155 | |
| 156 | testme * 1 |
| 157 | 1 * testme |
| 158 | |
Neal Norwitz | bcc0db8 | 2006-03-24 08:14:36 +0000 | [diff] [blame] | 159 | testme / 1 |
| 160 | 1 / testme |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 161 | |
| 162 | testme % 1 |
| 163 | 1 % testme |
| 164 | |
| 165 | divmod(testme,1) |
| 166 | divmod(1, testme) |
| 167 | |
| 168 | testme ** 1 |
| 169 | 1 ** testme |
| 170 | |
| 171 | testme >> 1 |
| 172 | 1 >> testme |
| 173 | |
| 174 | testme << 1 |
| 175 | 1 << testme |
| 176 | |
| 177 | testme & 1 |
| 178 | 1 & testme |
| 179 | |
| 180 | testme | 1 |
| 181 | 1 | testme |
| 182 | |
| 183 | testme ^ 1 |
| 184 | 1 ^ testme |
| 185 | |
| 186 | |
| 187 | # List/dict operations |
| 188 | |
| 189 | 1 in testme |
| 190 | |
| 191 | testme[1] |
| 192 | testme[1] = 1 |
| 193 | del testme[1] |
| 194 | |
| 195 | testme[:42] |
| 196 | testme[:42] = "The Answer" |
| 197 | del testme[:42] |
| 198 | |
| 199 | testme[2:1024:10] |
| 200 | testme[2:1024:10] = "A lot" |
| 201 | del testme[2:1024:10] |
| 202 | |
| 203 | testme[:42, ..., :24:, 24, 100] |
| 204 | testme[:42, ..., :24:, 24, 100] = "Strange" |
| 205 | del testme[:42, ..., :24:, 24, 100] |
| 206 | |
| 207 | |
| 208 | # Now remove the slice hooks to see if converting normal slices to slice |
| 209 | # object works. |
| 210 | |
| 211 | del AllTests.__getslice__ |
| 212 | del AllTests.__setslice__ |
| 213 | del AllTests.__delslice__ |
| 214 | |
Barry Warsaw | 07d8d64 | 2001-08-20 20:29:07 +0000 | [diff] [blame] | 215 | import sys |
| 216 | if sys.platform[:4] != 'java': |
| 217 | testme[:42] |
| 218 | testme[:42] = "The Answer" |
| 219 | del testme[:42] |
| 220 | else: |
| 221 | # This works under Jython, but the actual slice values are |
| 222 | # different. |
| 223 | print "__getitem__: (slice(0, 42, None),)" |
| 224 | print "__setitem__: (slice(0, 42, None), 'The Answer')" |
| 225 | print "__delitem__: (slice(0, 42, None),)" |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 226 | |
| 227 | # Unary operations |
| 228 | |
| 229 | -testme |
| 230 | +testme |
| 231 | abs(testme) |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 232 | int(testme) |
| 233 | long(testme) |
| 234 | float(testme) |
| 235 | oct(testme) |
| 236 | hex(testme) |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 237 | |
| 238 | # And the rest... |
| 239 | |
| 240 | hash(testme) |
| 241 | repr(testme) |
| 242 | str(testme) |
| 243 | |
| 244 | testme == 1 |
| 245 | testme < 1 |
| 246 | testme > 1 |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 247 | testme != 1 |
| 248 | 1 == testme |
| 249 | 1 < testme |
| 250 | 1 > testme |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 251 | 1 != testme |
| 252 | |
| 253 | # This test has to be last (duh.) |
| 254 | |
| 255 | del testme |
Barry Warsaw | 07d8d64 | 2001-08-20 20:29:07 +0000 | [diff] [blame] | 256 | if sys.platform[:4] == 'java': |
| 257 | import java |
| 258 | java.lang.System.gc() |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 259 | |
| 260 | # Interfering tests |
| 261 | |
| 262 | class ExtraTests: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 263 | def __getattr__(self, *args): |
| 264 | print "__getattr__:", args |
| 265 | return "SomeVal" |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 266 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 267 | def __setattr__(self, *args): |
| 268 | print "__setattr__:", args |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 269 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 270 | def __delattr__(self, *args): |
| 271 | print "__delattr__:", args |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 272 | |
| 273 | testme = ExtraTests() |
| 274 | testme.spam |
| 275 | testme.eggs = "spam, spam, spam and ham" |
| 276 | del testme.cardinal |
Guido van Rossum | 2312024 | 2001-01-18 23:47:15 +0000 | [diff] [blame] | 277 | |
| 278 | |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 279 | # return values of some method are type-checked |
| 280 | class BadTypeClass: |
| 281 | def __int__(self): |
| 282 | return None |
| 283 | __float__ = __int__ |
| 284 | __long__ = __int__ |
| 285 | __str__ = __int__ |
| 286 | __repr__ = __int__ |
| 287 | __oct__ = __int__ |
| 288 | __hex__ = __int__ |
| 289 | |
| 290 | def check_exc(stmt, exception): |
| 291 | """Raise TestFailed if executing 'stmt' does not raise 'exception' |
| 292 | """ |
| 293 | try: |
| 294 | exec stmt |
| 295 | except exception: |
| 296 | pass |
| 297 | else: |
| 298 | raise TestFailed, "%s should raise %s" % (stmt, exception) |
| 299 | |
| 300 | check_exc("int(BadTypeClass())", TypeError) |
| 301 | check_exc("float(BadTypeClass())", TypeError) |
| 302 | check_exc("long(BadTypeClass())", TypeError) |
| 303 | check_exc("str(BadTypeClass())", TypeError) |
| 304 | check_exc("repr(BadTypeClass())", TypeError) |
| 305 | check_exc("oct(BadTypeClass())", TypeError) |
| 306 | check_exc("hex(BadTypeClass())", TypeError) |
| 307 | |
| 308 | # mixing up ints and longs is okay |
| 309 | class IntLongMixClass: |
| 310 | def __int__(self): |
| 311 | return 0L |
| 312 | |
| 313 | def __long__(self): |
| 314 | return 0 |
| 315 | |
| 316 | try: |
| 317 | int(IntLongMixClass()) |
| 318 | except TypeError: |
| 319 | raise TestFailed, "TypeError should not be raised" |
| 320 | |
| 321 | try: |
| 322 | long(IntLongMixClass()) |
| 323 | except TypeError: |
| 324 | raise TestFailed, "TypeError should not be raised" |
| 325 | |
| 326 | |
Guido van Rossum | 2312024 | 2001-01-18 23:47:15 +0000 | [diff] [blame] | 327 | # Test correct errors from hash() on objects with comparisons but no __hash__ |
| 328 | |
| 329 | class C0: |
| 330 | pass |
| 331 | |
| 332 | hash(C0()) # This should work; the next two should raise TypeError |
| 333 | |
| 334 | class C1: |
| 335 | def __cmp__(self, other): return 0 |
| 336 | |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 337 | check_exc("hash(C1())", TypeError) |
Guido van Rossum | 2312024 | 2001-01-18 23:47:15 +0000 | [diff] [blame] | 338 | |
| 339 | class C2: |
| 340 | def __eq__(self, other): return 1 |
| 341 | |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 342 | check_exc("hash(C2())", TypeError) |
Guido van Rossum | 16b93b3 | 2002-06-13 21:32:51 +0000 | [diff] [blame] | 343 | |
| 344 | # Test for SF bug 532646 |
| 345 | |
| 346 | class A: |
| 347 | pass |
| 348 | A.__call__ = A() |
| 349 | a = A() |
| 350 | try: |
| 351 | a() # This should not segfault |
| 352 | except RuntimeError: |
| 353 | pass |
| 354 | else: |
| 355 | raise TestFailed, "how could this not have overflowed the stack?" |
Guido van Rossum | 2c9590f | 2002-10-29 19:08:29 +0000 | [diff] [blame] | 356 | |
| 357 | |
| 358 | # Tests for exceptions raised in instance_getattr2(). |
| 359 | |
| 360 | def booh(self): |
| 361 | raise AttributeError, "booh" |
| 362 | |
| 363 | class A: |
| 364 | a = property(booh) |
| 365 | try: |
| 366 | A().a # Raised AttributeError: A instance has no attribute 'a' |
| 367 | except AttributeError, x: |
Michael W. Hudson | abb103b | 2005-05-04 11:59:38 +0000 | [diff] [blame] | 368 | if str(x) != "booh": |
Guido van Rossum | 2c9590f | 2002-10-29 19:08:29 +0000 | [diff] [blame] | 369 | print "attribute error for A().a got masked:", str(x) |
| 370 | |
| 371 | class E: |
| 372 | __eq__ = property(booh) |
| 373 | E() == E() # In debug mode, caused a C-level assert() to fail |
| 374 | |
| 375 | class I: |
| 376 | __init__ = property(booh) |
| 377 | try: |
| 378 | I() # In debug mode, printed XXX undetected error and raises AttributeError |
| 379 | except AttributeError, x: |
| 380 | pass |
| 381 | else: |
| 382 | print "attribute error for I.__init__ got masked" |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 383 | |
| 384 | |
| 385 | # Test comparison and hash of methods |
| 386 | class A: |
| 387 | def __init__(self, x): |
| 388 | self.x = x |
| 389 | def f(self): |
| 390 | pass |
| 391 | def g(self): |
| 392 | pass |
| 393 | def __eq__(self, other): |
| 394 | return self.x == other.x |
| 395 | def __hash__(self): |
| 396 | return self.x |
| 397 | class B(A): |
| 398 | pass |
| 399 | |
| 400 | a1 = A(1) |
| 401 | a2 = A(2) |
| 402 | assert a1.f == a1.f |
| 403 | assert a1.f != a2.f |
| 404 | assert a1.f != a1.g |
| 405 | assert a1.f == A(1).f |
| 406 | assert hash(a1.f) == hash(a1.f) |
| 407 | assert hash(a1.f) == hash(A(1).f) |
| 408 | |
| 409 | assert A.f != a1.f |
| 410 | assert A.f != A.g |
| 411 | assert B.f == A.f |
| 412 | assert hash(B.f) == hash(A.f) |
| 413 | |
| 414 | # the following triggers a SystemError in 2.4 |
| 415 | a = A(hash(A.f.im_func)^(-1)) |
| 416 | hash(a.f) |