Skip to content

gh-96239: Reject configparser writes with whitespace around keys - #157729

Open
sankalpsthakur wants to merge 1 commit into
python:mainfrom
sankalpsthakur:fix/gh-96239-write-whitespace-keys
Open

sankalpsthakur wants to merge 1 commit into
python:mainfrom
sankalpsthakur:fix/gh-96239-write-whitespace-keys

Conversation

@sankalpsthakur

Copy link
Copy Markdown

Fixes #96239.

Extend write-time InvalidWriteError validation to keys with leading or trailing whitespace. Reading the serialized output otherwise changes the key or absorbs it into the previous option's value.

This does not strip names in set() or change in-memory lookups, addressing the compatibility concern in the issue. Internal whitespace and an optionxform() override that strips names remain supported. Writing these non-round-trippable keys now raises instead of emitting corrupting output.

Adds three test methods covering 168 rejection subcases across parser types, sections, values and delimiter spacing, plus internal-whitespace and subclass controls. Includes a NEWS entry.

Validation: debug CPython run, Ubuntu x86-64/GCC 13.3, based on e2ff498:

  • Unchanged implementation: all 168 rejection subcases fail with InvalidWriteError not raised.
  • Patched test_configparser, test_logging, test_sysconfig: successful; 718 tests reported, 12 skipped.
  • New test class passes six -R 3:3 repetitions, with no leaks in measured repetitions.
  • git diff --check passes.

The full CPython suite and other operating systems were not run. AI tools assisted with implementation, testing and this description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ConfigParser read(write()) not idempotent

1 participant