Skip to content

Commit 9ac916c

Browse files
miss-islingtonsundeep8967encukou
authored
[3.12] gh-156353: Fix configparser space delimiter parsing (GH-156382) (#156560)
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 e114415 commit 9ac916c

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
@@ -550,7 +550,8 @@ class RawConfigParser(MutableMapping):
550550
_OPT_TMPL = r"""
551551
(?P<option> # very permissive!
552552
(?:(?!{delim})\S)* # non-delimiter non-whitespace
553-
(?:\s+(?:(?!{delim})\S)+)*) # optionally more words
553+
(?:(?:(?!{delim})\s)+ # optionally more
554+
(?:(?!{delim})\S)+)*) # space-separated words
554555
\s*(?P<vi>{delim})\s* # any number of space/tab,
555556
# followed by any of the
556557
# allowed delimiters,
@@ -560,7 +561,8 @@ class RawConfigParser(MutableMapping):
560561
_OPT_NV_TMPL = r"""
561562
(?P<option> # very permissive!
562563
(?:(?!{delim})\S)* # non-delimiter non-whitespace
563-
(?:\s+(?:(?!{delim})\S)+)*) # optionally more words
564+
(?:(?:(?!{delim})\s)+ # optionally more
565+
(?:(?!{delim})\S)+)*) # space-separated words
564566
\s*(?: # any number of space/tab,
565567
(?P<vi>{delim})\s* # optionally followed by
566568
# any of the allowed

‎Lib/test/test_configparser.py‎

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

47-
def newconfig(self, defaults=None):
47+
def newconfig(self, defaults=None, **kwargs):
4848
arguments = dict(
4949
defaults=defaults,
5050
allow_no_value=self.allow_no_value,
@@ -57,6 +57,7 @@ def newconfig(self, defaults=None):
5757
default_section=self.default_section,
5858
interpolation=self.interpolation,
5959
)
60+
arguments.update(kwargs)
6061
instance = self.config_class(**arguments)
6162
return instance
6263

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

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