gh-116957: configparser: Do post-process values after DuplicateOption… · python/cpython@b1bc375 · GitHub
Skip to content

Commit b1bc375

Browse files
authored
gh-116957: configparser: Do post-process values after DuplicateOptionError (GH-116958)
If you catch DuplicateOptionError / DuplicateSectionError when reading a config file (the intention is to skip invalid config files) and then attempt to use the ConfigParser instance, any values it *had* read successfully so far, were stored as a list instead of string! Later `get` calls would raise "AttributeError: 'list' object has no attribute 'find'" from somewhere deep in the interpolation code.
1 parent a8e93d3 commit b1bc375

3 files changed

Lines changed: 111 additions & 91 deletions

File tree

Lib/configparser.py

Lines changed: 93 additions & 91 deletions

Lib/test/test_configparser.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,21 @@ def test_weird_errors(self):
646646
"'opt' in section 'Bar' already exists")
647647
self.assertEqual(e.args, ("Bar", "opt", "<dict>", None))
648648

649+
def test_get_after_duplicate_option_error(self):
650+
cf = self.newconfig()
651+
ini = textwrap.dedent("""\
652+
[Foo]
653+
x{equals}1
654+
y{equals}2
655+
y{equals}3
656+
""".format(equals=self.delimiters[0]))
657+
if self.strict:
658+
with self.assertRaises(configparser.DuplicateOptionError):
659+
cf.read_string(ini)
660+
else:
661+
cf.read_string(ini)
662+
self.assertEqual(cf.get('Foo', 'x'), '1')
663+
649664
def test_write(self):
650665
config_string = (
651666
"[Long Line]\n"
Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)