FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-153477: Fix IDLE startup crash on a corrupt user config file by tonghuaroot · Pull Request #153478 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 6 additions & 2 deletions Lib/idlelib/config.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL