move sections
[python/dscho.git] / Lib / test / test_opcodes.py
blobc93ef86df2a94f36e589e1d0faa9c98edd1469b8
1 # Python test set -- part 2, opcodes
3 from test.test_support import run_unittest, check_py3k_warnings
4 import unittest
6 class OpcodeTest(unittest.TestCase):
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')
24 def test_raise_class_exceptions(self):
26 class AClass: pass
27 class BClass(AClass): pass
28 class CClass: pass
29 class DClass(AClass):
30 def __init__(self, ignore):
31 pass
33 try: raise AClass()
34 except: pass
36 try: raise AClass()
37 except AClass: pass
39 try: raise BClass()
40 except AClass: pass
42 try: raise BClass()
43 except CClass: self.fail()
44 except: pass
46 a = AClass()
47 b = BClass()
49 try: raise AClass, b
50 except BClass, v:
51 self.assertEqual(v, b)
52 else: self.fail("no exception")
54 try: raise b
55 except AClass, v:
56 self.assertEqual(v, b)
57 else:
58 self.fail("no exception")
60 # not enough arguments
61 try: raise BClass, a
62 except TypeError: pass
63 else: self.fail("no exception")
65 try: raise DClass, a
66 except DClass, v:
67 self.assertIsInstance(v, DClass)
68 else:
69 self.fail("no exception")
71 def test_compare_function_objects(self):
73 f = eval('lambda: None')
74 g = eval('lambda: None')
75 self.assertNotEquals(f, g)
77 f = eval('lambda a: a')
78 g = eval('lambda a: a')
79 self.assertNotEquals(f, g)
81 f = eval('lambda a=1: a')
82 g = eval('lambda a=1: a')
83 self.assertNotEquals(f, g)
85 f = eval('lambda: 0')
86 g = eval('lambda: 1')
87 self.assertNotEquals(f, g)
89 f = eval('lambda: None')
90 g = eval('lambda a: None')
91 self.assertNotEquals(f, g)
93 f = eval('lambda a: None')
94 g = eval('lambda b: None')
95 self.assertNotEquals(f, g)
97 f = eval('lambda a: None')
98 g = eval('lambda a=None: None')
99 self.assertNotEquals(f, g)
101 f = eval('lambda a=0: None')
102 g = eval('lambda a=1: None')
103 self.assertNotEquals(f, g)
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)
112 def 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)
120 if __name__ == '__main__':
121 test_main()