diff --git a/Lib/idlelib/autocomplete.py b/Lib/idlelib/autocomplete.py index 032d31225315fb7..a69fbb666d48a50 100644 --- a/Lib/idlelib/autocomplete.py +++ b/Lib/idlelib/autocomplete.py @@ -180,15 +180,18 @@ def fetch_completions(self, what, mode): else: if mode == ATTRS: if what == "": # Main module names. - namespace = {**__main__.__builtins__.__dict__, - **__main__.__dict__} - bigl = eval("dir()", namespace) - bigl.extend(completion_kwds) - bigl.sort() - if "__all__" in bigl: - smalll = sorted(eval("__all__", namespace)) - else: - smalll = [s for s in bigl if s[:1] != '_'] + try: + namespace = {**__main__.__builtins__.__dict__, + **__main__.__dict__} + bigl = eval("dir()", namespace) + bigl.extend(completion_kwds) + bigl.sort() + if "__all__" in bigl: + smalll = sorted(eval("__all__", namespace)) + else: + smalll = [s for s in bigl if s[:1] != '_'] + except Exception: # a broken __all__ must not abort completion + return [], [] else: try: entity = self.get_entity(what) diff --git a/Lib/idlelib/idle_test/test_autocomplete.py b/Lib/idlelib/idle_test/test_autocomplete.py index a811363c18d04e5..0d5741a76e6fc53 100644 --- a/Lib/idlelib/idle_test/test_autocomplete.py +++ b/Lib/idlelib/idle_test/test_autocomplete.py @@ -277,6 +277,15 @@ def _listdir(path): self.assertEqual(s, ['monty', 'python']) self.assertEqual(b, ['.hidden', 'monty', 'python']) + def test_fetch_completions_bad_all(self): + # gh-153506: a non-iterable __main__.__all__ must not abort + # module-name completion at an empty prefix. + acp = self.autocomplete + with patch.dict('__main__.__dict__', {'__all__': None}): + self.assertEqual(acp.fetch_completions('', ac.ATTRS), ([], [])) + # A valid namespace still yields real completions. + self.assertIn('__name__', acp.fetch_completions('', ac.ATTRS)[1]) + def test_get_entity(self): # Test that a name is in the namespace of sys.modules and # __main__.__dict__. diff --git a/Misc/NEWS.d/next/Library/2026-07-10-19-42-00.gh-issue-153506.AcAll7.rst b/Misc/NEWS.d/next/Library/2026-07-10-19-42-00.gh-issue-153506.AcAll7.rst new file mode 100644 index 000000000000000..da10be2b3af1580 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-10-19-42-00.gh-issue-153506.AcAll7.rst @@ -0,0 +1,2 @@ +IDLE no longer fails to show module-name completions when the interactive +namespace has a non-iterable ``__all__``.