gh-65697: Prevent configparser from writing keys it cannot properly read (#129270)

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
This commit is contained in:
Jacob Austin Lincoln 2025-02-23 08:06:33 -08:00 committed by GitHub
parent 1e4a4344af
commit 25a7ddf2ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 58 additions and 1 deletions

View file

@ -2192,6 +2192,30 @@ class SectionlessTestCase(unittest.TestCase):
self.assertEqual('2', cfg[configparser.UNNAMED_SECTION]['b'])
class InvalidInputTestCase(unittest.TestCase):
"""Tests for issue #65697, where configparser will write configs
it parses back differently. Ex: keys containing delimiters or
matching the section pattern"""
def test_delimiter_in_key(self):
cfg = configparser.ConfigParser(delimiters=('='))
cfg.add_section('section1')
cfg.set('section1', 'a=b', 'c')
output = io.StringIO()
with self.assertRaises(configparser.InvalidWriteError):
cfg.write(output)
output.close()
def test_section_bracket_in_key(self):
cfg = configparser.ConfigParser()
cfg.add_section('section1')
cfg.set('section1', '[this parses back as a section]', 'foo')
output = io.StringIO()
with self.assertRaises(configparser.InvalidWriteError):
cfg.write(output)
output.close()
class MiscTestCase(unittest.TestCase):
def test__all__(self):
support.check__all__(self, configparser, not_exported={"Error"})