2000-08-17 19:37:32 -03:00
|
|
|
"Test the functionality of Python classes implementing operators."
|
|
|
|
|
2001-01-18 19:47:15 -04:00
|
|
|
from test_support import TestFailed
|
2000-08-17 19:37:32 -03:00
|
|
|
|
|
|
|
testmeths = [
|
|
|
|
|
|
|
|
# Binary operations
|
|
|
|
"add",
|
|
|
|
"radd",
|
|
|
|
"sub",
|
|
|
|
"rsub",
|
|
|
|
"mul",
|
|
|
|
"rmul",
|
|
|
|
"div",
|
|
|
|
"rdiv",
|
|
|
|
"mod",
|
|
|
|
"rmod",
|
|
|
|
"divmod",
|
|
|
|
"rdivmod",
|
|
|
|
"pow",
|
|
|
|
"rpow",
|
|
|
|
"rshift",
|
|
|
|
"rrshift",
|
|
|
|
"lshift",
|
|
|
|
"rlshift",
|
|
|
|
"and",
|
|
|
|
"rand",
|
|
|
|
"or",
|
|
|
|
"ror",
|
|
|
|
"xor",
|
|
|
|
"rxor",
|
|
|
|
|
|
|
|
# List/dict operations
|
|
|
|
"contains",
|
|
|
|
"getitem",
|
|
|
|
"getslice",
|
|
|
|
"setitem",
|
|
|
|
"setslice",
|
|
|
|
"delitem",
|
|
|
|
"delslice",
|
|
|
|
|
|
|
|
# Unary operations
|
|
|
|
"neg",
|
|
|
|
"pos",
|
|
|
|
"abs",
|
|
|
|
"int",
|
|
|
|
"long",
|
|
|
|
"float",
|
|
|
|
"oct",
|
|
|
|
"hex",
|
|
|
|
|
|
|
|
# generic operations
|
|
|
|
"init",
|
|
|
|
]
|
|
|
|
|
|
|
|
# These need to return something other than None
|
|
|
|
# "coerce",
|
|
|
|
# "hash",
|
|
|
|
# "str",
|
|
|
|
# "repr",
|
|
|
|
|
|
|
|
# These are separate because they can influence the test of other methods.
|
|
|
|
# "getattr",
|
|
|
|
# "setattr",
|
|
|
|
# "delattr",
|
|
|
|
|
|
|
|
class AllTests:
|
|
|
|
def __coerce__(self, *args):
|
|
|
|
print "__coerce__:", args
|
2000-10-23 14:22:08 -03:00
|
|
|
return (self,) + args
|
2000-08-17 19:37:32 -03:00
|
|
|
|
|
|
|
def __hash__(self, *args):
|
|
|
|
print "__hash__:", args
|
2000-10-04 14:50:59 -03:00
|
|
|
return hash(id(self))
|
2000-08-17 19:37:32 -03:00
|
|
|
|
|
|
|
def __str__(self, *args):
|
|
|
|
print "__str__:", args
|
|
|
|
return "AllTests"
|
|
|
|
|
|
|
|
def __repr__(self, *args):
|
|
|
|
print "__repr__:", args
|
|
|
|
return "AllTests"
|
|
|
|
|
|
|
|
def __cmp__(self, *args):
|
|
|
|
print "__cmp__:", args
|
|
|
|
return 0
|
|
|
|
|
2001-08-20 17:29:07 -03:00
|
|
|
def __del__(self, *args):
|
|
|
|
print "__del__:", args
|
|
|
|
|
2000-08-17 19:37:32 -03:00
|
|
|
for method in testmeths:
|
2001-01-22 10:51:41 -04:00
|
|
|
exec """def __%(method)s__(self, *args):
|
2000-08-17 19:37:32 -03:00
|
|
|
print "__%(method)s__:", args
|
2001-01-22 10:51:41 -04:00
|
|
|
"""%locals() in AllTests.__dict__
|
2000-08-17 19:37:32 -03:00
|
|
|
|
|
|
|
# this also tests __init__ of course.
|
|
|
|
testme = AllTests()
|
|
|
|
|
|
|
|
# Binary operations
|
|
|
|
|
|
|
|
testme + 1
|
|
|
|
1 + testme
|
|
|
|
|
|
|
|
testme - 1
|
|
|
|
1 - testme
|
|
|
|
|
|
|
|
testme * 1
|
|
|
|
1 * testme
|
|
|
|
|
|
|
|
testme / 1
|
|
|
|
1 / testme
|
|
|
|
|
|
|
|
testme % 1
|
|
|
|
1 % testme
|
|
|
|
|
|
|
|
divmod(testme,1)
|
|
|
|
divmod(1, testme)
|
|
|
|
|
|
|
|
testme ** 1
|
|
|
|
1 ** testme
|
|
|
|
|
|
|
|
testme >> 1
|
|
|
|
1 >> testme
|
|
|
|
|
|
|
|
testme << 1
|
|
|
|
1 << testme
|
|
|
|
|
|
|
|
testme & 1
|
|
|
|
1 & testme
|
|
|
|
|
|
|
|
testme | 1
|
|
|
|
1 | testme
|
|
|
|
|
|
|
|
testme ^ 1
|
|
|
|
1 ^ testme
|
|
|
|
|
|
|
|
|
|
|
|
# List/dict operations
|
|
|
|
|
|
|
|
1 in testme
|
|
|
|
|
|
|
|
testme[1]
|
|
|
|
testme[1] = 1
|
|
|
|
del testme[1]
|
|
|
|
|
|
|
|
testme[:42]
|
|
|
|
testme[:42] = "The Answer"
|
|
|
|
del testme[:42]
|
|
|
|
|
|
|
|
testme[2:1024:10]
|
|
|
|
testme[2:1024:10] = "A lot"
|
|
|
|
del testme[2:1024:10]
|
|
|
|
|
|
|
|
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
|
|
|
|
# object works.
|
|
|
|
|
|
|
|
del AllTests.__getslice__
|
|
|
|
del AllTests.__setslice__
|
|
|
|
del AllTests.__delslice__
|
|
|
|
|
2001-08-20 17:29:07 -03:00
|
|
|
import sys
|
|
|
|
if sys.platform[:4] != 'java':
|
|
|
|
testme[:42]
|
|
|
|
testme[:42] = "The Answer"
|
|
|
|
del testme[:42]
|
|
|
|
else:
|
|
|
|
# This works under Jython, but the actual slice values are
|
|
|
|
# different.
|
|
|
|
print "__getitem__: (slice(0, 42, None),)"
|
|
|
|
print "__setitem__: (slice(0, 42, None), 'The Answer')"
|
|
|
|
print "__delitem__: (slice(0, 42, None),)"
|
2000-08-17 19:37:32 -03:00
|
|
|
|
|
|
|
# Unary operations
|
|
|
|
|
|
|
|
-testme
|
|
|
|
+testme
|
|
|
|
abs(testme)
|
2001-08-20 17:29:07 -03:00
|
|
|
if sys.platform[:4] != 'java':
|
|
|
|
int(testme)
|
|
|
|
long(testme)
|
|
|
|
float(testme)
|
|
|
|
oct(testme)
|
|
|
|
hex(testme)
|
|
|
|
else:
|
|
|
|
# Jython enforced that the these methods return
|
|
|
|
# a value of the expected type.
|
|
|
|
print "__int__: ()"
|
|
|
|
print "__long__: ()"
|
|
|
|
print "__float__: ()"
|
|
|
|
print "__oct__: ()"
|
|
|
|
print "__hex__: ()"
|
2000-08-17 19:37:32 -03:00
|
|
|
|
|
|
|
|
|
|
|
# And the rest...
|
|
|
|
|
|
|
|
hash(testme)
|
|
|
|
repr(testme)
|
|
|
|
str(testme)
|
|
|
|
|
|
|
|
testme == 1
|
|
|
|
testme < 1
|
|
|
|
testme > 1
|
|
|
|
testme <> 1
|
|
|
|
testme != 1
|
|
|
|
1 == testme
|
|
|
|
1 < testme
|
|
|
|
1 > testme
|
|
|
|
1 <> testme
|
|
|
|
1 != testme
|
|
|
|
|
|
|
|
# This test has to be last (duh.)
|
|
|
|
|
|
|
|
del testme
|
2001-08-20 17:29:07 -03:00
|
|
|
if sys.platform[:4] == 'java':
|
|
|
|
import java
|
|
|
|
java.lang.System.gc()
|
2000-08-17 19:37:32 -03:00
|
|
|
|
|
|
|
# Interfering tests
|
|
|
|
|
|
|
|
class ExtraTests:
|
2000-10-23 14:22:08 -03:00
|
|
|
def __getattr__(self, *args):
|
|
|
|
print "__getattr__:", args
|
|
|
|
return "SomeVal"
|
2000-08-17 19:37:32 -03:00
|
|
|
|
2000-10-23 14:22:08 -03:00
|
|
|
def __setattr__(self, *args):
|
|
|
|
print "__setattr__:", args
|
2000-08-17 19:37:32 -03:00
|
|
|
|
2000-10-23 14:22:08 -03:00
|
|
|
def __delattr__(self, *args):
|
|
|
|
print "__delattr__:", args
|
2000-08-17 19:37:32 -03:00
|
|
|
|
|
|
|
testme = ExtraTests()
|
|
|
|
testme.spam
|
|
|
|
testme.eggs = "spam, spam, spam and ham"
|
|
|
|
del testme.cardinal
|
2001-01-18 19:47:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Test correct errors from hash() on objects with comparisons but no __hash__
|
|
|
|
|
|
|
|
class C0:
|
|
|
|
pass
|
|
|
|
|
|
|
|
hash(C0()) # This should work; the next two should raise TypeError
|
|
|
|
|
|
|
|
class C1:
|
|
|
|
def __cmp__(self, other): return 0
|
|
|
|
|
|
|
|
try: hash(C1())
|
|
|
|
except TypeError: pass
|
|
|
|
else: raise TestFailed, "hash(C1()) should raise an exception"
|
|
|
|
|
|
|
|
class C2:
|
|
|
|
def __eq__(self, other): return 1
|
|
|
|
|
|
|
|
try: hash(C2())
|
|
|
|
except TypeError: pass
|
|
|
|
else: raise TestFailed, "hash(C2()) should raise an exception"
|