blob: ded3f1a8994b44aa7caa94bb4c1ccb7c996aa122 [file] [log] [blame]
Thomas Wouters1d75a792000-08-17 22:37:32 +00001"Test the functionality of Python classes implementing operators."
2
Barry Warsaw408b6d32002-07-30 23:27:12 +00003from test.test_support import TestFailed
Thomas Wouters1d75a792000-08-17 22:37:32 +00004
5testmeths = [
6
7# Binary operations
8 "add",
9 "radd",
10 "sub",
11 "rsub",
12 "mul",
13 "rmul",
Neal Norwitzbcc0db82006-03-24 08:14:36 +000014 "truediv",
15 "rtruediv",
Thomas Wouters1d75a792000-08-17 22:37:32 +000016 "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 Wouters1d75a792000-08-17 22:37:32 +000046
47# generic operations
48 "init",
Thomas Wouters1d75a792000-08-17 22:37:32 +000049 ]
50
51# These need to return something other than None
Thomas Wouters1d75a792000-08-17 22:37:32 +000052# "hash",
53# "str",
54# "repr",
Neil Schemenauer3a313e32004-07-19 16:29:17 +000055# "int",
56# "long",
57# "float",
58# "oct",
59# "hex",
Thomas Wouters1d75a792000-08-17 22:37:32 +000060
61# These are separate because they can influence the test of other methods.
62# "getattr",
63# "setattr",
64# "delattr",
65
66class AllTests:
Thomas Wouters1d75a792000-08-17 22:37:32 +000067 def __hash__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000068 print("__hash__:", args)
Trent Mickd68d0a62000-10-04 17:50:59 +000069 return hash(id(self))
Thomas Wouters1d75a792000-08-17 22:37:32 +000070
71 def __str__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000072 print("__str__:", args)
Thomas Wouters1d75a792000-08-17 22:37:32 +000073 return "AllTests"
74
75 def __repr__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000076 print("__repr__:", args)
Thomas Wouters1d75a792000-08-17 22:37:32 +000077 return "AllTests"
78
Neil Schemenauer3a313e32004-07-19 16:29:17 +000079 def __int__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000080 print("__int__:", args)
Neil Schemenauer3a313e32004-07-19 16:29:17 +000081 return 1
82
83 def __float__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000084 print("__float__:", args)
Neil Schemenauer3a313e32004-07-19 16:29:17 +000085 return 1.0
86
Neil Schemenauer3a313e32004-07-19 16:29:17 +000087 def __oct__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000088 print("__oct__:", args)
Neil Schemenauer3a313e32004-07-19 16:29:17 +000089 return '01'
90
91 def __hex__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000092 print("__hex__:", args)
Neil Schemenauer3a313e32004-07-19 16:29:17 +000093 return '0x1'
94
Thomas Wouters1d75a792000-08-17 22:37:32 +000095 def __cmp__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000096 print("__cmp__:", args)
Thomas Wouters1d75a792000-08-17 22:37:32 +000097 return 0
98
Guido van Rossum47b9ff62006-08-24 00:41:19 +000099 def __eq__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000100 print("__eq__:", args)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000101 return True
102
103 def __ne__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000104 print("__ne__:", args)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000105 return False
106
107 def __lt__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000108 print("__lt__:", args)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000109 return False
110
111 def __le__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000112 print("__le__:", args)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000113 return True
114
115 def __gt__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000116 print("__gt__:", args)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000117 return False
118
119 def __ge__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000120 print("__ge__:", args)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000121 return True
122
Barry Warsaw07d8d642001-08-20 20:29:07 +0000123 def __del__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000124 print("__del__:", args)
Barry Warsaw07d8d642001-08-20 20:29:07 +0000125
Tim Peters01705212001-12-11 19:28:47 +0000126# Synthesize AllTests methods from the names in testmeths.
127
128method_template = """\
129def __%(method)s__(self, *args):
Georg Brandl88fc6642007-02-09 21:28:07 +0000130 print("__%(method)s__:", args)
Tim Peters01705212001-12-11 19:28:47 +0000131"""
132
Thomas Wouters4cdada92006-04-15 09:19:16 +0000133d = {}
Thomas Wouters1d75a792000-08-17 22:37:32 +0000134for method in testmeths:
Georg Brandl7cae87c2006-09-06 06:51:57 +0000135 exec(method_template % locals(), d)
Thomas Wouters4cdada92006-04-15 09:19:16 +0000136for k in d:
137 setattr(AllTests, k, d[k])
138del d, k
Tim Peters01705212001-12-11 19:28:47 +0000139del method, method_template
Thomas Wouters1d75a792000-08-17 22:37:32 +0000140
141# this also tests __init__ of course.
142testme = AllTests()
143
144# Binary operations
145
146testme + 1
1471 + testme
148
149testme - 1
1501 - testme
151
152testme * 1
1531 * testme
154
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000155testme / 1
1561 / testme
Thomas Wouters1d75a792000-08-17 22:37:32 +0000157
158testme % 1
1591 % testme
160
161divmod(testme,1)
162divmod(1, testme)
163
164testme ** 1
1651 ** testme
166
167testme >> 1
1681 >> testme
169
170testme << 1
1711 << testme
172
173testme & 1
1741 & testme
175
176testme | 1
1771 | testme
178
179testme ^ 1
1801 ^ testme
181
182
183# List/dict operations
184
Thomas Wouters89f507f2006-12-13 04:49:30 +0000185class Empty: pass
186
187try:
188 1 in Empty()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000189 print('failed, should have raised TypeError')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190except TypeError:
191 pass
192
Thomas Wouters1d75a792000-08-17 22:37:32 +00001931 in testme
194
195testme[1]
196testme[1] = 1
197del testme[1]
198
199testme[:42]
200testme[:42] = "The Answer"
201del testme[:42]
202
203testme[2:1024:10]
204testme[2:1024:10] = "A lot"
205del testme[2:1024:10]
206
207testme[:42, ..., :24:, 24, 100]
208testme[:42, ..., :24:, 24, 100] = "Strange"
209del testme[:42, ..., :24:, 24, 100]
210
211
212# Now remove the slice hooks to see if converting normal slices to slice
213# object works.
214
215del AllTests.__getslice__
216del AllTests.__setslice__
217del AllTests.__delslice__
218
Barry Warsaw07d8d642001-08-20 20:29:07 +0000219import sys
220if sys.platform[:4] != 'java':
221 testme[:42]
222 testme[:42] = "The Answer"
223 del testme[:42]
224else:
225 # This works under Jython, but the actual slice values are
226 # different.
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000227 print("__getitem__: (slice(0, 42, None),)")
228 print("__setitem__: (slice(0, 42, None), 'The Answer')")
229 print("__delitem__: (slice(0, 42, None),)")
Thomas Wouters1d75a792000-08-17 22:37:32 +0000230
231# Unary operations
232
233-testme
234+testme
235abs(testme)
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000236int(testme)
Guido van Rossume2a383d2007-01-15 16:59:06 +0000237int(testme)
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000238float(testme)
239oct(testme)
240hex(testme)
Thomas Wouters1d75a792000-08-17 22:37:32 +0000241
242# And the rest...
243
244hash(testme)
245repr(testme)
246str(testme)
247
248testme == 1
249testme < 1
250testme > 1
Thomas Wouters1d75a792000-08-17 22:37:32 +0000251testme != 1
2521 == testme
2531 < testme
2541 > testme
Thomas Wouters1d75a792000-08-17 22:37:32 +00002551 != testme
256
257# This test has to be last (duh.)
258
259del testme
Barry Warsaw07d8d642001-08-20 20:29:07 +0000260if sys.platform[:4] == 'java':
261 import java
262 java.lang.System.gc()
Thomas Wouters1d75a792000-08-17 22:37:32 +0000263
264# Interfering tests
265
266class ExtraTests:
Fred Drake004d5e62000-10-23 17:22:08 +0000267 def __getattr__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000268 print("__getattr__:", args)
Fred Drake004d5e62000-10-23 17:22:08 +0000269 return "SomeVal"
Thomas Wouters1d75a792000-08-17 22:37:32 +0000270
Fred Drake004d5e62000-10-23 17:22:08 +0000271 def __setattr__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000272 print("__setattr__:", args)
Thomas Wouters1d75a792000-08-17 22:37:32 +0000273
Fred Drake004d5e62000-10-23 17:22:08 +0000274 def __delattr__(self, *args):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000275 print("__delattr__:", args)
Thomas Wouters1d75a792000-08-17 22:37:32 +0000276
277testme = ExtraTests()
278testme.spam
279testme.eggs = "spam, spam, spam and ham"
280del testme.cardinal
Guido van Rossum23120242001-01-18 23:47:15 +0000281
282
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000283# return values of some method are type-checked
284class BadTypeClass:
285 def __int__(self):
286 return None
287 __float__ = __int__
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000288 __str__ = __int__
289 __repr__ = __int__
290 __oct__ = __int__
291 __hex__ = __int__
292
293def check_exc(stmt, exception):
294 """Raise TestFailed if executing 'stmt' does not raise 'exception'
295 """
296 try:
Georg Brandl7cae87c2006-09-06 06:51:57 +0000297 exec(stmt)
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000298 except exception:
299 pass
300 else:
301 raise TestFailed, "%s should raise %s" % (stmt, exception)
302
303check_exc("int(BadTypeClass())", TypeError)
304check_exc("float(BadTypeClass())", TypeError)
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000305check_exc("str(BadTypeClass())", TypeError)
306check_exc("repr(BadTypeClass())", TypeError)
307check_exc("oct(BadTypeClass())", TypeError)
308check_exc("hex(BadTypeClass())", TypeError)
309
Guido van Rossum23120242001-01-18 23:47:15 +0000310# Test correct errors from hash() on objects with comparisons but no __hash__
311
312class C0:
313 pass
314
315hash(C0()) # This should work; the next two should raise TypeError
316
317class C1:
318 def __cmp__(self, other): return 0
319
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000320check_exc("hash(C1())", TypeError)
Guido van Rossum23120242001-01-18 23:47:15 +0000321
322class C2:
323 def __eq__(self, other): return 1
324
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000325check_exc("hash(C2())", TypeError)
Guido van Rossum16b93b32002-06-13 21:32:51 +0000326
327# Test for SF bug 532646
328
329class A:
330 pass
331A.__call__ = A()
332a = A()
333try:
334 a() # This should not segfault
335except RuntimeError:
336 pass
337else:
338 raise TestFailed, "how could this not have overflowed the stack?"
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000339
340
341# Tests for exceptions raised in instance_getattr2().
342
343def booh(self):
344 raise AttributeError, "booh"
345
346class A:
347 a = property(booh)
348try:
349 A().a # Raised AttributeError: A instance has no attribute 'a'
Guido van Rossumb940e112007-01-10 16:19:56 +0000350except AttributeError as x:
Michael W. Hudsonabb103b2005-05-04 11:59:38 +0000351 if str(x) != "booh":
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000352 print("attribute error for A().a got masked:", str(x))
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000353
354class E:
355 __eq__ = property(booh)
356E() == E() # In debug mode, caused a C-level assert() to fail
357
358class I:
359 __init__ = property(booh)
360try:
361 I() # In debug mode, printed XXX undetected error and raises AttributeError
Guido van Rossumb940e112007-01-10 16:19:56 +0000362except AttributeError as x:
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000363 pass
364else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000365 print("attribute error for I.__init__ got masked")
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000366
367
368# Test comparison and hash of methods
369class A:
370 def __init__(self, x):
371 self.x = x
372 def f(self):
373 pass
374 def g(self):
375 pass
376 def __eq__(self, other):
377 return self.x == other.x
378 def __hash__(self):
379 return self.x
380class B(A):
381 pass
382
383a1 = A(1)
384a2 = A(2)
385assert a1.f == a1.f
386assert a1.f != a2.f
387assert a1.f != a1.g
388assert a1.f == A(1).f
389assert hash(a1.f) == hash(a1.f)
390assert hash(a1.f) == hash(A(1).f)
391
392assert A.f != a1.f
393assert A.f != A.g
394assert B.f == A.f
395assert hash(B.f) == hash(A.f)
396
397# the following triggers a SystemError in 2.4
398a = A(hash(A.f.im_func)^(-1))
399hash(a.f)