Skip to content

Commit 15fbcae

Browse files
committed
gh-160810: Fix symtable.Symbol repr under -X lazy_imports=all
`repr()` of a `symtable.Symbol` no longer raises `TypeError` when all imports are lazy Refs #149640
1 parent a9a81b1 commit 15fbcae

4 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎Lib/symtable.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,9 @@ def get_namespace(self):
386386

387387

388388
_flags = [('USE', USE)]
389-
_flags.extend(kv for kv in globals().items() if kv[0].startswith('DEF_'))
389+
_flags.extend(kv for kv in vars(_symtable).items() if kv[0].startswith('DEF_'))
390390
_scopes_names = ('FREE', 'LOCAL', 'GLOBAL_IMPLICIT', 'GLOBAL_EXPLICIT', 'CELL')
391-
_scopes_value_to_name = frozendict({globals()[n]: n for n in _scopes_names})
391+
_scopes_value_to_name = frozendict({getattr(_symtable, n): n for n in _scopes_names})
392392

393393

394394
def main(args):

‎Lib/test/lazy_imports_all_exclude.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ test_profile
3333
test_profiling
3434
test_pyrepl
3535
test_subprocess
36-
test_symtable
3736
test_tools
3837
test_type_annotations
3938
test_unittest

‎Lib/test/test_symtable.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from test import support
1111
from test.support import os_helper
12+
from test.support.script_helper import assert_python_ok
1213

1314

1415
TEST_CODE = """
@@ -446,6 +447,14 @@ def test_symbol_repr(self):
446447
self.assertEqual(repr(class_A.lookup('x')),
447448
"<symbol 'x': LOCAL, DEF_LOCAL|DEF_FREE_CLASS>")
448449

450+
@support.requires_subprocess()
451+
def test_symbol_repr_lazy_imports(self):
452+
code = ("import symtable\n"
453+
"top = symtable.symtable('x = 1', '?', 'exec')\n"
454+
"print(repr(top.lookup('x')))\n")
455+
_, out, _ = assert_python_ok('-X', 'lazy_imports=all', '-c', code)
456+
self.assertEqual(out.strip(), b"<symbol 'x': LOCAL, DEF_LOCAL>")
457+
449458
def test_symtable_entry_repr(self):
450459
expected = f"<symtable entry top({self.top.get_id()}), line {self.top.get_lineno()}>"
451460
self.assertEqual(repr(self.top._table), expected)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`repr` of :class:`symtable.Symbol` raising :exc:`TypeError` when
2+
all imports are lazy (``-X lazy_imports=all``).

0 commit comments

Comments
 (0)