Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Lib/idlelib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"""
# TODOs added Oct 2014, tjr

from configparser import ConfigParser
from configparser import ConfigParser, Error
import os
import sys

Expand Down Expand Up @@ -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):
"""
Expand Down
14 changes: 14 additions & 0 deletions Lib/idlelib/idle_test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading