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