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