1# Python test set -- part 2, opcodes 2 3from test.test_support import run_unittest, check_py3k_warnings 4import unittest 5 6class OpcodeTest(unittest.TestCase): 7 8 def test_try_inside_for_loop(self): 9 n = 0 10 for i in range(10): 11 n = n+i 12 try: 1 // 0 13 except NameError: pass 14 except ZeroDivisionError: pass 15 except TypeError: pass 16 try: pass 17 except: pass 18 try: pass 19 finally: pass 20 n = n+i 21 if n != 90: 22 self.fail('try inside for') 23 24 def test_raise_class_exceptions(self): 25 26 class AClass: pass 27 class BClass(AClass): pass 28 class CClass: pass 29 class DClass(AClass): 30 def __init__(self, ignore): 31 pass 32 33 try: raise AClass() 34 except: pass 35 36 try: raise AClass() 37 except AClass: pass 38 39 try: raise BClass() 40 except AClass: pass 41 42 try: raise BClass() 43 except CClass: self.fail() 44 except: pass 45 46 a = AClass() 47 b = BClass() 48 49 try: raise AClass, b 50 except BClass, v: 51 self.assertEqual(v, b) 52 else: self.fail("no exception") 53 54 try: raise b 55 except AClass, v: 56 self.assertEqual(v, b) 57 else: 58 self.fail("no exception") 59 60 # not enough arguments 61 try: raise BClass, a 62 except TypeError: pass 63 else: self.fail("no exception") 64 65 try: raise DClass, a 66 except DClass, v: 67 self.assertIsInstance(v, DClass) 68 else: 69 self.fail("no exception") 70 71 def test_compare_function_objects(self): 72 73 f = eval('lambda: None') 74 g = eval('lambda: None') 75 self.assertNotEqual(f, g) 76 77 f = eval('lambda a: a') 78 g = eval('lambda a: a') 79 self.assertNotEqual(f, g) 80 81 f = eval('lambda a=1: a') 82 g = eval('lambda a=1: a') 83 self.assertNotEqual(f, g) 84 85 f = eval('lambda: 0') 86 g = eval('lambda: 1') 87 self.assertNotEqual(f, g) 88 89 f = eval('lambda: None') 90 g = eval('lambda a: None') 91 self.assertNotEqual(f, g) 92 93 f = eval('lambda a: None') 94 g = eval('lambda b: None') 95 self.assertNotEqual(f, g) 96 97 f = eval('lambda a: None') 98 g = eval('lambda a=None: None') 99 self.assertNotEqual(f, g) 100 101 f = eval('lambda a=0: None') 102 g = eval('lambda a=1: None') 103 self.assertNotEqual(f, g) 104 105 def test_modulo_of_string_subclasses(self): 106 class MyString(str): 107 def __mod__(self, value): 108 return 42 109 self.assertEqual(MyString() % 3, 42) 110 111 112def test_main(): 113 with check_py3k_warnings(("exceptions must derive from BaseException", 114 DeprecationWarning), 115 ("catching classes that don't inherit " 116 "from BaseException is not allowed", 117 DeprecationWarning)): 118 run_unittest(OpcodeTest) 119 120if __name__ == '__main__': 121 test_main() 122