Update version number and release date.
[python/dscho.git] / Lib / test / test_class.py
blob3c3ea063b10b890c515463139cb99f85c36076bc
1 "Test the functionality of Python classes implementing operators."
3 from test.test_support import TestFailed
5 testmeths = [
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",
33 # List/dict operations
34 "contains",
35 "getitem",
36 "getslice",
37 "setitem",
38 "setslice",
39 "delitem",
40 "delslice",
42 # Unary operations
43 "neg",
44 "pos",
45 "abs",
46 "int",
47 "long",
48 "float",
49 "oct",
50 "hex",
52 # generic operations
53 "init",
56 # These need to return something other than None
57 # "coerce",
58 # "hash",
59 # "str",
60 # "repr",
62 # These are separate because they can influence the test of other methods.
63 # "getattr",
64 # "setattr",
65 # "delattr",
67 class AllTests:
68 def __coerce__(self, *args):
69 print "__coerce__:", args
70 return (self,) + args
72 def __hash__(self, *args):
73 print "__hash__:", args
74 return hash(id(self))
76 def __str__(self, *args):
77 print "__str__:", args
78 return "AllTests"
80 def __repr__(self, *args):
81 print "__repr__:", args
82 return "AllTests"
84 def __cmp__(self, *args):
85 print "__cmp__:", args
86 return 0
88 def __del__(self, *args):
89 print "__del__:", args
91 # Synthesize AllTests methods from the names in testmeths.
93 method_template = """\
94 def __%(method)s__(self, *args):
95 print "__%(method)s__:", args
96 """
98 for method in testmeths:
99 exec method_template % locals() in AllTests.__dict__
101 del method, method_template
103 # this also tests __init__ of course.
104 testme = AllTests()
106 # Binary operations
108 testme + 1
109 1 + testme
111 testme - 1
112 1 - testme
114 testme * 1
115 1 * testme
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)
128 testme % 1
129 1 % testme
131 divmod(testme,1)
132 divmod(1, testme)
134 testme ** 1
135 1 ** testme
137 testme >> 1
138 1 >> testme
140 testme << 1
141 1 << testme
143 testme & 1
144 1 & testme
146 testme | 1
147 1 | testme
149 testme ^ 1
150 1 ^ testme
153 # List/dict operations
155 1 in testme
157 testme[1]
158 testme[1] = 1
159 del testme[1]
161 testme[:42]
162 testme[:42] = "The Answer"
163 del testme[:42]
165 testme[2:1024:10]
166 testme[2:1024:10] = "A lot"
167 del testme[2:1024:10]
169 testme[:42, ..., :24:, 24, 100]
170 testme[:42, ..., :24:, 24, 100] = "Strange"
171 del testme[:42, ..., :24:, 24, 100]
174 # Now remove the slice hooks to see if converting normal slices to slice
175 # object works.
177 del AllTests.__getslice__
178 del AllTests.__setslice__
179 del AllTests.__delslice__
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),)"
193 # Unary operations
195 -testme
196 +testme
197 abs(testme)
198 if sys.platform[:4] != 'java':
199 int(testme)
200 long(testme)
201 float(testme)
202 oct(testme)
203 hex(testme)
204 else:
205 # Jython enforced that the these methods return
206 # a value of the expected type.
207 print "__int__: ()"
208 print "__long__: ()"
209 print "__float__: ()"
210 print "__oct__: ()"
211 print "__hex__: ()"
214 # And the rest...
216 hash(testme)
217 repr(testme)
218 str(testme)
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
231 # This test has to be last (duh.)
233 del testme
234 if sys.platform[:4] == 'java':
235 import java
236 java.lang.System.gc()
238 # Interfering tests
240 class ExtraTests:
241 def __getattr__(self, *args):
242 print "__getattr__:", args
243 return "SomeVal"
245 def __setattr__(self, *args):
246 print "__setattr__:", args
248 def __delattr__(self, *args):
249 print "__delattr__:", args
251 testme = ExtraTests()
252 testme.spam
253 testme.eggs = "spam, spam, spam and ham"
254 del testme.cardinal
257 # Test correct errors from hash() on objects with comparisons but no __hash__
259 class C0:
260 pass
262 hash(C0()) # This should work; the next two should raise TypeError
264 class C1:
265 def __cmp__(self, other): return 0
267 try: hash(C1())
268 except TypeError: pass
269 else: raise TestFailed, "hash(C1()) should raise an exception"
271 class C2:
272 def __eq__(self, other): return 1
274 try: hash(C2())
275 except TypeError: pass
276 else: raise TestFailed, "hash(C2()) should raise an exception"
279 # Test for SF bug 532646
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?"
293 # Tests for exceptions raised in instance_getattr2().
295 def booh(self):
296 raise AttributeError, "booh"
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)
306 class E:
307 __eq__ = property(booh)
308 E() == E() # In debug mode, caused a C-level assert() to fail
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"