"Test the functionality of Python classes implementing operators."
from test
.test_support
import TestFailed
# These need to return something other than None
# These are separate because they can influence the test of other methods.
def __coerce__(self
, *args
):
print "__coerce__:", args
def __hash__(self
, *args
):
def __str__(self
, *args
):
def __repr__(self
, *args
):
def __int__(self
, *args
):
def __float__(self
, *args
):
def __long__(self
, *args
):
def __oct__(self
, *args
):
def __hex__(self
, *args
):
def __cmp__(self
, *args
):
def __del__(self
, *args
):
# Synthesize AllTests methods from the names in testmeths.
def __%(method)s__(self, *args):
print "__%(method)s__:", args
exec method_template
% locals() in AllTests
.__dict
__
del method
, method_template
# this also tests __init__ of course.
# True division is in effect, so "/" doesn't map to __div__ etc; but
# the canned expected-output file requires that __div__ etc get called.
testme
[:42] = "The Answer"
testme
[2:1024:10] = "A lot"
testme
[:42, ..., :24:, 24, 100]
testme
[:42, ..., :24:, 24, 100] = "Strange"
del testme
[:42, ..., :24:, 24, 100]
# Now remove the slice hooks to see if converting normal slices to slice
del AllTests
.__getslice
__
del AllTests
.__setslice
__
del AllTests
.__delslice
__
if sys
.platform
[:4] != 'java':
testme
[:42] = "The Answer"
# This works under Jython, but the actual slice values are
print "__getitem__: (slice(0, 42, None),)"
print "__setitem__: (slice(0, 42, None), 'The Answer')"
print "__delitem__: (slice(0, 42, None),)"
# This test has to be last (duh.)
if sys
.platform
[:4] == 'java':
def __getattr__(self
, *args
):
print "__getattr__:", args
def __setattr__(self
, *args
):
print "__setattr__:", args
def __delattr__(self
, *args
):
print "__delattr__:", args
testme
.eggs
= "spam, spam, spam and ham"
# return values of some method are type-checked
def check_exc(stmt
, exception
):
"""Raise TestFailed if executing 'stmt' does not raise 'exception'
raise TestFailed
, "%s should raise %s" % (stmt
, exception
)
check_exc("int(BadTypeClass())", TypeError)
check_exc("float(BadTypeClass())", TypeError)
check_exc("long(BadTypeClass())", TypeError)
check_exc("str(BadTypeClass())", TypeError)
check_exc("repr(BadTypeClass())", TypeError)
check_exc("oct(BadTypeClass())", TypeError)
check_exc("hex(BadTypeClass())", TypeError)
# mixing up ints and longs is okay
raise TestFailed
, "TypeError should not be raised"
raise TestFailed
, "TypeError should not be raised"
# Test correct errors from hash() on objects with comparisons but no __hash__
hash(C0()) # This should work; the next two should raise TypeError
def __cmp__(self
, other
): return 0
check_exc("hash(C1())", TypeError)
def __eq__(self
, other
): return 1
check_exc("hash(C2())", TypeError)
a() # This should not segfault
raise TestFailed
, "how could this not have overflowed the stack?"
# Tests for exceptions raised in instance_getattr2().
raise AttributeError, "booh"
A().a
# Raised AttributeError: A instance has no attribute 'a'
except AttributeError, x
:
print "attribute error for A().a got masked:", str(x
)
E() == E() # In debug mode, caused a C-level assert() to fail
__init__
= property(booh
)
I() # In debug mode, printed XXX undetected error and raises AttributeError
except AttributeError, x
:
print "attribute error for I.__init__ got masked"