Escape multiline strings containing triple quotes in to_hocon#347
Open
gaoflow wants to merge 1 commit into
Open
Escape multiline strings containing triple quotes in to_hocon#347gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
HOCONConverter.to_hocon emitted any string containing a newline as a triple-quoted literal, even when the string itself contained a """ sequence. The embedded """ terminates the literal early, so the output either failed to re-parse or silently dropped the """ markers, breaking the to_hocon -> parse round-trip. Only use the triple-quoted form when the value does not contain """; otherwise fall back to the existing escaped single-quoted form (which already escapes newlines), in both the str and ConfigQuotedString branches.
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
HOCONConverter.to_hoconemits any string containing a newline as a triple-quoted ("""...""") literal — even when the string itself contains a"""sequence. The embedded"""terminates the literal early, so the output either fails to re-parse or silently drops the"""markers, breaking theto_hocon→ parse round-trip.to_jsonhandles the identical values correctly, confirming the value is representable.Cause
In
to_hocon(pyhocon/converter.py), both thestrbranch and theConfigQuotedStringbranch chose the"""..."""form solely on the presence of a newline:This ignores that a HOCON triple-quoted string cannot contain the
"""sequence.Fix
Only use the triple-quoted form when the value does not contain
"""; otherwise fall through to the existing escaped single-quoted path (_escape_string, which already escapes\n→\n). Applied to both branches. Normal multiline strings (no""") are unchanged, preservingtest_format_multiline_string.Verification
test_format_multiline_string_with_triple_quote, which asserts the escaped output form and round-trips three"""-containing values through the parser. It fails before the fix and passes after.flake8 pyhocon tests setup.pyexits 0.This pull request was prepared with the assistance of AI, under my direction and review.