Skip to content

Commit 2af3ca5

Browse files
miss-islingtonsundeep8967encukou
authored
[3.10] gh-156353: Fix configparser space delimiter parsing (GH-156382) (#156562)
gh-156353: Fix configparser space delimiter parsing (GH-156382) (cherry picked from commit f973bd9) Signed-off-by: sundeep8967 <sundeep8967@gmail.com> Co-authored-by: sundeep8967 <71071718+sundeep8967@users.noreply.github.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent d397a49 commit 2af3ca5

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎Lib/configparser.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,8 @@ class RawConfigParser(MutableMapping):
570570
_OPT_TMPL = r"""
571571
(?P<option> # very permissive!
572572
(?:(?!{delim})\S)* # non-delimiter non-whitespace
573-
(?:\s+(?:(?!{delim})\S)+)*) # optionally more words
573+
(?:(?:(?!{delim})\s)+ # optionally more
574+
(?:(?!{delim})\S)+)*) # space-separated words
574575
\s*(?P<vi>{delim})\s* # any number of space/tab,
575576
# followed by any of the
576577
# allowed delimiters,
@@ -580,7 +581,8 @@ class RawConfigParser(MutableMapping):
580581
_OPT_NV_TMPL = r"""
581582
(?P<option> # very permissive!
582583
(?:(?!{delim})\S)* # non-delimiter non-whitespace
583-
(?:\s+(?:(?!{delim})\S)+)*) # optionally more words
584+
(?:(?:(?!{delim})\s)+ # optionally more
585+
(?:(?!{delim})\S)+)*) # space-separated words
584586
\s*(?: # any number of space/tab,
585587
(?P<vi>{delim})\s* # optionally followed by
586588
# any of the allowed

‎Lib/test/test_configparser.py‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class CfgParserTestCaseClass:
4545
default_section = configparser.DEFAULTSECT
4646
interpolation = configparser._UNSET
4747

48-
def newconfig(self, defaults=None):
48+
def newconfig(self, defaults=None, **kwargs):
4949
arguments = dict(
5050
defaults=defaults,
5151
allow_no_value=self.allow_no_value,
@@ -58,6 +58,7 @@ def newconfig(self, defaults=None):
5858
default_section=self.default_section,
5959
interpolation=self.interpolation,
6060
)
61+
arguments.update(kwargs)
6162
instance = self.config_class(**arguments)
6263
return instance
6364

@@ -360,6 +361,32 @@ def test_basic(self):
360361
the larch {0[1]} 1
361362
""".format(self.delimiters)))
362363

364+
@support.subTests('data', [
365+
'foo bar=baz',
366+
'foo bar=baz',
367+
'foo=bar=baz',
368+
'foo = bar=baz',
369+
'foo\t \t=\t \tbar=baz',
370+
])
371+
def test_space_delimiter(self, data):
372+
# gh-156353: Space should be accepted as a delimiter
373+
cf = self.newconfig(delimiters=(' ', '='))
374+
cf.read_string(f"[all]\n{data}")
375+
self.assertEqual(cf.options('all'), ['foo'])
376+
self.assertEqual(cf.get('all', 'foo'), 'bar=baz')
377+
378+
@support.subTests('delimiter', ' =:;#x\t\0\N{RS}\N{CEDILLA}\N{CAT}')
379+
@support.subTests('space_before', ['', ' ', '\t', ' \t'])
380+
@support.subTests('space_after', ['', ' ', '\t', ' \t'])
381+
def test_any_delimiter(self, delimiter, space_before, space_after):
382+
cf = self.newconfig(
383+
delimiters=(delimiter,),
384+
inline_comment_prefixes=None,
385+
)
386+
cf.read_string(f"[all]\nfoo{space_before}{delimiter}{space_after}bar=baz")
387+
self.assertEqual(cf.options('all'), ['foo'])
388+
self.assertEqual(cf.get('all', 'foo'), 'bar=baz')
389+
363390
def test_basic_from_dict(self):
364391
config = {
365392
"Foo Bar": {
@@ -1956,8 +1983,8 @@ class ConvertersTestCase(BasicTestCase, unittest.TestCase):
19561983

19571984
config_class = configparser.ConfigParser
19581985

1959-
def newconfig(self, defaults=None):
1960-
instance = super().newconfig(defaults=defaults)
1986+
def newconfig(self, defaults=None, **kwargs):
1987+
instance = super().newconfig(defaults=defaults, **kwargs)
19611988
instance.converters['list'] = lambda v: [e.strip() for e in v.split()
19621989
if e.strip()]
19631990
return instance
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`configparser` parsing when using whitespace in *delimiters*.

0 commit comments

Comments
 (0)