fix(config): preserve long quoted scalars

This commit is contained in:
Hukla
2026-09-23 15:25:57 +03:00
committed by Teknium
parent 93c5856bd5
commit 7f59505685
2 changed files with 23 additions and 0 deletions

View File

@@ -120,6 +120,20 @@ class TestAtomicRoundtripYamlSave:
assert "\\u4f60" not in text
assert "\\u30CE" not in text
def test_preserves_long_double_quoted_scalar_with_backslash(self, config_path):
"""A no-op save must not turn fold indentation after a backslash into data."""
value = "A" * 74 + r"D:\CentBrowserPortable " + "B" * 40
config_path.write_text(
'policy: "' + value.replace("\\", "\\\\") + '"\n',
encoding="utf-8",
)
from utils import atomic_roundtrip_yaml_save
atomic_roundtrip_yaml_save(config_path, {"policy": value})
assert yaml.safe_load(config_path.read_text(encoding="utf-8"))["policy"] == value
def test_appends_new_keys(self, config_path):
config_path.write_text(
"model:\n"

View File

@@ -431,6 +431,14 @@ def atomic_yaml_write(path: Union[str, Path], data: Any, *, default_flow_style:
_atomic_write(path, _write, prefix=f".{path.stem}_", mode=_mode_for_write(path, create_mode))
# ruamel's emitter can change a double-quoted value when it folds a long line right after an
# escaped backslash (``D:\\Cent…`` → ``D:\\`` + bare newline): the fold reloads as a literal space
# and a no-op save mutates the stored value (#119844). Config writes must be value-preserving, so
# every round-trip emitter in the tree keeps scalars on one line instead of folding (``None``
# does NOT disable folding on 0.18.x; only a large width does).
ROUNDTRIP_YAML_WIDTH = 2**31 - 1
def _roundtrip_load(path: Path):
"""``(yaml_rt, CommentedMap)``: a ruamel round-trip loader keeping quotes/Unicode with 2-space
indents, plus *path* loaded through it (empty map when missing/blank)."""
@@ -438,6 +446,7 @@ def _roundtrip_load(path: Path):
from ruamel.yaml.comments import CommentedMap
yaml_rt = YAML(typ="rt")
yaml_rt.width = ROUNDTRIP_YAML_WIDTH
yaml_rt.preserve_quotes = True
yaml_rt.allow_unicode = True
yaml_rt.default_flow_style = False