fix: escape backslashes in set_key to preserve round-trip#666
Open
gaoflow wants to merge 1 commit into
Open
Conversation
set_key() writes values wrapped in single quotes, and the parser
decodes \\ as a literal backslash via decode_escapes(). But
set_key() only escaped single-quote characters (\'), not backslashes
themselves. This meant any value containing two or more consecutive
backslashes lost one during round-trip:
set_key(path, 'K', 'a\\\\b')
dotenv_values(path)['K'] # -> 'a\\b' (lost one backslash)
The parser's _single_quote_escapes regex matches both \\' and \\\\,
so backslashes MUST be doubled when writing to preserve them. Fix by
escaping backslashes before single-quotes in the value quoting step.
- src/dotenv/main.py: +2/-0
- tests/test_main.py: +23 (parametrized round-trip regression test)
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.
Bug
set_key()writes values wrapped in single quotes, and the parser decodes\\as a literal backslash viadecode_escapes(). Butset_key()only escaped single-quote characters (\'), not backslashes themselves. This meant any value containing two or more consecutive backslashes lost one during round-trip:Also reproduces for Windows paths (
C:\Users\...) and any value with\\sequences.Root Cause
The parser's
_single_quote_escapesregex matches both\'and\\, decoding them viacodecs.decode(..., 'unicode-escape'). Whenset_keywrites a value containing literal\\, the parser interprets it as an escaped backslash and decodes it to\, losing one backslash on each consecutive pair.Fix
Escape backslashes in addition to single-quotes in
set_key(), before wrapping in single quotes. The order is important: backslashes must be escaped first (\\->\\\\), then single-quotes ('->\').Diff
src/dotenv/main.py: +2/-0 (escape backslashes before single-quotes)tests/test_main.py: +23 (parametrized round-trip regression test covering 0-3 backslashes, Windows paths, and mixed quotes+backslashes)