gh-157456: Fix configparser parsing an empty value with a whitespace-ending delimiter - #157457
Open
winklemad wants to merge 1 commit into
Open
gh-157456: Fix configparser parsing an empty value with a whitespace-ending delimiter#157457winklemad wants to merge 1 commit into
winklemad wants to merge 1 commit into
Conversation
…space-ending delimiter
An option whose delimiter ends in whitespace (e.g. ``delimiters=(' ',)``) and
has an empty value could not be parsed, and the parser could not read back the
output produced by ``write()``: ``_read`` matched option lines against
``line.clean``, which is ``str.strip``-ed on both sides, so for a whitespace
delimiter the trailing whitespace that separates the key from the (empty) value
was removed and ``OPTCRE`` no longer matched.
Match the option regex against a form that preserves trailing whitespace. The
matched value is stripped immediately afterwards, so ordinary values keep no
trailing whitespace, and the other uses of ``line.clean`` are unchanged.
Add a regression test; the pythongh-156353 whitespace-delimiter tests only used
non-empty values, so this edge was unpinned.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The whitespace-in-delimiters feature (gh-156353) can't parse an option with an empty value when the delimiter ends in whitespace — including its own
write()output._readmatches option lines againstline.clean, which isstr.strip-ed on both sides (plus comment removal). For a whitespace delimiter the delimiter is the trailing whitespace separating the key from the (empty) value, sostrip()removes it andOPTCREno longer matches.Empty values are first-class for every other delimiter (
key=,key:,key->,key||→{'key': ''}), so whitespace delimiters should match.Fix: match the option regex against a form that preserves trailing whitespace (the delimiter). The matched value is
.strip()-ed immediately afterwards, so ordinary values keep no trailing whitespace, and the other uses ofline.clean(blank-line detection, continuation values, section headers) are unchanged. Withallow_no_value=Truea whitespace delimiter can't distinguish an empty value from a valueless option, so it reads asNonethere (unchanged, and self-consistent on round-trip); the regression test covers both modes../python -m test test_configparser→ SUCCESS (396 tests); the newtest_whitespace_delimiter_empty_valuefails onmain(12 errors across parser variants) and passes with the change.Misc/NEWS.dentry.AI-tools disclosure (per the devguide policy): I used an AI assistant to help locate the bug and draft the patch and test. I reproduced and verified the behaviour and the fix against a locally built interpreter, understand the change, and stand by it.