Quote keys containing '=' when writing (fixes #273)#274
Open
gaoflow wants to merge 1 commit into
Open
Conversation
A config key containing '=' was written unquoted, so on the next parse
the first '=' was read as the key/value divider, splitting the key and
corrupting the value: ConfigObj({'a = b': 'val'}) round-tripped to
{'a': 'b = val'}, and keys like "x <a='b'>" produced an unparseable
file (issue DiffSK#273).
_quote() is shared by keys, values, list elements and section names;
'=' is only special for a key (the first '=' on a line is the divider).
Add an is_key flag and, when set and the key contains '=', force a
quoted form via _get_single_quote(). _write_line passes is_key=True for
the keyword; values, list elements and section markers are unchanged.
Fixes DiffSK#273
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.
Summary
Fixes #273. A config key containing
=is written unquoted, so on the next parse the first=is read as the key/value divider — silently splitting the key and corrupting the value:The parser already accepts the correct quoted form (
"a = b" = val) — only the writer fails to emit the quotes. The issue #273 example"x <a='b'>"is worse: it produces a file that raisesParseErroron the next read.Cause
_quote()is shared by keys, values, list elements and section names. Its "no quoting needed" branch checks for leading/trailing whitespace-plus chars and,, but not=. That is correct for values, list elements and section names —=is legal unquoted there — but not for a key, where the first=on the line is the divider._write_linequoted the key with no key-context flag.Fix
Add an
is_keyflag to_quote(). When it is set and the key contains=, force a quoted form via_get_single_quote()(which already picks"/'correctly, even when the key itself contains a quote char)._write_linepassesis_key=Truefor the keyword; values, list elements and section markers are unchanged (is_key=False).The guard sits ahead of both no-quote paths, so it also covers
list_values=False(where the same corruption occurred).Verification
test_key_with_equals_is_quotedasserts the written form ("a = b" = val) and that it round-trips back to{'a = b': 'val'}. It fails before the fix and passes after.list_values=Falsepath, issue removed quotation marks if writing keys containing equal sign and "less than" sign #273's"x <a='b'>"example, and nested keys all round-trip; section names containing=stay unquoted ([sec = x]) and round-trip; normal keys and values are written exactly as before.flake8 --max-line-length=132introduces zero new warnings.This pull request was prepared with the assistance of AI, under my direction and review.