Skip to content

Commit 3478cd5

Browse files
sundeep8967encukou
authored andcommitted
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>
1 parent 9eaf48a commit 3478cd5

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
@@ -579,7 +579,8 @@ class RawConfigParser(MutableMapping):
579579
_OPT_TMPL = r"""
580580
(?P<option> # very permissive!
581581
(?:(?!{delim})\S)* # non-delimiter non-whitespace
582-
(?:\s+(?:(?!{delim})\S)+)*) # optionally more words
582+
(?:(?:(?!{delim})\s)+ # optionally more
583+
(?:(?!{delim})\S)+)*) # space-separated words
583584
\s*(?P<vi>{delim})\s* # any number of space/tab,
584585
# followed by any of the
585586
# allowed delimiters,
@@ -589,7 +590,8 @@ class RawConfigParser(MutableMapping):
589590
_OPT_NV_TMPL = r"""
590591
(?P<option> # very permissive!
591592
(?:(?!{delim})\S)* # non-delimiter non-whitespace
592-
(?:\s+(?:(?!{delim})\S)+)*) # optionally more words
593+
(?:(?:(?!{delim})\s)+ # optionally more
594+
(?:(?!{delim})\S)+)*) # space-separated words
593595
\s*(?: # any number of space/tab,
594596
(?P<vi>{delim})\s* # optionally followed by
595597
# 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": {
@@ -1989,8 +2016,8 @@ class ConvertersTestCase(BasicTestCase, unittest.TestCase):
19892016

19902017
config_class = configparser.ConfigParser
19912018

1992-
def newconfig(self, defaults=None):
1993-
instance = super().newconfig(defaults=defaults)
2019+
def newconfig(self, defaults=None, **kwargs):
2020+
instance = super().newconfig(defaults=defaults, **kwargs)
19942021
instance.converters['list'] = lambda v: [e.strip() for e in v.split()
19952022
if e.strip()]
19962023
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)