blob: 8b7fd9e7a88f4f9dabd537d4c251863b00a5f0e8 [file] [log] [blame]
Guido van Rossum3bead091992-01-27 17:00:37 +00001# Python test set -- part 2, opcodes
2
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test.support import run_unittest
Thomas Wouters89f507f2006-12-13 04:49:30 +00004import 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(Exception): pass
27 class BClass(AClass): pass
28 class CClass(Exception): 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
Collin Winter828f04a2007-08-31 00:04:24 +000049 try:
50 raise b
Guido van Rossumb940e112007-01-10 16:19:56 +000051 except AClass as v:
Collin Winter828f04a2007-08-31 00:04:24 +000052 self.assertEqual(v, b)
Thomas Wouters89f507f2006-12-13 04:49:30 +000053 else:
54 self.fail("no exception")
55
56 # not enough arguments
57 ##try: raise BClass, a
58 ##except TypeError: pass
59 ##else: self.fail("no exception")
60
Collin Winter828f04a2007-08-31 00:04:24 +000061 try: raise DClass(a)
Guido van Rossumb940e112007-01-10 16:19:56 +000062 except DClass as v:
Georg Brandlab91fde2009-08-13 08:51:18 +000063 self.assertTrue(isinstance(v, DClass))
Thomas Wouters89f507f2006-12-13 04:49:30 +000064 else:
65 self.fail("no exception")
66
67 def test_compare_function_objects(self):
68
69 f = eval('lambda: None')
70 g = eval('lambda: None')
Georg Brandlb533e262008-05-25 18:19:30 +000071 self.assertNotEquals(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +000072
73 f = eval('lambda a: a')
74 g = eval('lambda a: a')
Georg Brandlb533e262008-05-25 18:19:30 +000075 self.assertNotEquals(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +000076
77 f = eval('lambda a=1: a')
78 g = eval('lambda a=1: a')
Georg Brandlb533e262008-05-25 18:19:30 +000079 self.assertNotEquals(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +000080
81 f = eval('lambda: 0')
82 g = eval('lambda: 1')
Georg Brandlb533e262008-05-25 18:19:30 +000083 self.assertNotEquals(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +000084
85 f = eval('lambda: None')
86 g = eval('lambda a: None')
Georg Brandlb533e262008-05-25 18:19:30 +000087 self.assertNotEquals(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +000088
89 f = eval('lambda a: None')
90 g = eval('lambda b: None')
Georg Brandlb533e262008-05-25 18:19:30 +000091 self.assertNotEquals(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +000092
93 f = eval('lambda a: None')
94 g = eval('lambda a=None: None')
Georg Brandlb533e262008-05-25 18:19:30 +000095 self.assertNotEquals(f, g)
Thomas Wouters89f507f2006-12-13 04:49:30 +000096
97 f = eval('lambda a=0: None')
98 g = eval('lambda a=1: None')
Georg Brandlb533e262008-05-25 18:19:30 +000099 self.assertNotEquals(f, g)
Guido van Rossum3bead091992-01-27 17:00:37 +0000100
Benjamin Petersonefb06b02009-02-26 18:55:48 +0000101 def test_modulo_of_string_subclasses(self):
102 class MyString(str):
103 def __mod__(self, value):
104 return 42
105 self.assertEqual(MyString() % 3, 42)
106
Guido van Rossum3bead091992-01-27 17:00:37 +0000107
Thomas Wouters89f507f2006-12-13 04:49:30 +0000108def test_main():
109 run_unittest(OpcodeTest)
Guido van Rossum3bead091992-01-27 17:00:37 +0000110
Thomas Wouters89f507f2006-12-13 04:49:30 +0000111if __name__ == '__main__':
112 test_main()