Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-120029: make symtable.Symbol.__repr__ correctly reflect the compiler's flags #120099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
ecf13fa6b524734fe8b3473f8dc7477ad7fd04b013ff02dfa5c612de1e84973388e40e9a74d6b46f1604f853905f2ef899c915f04d101a586c450fac44622b379c3688420820ede2a589d8dfb4de459bb5e569ec2cbae12ac51d69File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4,7 +4,10 @@ | ||
| from _symtable import ( | ||
| USE, | ||
| DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL, | ||
| DEF_PARAM, DEF_TYPE_PARAM, DEF_IMPORT, DEF_BOUND, DEF_ANNOT, | ||
| DEF_PARAM, DEF_TYPE_PARAM, | ||
| DEF_FREE_CLASS, | ||
| DEF_IMPORT, DEF_BOUND, DEF_ANNOT, | ||
| DEF_COMP_ITER, DEF_COMP_CELL, | ||
| SCOPE_OFF, SCOPE_MASK, | ||
picnixz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| FREE, LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL | ||
| ) | ||
| @@ -158,6 +161,10 @@ def get_children(self): | ||
| for st in self._table.children] | ||
| def _get_scope(flags): # like _PyST_GetScope() | ||
| return (flags >> SCOPE_OFF) & SCOPE_MASK | ||
| class Function(SymbolTable): | ||
| # Default values for instance variables | ||
| @@ -183,7 +190,7 @@ def get_locals(self): | ||
| """ | ||
| if self.__locals is None: | ||
| locs = (LOCAL, CELL) | ||
| test = lambda x: ((x >> SCOPE_OFF) & SCOPE_MASK) in locs | ||
| test = lambda x: _get_scope(x) in locs | ||
| self.__locals = self.__idents_matching(test) | ||
| return self.__locals | ||
| @@ -192,7 +199,7 @@ def get_globals(self): | ||
| """ | ||
| if self.__globals is None: | ||
| glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT) | ||
| test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob | ||
| test = lambda x: _get_scope(x) in glob | ||
| self.__globals = self.__idents_matching(test) | ||
| return self.__globals | ||
| @@ -207,7 +214,7 @@ def get_frees(self): | ||
| """Return a tuple of free variables in the function. | ||
| """ | ||
| if self.__frees is None: | ||
| is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE | ||
| is_free = lambda x: _get_scope(x) == FREE | ||
| self.__frees = self.__idents_matching(is_free) | ||
| return self.__frees | ||
| @@ -234,7 +241,7 @@ class Symbol: | ||
| def __init__(self, name, flags, namespaces=None, *, module_scope=False): | ||
| self.__name = name | ||
| self.__flags = flags | ||
| self.__scope = (flags >> SCOPE_OFF) & SCOPE_MASK # like PyST_GetScope() | ||
| self.__scope = _get_scope(flags) | ||
| self.__namespaces = namespaces or () | ||
| self.__module_scope = module_scope | ||
| @@ -303,6 +310,11 @@ def is_free(self): | ||
| """ | ||
| return bool(self.__scope == FREE) | ||
| def is_free_class(self): | ||
| """Return *True* if a class-scoped symbol is free from | ||
| the perspective of a method.""" | ||
| return bool(self.__flags & DEF_FREE_CLASS) | ||
| def is_imported(self): | ||
| """Return *True* if the symbol is created from | ||
| an import statement. | ||
| @@ -313,6 +325,16 @@ def is_assigned(self): | ||
| """Return *True* if a symbol is assigned to.""" | ||
| return bool(self.__flags & DEF_LOCAL) | ||
| def is_comp_iter(self): | ||
| """Return *True* if the symbol is a comprehension iteration variable. | ||
| """ | ||
| return bool(self.__flags & DEF_COMP_ITER) | ||
| def is_comp_cell(self): | ||
picnixz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """Return *True* if the symbol is a cell in an inlined comprehension. | ||
| """ | ||
| return bool(self.__flags & DEF_COMP_CELL) | ||
| def is_namespace(self): | ||
| """Returns *True* if name binding introduces new namespace. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -304,6 +304,27 @@ def test_symbol_repr(self): | ||
| self.assertEqual(repr(self.GenericMine.lookup("T")), | ||
| "<symbol 'T': LOCAL, DEF_LOCAL|DEF_TYPE_PARAM>") | ||
| st1 = symtable.symtable("[x for x in [1]]", "?", "exec") | ||
| self.assertEqual(repr(st1.lookup("x")), | ||
| "<symbol 'x': LOCAL, USE|DEF_LOCAL|DEF_COMP_ITER>") | ||
| st2 = symtable.symtable("[(lambda: x) for x in [1]]", "?", "exec") | ||
| self.assertEqual(repr(st2.lookup("x")), | ||
| "<symbol 'x': CELL, DEF_LOCAL|DEF_COMP_ITER|DEF_COMP_CELL>") | ||
| st3 = symtable.symtable("def f():\n" | ||
| " x = 1\n" | ||
| " class A:\n" | ||
| " x = 2\n" | ||
| " def method():\n" | ||
| " return x\n", | ||
| "?", "exec") | ||
| # child 0 is for __annotate__ | ||
| func_f = st3.get_children()[1] | ||
picnixz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| class_A = func_f.get_children()[0] | ||
| self.assertEqual(repr(class_A.lookup('x')), | ||
| "<symbol 'x': LOCAL, DEF_LOCAL|DEF_FREE_CLASS>") | ||
| def test_symtable_entry_repr(self): | ||
| expected = f"<symtable entry top({self.top.get_id()}), line {self.top.get_lineno()}>" | ||
| self.assertEqual(repr(self.top._table), expected) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Expose :class:`symtable.Symbol` methods :meth:`~symtable.Symbol.is_free_class`, | ||
| :meth:`~symtable.Symbol.is_comp_iter` and :meth:`~symtable.Symbol.is_comp_cell`. | ||
| Patch by Bénédikt Tran. | ||
Uh oh!
There was an error while loading. Please reload this page.