gh-157466: Fix configparser writing a trailing space for empty values - #157467
Open
jang-hs wants to merge 2 commits into
Open
gh-157466: Fix configparser writing a trailing space for empty values#157467jang-hs wants to merge 2 commits into
jang-hs wants to merge 2 commits into
Conversation
…values
RawConfigParser._write_section() always concatenates the padded
delimiter, so an option with an empty value is written as "key = ",
leaving a space at the end of the line.
Drop that space when the value is empty, but keep it when the
delimiter itself ends in whitespace (delimiters=(' ',)), where it is
the separator and is needed to read the option back. Values that
genuinely end in whitespace are untouched.
write() is the only caller of _write_section(), and all three call
sites pass the same delimiter, so the guard belongs there.
jang-hs
force-pushed
the
fix/issue-157466-configparser-empty-value-trailing-space
branch
from
September 14, 2026 05:20
59093b5 to
204e82d
Compare
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.
RawConfigParser.write()builds the delimiter as" {} ".format(self._delimiters[0])whenspace_around_delimitersis true, and_write_section()concatenates it unconditionally. An option with an empty value therefore gets only the delimiter, and the padding space is left dangling at the end of the line:This drops the space when the value is empty, giving
error =.Two cases it deliberately leaves alone:
delimiters=(' ',)). There the trailing whitespace is the separator, not padding, so stripping it would produce a barekeythat cannot be read back. The guard checksself._delimiters[0]rather than the padded delimiter for exactly this reason.{"k": "v "}→k = v). Stripping the whole line, as the issue suggests, would silently truncate them.write()is the only caller of_write_section()and all three call sites pass the same delimiter, so one guard there covers every path; no sibling caller has the same bug. The behaviour dates to 96a60ae (2010), which introducedspace_around_delimitersand concatenated the padded delimiter without an empty-value case - the trailing space looks like an artifact rather than a deliberate choice.Output for non-empty values, for
space_around_delimiters=False, and for valueless options underallow_no_valueis byte-for-byte unchanged.Fixes #157466
Testing
test_write_empty_valueis added next totest_write, so it runs across the existing delimiter and parser variants (13 parameterisations). It pins both halves: the empty value loses the space, the whitespace-ending value keeps it. It fails onmainin all 13 and passes with the change.Also run: full
test_configparser(396 tests), plustest_logging test_collections test_sysconfig test_venv test_importlib test_zipfile test_email(4,540 tests) - all pass.make patchcheckand theruff-checkconfigs CI uses forLib/andLib/test/are clean. IDLE is the other stdlib consumer that writes config files; its tests could not run here because_tkinterwas not built, butidle_test.test_configonly asserts file existence, not written text.I did not touch the whitespace-delimiter round-trip failure visible in that area - that is #157456, and gh-157457 is already fixing it on the parse side.
Found with AI assistance; I reproduced and verified the behaviour and the fix on a locally built interpreter and understand them.