blob: fc61d21a22abede1392509a87d219cffb5c1a726 [file] [log] [blame]
Thomas Wouters1d75a792000-08-17 22:37:32 +00001"Test the functionality of Python classes implementing operators."
2
Georg Brandlc3255562007-08-24 19:33:53 +00003import unittest
Senthil Kumarance8e33a2010-01-08 19:04:16 +00004
Georg Brandlc3255562007-08-24 19:33:53 +00005from test import test_support
Thomas Wouters1d75a792000-08-17 22:37:32 +00006
7testmeths = [
8
9# Binary operations
10 "add",
11 "radd",
12 "sub",
13 "rsub",
14 "mul",
15 "rmul",
16 "div",
17 "rdiv",
18 "mod",
19 "rmod",
20 "divmod",
21 "rdivmod",
22 "pow",
23 "rpow",
24 "rshift",
25 "rrshift",
26 "lshift",
27 "rlshift",
28 "and",
29 "rand",
30 "or",
31 "ror",
32 "xor",
33 "rxor",
34
35# List/dict operations
36 "contains",
37 "getitem",
38 "getslice",
39 "setitem",
40 "setslice",
41 "delitem",
42 "delslice",
43
44# Unary operations
45 "neg",
46 "pos",
47 "abs",
Thomas Wouters1d75a792000-08-17 22:37:32 +000048
49# generic operations
50 "init",
Thomas Wouters1d75a792000-08-17 22:37:32 +000051 ]
52
53# These need to return something other than None
54# "coerce",
55# "hash",
56# "str",
57# "repr",
Neil Schemenauer3a313e32004-07-19 16:29:17 +000058# "int",
59# "long",
60# "float",
61# "oct",
62# "hex",
Thomas Wouters1d75a792000-08-17 22:37:32 +000063
64# These are separate because they can influence the test of other methods.
65# "getattr",
66# "setattr",
67# "delattr",
68
Georg Brandlc3255562007-08-24 19:33:53 +000069callLst = []
70def trackCall(f):
71 def track(*args, **kwargs):
72 callLst.append((f.__name__, args))
73 return f(*args, **kwargs)
74 return track
75
Thomas Wouters1d75a792000-08-17 22:37:32 +000076class AllTests:
Georg Brandlc3255562007-08-24 19:33:53 +000077 trackCall = trackCall
78
79 @trackCall
Thomas Wouters1d75a792000-08-17 22:37:32 +000080 def __coerce__(self, *args):
Fred Drake004d5e62000-10-23 17:22:08 +000081 return (self,) + args
Thomas Wouters1d75a792000-08-17 22:37:32 +000082
Georg Brandlc3255562007-08-24 19:33:53 +000083 @trackCall
Thomas Wouters1d75a792000-08-17 22:37:32 +000084 def __hash__(self, *args):
Trent Mickd68d0a62000-10-04 17:50:59 +000085 return hash(id(self))
Thomas Wouters1d75a792000-08-17 22:37:32 +000086
Georg Brandlc3255562007-08-24 19:33:53 +000087 @trackCall
Thomas Wouters1d75a792000-08-17 22:37:32 +000088 def __str__(self, *args):
Thomas Wouters1d75a792000-08-17 22:37:32 +000089 return "AllTests"
90
Georg Brandlc3255562007-08-24 19:33:53 +000091 @trackCall
Thomas Wouters1d75a792000-08-17 22:37:32 +000092 def __repr__(self, *args):
Thomas Wouters1d75a792000-08-17 22:37:32 +000093 return "AllTests"
94
Georg Brandlc3255562007-08-24 19:33:53 +000095 @trackCall
Neil Schemenauer3a313e32004-07-19 16:29:17 +000096 def __int__(self, *args):
Neil Schemenauer3a313e32004-07-19 16:29:17 +000097 return 1
98
Georg Brandlc3255562007-08-24 19:33:53 +000099 @trackCall
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000100 def __float__(self, *args):
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000101 return 1.0
102
Georg Brandlc3255562007-08-24 19:33:53 +0000103 @trackCall
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000104 def __long__(self, *args):
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000105 return 1L
106
Georg Brandlc3255562007-08-24 19:33:53 +0000107 @trackCall
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000108 def __oct__(self, *args):
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000109 return '01'
110
Georg Brandlc3255562007-08-24 19:33:53 +0000111 @trackCall
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000112 def __hex__(self, *args):
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000113 return '0x1'
114
Georg Brandlc3255562007-08-24 19:33:53 +0000115 @trackCall
Thomas Wouters1d75a792000-08-17 22:37:32 +0000116 def __cmp__(self, *args):
Thomas Wouters1d75a792000-08-17 22:37:32 +0000117 return 0
118
Georg Brandlc3255562007-08-24 19:33:53 +0000119# Synthesize all the other AllTests methods from the names in testmeths.
Tim Peters01705212001-12-11 19:28:47 +0000120
121method_template = """\
Georg Brandlc3255562007-08-24 19:33:53 +0000122@trackCall
Tim Peters01705212001-12-11 19:28:47 +0000123def __%(method)s__(self, *args):
Georg Brandlc3255562007-08-24 19:33:53 +0000124 pass
Tim Peters01705212001-12-11 19:28:47 +0000125"""
126
Thomas Wouters1d75a792000-08-17 22:37:32 +0000127for method in testmeths:
Tim Peters01705212001-12-11 19:28:47 +0000128 exec method_template % locals() in AllTests.__dict__
129
130del method, method_template
Thomas Wouters1d75a792000-08-17 22:37:32 +0000131
Georg Brandlc3255562007-08-24 19:33:53 +0000132class ClassTests(unittest.TestCase):
133 def setUp(self):
134 callLst[:] = []
Thomas Wouters1d75a792000-08-17 22:37:32 +0000135
Georg Brandlc3255562007-08-24 19:33:53 +0000136 def assertCallStack(self, expected_calls):
137 actualCallList = callLst[:] # need to copy because the comparison below will add
138 # additional calls to callLst
139 if expected_calls != actualCallList:
140 self.fail("Expected call list:\n %s\ndoes not match actual call list\n %s" %
141 (expected_calls, actualCallList))
Thomas Wouters1d75a792000-08-17 22:37:32 +0000142
Georg Brandlc3255562007-08-24 19:33:53 +0000143 def testInit(self):
144 foo = AllTests()
145 self.assertCallStack([("__init__", (foo,))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000146
Georg Brandlc3255562007-08-24 19:33:53 +0000147 def testBinaryOps(self):
148 testme = AllTests()
149 # Binary operations
Thomas Wouters1d75a792000-08-17 22:37:32 +0000150
Georg Brandlc3255562007-08-24 19:33:53 +0000151 callLst[:] = []
152 testme + 1
153 self.assertCallStack([("__coerce__", (testme, 1)), ("__add__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000154
Georg Brandlc3255562007-08-24 19:33:53 +0000155 callLst[:] = []
156 1 + testme
157 self.assertCallStack([("__coerce__", (testme, 1)), ("__radd__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000158
Georg Brandlc3255562007-08-24 19:33:53 +0000159 callLst[:] = []
160 testme - 1
161 self.assertCallStack([("__coerce__", (testme, 1)), ("__sub__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000162
Georg Brandlc3255562007-08-24 19:33:53 +0000163 callLst[:] = []
164 1 - testme
165 self.assertCallStack([("__coerce__", (testme, 1)), ("__rsub__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000166
Georg Brandlc3255562007-08-24 19:33:53 +0000167 callLst[:] = []
168 testme * 1
169 self.assertCallStack([("__coerce__", (testme, 1)), ("__mul__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000170
Georg Brandlc3255562007-08-24 19:33:53 +0000171 callLst[:] = []
172 1 * testme
173 self.assertCallStack([("__coerce__", (testme, 1)), ("__rmul__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000174
Georg Brandlc3255562007-08-24 19:33:53 +0000175 if 1/2 == 0:
176 callLst[:] = []
177 testme / 1
178 self.assertCallStack([("__coerce__", (testme, 1)), ("__div__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000179
Thomas Wouters1d75a792000-08-17 22:37:32 +0000180
Georg Brandlc3255562007-08-24 19:33:53 +0000181 callLst[:] = []
182 1 / testme
183 self.assertCallStack([("__coerce__", (testme, 1)), ("__rdiv__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000184
Georg Brandlc3255562007-08-24 19:33:53 +0000185 callLst[:] = []
186 testme % 1
187 self.assertCallStack([("__coerce__", (testme, 1)), ("__mod__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000188
Georg Brandlc3255562007-08-24 19:33:53 +0000189 callLst[:] = []
190 1 % testme
191 self.assertCallStack([("__coerce__", (testme, 1)), ("__rmod__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000192
Thomas Wouters1d75a792000-08-17 22:37:32 +0000193
Georg Brandlc3255562007-08-24 19:33:53 +0000194 callLst[:] = []
195 divmod(testme,1)
196 self.assertCallStack([("__coerce__", (testme, 1)), ("__divmod__", (testme, 1))])
Martin v. Löwis3a624042006-11-08 06:46:37 +0000197
Georg Brandlc3255562007-08-24 19:33:53 +0000198 callLst[:] = []
199 divmod(1, testme)
200 self.assertCallStack([("__coerce__", (testme, 1)), ("__rdivmod__", (testme, 1))])
Martin v. Löwis3a624042006-11-08 06:46:37 +0000201
Georg Brandlc3255562007-08-24 19:33:53 +0000202 callLst[:] = []
203 testme ** 1
204 self.assertCallStack([("__coerce__", (testme, 1)), ("__pow__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000205
Georg Brandlc3255562007-08-24 19:33:53 +0000206 callLst[:] = []
207 1 ** testme
208 self.assertCallStack([("__coerce__", (testme, 1)), ("__rpow__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000209
Georg Brandlc3255562007-08-24 19:33:53 +0000210 callLst[:] = []
211 testme >> 1
212 self.assertCallStack([("__coerce__", (testme, 1)), ("__rshift__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000213
Georg Brandlc3255562007-08-24 19:33:53 +0000214 callLst[:] = []
215 1 >> testme
216 self.assertCallStack([("__coerce__", (testme, 1)), ("__rrshift__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000217
Georg Brandlc3255562007-08-24 19:33:53 +0000218 callLst[:] = []
219 testme << 1
220 self.assertCallStack([("__coerce__", (testme, 1)), ("__lshift__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000221
Georg Brandlc3255562007-08-24 19:33:53 +0000222 callLst[:] = []
223 1 << testme
224 self.assertCallStack([("__coerce__", (testme, 1)), ("__rlshift__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000225
Georg Brandlc3255562007-08-24 19:33:53 +0000226 callLst[:] = []
227 testme & 1
228 self.assertCallStack([("__coerce__", (testme, 1)), ("__and__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000229
Georg Brandlc3255562007-08-24 19:33:53 +0000230 callLst[:] = []
231 1 & testme
232 self.assertCallStack([("__coerce__", (testme, 1)), ("__rand__", (testme, 1))])
233
234 callLst[:] = []
235 testme | 1
236 self.assertCallStack([("__coerce__", (testme, 1)), ("__or__", (testme, 1))])
237
238 callLst[:] = []
239 1 | testme
240 self.assertCallStack([("__coerce__", (testme, 1)), ("__ror__", (testme, 1))])
241
242 callLst[:] = []
243 testme ^ 1
244 self.assertCallStack([("__coerce__", (testme, 1)), ("__xor__", (testme, 1))])
245
246 callLst[:] = []
247 1 ^ testme
248 self.assertCallStack([("__coerce__", (testme, 1)), ("__rxor__", (testme, 1))])
249
250 def testListAndDictOps(self):
251 testme = AllTests()
252
253 # List/dict operations
254
255 class Empty: pass
256
257 try:
258 1 in Empty()
259 self.fail('failed, should have raised TypeError')
260 except TypeError:
261 pass
262
263 callLst[:] = []
264 1 in testme
265 self.assertCallStack([('__contains__', (testme, 1))])
266
267 callLst[:] = []
268 testme[1]
269 self.assertCallStack([('__getitem__', (testme, 1))])
270
271 callLst[:] = []
272 testme[1] = 1
273 self.assertCallStack([('__setitem__', (testme, 1, 1))])
274
275 callLst[:] = []
276 del testme[1]
277 self.assertCallStack([('__delitem__', (testme, 1))])
278
279 callLst[:] = []
280 testme[:42]
281 self.assertCallStack([('__getslice__', (testme, 0, 42))])
282
283 callLst[:] = []
284 testme[:42] = "The Answer"
285 self.assertCallStack([('__setslice__', (testme, 0, 42, "The Answer"))])
286
287 callLst[:] = []
288 del testme[:42]
289 self.assertCallStack([('__delslice__', (testme, 0, 42))])
290
291 callLst[:] = []
292 testme[2:1024:10]
293 self.assertCallStack([('__getitem__', (testme, slice(2, 1024, 10)))])
294
295 callLst[:] = []
296 testme[2:1024:10] = "A lot"
297 self.assertCallStack([('__setitem__', (testme, slice(2, 1024, 10),
298 "A lot"))])
299 callLst[:] = []
300 del testme[2:1024:10]
301 self.assertCallStack([('__delitem__', (testme, slice(2, 1024, 10)))])
302
303 callLst[:] = []
304 testme[:42, ..., :24:, 24, 100]
305 self.assertCallStack([('__getitem__', (testme, (slice(None, 42, None),
306 Ellipsis,
307 slice(None, 24, None),
308 24, 100)))])
309 callLst[:] = []
310 testme[:42, ..., :24:, 24, 100] = "Strange"
311 self.assertCallStack([('__setitem__', (testme, (slice(None, 42, None),
312 Ellipsis,
313 slice(None, 24, None),
314 24, 100), "Strange"))])
315 callLst[:] = []
316 del testme[:42, ..., :24:, 24, 100]
317 self.assertCallStack([('__delitem__', (testme, (slice(None, 42, None),
318 Ellipsis,
319 slice(None, 24, None),
320 24, 100)))])
321
322 # Now remove the slice hooks to see if converting normal slices to
323 # slice object works.
324
325 getslice = AllTests.__getslice__
326 del AllTests.__getslice__
327 setslice = AllTests.__setslice__
328 del AllTests.__setslice__
329 delslice = AllTests.__delslice__
330 del AllTests.__delslice__
331
332 # XXX when using new-style classes the slice testme[:42] produces
333 # slice(None, 42, None) instead of slice(0, 42, None). py3k will have
334 # to change this test.
335 callLst[:] = []
336 testme[:42]
337 self.assertCallStack([('__getitem__', (testme, slice(0, 42, None)))])
338
339 callLst[:] = []
340 testme[:42] = "The Answer"
341 self.assertCallStack([('__setitem__', (testme, slice(0, 42, None),
342 "The Answer"))])
343 callLst[:] = []
344 del testme[:42]
345 self.assertCallStack([('__delitem__', (testme, slice(0, 42, None)))])
346
347 # Restore the slice methods, or the tests will fail with regrtest -R.
348 AllTests.__getslice__ = getslice
349 AllTests.__setslice__ = setslice
350 AllTests.__delslice__ = delslice
351
352
353 def testUnaryOps(self):
354 testme = AllTests()
355
356 callLst[:] = []
357 -testme
358 self.assertCallStack([('__neg__', (testme,))])
359 callLst[:] = []
360 +testme
361 self.assertCallStack([('__pos__', (testme,))])
362 callLst[:] = []
363 abs(testme)
364 self.assertCallStack([('__abs__', (testme,))])
365 callLst[:] = []
366 int(testme)
367 self.assertCallStack([('__int__', (testme,))])
368 callLst[:] = []
369 long(testme)
370 self.assertCallStack([('__long__', (testme,))])
371 callLst[:] = []
372 float(testme)
373 self.assertCallStack([('__float__', (testme,))])
374 callLst[:] = []
375 oct(testme)
376 self.assertCallStack([('__oct__', (testme,))])
377 callLst[:] = []
378 hex(testme)
379 self.assertCallStack([('__hex__', (testme,))])
380
381
382 def testMisc(self):
383 testme = AllTests()
384
385 callLst[:] = []
386 hash(testme)
387 self.assertCallStack([('__hash__', (testme,))])
388
389 callLst[:] = []
390 repr(testme)
391 self.assertCallStack([('__repr__', (testme,))])
392
393 callLst[:] = []
394 str(testme)
395 self.assertCallStack([('__str__', (testme,))])
396
397 callLst[:] = []
398 testme == 1
399 self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (testme, 1))])
400
401 callLst[:] = []
402 testme < 1
403 self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (testme, 1))])
404
405 callLst[:] = []
406 testme > 1
407 self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (testme, 1))])
408
409 callLst[:] = []
Senthil Kumarance8e33a2010-01-08 19:04:16 +0000410 testme <> 1 # XXX kill this in py3k
Georg Brandlc3255562007-08-24 19:33:53 +0000411 self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (testme, 1))])
412
413 callLst[:] = []
414 testme != 1
415 self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (testme, 1))])
416
417 callLst[:] = []
418 1 == testme
419 self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (1, testme))])
420
421 callLst[:] = []
422 1 < testme
423 self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (1, testme))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000424
Georg Brandlc3255562007-08-24 19:33:53 +0000425 callLst[:] = []
426 1 > testme
427 self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (1, testme))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000428
Georg Brandlc3255562007-08-24 19:33:53 +0000429 callLst[:] = []
Senthil Kumarance8e33a2010-01-08 19:04:16 +0000430 1 <> testme
Georg Brandlc3255562007-08-24 19:33:53 +0000431 self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (1, testme))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000432
Georg Brandlc3255562007-08-24 19:33:53 +0000433 callLst[:] = []
434 1 != testme
435 self.assertCallStack([("__coerce__", (testme, 1)), ('__cmp__', (1, testme))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000436
Thomas Wouters1d75a792000-08-17 22:37:32 +0000437
Georg Brandlc3255562007-08-24 19:33:53 +0000438 def testGetSetAndDel(self):
439 # Interfering tests
440 class ExtraTests(AllTests):
441 @trackCall
442 def __getattr__(self, *args):
443 return "SomeVal"
Thomas Wouters1d75a792000-08-17 22:37:32 +0000444
Georg Brandlc3255562007-08-24 19:33:53 +0000445 @trackCall
446 def __setattr__(self, *args):
447 pass
Thomas Wouters1d75a792000-08-17 22:37:32 +0000448
Georg Brandlc3255562007-08-24 19:33:53 +0000449 @trackCall
450 def __delattr__(self, *args):
451 pass
Thomas Wouters1d75a792000-08-17 22:37:32 +0000452
Georg Brandlc3255562007-08-24 19:33:53 +0000453 testme = ExtraTests()
Thomas Wouters1d75a792000-08-17 22:37:32 +0000454
Georg Brandlc3255562007-08-24 19:33:53 +0000455 callLst[:] = []
456 testme.spam
457 self.assertCallStack([('__getattr__', (testme, "spam"))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000458
Georg Brandlc3255562007-08-24 19:33:53 +0000459 callLst[:] = []
460 testme.eggs = "spam, spam, spam and ham"
461 self.assertCallStack([('__setattr__', (testme, "eggs",
462 "spam, spam, spam and ham"))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000463
Georg Brandlc3255562007-08-24 19:33:53 +0000464 callLst[:] = []
465 del testme.cardinal
466 self.assertCallStack([('__delattr__', (testme, "cardinal"))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000467
Georg Brandlc3255562007-08-24 19:33:53 +0000468 def testDel(self):
469 x = []
Thomas Wouters1d75a792000-08-17 22:37:32 +0000470
Georg Brandlc3255562007-08-24 19:33:53 +0000471 class DelTest:
472 def __del__(self):
473 x.append("crab people, crab people")
474 testme = DelTest()
475 del testme
476 import gc
477 gc.collect()
478 self.assertEquals(["crab people, crab people"], x)
Guido van Rossum23120242001-01-18 23:47:15 +0000479
Georg Brandlc3255562007-08-24 19:33:53 +0000480 def testBadTypeReturned(self):
481 # return values of some method are type-checked
482 class BadTypeClass:
483 def __int__(self):
484 return None
485 __float__ = __int__
486 __long__ = __int__
487 __str__ = __int__
488 __repr__ = __int__
489 __oct__ = __int__
490 __hex__ = __int__
Guido van Rossum23120242001-01-18 23:47:15 +0000491
Georg Brandlc3255562007-08-24 19:33:53 +0000492 for f in [int, float, long, str, repr, oct, hex]:
493 self.assertRaises(TypeError, f, BadTypeClass())
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000494
Georg Brandlc3255562007-08-24 19:33:53 +0000495 def testMixIntsAndLongs(self):
496 # mixing up ints and longs is okay
497 class IntLongMixClass:
498 @trackCall
499 def __int__(self):
500 return 42L
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000501
Georg Brandlc3255562007-08-24 19:33:53 +0000502 @trackCall
503 def __long__(self):
504 return 64
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000505
Georg Brandlc3255562007-08-24 19:33:53 +0000506 mixIntAndLong = IntLongMixClass()
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000507
Georg Brandlc3255562007-08-24 19:33:53 +0000508 callLst[:] = []
509 as_int = int(mixIntAndLong)
510 self.assertEquals(type(as_int), long)
511 self.assertEquals(as_int, 42L)
512 self.assertCallStack([('__int__', (mixIntAndLong,))])
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000513
Georg Brandlc3255562007-08-24 19:33:53 +0000514 callLst[:] = []
515 as_long = long(mixIntAndLong)
516 self.assertEquals(type(as_long), int)
517 self.assertEquals(as_long, 64)
518 self.assertCallStack([('__long__', (mixIntAndLong,))])
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000519
Georg Brandlc3255562007-08-24 19:33:53 +0000520 def testHashStuff(self):
521 # Test correct errors from hash() on objects with comparisons but
522 # no __hash__
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000523
Georg Brandlc3255562007-08-24 19:33:53 +0000524 class C0:
525 pass
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000526
Georg Brandlc3255562007-08-24 19:33:53 +0000527 hash(C0()) # This should work; the next two should raise TypeError
Guido van Rossum23120242001-01-18 23:47:15 +0000528
Georg Brandlc3255562007-08-24 19:33:53 +0000529 class C1:
530 def __cmp__(self, other): return 0
Guido van Rossum23120242001-01-18 23:47:15 +0000531
Georg Brandlc3255562007-08-24 19:33:53 +0000532 self.assertRaises(TypeError, hash, C1())
Guido van Rossum23120242001-01-18 23:47:15 +0000533
Georg Brandlc3255562007-08-24 19:33:53 +0000534 class C2:
535 def __eq__(self, other): return 1
Guido van Rossum23120242001-01-18 23:47:15 +0000536
Georg Brandlc3255562007-08-24 19:33:53 +0000537 self.assertRaises(TypeError, hash, C2())
Guido van Rossum23120242001-01-18 23:47:15 +0000538
Guido van Rossum23120242001-01-18 23:47:15 +0000539
Georg Brandlc3255562007-08-24 19:33:53 +0000540 def testSFBug532646(self):
541 # Test for SF bug 532646
Guido van Rossum16b93b32002-06-13 21:32:51 +0000542
Georg Brandlc3255562007-08-24 19:33:53 +0000543 class A:
544 pass
545 A.__call__ = A()
546 a = A()
Guido van Rossum16b93b32002-06-13 21:32:51 +0000547
Georg Brandlc3255562007-08-24 19:33:53 +0000548 try:
549 a() # This should not segfault
550 except RuntimeError:
551 pass
552 else:
553 self.fail("Failed to raise RuntimeError")
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000554
Georg Brandlc3255562007-08-24 19:33:53 +0000555 def testForExceptionsRaisedInInstanceGetattr2(self):
556 # Tests for exceptions raised in instance_getattr2().
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000557
Georg Brandlc3255562007-08-24 19:33:53 +0000558 def booh(self):
559 raise AttributeError("booh")
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000560
Georg Brandlc3255562007-08-24 19:33:53 +0000561 class A:
562 a = property(booh)
563 try:
564 A().a # Raised AttributeError: A instance has no attribute 'a'
565 except AttributeError, x:
566 if str(x) != "booh":
567 self.fail("attribute error for A().a got masked: %s" % x)
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000568
Georg Brandlc3255562007-08-24 19:33:53 +0000569 class E:
570 __eq__ = property(booh)
571 E() == E() # In debug mode, caused a C-level assert() to fail
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000572
Georg Brandlc3255562007-08-24 19:33:53 +0000573 class I:
574 __init__ = property(booh)
575 try:
576 # In debug mode, printed XXX undetected error and
577 # raises AttributeError
578 I()
579 except AttributeError, x:
580 pass
581 else:
582 self.fail("attribute error for I.__init__ got masked")
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000583
Georg Brandlc3255562007-08-24 19:33:53 +0000584 def testHashComparisonOfMethods(self):
585 # Test comparison and hash of methods
586 class A:
587 def __init__(self, x):
588 self.x = x
589 def f(self):
590 pass
591 def g(self):
592 pass
593 def __eq__(self, other):
594 return self.x == other.x
595 def __hash__(self):
596 return self.x
597 class B(A):
598 pass
Armin Rigofd01d792006-06-08 10:56:24 +0000599
Georg Brandlc3255562007-08-24 19:33:53 +0000600 a1 = A(1)
601 a2 = A(2)
602 self.assertEquals(a1.f, a1.f)
603 self.assertNotEquals(a1.f, a2.f)
604 self.assertNotEquals(a1.f, a1.g)
605 self.assertEquals(a1.f, A(1).f)
606 self.assertEquals(hash(a1.f), hash(a1.f))
607 self.assertEquals(hash(a1.f), hash(A(1).f))
Armin Rigofd01d792006-06-08 10:56:24 +0000608
Georg Brandlc3255562007-08-24 19:33:53 +0000609 self.assertNotEquals(A.f, a1.f)
610 self.assertNotEquals(A.f, A.g)
611 self.assertEquals(B.f, A.f)
612 self.assertEquals(hash(B.f), hash(A.f))
Armin Rigofd01d792006-06-08 10:56:24 +0000613
Georg Brandlc3255562007-08-24 19:33:53 +0000614 # the following triggers a SystemError in 2.4
615 a = A(hash(A.f.im_func)^(-1))
616 hash(a.f)
Armin Rigofd01d792006-06-08 10:56:24 +0000617
Georg Brandlc3255562007-08-24 19:33:53 +0000618def test_main():
Senthil Kumarance8e33a2010-01-08 19:04:16 +0000619 test_support.run_unittest(ClassTests)
Armin Rigofd01d792006-06-08 10:56:24 +0000620
Georg Brandlc3255562007-08-24 19:33:53 +0000621if __name__=='__main__':
622 test_main()