0001 "Test the functionality of Python classes implementing operators." 0002 0003 from test.test_support import TestFailed 0004 0005 testmeths = [ 0006 0007 # Binary operations 0008 "add", 0009 "radd", 0010 "sub", 0011 "rsub", 0012 "mul", 0013 "rmul", 0014 "div", 0015 "rdiv", 0016 "mod", 0017 "rmod", 0018 "divmod", 0019 "rdivmod", 0020 "pow", 0021 "rpow", 0022 "rshift", 0023 "rrshift", 0024 "lshift", 0025 "rlshift", 0026 "and", 0027 "rand", 0028 "or", 0029 "ror", 0030 "xor", 0031 "rxor", 0032 0033 # List/dict operations 0034 "contains", 0035 "getitem", 0036 "getslice", 0037 "setitem", 0038 "setslice", 0039 "delitem", 0040 "delslice", 0041 0042 # Unary operations 0043 "neg", 0044 "pos", 0045 "abs", 0046 0047 # generic operations 0048 "init", 0049 ] 0050 0051 # These need to return something other than None 0052 # "coerce", 0053 # "hash", 0054 # "str", 0055 # "repr", 0056 # "int", 0057 # "long", 0058 # "float", 0059 # "oct", 0060 # "hex", 0061 0062 # These are separate because they can influence the test of other methods. 0063 # "getattr", 0064 # "setattr", 0065 # "delattr", 0066 0067 class AllTests: 0068 def __coerce__(self, *args): 0069 print "__coerce__:", args 0070 return (self,) + args 0071 0072 def __hash__(self, *args): 0073 print "__hash__:", args 0074 return hash(id(self)) 0075 0076 def __str__(self, *args): 0077 print "__str__:", args 0078 return "AllTests" 0079 0080 def __repr__(self, *args): 0081 print "__repr__:", args 0082 return "AllTests" 0083 0084 def __int__(self, *args): 0085 print "__int__:", args 0086 return 1 0087 0088 def __float__(self, *args): 0089 print "__float__:", args 0090 return 1.0 0091 0092 def __long__(self, *args): 0093 print "__long__:", args 0094 return 1L 0095 0096 def __oct__(self, *args): 0097 print "__oct__:", args 0098 return '01' 0099 0100 def __hex__(self, *args): 0101 print "__hex__:", args 0102 return '0x1' 0103 0104 def __cmp__(self, *args): 0105 print "__cmp__:", args 0106 return 0 0107 0108 def __del__(self, *args): 0109 print "__del__:", args 0110 0111 # Synthesize AllTests methods from the names in testmeths. 0112 0113 method_template = """\ 0114 def __%(method)s__(self, *args): 0115 print "__%(method)s__:", args 0116 """ 0117 0118 for method in testmeths: 0119 exec method_template % locals() in AllTests.__dict__ 0120 0121 del method, method_template 0122 0123 # this also tests __init__ of course. 0124 testme = AllTests() 0125 0126 # Binary operations 0127 0128 testme + 1 0129 1 + testme 0130 0131 testme - 1 0132 1 - testme 0133 0134 testme * 1 0135 1 * testme 0136 0137 if 1/2 == 0: 0138 testme / 1 0139 1 / testme 0140 else: 0141 # True division is in effect, so "/" doesn't map to __div__ etc; but 0142 # the canned expected-output file requires that __div__ etc get called. 0143 testme.__coerce__(1) 0144 testme.__div__(1) 0145 testme.__coerce__(1) 0146 testme.__rdiv__(1) 0147 0148 testme % 1 0149 1 % testme 0150 0151 divmod(testme,1) 0152 divmod(1, testme) 0153 0154 testme ** 1 0155 1 ** testme 0156 0157 testme >> 1 0158 1 >> testme 0159 0160 testme << 1 0161 1 << testme 0162 0163 testme & 1 0164 1 & testme 0165 0166 testme | 1 0167 1 | testme 0168 0169 testme ^ 1 0170 1 ^ testme 0171 0172 0173 # List/dict operations 0174 0175 1 in testme 0176 0177 testme[1] 0178 testme[1] = 1 0179 del testme[1] 0180 0181 testme[:42] 0182 testme[:42] = "The Answer" 0183 del testme[:42] 0184 0185 testme[2:1024:10] 0186 testme[2:1024:10] = "A lot" 0187 del testme[2:1024:10] 0188 0189 testme[:42, ..., :24:, 24, 100] 0190 testme[:42, ..., :24:, 24, 100] = "Strange" 0191 del testme[:42, ..., :24:, 24, 100] 0192 0193 0194 # Now remove the slice hooks to see if converting normal slices to slice 0195 # object works. 0196 0197 del AllTests.__getslice__ 0198 del AllTests.__setslice__ 0199 del AllTests.__delslice__ 0200 0201 import sys 0202 if sys.platform[:4] != 'java': 0203 testme[:42] 0204 testme[:42] = "The Answer" 0205 del testme[:42] 0206 else: 0207 # This works under Jython, but the actual slice values are 0208 # different. 0209 print "__getitem__: (slice(0, 42, None),)" 0210 print "__setitem__: (slice(0, 42, None), 'The Answer')" 0211 print "__delitem__: (slice(0, 42, None),)" 0212 0213 # Unary operations 0214 0215 -testme 0216 +testme 0217 abs(testme) 0218 int(testme) 0219 long(testme) 0220 float(testme) 0221 oct(testme) 0222 hex(testme) 0223 0224 # And the rest... 0225 0226 hash(testme) 0227 repr(testme) 0228 str(testme) 0229 0230 testme == 1 0231 testme < 1 0232 testme > 1 0233 testme <> 1 0234 testme != 1 0235 1 == testme 0236 1 < testme 0237 1 > testme 0238 1 <> testme 0239 1 != testme 0240 0241 # This test has to be last (duh.) 0242 0243 del testme 0244 if sys.platform[:4] == 'java': 0245 import java 0246 java.lang.System.gc() 0247 0248 # Interfering tests 0249 0250 class ExtraTests: 0251 def __getattr__(self, *args): 0252 print "__getattr__:", args 0253 return "SomeVal" 0254 0255 def __setattr__(self, *args): 0256 print "__setattr__:", args 0257 0258 def __delattr__(self, *args): 0259 print "__delattr__:", args 0260 0261 testme = ExtraTests() 0262 testme.spam 0263 testme.eggs = "spam, spam, spam and ham" 0264 del testme.cardinal 0265 0266 0267 # return values of some method are type-checked 0268 class BadTypeClass: 0269 def __int__(self): 0270 return None 0271 __float__ = __int__ 0272 __long__ = __int__ 0273 __str__ = __int__ 0274 __repr__ = __int__ 0275 __oct__ = __int__ 0276 __hex__ = __int__ 0277 0278 def check_exc(stmt, exception): 0279 """Raise TestFailed if executing 'stmt' does not raise 'exception' 0280 """ 0281 try: 0282 exec stmt 0283 except exception: 0284 pass 0285 else: 0286 raise TestFailed, "%s should raise %s" % (stmt, exception) 0287 0288 check_exc("int(BadTypeClass())", TypeError) 0289 check_exc("float(BadTypeClass())", TypeError) 0290 check_exc("long(BadTypeClass())", TypeError) 0291 check_exc("str(BadTypeClass())", TypeError) 0292 check_exc("repr(BadTypeClass())", TypeError) 0293 check_exc("oct(BadTypeClass())", TypeError) 0294 check_exc("hex(BadTypeClass())", TypeError) 0295 0296 # mixing up ints and longs is okay 0297 class IntLongMixClass: 0298 def __int__(self): 0299 return 0L 0300 0301 def __long__(self): 0302 return 0 0303 0304 try: 0305 int(IntLongMixClass()) 0306 except TypeError: 0307 raise TestFailed, "TypeError should not be raised" 0308 0309 try: 0310 long(IntLongMixClass()) 0311 except TypeError: 0312 raise TestFailed, "TypeError should not be raised" 0313 0314 0315 # Test correct errors from hash() on objects with comparisons but no __hash__ 0316 0317 class C0: 0318 pass 0319 0320 hash(C0()) # This should work; the next two should raise TypeError 0321 0322 class C1: 0323 def __cmp__(self, other): return 0 0324 0325 check_exc("hash(C1())", TypeError) 0326 0327 class C2: 0328 def __eq__(self, other): return 1 0329 0330 check_exc("hash(C2())", TypeError) 0331 0332 # Test for SF bug 532646 0333 0334 class A: 0335 pass 0336 A.__call__ = A() 0337 a = A() 0338 try: 0339 a() # This should not segfault 0340 except RuntimeError: 0341 pass 0342 else: 0343 raise TestFailed, "how could this not have overflowed the stack?" 0344 0345 0346 # Tests for exceptions raised in instance_getattr2(). 0347 0348 def booh(self): 0349 raise AttributeError, "booh" 0350 0351 class A: 0352 a = property(booh) 0353 try: 0354 A().a # Raised AttributeError: A instance has no attribute 'a' 0355 except AttributeError, x: 0356 if str(x) is not "booh": 0357 print "attribute error for A().a got masked:", str(x) 0358 0359 class E: 0360 __eq__ = property(booh) 0361 E() == E() # In debug mode, caused a C-level assert() to fail 0362 0363 class I: 0364 __init__ = property(booh) 0365 try: 0366 I() # In debug mode, printed XXX undetected error and raises AttributeError 0367 except AttributeError, x: 0368 pass 0369 else: 0370 print "attribute error for I.__init__ got masked" 0371
Generated by PyXR 0.9.4