1 "Test the functionality of Python classes implementing operators."
3 from test
.test_support
import TestFailed
33 # List/dict operations
51 # These need to return something other than None
62 # These are separate because they can influence the test of other methods.
68 def __coerce__(self
, *args
):
69 print "__coerce__:", args
72 def __hash__(self
, *args
):
73 print "__hash__:", args
76 def __str__(self
, *args
):
77 print "__str__:", args
80 def __repr__(self
, *args
):
81 print "__repr__:", args
84 def __int__(self
, *args
):
85 print "__int__:", args
88 def __float__(self
, *args
):
89 print "__float__:", args
92 def __long__(self
, *args
):
93 print "__long__:", args
96 def __oct__(self
, *args
):
97 print "__oct__:", args
100 def __hex__(self
, *args
):
101 print "__hex__:", args
104 def __cmp__(self
, *args
):
105 print "__cmp__:", args
108 def __del__(self
, *args
):
109 print "__del__:", args
111 # Synthesize AllTests methods from the names in testmeths.
113 method_template
= """\
114 def __%(method)s__(self, *args):
115 print "__%(method)s__:", args
118 for method
in testmeths
:
119 exec method_template
% locals() in AllTests
.__dict
__
121 del method
, method_template
123 # this also tests __init__ of course.
141 # True division is in effect, so "/" doesn't map to __div__ etc; but
142 # the canned expected-output file requires that __div__ etc get called.
173 # List/dict operations
182 testme
[:42] = "The Answer"
186 testme
[2:1024:10] = "A lot"
187 del testme
[2:1024:10]
189 testme
[:42, ..., :24:, 24, 100]
190 testme
[:42, ..., :24:, 24, 100] = "Strange"
191 del testme
[:42, ..., :24:, 24, 100]
194 # Now remove the slice hooks to see if converting normal slices to slice
197 del AllTests
.__getslice
__
198 del AllTests
.__setslice
__
199 del AllTests
.__delslice
__
202 if sys
.platform
[:4] != 'java':
204 testme
[:42] = "The Answer"
207 # This works under Jython, but the actual slice values are
209 print "__getitem__: (slice(0, 42, None),)"
210 print "__setitem__: (slice(0, 42, None), 'The Answer')"
211 print "__delitem__: (slice(0, 42, None),)"
241 # This test has to be last (duh.)
244 if sys
.platform
[:4] == 'java':
246 java
.lang
.System
.gc()
251 def __getattr__(self
, *args
):
252 print "__getattr__:", args
255 def __setattr__(self
, *args
):
256 print "__setattr__:", args
258 def __delattr__(self
, *args
):
259 print "__delattr__:", args
261 testme
= ExtraTests()
263 testme
.eggs
= "spam, spam, spam and ham"
267 # return values of some method are type-checked
278 def check_exc(stmt
, exception
):
279 """Raise TestFailed if executing 'stmt' does not raise 'exception'
286 raise TestFailed
, "%s should raise %s" % (stmt
, exception
)
288 check_exc("int(BadTypeClass())", TypeError)
289 check_exc("float(BadTypeClass())", TypeError)
290 check_exc("long(BadTypeClass())", TypeError)
291 check_exc("str(BadTypeClass())", TypeError)
292 check_exc("repr(BadTypeClass())", TypeError)
293 check_exc("oct(BadTypeClass())", TypeError)
294 check_exc("hex(BadTypeClass())", TypeError)
296 # mixing up ints and longs is okay
297 class IntLongMixClass
:
305 int(IntLongMixClass())
307 raise TestFailed
, "TypeError should not be raised"
310 long(IntLongMixClass())
312 raise TestFailed
, "TypeError should not be raised"
315 # Test correct errors from hash() on objects with comparisons but no __hash__
320 hash(C0()) # This should work; the next two should raise TypeError
323 def __cmp__(self
, other
): return 0
325 check_exc("hash(C1())", TypeError)
328 def __eq__(self
, other
): return 1
330 check_exc("hash(C2())", TypeError)
332 # Test for SF bug 532646
339 a() # This should not segfault
343 raise TestFailed
, "how could this not have overflowed the stack?"
346 # Tests for exceptions raised in instance_getattr2().
349 raise AttributeError, "booh"
354 A().a
# Raised AttributeError: A instance has no attribute 'a'
355 except AttributeError, x
:
357 print "attribute error for A().a got masked:", str(x
)
360 __eq__
= property(booh
)
361 E() == E() # In debug mode, caused a C-level assert() to fail
364 __init__
= property(booh
)
366 I() # In debug mode, printed XXX undetected error and raises AttributeError
367 except AttributeError, x
:
370 print "attribute error for I.__init__ got masked"