Repository navigation
Commit 8cd44f5
authored
fix: preserve leading newline of multiline string built with string() (#551)
`tomlkit.string(value, multiline=True)` rendered the value as
`"""<value>"""`. When `value` starts with a newline the output looks
like `"""\n..."""`, and per the TOML spec a newline immediately
following the opening delimiter is trimmed on parse. So a value that
begins with a newline was silently lost on the very next round-trip:
>>> s = tomlkit.string("\nfoo", multiline=True)
>>> str(s)
'\nfoo'
>>> s.as_string()
'"""\nfoo"""'
>>> str(tomlkit.parse(f"k = {s.as_string()}")["k"])
'foo' # leading newline dropped
The value the String object reports is correct; only its serialized
form was wrong, so the corruption only surfaced after re-parsing.
Both tomllib and tomlkit's own parser agree the old output decodes to
the wrong value, so this is a genuine serialization bug (not a TOML
1.0 vs 1.1 difference). Affects basic and literal multiline strings,
including values that start with `\r\n`.
Fix: in `String.from_raw`, when the string is multiline and its
rendered body starts with a newline, emit an extra leading newline
(the one the parser trims) so the value survives round-tripping. This
is exactly how the spec suggests representing such a value. Single-line
strings and multiline strings without a leading newline are unchanged.
An existing `test_create_string` case asserted the old lossy output
(`"""\nMy\nString\n"""`); it only checked `as_string()`, never a
round-trip, so it had locked in the bug. Updated it to the correct,
round-trippable rendering and added a dedicated round-trip regression
test covering basic/literal multiline and `\r\n`-leading values.1 parent 67d3e86 commit 8cd44f5
3 files changed
Lines changed: 33 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
506 | 506 | | |
507 | 507 | | |
508 | 508 | | |
509 | | - | |
| 509 | + | |
510 | 510 | | |
511 | 511 | | |
512 | 512 | | |
| |||
537 | 537 | | |
538 | 538 | | |
539 | 539 | | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
540 | 564 | | |
541 | 565 | | |
542 | 566 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2251 | 2251 | | |
2252 | 2252 | | |
2253 | 2253 | | |
| 2254 | + | |
| 2255 | + | |
| 2256 | + | |
| 2257 | + | |
| 2258 | + | |
| 2259 | + | |
| 2260 | + | |
2254 | 2261 | | |
2255 | 2262 | | |
2256 | 2263 | | |
| |||
0 commit comments