Skip to content

Commit 80fc171

Browse files
committed
gh-153477: Fix IDLE startup crash on a corrupt user config file
IdleConfParser.Load raised the configparser exception on a corrupt or partially written user configuration file, which aborted IDLE startup. Catch it, warn, and fall back to the default configuration, matching the module's documented graceful-degradation behavior.
1 parent d83d267 commit 80fc171

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

Lib/idlelib/config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"""
2626
# TODOs added Oct 2014, tjr
2727

28-
from configparser import ConfigParser
28+
from configparser import ConfigParser, Error
2929
import os
3030
import sys
3131

@@ -75,7 +75,11 @@ def Load(self):
7575
"Load the configuration file from disk."
7676
if self.file and os.path.exists(self.file):
7777
with open(self.file, encoding='utf-8', errors='replace') as f:
78-
self.read_file(f)
78+
try:
79+
self.read_file(f)
80+
except Error as err: # any configparser parse error
81+
_warn(f'\nInvalid config file, using default config:\n'
82+
f'{self.file!r}\n{err}\n', self.file)
7983

8084
class IdleUserConfParser(IdleConfParser):
8185
"""

Lib/idlelib/idle_test/test_config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ def test_load_file(self):
9393
self.assertEqual(parser.Get('Foo Bar', 'foo'), 'newbar')
9494
self.assertEqual(parser.GetOptionList('Foo Bar'), ['foo'])
9595

96+
def test_load_invalid_file(self):
97+
# gh-000000: a corrupt config file must warn and fall back to
98+
# defaults, not abort IDLE startup.
99+
self.addCleanup(config._warned.clear)
100+
with tempfile.TemporaryDirectory() as tdir:
101+
path = os.path.join(tdir, 'invalid.cfg')
102+
with open(path, 'w', encoding='utf-8') as f:
103+
f.write('option = value\n') # missing section header
104+
parser = config.IdleConfParser(path)
105+
with captured_stderr() as stderr:
106+
parser.Load() # must not raise
107+
self.assertEqual(parser.sections(), [])
108+
self.assertIn('Invalid config file', stderr.getvalue())
109+
96110

97111
class IdleUserConfParserTest(unittest.TestCase):
98112
"""Test that IdleUserConfParser works"""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
IDLE now warns and falls back to the default configuration when a user
2+
configuration file cannot be parsed, instead of failing to start.

0 commit comments

Comments
 (0)