1# Python test set -- part 2, opcodes
2
3import unittest
4from test import ann_module, support
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_setup_annotations_line(self):
25        # check that SETUP_ANNOTATIONS does not create spurious line numbers
26        try:
27            with open(ann_module.__file__) as f:
28                txt = f.read()
29            co = compile(txt, ann_module.__file__, 'exec')
30            self.assertEqual(co.co_firstlineno, 3)
31        except OSError:
32            pass
33
34    def test_no_annotations_if_not_needed(self):
35        class C: pass
36        with self.assertRaises(AttributeError):
37            C.__annotations__
38
39    def test_use_existing_annotations(self):
40        ns = {'__annotations__': {1: 2}}
41        exec('x: int', ns)
42        self.assertEqual(ns['__annotations__'], {'x': int, 1: 2})
43
44    def test_do_not_recreate_annotations(self):
45        # Don't rely on the existence of the '__annotations__' global.
46        with support.swap_item(globals(), '__annotations__', {}):
47            del globals()['__annotations__']
48            class C:
49                del __annotations__
50                with self.assertRaises(NameError):
51                    x: int
52
53    def test_raise_class_exceptions(self):
54
55        class AClass(Exception): pass
56        class BClass(AClass): pass
57        class CClass(Exception): pass
58        class DClass(AClass):
59            def __init__(self, ignore):
60                pass
61
62        try: raise AClass()
63        except: pass
64
65        try: raise AClass()
66        except AClass: pass
67
68        try: raise BClass()
69        except AClass: pass
70
71        try: raise BClass()
72        except CClass: self.fail()
73        except: pass
74
75        a = AClass()
76        b = BClass()
77
78        try:
79            raise b
80        except AClass as v:
81            self.assertEqual(v, b)
82        else:
83            self.fail("no exception")
84
85        # not enough arguments
86        ##try:  raise BClass, a
87        ##except TypeError: pass
88        ##else: self.fail("no exception")
89
90        try:  raise DClass(a)
91        except DClass as v:
92            self.assertIsInstance(v, DClass)
93        else:
94            self.fail("no exception")
95
96    def test_compare_function_objects(self):
97
98        f = eval('lambda: None')
99        g = eval('lambda: None')
100        self.assertNotEqual(f, g)
101
102        f = eval('lambda a: a')
103        g = eval('lambda a: a')
104        self.assertNotEqual(f, g)
105
106        f = eval('lambda a=1: a')
107        g = eval('lambda a=1: a')
108        self.assertNotEqual(f, g)
109
110        f = eval('lambda: 0')
111        g = eval('lambda: 1')
112        self.assertNotEqual(f, g)
113
114        f = eval('lambda: None')
115        g = eval('lambda a: None')
116        self.assertNotEqual(f, g)
117
118        f = eval('lambda a: None')
119        g = eval('lambda b: None')
120        self.assertNotEqual(f, g)
121
122        f = eval('lambda a: None')
123        g = eval('lambda a=None: None')
124        self.assertNotEqual(f, g)
125
126        f = eval('lambda a=0: None')
127        g = eval('lambda a=1: None')
128        self.assertNotEqual(f, g)
129
130    def test_modulo_of_string_subclasses(self):
131        class MyString(str):
132            def __mod__(self, value):
133                return 42
134        self.assertEqual(MyString() % 3, 42)
135
136
137if __name__ == '__main__':
138    unittest.main()
139