From 15fbcae5b398520c6f25402736bb44f116e7af74 Mon Sep 17 00:00:00 2001 From: Kumar Anirudha Date: Thu, 29 Oct 2026 10:41:26 +0530 Subject: [PATCH 1/2] 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 --- Lib/symtable.py | 4 ++-- Lib/test/lazy_imports_all_exclude.txt | 1 - Lib/test/test_symtable.py | 9 +++++++++ .../2026-10-29-04-59-41.gh-issue-160810.LpNp7z.rst | 2 ++ 4 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-10-29-04-59-41.gh-issue-160810.LpNp7z.rst 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..5615361565283a5 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,14 @@ def test_symbol_repr(self): self.assertEqual(repr(class_A.lookup('x')), "") + @support.requires_subprocess() + def test_symbol_repr_lazy_imports(self): + 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``). From 26a5cc1f424da1065ad55726cbc31534d7303615 Mon Sep 17 00:00:00 2001 From: Ani <5357586+anistark@users.noreply.github.com> Date: Thu, 29 Oct 2026 19:55:04 +0530 Subject: [PATCH 2/2] Update Lib/test/test_symtable.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/test/test_symtable.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_symtable.py b/Lib/test/test_symtable.py index 5615361565283a5..6186c0764659ba1 100644 --- a/Lib/test/test_symtable.py +++ b/Lib/test/test_symtable.py @@ -449,6 +449,7 @@ def test_symbol_repr(self): @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")