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