ยปCore Development>Code coverage>Lib/test/test_opcodes.py

Python code coverage for Lib/test/test_opcodes.py

#countcontent
1n/a# Python test set -- part 2, opcodes
2n/a
3n/aimport unittest
4n/afrom test import ann_module
5n/a
6n/aclass OpcodeTest(unittest.TestCase):
7n/a
8n/a def test_try_inside_for_loop(self):
9n/a n = 0
10n/a for i in range(10):
11n/a n = n+i
12n/a try: 1/0
13n/a except NameError: pass
14n/a except ZeroDivisionError: pass
15n/a except TypeError: pass
16n/a try: pass
17n/a except: pass
18n/a try: pass
19n/a finally: pass
20n/a n = n+i
21n/a if n != 90:
22n/a self.fail('try inside for')
23n/a
24n/a def test_setup_annotations_line(self):
25n/a # check that SETUP_ANNOTATIONS does not create spurious line numbers
26n/a try:
27n/a with open(ann_module.__file__) as f:
28n/a txt = f.read()
29n/a co = compile(txt, ann_module.__file__, 'exec')
30n/a self.assertEqual(co.co_firstlineno, 6)
31n/a except OSError:
32n/a pass
33n/a
34n/a def test_no_annotations_if_not_needed(self):
35n/a class C: pass
36n/a with self.assertRaises(AttributeError):
37n/a C.__annotations__
38n/a
39n/a def test_use_existing_annotations(self):
40n/a ns = {'__annotations__': {1: 2}}
41n/a exec('x: int', ns)
42n/a self.assertEqual(ns['__annotations__'], {'x': int, 1: 2})
43n/a
44n/a def test_do_not_recreate_annotations(self):
45n/a class C:
46n/a del __annotations__
47n/a with self.assertRaises(NameError):
48n/a x: int
49n/a
50n/a def test_raise_class_exceptions(self):
51n/a
52n/a class AClass(Exception): pass
53n/a class BClass(AClass): pass
54n/a class CClass(Exception): pass
55n/a class DClass(AClass):
56n/a def __init__(self, ignore):
57n/a pass
58n/a
59n/a try: raise AClass()
60n/a except: pass
61n/a
62n/a try: raise AClass()
63n/a except AClass: pass
64n/a
65n/a try: raise BClass()
66n/a except AClass: pass
67n/a
68n/a try: raise BClass()
69n/a except CClass: self.fail()
70n/a except: pass
71n/a
72n/a a = AClass()
73n/a b = BClass()
74n/a
75n/a try:
76n/a raise b
77n/a except AClass as v:
78n/a self.assertEqual(v, b)
79n/a else:
80n/a self.fail("no exception")
81n/a
82n/a # not enough arguments
83n/a ##try: raise BClass, a
84n/a ##except TypeError: pass
85n/a ##else: self.fail("no exception")
86n/a
87n/a try: raise DClass(a)
88n/a except DClass as v:
89n/a self.assertIsInstance(v, DClass)
90n/a else:
91n/a self.fail("no exception")
92n/a
93n/a def test_compare_function_objects(self):
94n/a
95n/a f = eval('lambda: None')
96n/a g = eval('lambda: None')
97n/a self.assertNotEqual(f, g)
98n/a
99n/a f = eval('lambda a: a')
100n/a g = eval('lambda a: a')
101n/a self.assertNotEqual(f, g)
102n/a
103n/a f = eval('lambda a=1: a')
104n/a g = eval('lambda a=1: a')
105n/a self.assertNotEqual(f, g)
106n/a
107n/a f = eval('lambda: 0')
108n/a g = eval('lambda: 1')
109n/a self.assertNotEqual(f, g)
110n/a
111n/a f = eval('lambda: None')
112n/a g = eval('lambda a: None')
113n/a self.assertNotEqual(f, g)
114n/a
115n/a f = eval('lambda a: None')
116n/a g = eval('lambda b: None')
117n/a self.assertNotEqual(f, g)
118n/a
119n/a f = eval('lambda a: None')
120n/a g = eval('lambda a=None: None')
121n/a self.assertNotEqual(f, g)
122n/a
123n/a f = eval('lambda a=0: None')
124n/a g = eval('lambda a=1: None')
125n/a self.assertNotEqual(f, g)
126n/a
127n/a def test_modulo_of_string_subclasses(self):
128n/a class MyString(str):
129n/a def __mod__(self, value):
130n/a return 42
131n/a self.assertEqual(MyString() % 3, 42)
132n/a
133n/a
134n/aif __name__ == '__main__':
135n/a unittest.main()