fix: unescape escape sequences in double-quoted section header keys - #458
Open
gaoflow wants to merge 1 commit into
Open
fix: unescape escape sequences in double-quoted section header keys#458gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
TOML spec §2.4 (Keys) says quoted keys follow the same escaping rules as basic strings. The section-header parsing loop stripped surrounding quotes from double-quoted key names (line 451 of decoder.py) but never called `_unescape()`, leaving sequences like `\t`, `\n`, `\r`, and `\\` as literal backslash-letter pairs instead of their decoded equivalents. The same call to `_unescape()` that `load_line` already makes for inline double-quoted keys (line 769) is now applied consistently to section-header keys. Single-quoted keys are unaffected: they remain literal per TOML spec. Fixes: double-quoted section header keys containing `\t`, `\n`, `\r`, or `\\` returning the wrong string (one extra backslash character per escape sequence compared to the correct stdlib `tomllib` result).
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
TOML spec §2.4 (Keys) says quoted keys follow
the same escaping rules as basic strings. The section-header parsing loop in
decoder.pystripped surrounding quotes from double-quoted key names but nevercalled
_unescape(). As a result escape sequences like\t,\n,\r, and\\were left as literal backslash-letter pairs.The same
_unescape()call thatload_linealready makes for inlinedouble-quoted keys (line 769) was simply missing for section-header keys.
Fix
In
toml/decoder.py, the quoted-key stripping inside the group-name loopnow calls
_unescape()for double-quoted key names. Single-quoted keysare left unchanged (TOML spec: literal, no escaping).
Tests
Added
test_section_header_key_escape_sequenceswhich checks\t,\n,\r, and\\against the stdlibtomllibreference. All 25 tests pass.This pull request was prepared with the assistance of AI, under my direction and review.