diff --git a/Lib/idlelib/config.py b/Lib/idlelib/config.py index 82afd6c49269d2d..a02c7365484e1b7 100644 --- a/Lib/idlelib/config.py +++ b/Lib/idlelib/config.py @@ -25,7 +25,7 @@ """ # TODOs added Oct 2014, tjr -from configparser import ConfigParser +from configparser import ConfigParser, Error import os import sys @@ -75,7 +75,11 @@ def Load(self): "Load the configuration file from disk." if self.file and os.path.exists(self.file): with open(self.file, encoding='utf-8', errors='replace') as f: - self.read_file(f) + try: + self.read_file(f) + except Error as err: # any configparser parse error + _warn(f'\nInvalid config file, using default config:\n' + f'{self.file!r}\n{err}\n', self.file) class IdleUserConfParser(IdleConfParser): """ diff --git a/Lib/idlelib/idle_test/test_config.py b/Lib/idlelib/idle_test/test_config.py index 6d75cf7aa67dcce..7ef2590c3ec84ad 100644 --- a/Lib/idlelib/idle_test/test_config.py +++ b/Lib/idlelib/idle_test/test_config.py @@ -93,6 +93,20 @@ def test_load_file(self): self.assertEqual(parser.Get('Foo Bar', 'foo'), 'newbar') self.assertEqual(parser.GetOptionList('Foo Bar'), ['foo']) + def test_load_invalid_file(self): + # gh-153477: a corrupt config file must warn and fall back to + # defaults, not abort IDLE startup. + self.addCleanup(config._warned.clear) + with tempfile.TemporaryDirectory() as tdir: + path = os.path.join(tdir, 'invalid.cfg') + with open(path, 'w', encoding='utf-8') as f: + f.write('option = value\n') # missing section header + parser = config.IdleConfParser(path) + with captured_stderr() as stderr: + parser.Load() # must not raise + self.assertEqual(parser.sections(), []) + self.assertIn('Invalid config file', stderr.getvalue()) + class IdleUserConfParserTest(unittest.TestCase): """Test that IdleUserConfParser works""" diff --git a/Misc/NEWS.d/next/Library/2026-07-10-19-40-00.gh-issue-153477.Cf9Ld2.rst b/Misc/NEWS.d/next/Library/2026-07-10-19-40-00.gh-issue-153477.Cf9Ld2.rst new file mode 100644 index 000000000000000..1b34fba74a1eb8a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-10-19-40-00.gh-issue-153477.Cf9Ld2.rst @@ -0,0 +1,2 @@ +IDLE now warns and falls back to the default configuration when a user +configuration file cannot be parsed, instead of failing to start.