Skip to content

Escape multiline strings containing triple quotes in to_hocon#347

Open
gaoflow wants to merge 1 commit into
chimpler:masterfrom
gaoflow:fix-to-hocon-triple-quote-multiline
Open

Escape multiline strings containing triple quotes in to_hocon#347
gaoflow wants to merge 1 commit into
chimpler:masterfrom
gaoflow:fix-to-hocon-triple-quote-multiline

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 20, 2026

Copy link
Copy Markdown

Summary

HOCONConverter.to_hocon emits 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 the to_hocon → parse round-trip.

from pyhocon import ConfigFactory
from pyhocon.converter import HOCONConverter

out = HOCONConverter.to_hocon(ConfigFactory.from_dict({"k": 'a\nb"""c'}))
# -> k = """a\nb"""c"""
ConfigFactory.parse_string(out)   # ParseException

out = HOCONConverter.to_hocon(ConfigFactory.from_dict({"k": 'before\n"""middle"""\nafter'}))
ConfigFactory.parse_string(out)["k"]   # silently 'before\nmiddle\nafter' — the """ markers dropped

to_json handles the identical values correctly, confirming the value is representable.

Cause

In to_hocon (pyhocon/converter.py), both the str branch and the ConfigQuotedString branch chose the """...""" form solely on the presence of a newline:

if '\n' in config and len(config) > 1:
    lines = '"""{value}"""'.format(value=config)  # multilines

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, preserving test_format_multiline_string.

Verification

  • Added 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.
  • Full suite: 309 passed, 1 xfailed (was 308 + 1 xfailed; no regressions). flake8 pyhocon tests setup.py exits 0.

This pull request was prepared with the assistance of AI, under my direction and review.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant