Skip to content

Commit b73f7ae

Browse files
committed
gh-156353: Fix configparser space delimiter parsing
This commit fixes a bug introduced in gh-146333 where using a space as a delimiter would cause the option name parsing to incorrectly absorb the space. The regular expressions _OPT_TMPL and _OPT_NV_TMPL have been adjusted to ensure that whitespace matches do not consume valid delimiters. Signed-off-by: sundeep8967 <sundeep8967@gmail.com>
1 parent 98bd716 commit b73f7ae

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

‎Lib/configparser.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ class RawConfigParser(MutableMapping):
618618
_OPT_TMPL = r"""
619619
(?P<option> # very permissive!
620620
(?:(?!{delim})\S)* # non-delimiter non-whitespace
621-
(?:\s+(?:(?!{delim})\S)+)*) # optionally more words
621+
(?:(?:(?!{delim})\s)+(?:(?!{delim})\S)+)*) # optionally more words
622622
\s*(?P<vi>{delim})\s* # any number of space/tab,
623623
# followed by any of the
624624
# allowed delimiters,
@@ -628,7 +628,7 @@ class RawConfigParser(MutableMapping):
628628
_OPT_NV_TMPL = r"""
629629
(?P<option> # very permissive!
630630
(?:(?!{delim})\S)* # non-delimiter non-whitespace
631-
(?:\s+(?:(?!{delim})\S)+)*) # optionally more words
631+
(?:(?:(?!{delim})\s)+(?:(?!{delim})\S)+)*) # optionally more words
632632
\s*(?: # any number of space/tab,
633633
(?P<vi>{delim})\s* # optionally followed by
634634
# any of the allowed

‎Lib/test/test_configparser.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,13 @@ def test_basic(self):
358358
the larch {0[1]} 1
359359
""".format(self.delimiters)))
360360

361+
def test_space_delimiter(self):
362+
# gh-156353: Space should be accepted as a delimiter
363+
cf = self.newconfig(delimiters=(' ', '='))
364+
cf.read_string("[all]\nfoo bar=baz")
365+
self.assertEqual(cf.options('all'), ['foo'])
366+
self.assertEqual(cf.get('all', 'foo'), 'bar=baz')
367+
361368
def test_basic_from_dict(self):
362369
config = {
363370
"Foo Bar": {

0 commit comments

Comments
 (0)