FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.10] gh-156353: Fix configparser space delimiter parsing (GH-156382) by miss-islington · Pull Request #156562 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 4 additions & 2 deletions Lib/configparser.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,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 @@ -580,7 +581,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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,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 @@ -58,6 +58,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 @@ -360,6 +361,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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :mod:`configparser` parsing when using whitespace in *delimiters*.
Loading

Back | FazBrowse Home | New Git URL