Skip to content
Merged
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
gh-156353: Fix configparser space delimiter parsing (GH-156382)
(cherry picked from commit f973bd9)

Co-authored-by: sundeep8967 <71071718+sundeep8967@users.noreply.github.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Signed-off-by: sundeep8967 <sundeep8967@gmail.com>
  • Loading branch information
2 people authored and miss-islington committed Aug 29, 2026
commit 1dc946b863e6b1c7656773aa4238d55b0a1a113e
6 changes: 4 additions & 2 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ class RawConfigParser(MutableMapping):
_OPT_TMPL = r"""
(?P<option> # very permissive!
(?:(?!{delim})\S)* # non-delimiter non-whitespace
(?:\s+(?:(?!{delim})\S)+)*) # optionally more words
(?:(?:(?!{delim})\s)+ # optionally more
(?:(?!{delim})\S)+)*) # space-separated words
\s*(?P<vi>{delim})\s* # any number of space/tab,
# followed by any of the
# allowed delimiters,
Expand All @@ -560,7 +561,8 @@ class RawConfigParser(MutableMapping):
_OPT_NV_TMPL = r"""
(?P<option> # very permissive!
(?:(?!{delim})\S)* # non-delimiter non-whitespace
(?:\s+(?:(?!{delim})\S)+)*) # optionally more words
(?:(?:(?!{delim})\s)+ # optionally more
(?:(?!{delim})\S)+)*) # space-separated words
\s*(?: # any number of space/tab,
(?P<vi>{delim})\s* # optionally followed by
# any of the allowed
Expand Down
33 changes: 30 additions & 3 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CfgParserTestCaseClass:
default_section = configparser.DEFAULTSECT
interpolation = configparser._UNSET

def newconfig(self, defaults=None):
def newconfig(self, defaults=None, **kwargs):
arguments = dict(
defaults=defaults,
allow_no_value=self.allow_no_value,
Expand All @@ -57,6 +57,7 @@ def newconfig(self, defaults=None):
default_section=self.default_section,
interpolation=self.interpolation,
)
arguments.update(kwargs)
instance = self.config_class(**arguments)
return instance

Expand Down Expand Up @@ -359,6 +360,32 @@ def test_basic(self):
the larch {0[1]} 1
""".format(self.delimiters)))

@support.subTests('data', [
'foo bar=baz',
'foo bar=baz',
'foo=bar=baz',
'foo = bar=baz',
'foo\t \t=\t \tbar=baz',
])
def test_space_delimiter(self, data):
# gh-156353: Space should be accepted as a delimiter
cf = self.newconfig(delimiters=(' ', '='))
cf.read_string(f"[all]\n{data}")
self.assertEqual(cf.options('all'), ['foo'])
self.assertEqual(cf.get('all', 'foo'), 'bar=baz')

@support.subTests('delimiter', ' =:;#x\t\0\N{RS}\N{CEDILLA}\N{CAT}')
@support.subTests('space_before', ['', ' ', '\t', ' \t'])
@support.subTests('space_after', ['', ' ', '\t', ' \t'])
def test_any_delimiter(self, delimiter, space_before, space_after):
cf = self.newconfig(
delimiters=(delimiter,),
inline_comment_prefixes=None,
)
cf.read_string(f"[all]\nfoo{space_before}{delimiter}{space_after}bar=baz")
self.assertEqual(cf.options('all'), ['foo'])
self.assertEqual(cf.get('all', 'foo'), 'bar=baz')

def test_basic_from_dict(self):
config = {
"Foo Bar": {
Expand Down Expand Up @@ -1956,8 +1983,8 @@ class ConvertersTestCase(BasicTestCase, unittest.TestCase):

config_class = configparser.ConfigParser

def newconfig(self, defaults=None):
instance = super().newconfig(defaults=defaults)
def newconfig(self, defaults=None, **kwargs):
instance = super().newconfig(defaults=defaults, **kwargs)
instance.converters['list'] = lambda v: [e.strip() for e in v.split()
if e.strip()]
return instance
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :mod:`configparser` parsing when using whitespace in *delimiters*.
Loading