Escape backslashes so set_key values round-trip#673
Open
eeshsaxena wants to merge 2 commits into
Open
Conversation
set_key writes single-quoted values but only escaped single quotes, not backslashes. Since the single-quoted-value parser treats backslash as an escape character, values containing backslashes did not round-trip: an embedded "\\" collapsed to "\", and a value ending in a backslash (e.g. a Windows path like "C:\") wrote a trailing "\'" that swallowed the closing quote and corrupted the whole file. Escape backslashes before single quotes in set_key, and update the single-quoted-value tokenizer to treat every "\<char>" as one escaped unit so an escaped trailing backslash no longer merges with the closing quote. Fixes theskumar#661
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 #661.
set_key(anddotenv set) writes single-quoted values but only escapes single quotes, not backslashes. Because the single-quoted-value parser treats backslash as an escape character, values containing backslashes did not round-trip:\collapsed to\on read;C:\) wrote a trailing\', which the parser read as an escaped quote - swallowing the closing quote and corrupting the whole file (subsequent lines became unparseable).Reproduction (before)
Fix
set_key, escape backslashes before single quotes (order matters) so the written value round-trips._single_quoted_valuetokenizer from(?:\'|[^'])*to(?:\[\s\S]|[^'\])*so every\<char>(including\) is consumed as one escaped unit. Without this, even a correctly-escaped trailing backslash ('C:\') merges its second backslash with the closing quote. The[\s\S]keeps the previous acceptance of newlines after a backslash inside single quotes.The change is scoped to single-quoted values (the form
set_keyemits). Values without backslashes are unaffected, so all existingset_keyoutput expectations are unchanged.Tests
test_set_key_backslash_round_trip- round-trips Windows paths, regexes, embedded/trailing backslashes, and backslash-before-quote.test_parse_streamcases - an escaped backslash decodes to one backslash, and a trailing escaped backslash does not consume the closing quote.Full
test_parser.py+test_main.pypass (124 passed). (test_cli.py::test_run_with_invalid_cmdfails onmaintoo - a pre-existing Windows-only error-message assertion, unrelated to this change.)