| 1 |
""" |
| 2 |
Test the API of the symtable module. |
| 3 |
""" |
| 4 |
|
| 5 |
import re |
| 6 |
import textwrap |
| 7 |
import symtable |
| 8 |
import unittest |
| 9 |
|
| 10 |
from test import support |
| 11 |
from test.support import os_helper |
| 12 |
|
| 13 |
|
| 14 |
TEST_CODE = """ |
| 15 |
import sys |
| 16 |
|
| 17 |
glob = 42 |
| 18 |
some_var = 12 |
| 19 |
some_non_assigned_global_var: int |
| 20 |
some_assigned_global_var = 11 |
| 21 |
|
| 22 |
class Mine: |
| 23 |
instance_var = 24 |
| 24 |
def a_method(p1, p2): |
| 25 |
pass |
| 26 |
|
| 27 |
def spam(a, b, *var, **kw): |
| 28 |
global bar |
| 29 |
global some_assigned_global_var |
| 30 |
some_assigned_global_var = 12 |
| 31 |
bar = 47 |
| 32 |
some_var = 10 |
| 33 |
x = 23 |
| 34 |
glob |
| 35 |
def internal(): |
| 36 |
return x |
| 37 |
def other_internal(): |
| 38 |
nonlocal some_var |
| 39 |
some_var = 3 |
| 40 |
return some_var |
| 41 |
return internal |
| 42 |
|
| 43 |
def foo(): |
| 44 |
pass |
| 45 |
|
| 46 |
def namespace_test(): pass |
| 47 |
def namespace_test(): pass |
| 48 |
|
| 49 |
type Alias = int |
| 50 |
type GenericAlias[T] = list[T] |
| 51 |
|
| 52 |
def generic_spam[T](a): |
| 53 |
pass |
| 54 |
|
| 55 |
class GenericMine[T: int, U: (int, str) = int]: |
| 56 |
pass |
| 57 |
""" |
| 58 |
|
| 59 |
TEST_COMPLEX_CLASS_CODE = """ |
| 60 |
# The following symbols are defined in ComplexClass |
| 61 |
# without being introduced by a 'global' statement. |
| 62 |
glob_unassigned_meth: Any |
| 63 |
glob_unassigned_meth_pep_695: Any |
| 64 |
|
| 65 |
glob_unassigned_async_meth: Any |
| 66 |
glob_unassigned_async_meth_pep_695: Any |
| 67 |
|
| 68 |
def glob_assigned_meth(): pass |
| 69 |
def glob_assigned_meth_pep_695[T](): pass |
| 70 |
|
| 71 |
async def glob_assigned_async_meth(): pass |
| 72 |
async def glob_assigned_async_meth_pep_695[T](): pass |
| 73 |
|
| 74 |
# The following symbols are defined in ComplexClass after |
| 75 |
# being introduced by a 'global' statement (and therefore |
| 76 |
# are not considered as local symbols of ComplexClass). |
| 77 |
glob_unassigned_meth_ignore: Any |
| 78 |
glob_unassigned_meth_pep_695_ignore: Any |
| 79 |
|
| 80 |
glob_unassigned_async_meth_ignore: Any |
| 81 |
glob_unassigned_async_meth_pep_695_ignore: Any |
| 82 |
|
| 83 |
def glob_assigned_meth_ignore(): pass |
| 84 |
def glob_assigned_meth_pep_695_ignore[T](): pass |
| 85 |
|
| 86 |
async def glob_assigned_async_meth_ignore(): pass |
| 87 |
async def glob_assigned_async_meth_pep_695_ignore[T](): pass |
| 88 |
|
| 89 |
class ComplexClass: |
| 90 |
a_var = 1234 |
| 91 |
a_genexpr = (x for x in []) |
| 92 |
a_lambda = lambda x: x |
| 93 |
|
| 94 |
type a_type_alias = int |
| 95 |
type a_type_alias_pep_695[T] = list[T] |
| 96 |
|
| 97 |
class a_class: pass |
| 98 |
class a_class_pep_695[T]: pass |
| 99 |
|
| 100 |
def a_method(self): pass |
| 101 |
def a_method_pep_695[T](self): pass |
| 102 |
|
| 103 |
async def an_async_method(self): pass |
| 104 |
async def an_async_method_pep_695[T](self): pass |
| 105 |
|
| 106 |
@classmethod |
| 107 |
def a_classmethod(cls): pass |
| 108 |
@classmethod |
| 109 |
def a_classmethod_pep_695[T](self): pass |
| 110 |
|
| 111 |
@classmethod |
| 112 |
async def an_async_classmethod(cls): pass |
| 113 |
@classmethod |
| 114 |
async def an_async_classmethod_pep_695[T](self): pass |
| 115 |
|
| 116 |
@staticmethod |
| 117 |
def a_staticmethod(): pass |
| 118 |
@staticmethod |
| 119 |
def a_staticmethod_pep_695[T](self): pass |
| 120 |
|
| 121 |
@staticmethod |
| 122 |
async def an_async_staticmethod(): pass |
| 123 |
@staticmethod |
| 124 |
async def an_async_staticmethod_pep_695[T](self): pass |
| 125 |
|
| 126 |
# These ones will be considered as methods because of the 'def' although |
| 127 |
# they are *not* valid methods at runtime since they are not decorated |
| 128 |
# with @staticmethod. |
| 129 |
def a_fakemethod(): pass |
| 130 |
def a_fakemethod_pep_695[T](): pass |
| 131 |
|
| 132 |
async def an_async_fakemethod(): pass |
| 133 |
async def an_async_fakemethod_pep_695[T](): pass |
| 134 |
|
| 135 |
# Check that those are still considered as methods |
| 136 |
# since they are not using the 'global' keyword. |
| 137 |
def glob_unassigned_meth(): pass |
| 138 |
def glob_unassigned_meth_pep_695[T](): pass |
| 139 |
|
| 140 |
async def glob_unassigned_async_meth(): pass |
| 141 |
async def glob_unassigned_async_meth_pep_695[T](): pass |
| 142 |
|
| 143 |
def glob_assigned_meth(): pass |
| 144 |
def glob_assigned_meth_pep_695[T](): pass |
| 145 |
|
| 146 |
async def glob_assigned_async_meth(): pass |
| 147 |
async def glob_assigned_async_meth_pep_695[T](): pass |
| 148 |
|
| 149 |
# The following are not picked as local symbols because they are not |
| 150 |
# visible by the class at runtime (this is equivalent to having the |
| 151 |
# definitions outside of the class). |
| 152 |
global glob_unassigned_meth_ignore |
| 153 |
def glob_unassigned_meth_ignore(): pass |
| 154 |
global glob_unassigned_meth_pep_695_ignore |
| 155 |
def glob_unassigned_meth_pep_695_ignore[T](): pass |
| 156 |
|
| 157 |
global glob_unassigned_async_meth_ignore |
| 158 |
async def glob_unassigned_async_meth_ignore(): pass |
| 159 |
global glob_unassigned_async_meth_pep_695_ignore |
| 160 |
async def glob_unassigned_async_meth_pep_695_ignore[T](): pass |
| 161 |
|
| 162 |
global glob_assigned_meth_ignore |
| 163 |
def glob_assigned_meth_ignore(): pass |
| 164 |
global glob_assigned_meth_pep_695_ignore |
| 165 |
def glob_assigned_meth_pep_695_ignore[T](): pass |
| 166 |
|
| 167 |
global glob_assigned_async_meth_ignore |
| 168 |
async def glob_assigned_async_meth_ignore(): pass |
| 169 |
global glob_assigned_async_meth_pep_695_ignore |
| 170 |
async def glob_assigned_async_meth_pep_695_ignore[T](): pass |
| 171 |
""" |
| 172 |
|
| 173 |
|
| 174 |
def find_block(block, name): |
| 175 |
for ch in block.get_children(): |
| 176 |
if ch.get_name() == name: |
| 177 |
return ch |
| 178 |
|
| 179 |
|
| 180 |
class SymtableTest(unittest.TestCase): |
| 181 |
|
| 182 |
top = symtable.symtable(TEST_CODE, "?", "exec") |
| 183 |
# These correspond to scopes in TEST_CODE |
| 184 |
Mine = find_block(top, "Mine") |
| 185 |
|
| 186 |
a_method = find_block(Mine, "a_method") |
| 187 |
spam = find_block(top, "spam") |
| 188 |
internal = find_block(spam, "internal") |
| 189 |
other_internal = find_block(spam, "other_internal") |
| 190 |
foo = find_block(top, "foo") |
| 191 |
Alias = find_block(top, "Alias") |
| 192 |
GenericAlias = find_block(top, "GenericAlias") |
| 193 |
GenericAlias_inner = find_block(GenericAlias, "GenericAlias") |
| 194 |
generic_spam = find_block(top, "generic_spam") |
| 195 |
generic_spam_inner = find_block(generic_spam, "generic_spam") |
| 196 |
GenericMine = find_block(top, "GenericMine") |
| 197 |
GenericMine_inner = find_block(GenericMine, "GenericMine") |
| 198 |
T = find_block(GenericMine, "T") |
| 199 |
U = find_block(GenericMine, "U") |
| 200 |
|
| 201 |
def test_type(self): |
| 202 |
self.assertEqual(self.top.get_type(), "module") |
| 203 |
self.assertEqual(self.Mine.get_type(), "class") |
| 204 |
self.assertEqual(self.a_method.get_type(), "function") |
| 205 |
self.assertEqual(self.spam.get_type(), "function") |
| 206 |
self.assertEqual(self.internal.get_type(), "function") |
| 207 |
self.assertEqual(self.foo.get_type(), "function") |
| 208 |
self.assertEqual(self.Alias.get_type(), "type alias") |
| 209 |
self.assertEqual(self.GenericAlias.get_type(), "type parameters") |
| 210 |
self.assertEqual(self.GenericAlias_inner.get_type(), "type alias") |
| 211 |
self.assertEqual(self.generic_spam.get_type(), "type parameters") |
| 212 |
self.assertEqual(self.generic_spam_inner.get_type(), "function") |
| 213 |
self.assertEqual(self.GenericMine.get_type(), "type parameters") |
| 214 |
self.assertEqual(self.GenericMine_inner.get_type(), "class") |
| 215 |
self.assertEqual(self.T.get_type(), "type variable") |
| 216 |
self.assertEqual(self.U.get_type(), "type variable") |
| 217 |
|
| 218 |
def test_id(self): |
| 219 |
self.assertGreater(self.top.get_id(), 0) |
| 220 |
self.assertGreater(self.Mine.get_id(), 0) |
| 221 |
self.assertGreater(self.a_method.get_id(), 0) |
| 222 |
self.assertGreater(self.spam.get_id(), 0) |
| 223 |
self.assertGreater(self.internal.get_id(), 0) |
| 224 |
self.assertGreater(self.foo.get_id(), 0) |
| 225 |
self.assertGreater(self.Alias.get_id(), 0) |
| 226 |
self.assertGreater(self.GenericAlias.get_id(), 0) |
| 227 |
self.assertGreater(self.generic_spam.get_id(), 0) |
| 228 |
self.assertGreater(self.GenericMine.get_id(), 0) |
| 229 |
|
| 230 |
def test_optimized(self): |
| 231 |
self.assertFalse(self.top.is_optimized()) |
| 232 |
|
| 233 |
self.assertTrue(self.spam.is_optimized()) |
| 234 |
|
| 235 |
def test_nested(self): |
| 236 |
self.assertFalse(self.top.is_nested()) |
| 237 |
self.assertFalse(self.Mine.is_nested()) |
| 238 |
self.assertFalse(self.spam.is_nested()) |
| 239 |
self.assertTrue(self.internal.is_nested()) |
| 240 |
|
| 241 |
def test_children(self): |
| 242 |
self.assertTrue(self.top.has_children()) |
| 243 |
self.assertTrue(self.Mine.has_children()) |
| 244 |
self.assertFalse(self.foo.has_children()) |
| 245 |
|
| 246 |
def test_lineno(self): |
| 247 |
self.assertEqual(self.top.get_lineno(), 0) |
| 248 |
self.assertEqual(self.spam.get_lineno(), 14) |
| 249 |
|
| 250 |
def test_function_info(self): |
| 251 |
func = self.spam |
| 252 |
self.assertEqual(sorted(func.get_parameters()), ["a", "b", "kw", "var"]) |
| 253 |
expected = ['a', 'b', 'internal', 'kw', 'other_internal', 'some_var', 'var', 'x'] |
| 254 |
self.assertEqual(sorted(func.get_locals()), expected) |
| 255 |
self.assertEqual(sorted(func.get_globals()), ["bar", "glob", "some_assigned_global_var"]) |
| 256 |
self.assertEqual(self.internal.get_frees(), ("x",)) |
| 257 |
|
| 258 |
def test_globals(self): |
| 259 |
self.assertTrue(self.spam.lookup("glob").is_global()) |
| 260 |
self.assertFalse(self.spam.lookup("glob").is_declared_global()) |
| 261 |
self.assertTrue(self.spam.lookup("bar").is_global()) |
| 262 |
self.assertTrue(self.spam.lookup("bar").is_declared_global()) |
| 263 |
self.assertFalse(self.internal.lookup("x").is_global()) |
| 264 |
self.assertFalse(self.Mine.lookup("instance_var").is_global()) |
| 265 |
self.assertTrue(self.spam.lookup("bar").is_global()) |
| 266 |
# Module-scope globals are both global and local |
| 267 |
self.assertTrue(self.top.lookup("some_non_assigned_global_var").is_global()) |
| 268 |
self.assertTrue(self.top.lookup("some_assigned_global_var").is_global()) |
| 269 |
|
| 270 |
def test_nonlocal(self): |
| 271 |
self.assertFalse(self.spam.lookup("some_var").is_nonlocal()) |
| 272 |
self.assertTrue(self.other_internal.lookup("some_var").is_nonlocal()) |
| 273 |
expected = ("some_var",) |
| 274 |
self.assertEqual(self.other_internal.get_nonlocals(), expected) |
| 275 |
|
| 276 |
def test_local(self): |
| 277 |
self.assertTrue(self.spam.lookup("x").is_local()) |
| 278 |
self.assertFalse(self.spam.lookup("bar").is_local()) |
| 279 |
# Module-scope globals are both global and local |
| 280 |
self.assertTrue(self.top.lookup("some_non_assigned_global_var").is_local()) |
| 281 |
self.assertTrue(self.top.lookup("some_assigned_global_var").is_local()) |
| 282 |
|
| 283 |
def test_free(self): |
| 284 |
self.assertTrue(self.internal.lookup("x").is_free()) |
| 285 |
|
| 286 |
def test_referenced(self): |
| 287 |
self.assertTrue(self.internal.lookup("x").is_referenced()) |
| 288 |
self.assertTrue(self.spam.lookup("internal").is_referenced()) |
| 289 |
self.assertFalse(self.spam.lookup("x").is_referenced()) |
| 290 |
|
| 291 |
def test_parameters(self): |
| 292 |
for sym in ("a", "var", "kw"): |
| 293 |
self.assertTrue(self.spam.lookup(sym).is_parameter()) |
| 294 |
self.assertFalse(self.spam.lookup("x").is_parameter()) |
| 295 |
|
| 296 |
def test_symbol_lookup(self): |
| 297 |
self.assertEqual(len(self.top.get_identifiers()), |
| 298 |
len(self.top.get_symbols())) |
| 299 |
|
| 300 |
self.assertRaises(KeyError, self.top.lookup, "not_here") |
| 301 |
|
| 302 |
def test_namespaces(self): |
| 303 |
self.assertTrue(self.top.lookup("Mine").is_namespace()) |
| 304 |
self.assertTrue(self.Mine.lookup("a_method").is_namespace()) |
| 305 |
self.assertTrue(self.top.lookup("spam").is_namespace()) |
| 306 |
self.assertTrue(self.spam.lookup("internal").is_namespace()) |
| 307 |
self.assertTrue(self.top.lookup("namespace_test").is_namespace()) |
| 308 |
self.assertFalse(self.spam.lookup("x").is_namespace()) |
| 309 |
|
| 310 |
self.assertTrue(self.top.lookup("spam").get_namespace() is self.spam) |
| 311 |
ns_test = self.top.lookup("namespace_test") |
| 312 |
self.assertEqual(len(ns_test.get_namespaces()), 2) |
| 313 |
self.assertRaises(ValueError, ns_test.get_namespace) |
| 314 |
|
| 315 |
ns_test_2 = self.top.lookup("glob") |
| 316 |
self.assertEqual(len(ns_test_2.get_namespaces()), 0) |
| 317 |
self.assertRaises(ValueError, ns_test_2.get_namespace) |
| 318 |
|
| 319 |
def test_assigned(self): |
| 320 |
self.assertTrue(self.spam.lookup("x").is_assigned()) |
| 321 |
self.assertTrue(self.spam.lookup("bar").is_assigned()) |
| 322 |
self.assertTrue(self.top.lookup("spam").is_assigned()) |
| 323 |
self.assertTrue(self.Mine.lookup("a_method").is_assigned()) |
| 324 |
self.assertFalse(self.internal.lookup("x").is_assigned()) |
| 325 |
|
| 326 |
def test_annotated(self): |
| 327 |
st1 = symtable.symtable('def f():\n x: int\n', 'test', 'exec') |
| 328 |
st2 = st1.get_children()[1] |
| 329 |
self.assertEqual(st2.get_type(), "function") |
| 330 |
self.assertTrue(st2.lookup('x').is_local()) |
| 331 |
self.assertTrue(st2.lookup('x').is_annotated()) |
| 332 |
self.assertFalse(st2.lookup('x').is_global()) |
| 333 |
st3 = symtable.symtable('def f():\n x = 1\n', 'test', 'exec') |
| 334 |
st4 = st3.get_children()[1] |
| 335 |
self.assertEqual(st4.get_type(), "function") |
| 336 |
self.assertTrue(st4.lookup('x').is_local()) |
| 337 |
self.assertFalse(st4.lookup('x').is_annotated()) |
| 338 |
|
| 339 |
# Test that annotations in the global scope are valid after the |
| 340 |
# variable is declared as nonlocal. |
| 341 |
st5 = symtable.symtable('global x\nx: int', 'test', 'exec') |
| 342 |
self.assertTrue(st5.lookup("x").is_global()) |
| 343 |
|
| 344 |
# Test that annotations for nonlocals are valid after the |
| 345 |
# variable is declared as nonlocal. |
| 346 |
st6 = symtable.symtable('def g():\n' |
| 347 |
' x = 2\n' |
| 348 |
' def f():\n' |
| 349 |
' nonlocal x\n' |
| 350 |
' x: int', |
| 351 |
'test', 'exec') |
| 352 |
|
| 353 |
def test_imported(self): |
| 354 |
self.assertTrue(self.top.lookup("sys").is_imported()) |
| 355 |
|
| 356 |
def test_name(self): |
| 357 |
self.assertEqual(self.top.get_name(), "top") |
| 358 |
self.assertEqual(self.spam.get_name(), "spam") |
| 359 |
self.assertEqual(self.spam.lookup("x").get_name(), "x") |
| 360 |
self.assertEqual(self.Mine.get_name(), "Mine") |
| 361 |
|
| 362 |
def test_class_get_methods(self): |
| 363 |
deprecation_mess = ( |
| 364 |
re.escape('symtable.Class.get_methods() is deprecated ' |
| 365 |
'and will be removed in Python 3.16.') |
| 366 |
) |
| 367 |
|
| 368 |
with self.assertWarnsRegex(DeprecationWarning, deprecation_mess): |
| 369 |
self.assertEqual(self.Mine.get_methods(), ('a_method',)) |
| 370 |
|
| 371 |
top = symtable.symtable(TEST_COMPLEX_CLASS_CODE, "?", "exec") |
| 372 |
this = find_block(top, "ComplexClass") |
| 373 |
|
| 374 |
with self.assertWarnsRegex(DeprecationWarning, deprecation_mess): |
| 375 |
self.assertEqual(this.get_methods(), ( |
| 376 |
'a_method', 'a_method_pep_695', |
| 377 |
'an_async_method', 'an_async_method_pep_695', |
| 378 |
'a_classmethod', 'a_classmethod_pep_695', |
| 379 |
'an_async_classmethod', 'an_async_classmethod_pep_695', |
| 380 |
'a_staticmethod', 'a_staticmethod_pep_695', |
| 381 |
'an_async_staticmethod', 'an_async_staticmethod_pep_695', |
| 382 |
'a_fakemethod', 'a_fakemethod_pep_695', |
| 383 |
'an_async_fakemethod', 'an_async_fakemethod_pep_695', |
| 384 |
'glob_unassigned_meth', 'glob_unassigned_meth_pep_695', |
| 385 |
'glob_unassigned_async_meth', 'glob_unassigned_async_meth_pep_695', |
| 386 |
'glob_assigned_meth', 'glob_assigned_meth_pep_695', |
| 387 |
'glob_assigned_async_meth', 'glob_assigned_async_meth_pep_695', |
| 388 |
)) |
| 389 |
|
| 390 |
# Test generator expressions that are of type TYPE_FUNCTION |
| 391 |
# but will not be reported by get_methods() since they are |
| 392 |
# not functions per se. |
| 393 |
# |
| 394 |
# Other kind of comprehensions such as list, set or dict |
| 395 |
# expressions do not have the TYPE_FUNCTION type. |
| 396 |
|
| 397 |
def check_body(body, expected_methods): |
| 398 |
indented = textwrap.indent(body, ' ' * 4) |
| 399 |
top = symtable.symtable(f"class A:\n{indented}", "?", "exec") |
| 400 |
this = find_block(top, "A") |
| 401 |
with self.assertWarnsRegex(DeprecationWarning, deprecation_mess): |
| 402 |
self.assertEqual(this.get_methods(), expected_methods) |
| 403 |
|
| 404 |
# statements with 'genexpr' inside it |
| 405 |
GENEXPRS = ( |
| 406 |
'x = (x for x in [])', |
| 407 |
'x = (x async for x in [])', |
| 408 |
'type x[genexpr = (x for x in [])] = (x for x in [])', |
| 409 |
'type x[genexpr = (x async for x in [])] = (x async for x in [])', |
| 410 |
'genexpr = (x for x in [])', |
| 411 |
'genexpr = (x async for x in [])', |
| 412 |
'type genexpr[genexpr = (x for x in [])] = (x for x in [])', |
| 413 |
'type genexpr[genexpr = (x async for x in [])] = (x async for x in [])', |
| 414 |
) |
| 415 |
|
| 416 |
for gen in GENEXPRS: |
| 417 |
# test generator expression |
| 418 |
with self.subTest(gen=gen): |
| 419 |
check_body(gen, ()) |
| 420 |
|
| 421 |
# test generator expression + variable named 'genexpr' |
| 422 |
with self.subTest(gen=gen, isvar=True): |
| 423 |
check_body('\n'.join((gen, 'genexpr = 1')), ()) |
| 424 |
check_body('\n'.join(('genexpr = 1', gen)), ()) |
| 425 |
|
| 426 |
for paramlist in ('()', '(x)', '(x, y)', '(z: T)'): |
| 427 |
for func in ( |
| 428 |
f'def genexpr{paramlist}:pass', |
| 429 |
f'async def genexpr{paramlist}:pass', |
| 430 |
f'def genexpr[T]{paramlist}:pass', |
| 431 |
f'async def genexpr[T]{paramlist}:pass', |
| 432 |
): |
| 433 |
with self.subTest(func=func): |
| 434 |
# test function named 'genexpr' |
| 435 |
check_body(func, ('genexpr',)) |
| 436 |
|
| 437 |
for gen in GENEXPRS: |
| 438 |
with self.subTest(gen=gen, func=func): |
| 439 |
# test generator expression + function named 'genexpr' |
| 440 |
check_body('\n'.join((gen, func)), ('genexpr',)) |
| 441 |
check_body('\n'.join((func, gen)), ('genexpr',)) |
| 442 |
|
| 443 |
def test_filename_correct(self): |
| 444 |
### Bug tickler: SyntaxError file name correct whether error raised |
| 445 |
### while parsing or building symbol table. |
| 446 |
def checkfilename(brokencode, offset): |
| 447 |
try: |
| 448 |
symtable.symtable(brokencode, "spam", "exec") |
| 449 |
except SyntaxError as e: |
| 450 |
self.assertEqual(e.filename, "spam") |
| 451 |
self.assertEqual(e.lineno, 1) |
| 452 |
self.assertEqual(e.offset, offset) |
| 453 |
else: |
| 454 |
self.fail("no SyntaxError for %r" % (brokencode,)) |
| 455 |
checkfilename("def f(x): foo)(", 14) # parse-time |
| 456 |
checkfilename("def f(x): global x", 11) # symtable-build-time |
| 457 |
symtable.symtable("pass", b"spam", "exec") |
| 458 |
with self.assertRaises(TypeError): |
| 459 |
symtable.symtable("pass", bytearray(b"spam"), "exec") |
| 460 |
with self.assertRaises(TypeError): |
| 461 |
symtable.symtable("pass", memoryview(b"spam"), "exec") |
| 462 |
with self.assertRaises(TypeError): |
| 463 |
symtable.symtable("pass", list(b"spam"), "exec") |
| 464 |
|
| 465 |
def test_eval(self): |
| 466 |
symbols = symtable.symtable("42", "?", "eval") |
| 467 |
|
| 468 |
def test_single(self): |
| 469 |
symbols = symtable.symtable("42", "?", "single") |
| 470 |
|
| 471 |
def test_exec(self): |
| 472 |
symbols = symtable.symtable("def f(x): return x", "?", "exec") |
| 473 |
|
| 474 |
def test_bytes(self): |
| 475 |
top = symtable.symtable(TEST_CODE.encode('utf8'), "?", "exec") |
| 476 |
self.assertIsNotNone(find_block(top, "Mine")) |
| 477 |
|
| 478 |
code = b'# -*- coding: iso8859-15 -*-\nclass \xb4: pass\n' |
| 479 |
|
| 480 |
top = symtable.symtable(code, "?", "exec") |
| 481 |
self.assertIsNotNone(find_block(top, "\u017d")) |
| 482 |
|
| 483 |
def test_symtable_repr(self): |
| 484 |
self.assertEqual(str(self.top), "<SymbolTable for module ?>") |
| 485 |
self.assertEqual(str(self.spam), "<Function SymbolTable for spam in ?>") |
| 486 |
|
| 487 |
def test_symbol_repr(self): |
| 488 |
self.assertEqual(repr(self.spam.lookup("glob")), |
| 489 |
"<symbol 'glob': GLOBAL_IMPLICIT, USE>") |
| 490 |
self.assertEqual(repr(self.spam.lookup("bar")), |
| 491 |
"<symbol 'bar': GLOBAL_EXPLICIT, DEF_GLOBAL|DEF_LOCAL>") |
| 492 |
self.assertEqual(repr(self.spam.lookup("a")), |
| 493 |
"<symbol 'a': LOCAL, DEF_PARAM>") |
| 494 |
self.assertEqual(repr(self.spam.lookup("internal")), |
| 495 |
"<symbol 'internal': LOCAL, USE|DEF_LOCAL>") |
| 496 |
self.assertEqual(repr(self.spam.lookup("other_internal")), |
| 497 |
"<symbol 'other_internal': LOCAL, DEF_LOCAL>") |
| 498 |
self.assertEqual(repr(self.internal.lookup("x")), |
| 499 |
"<symbol 'x': FREE, USE>") |
| 500 |
self.assertEqual(repr(self.other_internal.lookup("some_var")), |
| 501 |
"<symbol 'some_var': FREE, USE|DEF_NONLOCAL|DEF_LOCAL>") |
| 502 |
self.assertEqual(repr(self.GenericMine.lookup("T")), |
| 503 |
"<symbol 'T': LOCAL, DEF_LOCAL|DEF_TYPE_PARAM>") |
| 504 |
|
| 505 |
st1 = symtable.symtable("[x for x in [1]]", "?", "exec") |
| 506 |
self.assertEqual(repr(st1.lookup("x")), |
| 507 |
"<symbol 'x': LOCAL, USE|DEF_LOCAL|DEF_COMP_ITER>") |
| 508 |
|
| 509 |
st2 = symtable.symtable("[(lambda: x) for x in [1]]", "?", "exec") |
| 510 |
self.assertEqual(repr(st2.lookup("x")), |
| 511 |
"<symbol 'x': CELL, DEF_LOCAL|DEF_COMP_ITER|DEF_COMP_CELL>") |
| 512 |
|
| 513 |
st3 = symtable.symtable("def f():\n" |
| 514 |
" x = 1\n" |
| 515 |
" class A:\n" |
| 516 |
" x = 2\n" |
| 517 |
" def method():\n" |
| 518 |
" return x\n", |
| 519 |
"?", "exec") |
| 520 |
# child 0 is for __annotate__ |
| 521 |
func_f = st3.get_children()[1] |
| 522 |
class_A = func_f.get_children()[0] |
| 523 |
self.assertEqual(repr(class_A.lookup('x')), |
| 524 |
"<symbol 'x': LOCAL, DEF_LOCAL|DEF_FREE_CLASS>") |
| 525 |
|
| 526 |
def test_symtable_entry_repr(self): |
| 527 |
expected = f"<symtable entry top({self.top.get_id()}), line {self.top.get_lineno()}>" |
| 528 |
self.assertEqual(repr(self.top._table), expected) |
| 529 |
|
| 530 |
def test__symtable_refleak(self): |
| 531 |
# Regression test for reference leak in PyUnicode_FSDecoder. |
| 532 |
# See https://github.com/python/cpython/issues/139748. |
| 533 |
mortal_str = 'this is a mortal string' |
| 534 |
# check error path when 'compile_type' AC conversion failed |
| 535 |
self.assertRaises(TypeError, symtable.symtable, '', mortal_str, 1) |
| 536 |
|
| 537 |
|
| 538 |
class ComprehensionTests(unittest.TestCase): |
| 539 |
def get_identifiers_recursive(self, st, res): |
| 540 |
res.extend(st.get_identifiers()) |
| 541 |
for ch in st.get_children(): |
| 542 |
self.get_identifiers_recursive(ch, res) |
| 543 |
|
| 544 |
def test_loopvar_in_only_one_scope(self): |
| 545 |
# ensure that the loop variable appears only once in the symtable |
| 546 |
comps = [ |
| 547 |
"[x for x in [1]]", |
| 548 |
"{x for x in [1]}", |
| 549 |
"{x:x*x for x in [1]}", |
| 550 |
] |
| 551 |
for comp in comps: |
| 552 |
with self.subTest(comp=comp): |
| 553 |
st = symtable.symtable(comp, "?", "exec") |
| 554 |
ids = [] |
| 555 |
self.get_identifiers_recursive(st, ids) |
| 556 |
self.assertEqual(len([x for x in ids if x == 'x']), 1) |
| 557 |
|
| 558 |
|
| 559 |
class CommandLineTest(unittest.TestCase): |
| 560 |
maxDiff = None |
| 561 |
|
| 562 |
def test_file(self): |
| 563 |
filename = os_helper.TESTFN |
| 564 |
self.addCleanup(os_helper.unlink, filename) |
| 565 |
with open(filename, 'w') as f: |
| 566 |
f.write(TEST_CODE) |
| 567 |
with support.captured_stdout() as stdout: |
| 568 |
symtable.main([filename]) |
| 569 |
out = stdout.getvalue() |
| 570 |
self.assertIn('\n\n', out) |
| 571 |
self.assertNotIn('\n\n\n', out) |
| 572 |
lines = out.splitlines() |
| 573 |
self.assertIn(f"symbol table for module from file {filename!r}:", lines) |
| 574 |
self.assertIn(" local symbol 'glob': def_local", lines) |
| 575 |
self.assertIn(" global_implicit symbol 'glob': use", lines) |
| 576 |
self.assertIn(" local symbol 'spam': def_local", lines) |
| 577 |
self.assertIn(" symbol table for function 'spam':", lines) |
| 578 |
|
| 579 |
def test_stdin(self): |
| 580 |
with support.captured_stdin() as stdin: |
| 581 |
stdin.write(TEST_CODE) |
| 582 |
stdin.seek(0) |
| 583 |
with support.captured_stdout() as stdout: |
| 584 |
symtable.main([]) |
| 585 |
out = stdout.getvalue() |
| 586 |
stdin.seek(0) |
| 587 |
with support.captured_stdout() as stdout: |
| 588 |
symtable.main(['-']) |
| 589 |
self.assertEqual(stdout.getvalue(), out) |
| 590 |
lines = out.splitlines() |
| 591 |
self.assertIn("symbol table for module from file '<stdin>':", lines) |
| 592 |
|
| 593 |
|
| 594 |
if __name__ == '__main__': |
| 595 |
unittest.main() |