1 | n/a | """ |
---|
2 | n/a | Test the API of the symtable module. |
---|
3 | n/a | """ |
---|
4 | n/a | import symtable |
---|
5 | n/a | import unittest |
---|
6 | n/a | |
---|
7 | n/a | |
---|
8 | n/a | |
---|
9 | n/a | TEST_CODE = """ |
---|
10 | n/a | import sys |
---|
11 | n/a | |
---|
12 | n/a | glob = 42 |
---|
13 | n/a | |
---|
14 | n/a | class Mine: |
---|
15 | n/a | instance_var = 24 |
---|
16 | n/a | def a_method(p1, p2): |
---|
17 | n/a | pass |
---|
18 | n/a | |
---|
19 | n/a | def spam(a, b, *var, **kw): |
---|
20 | n/a | global bar |
---|
21 | n/a | bar = 47 |
---|
22 | n/a | x = 23 |
---|
23 | n/a | glob |
---|
24 | n/a | def internal(): |
---|
25 | n/a | return x |
---|
26 | n/a | return internal |
---|
27 | n/a | |
---|
28 | n/a | def foo(): |
---|
29 | n/a | pass |
---|
30 | n/a | |
---|
31 | n/a | def namespace_test(): pass |
---|
32 | n/a | def namespace_test(): pass |
---|
33 | n/a | """ |
---|
34 | n/a | |
---|
35 | n/a | |
---|
36 | n/a | def find_block(block, name): |
---|
37 | n/a | for ch in block.get_children(): |
---|
38 | n/a | if ch.get_name() == name: |
---|
39 | n/a | return ch |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | class SymtableTest(unittest.TestCase): |
---|
43 | n/a | |
---|
44 | n/a | top = symtable.symtable(TEST_CODE, "?", "exec") |
---|
45 | n/a | # These correspond to scopes in TEST_CODE |
---|
46 | n/a | Mine = find_block(top, "Mine") |
---|
47 | n/a | a_method = find_block(Mine, "a_method") |
---|
48 | n/a | spam = find_block(top, "spam") |
---|
49 | n/a | internal = find_block(spam, "internal") |
---|
50 | n/a | foo = find_block(top, "foo") |
---|
51 | n/a | |
---|
52 | n/a | def test_type(self): |
---|
53 | n/a | self.assertEqual(self.top.get_type(), "module") |
---|
54 | n/a | self.assertEqual(self.Mine.get_type(), "class") |
---|
55 | n/a | self.assertEqual(self.a_method.get_type(), "function") |
---|
56 | n/a | self.assertEqual(self.spam.get_type(), "function") |
---|
57 | n/a | self.assertEqual(self.internal.get_type(), "function") |
---|
58 | n/a | |
---|
59 | n/a | def test_optimized(self): |
---|
60 | n/a | self.assertFalse(self.top.is_optimized()) |
---|
61 | n/a | self.assertFalse(self.top.has_exec()) |
---|
62 | n/a | |
---|
63 | n/a | self.assertTrue(self.spam.is_optimized()) |
---|
64 | n/a | |
---|
65 | n/a | def test_nested(self): |
---|
66 | n/a | self.assertFalse(self.top.is_nested()) |
---|
67 | n/a | self.assertFalse(self.Mine.is_nested()) |
---|
68 | n/a | self.assertFalse(self.spam.is_nested()) |
---|
69 | n/a | self.assertTrue(self.internal.is_nested()) |
---|
70 | n/a | |
---|
71 | n/a | def test_children(self): |
---|
72 | n/a | self.assertTrue(self.top.has_children()) |
---|
73 | n/a | self.assertTrue(self.Mine.has_children()) |
---|
74 | n/a | self.assertFalse(self.foo.has_children()) |
---|
75 | n/a | |
---|
76 | n/a | def test_lineno(self): |
---|
77 | n/a | self.assertEqual(self.top.get_lineno(), 0) |
---|
78 | n/a | self.assertEqual(self.spam.get_lineno(), 11) |
---|
79 | n/a | |
---|
80 | n/a | def test_function_info(self): |
---|
81 | n/a | func = self.spam |
---|
82 | n/a | self.assertEqual(sorted(func.get_parameters()), ["a", "b", "kw", "var"]) |
---|
83 | n/a | expected = ["a", "b", "internal", "kw", "var", "x"] |
---|
84 | n/a | self.assertEqual(sorted(func.get_locals()), expected) |
---|
85 | n/a | self.assertEqual(sorted(func.get_globals()), ["bar", "glob"]) |
---|
86 | n/a | self.assertEqual(self.internal.get_frees(), ("x",)) |
---|
87 | n/a | |
---|
88 | n/a | def test_globals(self): |
---|
89 | n/a | self.assertTrue(self.spam.lookup("glob").is_global()) |
---|
90 | n/a | self.assertFalse(self.spam.lookup("glob").is_declared_global()) |
---|
91 | n/a | self.assertTrue(self.spam.lookup("bar").is_global()) |
---|
92 | n/a | self.assertTrue(self.spam.lookup("bar").is_declared_global()) |
---|
93 | n/a | self.assertFalse(self.internal.lookup("x").is_global()) |
---|
94 | n/a | self.assertFalse(self.Mine.lookup("instance_var").is_global()) |
---|
95 | n/a | |
---|
96 | n/a | def test_local(self): |
---|
97 | n/a | self.assertTrue(self.spam.lookup("x").is_local()) |
---|
98 | n/a | self.assertFalse(self.internal.lookup("x").is_local()) |
---|
99 | n/a | |
---|
100 | n/a | def test_referenced(self): |
---|
101 | n/a | self.assertTrue(self.internal.lookup("x").is_referenced()) |
---|
102 | n/a | self.assertTrue(self.spam.lookup("internal").is_referenced()) |
---|
103 | n/a | self.assertFalse(self.spam.lookup("x").is_referenced()) |
---|
104 | n/a | |
---|
105 | n/a | def test_parameters(self): |
---|
106 | n/a | for sym in ("a", "var", "kw"): |
---|
107 | n/a | self.assertTrue(self.spam.lookup(sym).is_parameter()) |
---|
108 | n/a | self.assertFalse(self.spam.lookup("x").is_parameter()) |
---|
109 | n/a | |
---|
110 | n/a | def test_symbol_lookup(self): |
---|
111 | n/a | self.assertEqual(len(self.top.get_identifiers()), |
---|
112 | n/a | len(self.top.get_symbols())) |
---|
113 | n/a | |
---|
114 | n/a | self.assertRaises(KeyError, self.top.lookup, "not_here") |
---|
115 | n/a | |
---|
116 | n/a | def test_namespaces(self): |
---|
117 | n/a | self.assertTrue(self.top.lookup("Mine").is_namespace()) |
---|
118 | n/a | self.assertTrue(self.Mine.lookup("a_method").is_namespace()) |
---|
119 | n/a | self.assertTrue(self.top.lookup("spam").is_namespace()) |
---|
120 | n/a | self.assertTrue(self.spam.lookup("internal").is_namespace()) |
---|
121 | n/a | self.assertTrue(self.top.lookup("namespace_test").is_namespace()) |
---|
122 | n/a | self.assertFalse(self.spam.lookup("x").is_namespace()) |
---|
123 | n/a | |
---|
124 | n/a | self.assertTrue(self.top.lookup("spam").get_namespace() is self.spam) |
---|
125 | n/a | ns_test = self.top.lookup("namespace_test") |
---|
126 | n/a | self.assertEqual(len(ns_test.get_namespaces()), 2) |
---|
127 | n/a | self.assertRaises(ValueError, ns_test.get_namespace) |
---|
128 | n/a | |
---|
129 | n/a | def test_assigned(self): |
---|
130 | n/a | self.assertTrue(self.spam.lookup("x").is_assigned()) |
---|
131 | n/a | self.assertTrue(self.spam.lookup("bar").is_assigned()) |
---|
132 | n/a | self.assertTrue(self.top.lookup("spam").is_assigned()) |
---|
133 | n/a | self.assertTrue(self.Mine.lookup("a_method").is_assigned()) |
---|
134 | n/a | self.assertFalse(self.internal.lookup("x").is_assigned()) |
---|
135 | n/a | |
---|
136 | n/a | def test_annotated(self): |
---|
137 | n/a | st1 = symtable.symtable('def f():\n x: int\n', 'test', 'exec') |
---|
138 | n/a | st2 = st1.get_children()[0] |
---|
139 | n/a | self.assertTrue(st2.lookup('x').is_local()) |
---|
140 | n/a | self.assertTrue(st2.lookup('x').is_annotated()) |
---|
141 | n/a | self.assertFalse(st2.lookup('x').is_global()) |
---|
142 | n/a | st3 = symtable.symtable('def f():\n x = 1\n', 'test', 'exec') |
---|
143 | n/a | st4 = st3.get_children()[0] |
---|
144 | n/a | self.assertTrue(st4.lookup('x').is_local()) |
---|
145 | n/a | self.assertFalse(st4.lookup('x').is_annotated()) |
---|
146 | n/a | |
---|
147 | n/a | def test_imported(self): |
---|
148 | n/a | self.assertTrue(self.top.lookup("sys").is_imported()) |
---|
149 | n/a | |
---|
150 | n/a | def test_name(self): |
---|
151 | n/a | self.assertEqual(self.top.get_name(), "top") |
---|
152 | n/a | self.assertEqual(self.spam.get_name(), "spam") |
---|
153 | n/a | self.assertEqual(self.spam.lookup("x").get_name(), "x") |
---|
154 | n/a | self.assertEqual(self.Mine.get_name(), "Mine") |
---|
155 | n/a | |
---|
156 | n/a | def test_class_info(self): |
---|
157 | n/a | self.assertEqual(self.Mine.get_methods(), ('a_method',)) |
---|
158 | n/a | |
---|
159 | n/a | def test_filename_correct(self): |
---|
160 | n/a | ### Bug tickler: SyntaxError file name correct whether error raised |
---|
161 | n/a | ### while parsing or building symbol table. |
---|
162 | n/a | def checkfilename(brokencode, offset): |
---|
163 | n/a | try: |
---|
164 | n/a | symtable.symtable(brokencode, "spam", "exec") |
---|
165 | n/a | except SyntaxError as e: |
---|
166 | n/a | self.assertEqual(e.filename, "spam") |
---|
167 | n/a | self.assertEqual(e.lineno, 1) |
---|
168 | n/a | self.assertEqual(e.offset, offset) |
---|
169 | n/a | else: |
---|
170 | n/a | self.fail("no SyntaxError for %r" % (brokencode,)) |
---|
171 | n/a | checkfilename("def f(x): foo)(", 14) # parse-time |
---|
172 | n/a | checkfilename("def f(x): global x", 10) # symtable-build-time |
---|
173 | n/a | symtable.symtable("pass", b"spam", "exec") |
---|
174 | n/a | with self.assertWarns(DeprecationWarning), \ |
---|
175 | n/a | self.assertRaises(TypeError): |
---|
176 | n/a | symtable.symtable("pass", bytearray(b"spam"), "exec") |
---|
177 | n/a | with self.assertWarns(DeprecationWarning): |
---|
178 | n/a | symtable.symtable("pass", memoryview(b"spam"), "exec") |
---|
179 | n/a | with self.assertRaises(TypeError): |
---|
180 | n/a | symtable.symtable("pass", list(b"spam"), "exec") |
---|
181 | n/a | |
---|
182 | n/a | def test_eval(self): |
---|
183 | n/a | symbols = symtable.symtable("42", "?", "eval") |
---|
184 | n/a | |
---|
185 | n/a | def test_single(self): |
---|
186 | n/a | symbols = symtable.symtable("42", "?", "single") |
---|
187 | n/a | |
---|
188 | n/a | def test_exec(self): |
---|
189 | n/a | symbols = symtable.symtable("def f(x): return x", "?", "exec") |
---|
190 | n/a | |
---|
191 | n/a | |
---|
192 | n/a | if __name__ == '__main__': |
---|
193 | n/a | unittest.main() |
---|