diff --git a/Lib/symtable.py b/Lib/symtable.py index 3e56e8ab99c1c37..443342ce880c553 100644 --- a/Lib/symtable.py +++ b/Lib/symtable.py @@ -386,9 +386,9 @@ def get_namespace(self): _flags = [('USE', USE)] -_flags.extend(kv for kv in globals().items() if kv[0].startswith('DEF_')) +_flags.extend(kv for kv in vars(_symtable).items() if kv[0].startswith('DEF_')) _scopes_names = ('FREE', 'LOCAL', 'GLOBAL_IMPLICIT', 'GLOBAL_EXPLICIT', 'CELL') -_scopes_value_to_name = frozendict({globals()[n]: n for n in _scopes_names}) +_scopes_value_to_name = frozendict({getattr(_symtable, n): n for n in _scopes_names}) def main(args): diff --git a/Lib/test/lazy_imports_all_exclude.txt b/Lib/test/lazy_imports_all_exclude.txt index 45796d1db9f0a58..cb0dd8df8168227 100644 --- a/Lib/test/lazy_imports_all_exclude.txt +++ b/Lib/test/lazy_imports_all_exclude.txt @@ -33,7 +33,6 @@ test_profile test_profiling test_pyrepl test_subprocess -test_symtable test_tools test_type_annotations test_unittest diff --git a/Lib/test/test_symtable.py b/Lib/test/test_symtable.py index da00b0bfa74b125..6186c0764659ba1 100644 --- a/Lib/test/test_symtable.py +++ b/Lib/test/test_symtable.py @@ -9,6 +9,7 @@ from test import support from test.support import os_helper +from test.support.script_helper import assert_python_ok TEST_CODE = """ @@ -446,6 +447,15 @@ def test_symbol_repr(self): self.assertEqual(repr(class_A.lookup('x')), "") + @support.requires_subprocess() + def test_symbol_repr_lazy_imports(self): + # See https://github.com/python/cpython/issues/160810. + code = ("import symtable\n" + "top = symtable.symtable('x = 1', '?', 'exec')\n" + "print(repr(top.lookup('x')))\n") + _, out, _ = assert_python_ok('-X', 'lazy_imports=all', '-c', code) + self.assertEqual(out.strip(), b"") + def test_symtable_entry_repr(self): expected = f"" self.assertEqual(repr(self.top._table), expected) diff --git a/Misc/NEWS.d/next/Library/2026-10-29-04-59-41.gh-issue-160810.LpNp7z.rst b/Misc/NEWS.d/next/Library/2026-10-29-04-59-41.gh-issue-160810.LpNp7z.rst new file mode 100644 index 000000000000000..f1e65ab68153b0d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-29-04-59-41.gh-issue-160810.LpNp7z.rst @@ -0,0 +1,2 @@ +Fix :func:`repr` of :class:`symtable.Symbol` raising :exc:`TypeError` when +all imports are lazy (``-X lazy_imports=all``).