Skip to content
Closed
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
22 changes: 21 additions & 1 deletion Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,27 @@ def __init__(self, defaults=None, dict_type=_default_dict,
self._optcre = self.OPTCRE_NV if allow_no_value else self.OPTCRE
else:
d = "|".join(re.escape(d) for d in delimiters)
if allow_no_value:
if any(dl.strip() == "" for dl in delimiters):
if allow_no_value:
self._optcre = re.compile(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not fix the pattern that was broken, instead of adding yet another one?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. The broken pattern is _OPT_TMPL/_OPT_NV_TMPL where (?:\s+(?:(?!{delim})\S)+)* greedily treats space-separated tokens as part of option. When space itself is a delimiter (delimiters=(' ', '=')) that continuation should not apply, the delimiter is the word boundary not part of option. A single regex that handles both would need a conditional inside the pattern on whether delimiter contains whitespace, which is the same branching but hidden inside the regex and harder to read plus risky for the ReDoS-safe backtracking the original fix (PR 146399) added. This keeps two small patterns: single-token option for whitespace delimiters, multi-word for =/:. Happy to unify into one template with a conditional if you prefer.

r"""
(?P<option>
(?:(?!{delim})\S)+
)
\s*(?:
(?P<vi>{delim})\s*
(?P<value>.*))?$
""".format(delim=d), re.VERBOSE)
else:
self._optcre = re.compile(
r"""
(?P<option>
(?:(?!{delim})\S)+
)
\s*(?P<vi>{delim})\s*
(?P<value>.*)$
""".format(delim=d), re.VERBOSE)
elif allow_no_value:
self._optcre = re.compile(self._OPT_NV_TMPL.format(delim=d),
re.VERBOSE)
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix regression in :mod:`configparser` where using a space as a delimiter
no longer split option and value correctly. Parsing ``"foo bar=baz"`` with
``delimiters=(' ', '=')`` now correctly yields option ``"foo"``.
Loading