1 "Test the functionality of Python classes implementing operators."
3 from test
.test_support
import TestFailed
33 # List/dict operations
56 # 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 __cmp__(self
, *args
):
85 print "__cmp__:", args
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
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.
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.
153 # List/dict operations
162 testme
[:42] = "The Answer"
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
177 del AllTests
.__getslice
__
178 del AllTests
.__setslice
__
179 del AllTests
.__delslice
__
182 if sys
.platform
[:4] != 'java':
184 testme
[:42] = "The Answer"
187 # This works under Jython, but the actual slice values are
189 print "__getitem__: (slice(0, 42, None),)"
190 print "__setitem__: (slice(0, 42, None), 'The Answer')"
191 print "__delitem__: (slice(0, 42, None),)"
198 if sys
.platform
[:4] != 'java':
205 # Jython enforced that the these methods return
206 # a value of the expected type.
209 print "__float__: ()"
231 # This test has to be last (duh.)
234 if sys
.platform
[:4] == 'java':
236 java
.lang
.System
.gc()
241 def __getattr__(self
, *args
):
242 print "__getattr__:", args
245 def __setattr__(self
, *args
):
246 print "__setattr__:", args
248 def __delattr__(self
, *args
):
249 print "__delattr__:", args
251 testme
= ExtraTests()
253 testme
.eggs
= "spam, spam, spam and ham"
257 # Test correct errors from hash() on objects with comparisons but no __hash__
262 hash(C0()) # This should work; the next two should raise TypeError
265 def __cmp__(self
, other
): return 0
268 except TypeError: pass
269 else: raise TestFailed
, "hash(C1()) should raise an exception"
272 def __eq__(self
, other
): return 1
275 except TypeError: pass
276 else: raise TestFailed
, "hash(C2()) should raise an exception"
279 # Test for SF bug 532646
286 a() # This should not segfault
290 raise TestFailed
, "how could this not have overflowed the stack?"
293 # Tests for exceptions raised in instance_getattr2().
296 raise AttributeError, "booh"
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
)
307 __eq__
= property(booh
)
308 E() == E() # In debug mode, caused a C-level assert() to fail
311 __init__
= property(booh
)
313 I() # In debug mode, printed XXX undetected error and raises AttributeError
314 except AttributeError, x
:
317 print "attribute error for I.__init__ got masked"