Skip to content

configparser: write() silently drops leading whitespace from multiline values on round-trip #161317

Closed as not planned

Description

@BHUVANSH855

Bug report

If a value contains a newline followed by whitespace, writing and reading it back silently loses the whitespace with no error.

import configparser, io

c = configparser.ConfigParser()
c['s'] = {'k': 'line1\n\tline2'}

buf = io.StringIO()
c.write(buf)

c2 = configparser.ConfigParser()
c2.read_string(buf.getvalue())
print(repr(c2['s']['k']))
# Got:      'line1\nline2'
# Expected: 'line1\n\tline2'

The written file has:

[s]
k = line1
line2

_write_section adds \n\t to mark continuation lines. On read, _handle_continuation_line calls .strip() on each line — which strips both the added tab AND any whitespace that was part of the original value.

On 3.14+, write() should raise InvalidWriteError here, same as it already does for keys with leading whitespace (gh-96239). On earlier versions the data is just silently lost.

CPython versions tested on:

3.13+

Operating systems tested on:

Linux

Linked PRs

Activity

  1. BHUVANSH855 commented on Nov 4, 2026

    @BHUVANSH855
    ContributorAuthor

    this is reproducible on all 3.13, 3.14, 3.15 and main`. I have a fix ready for this and would love to have a PR for this after it got triaged by maintainers.

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 4, 2026
  3. NekoAsakura commented on Nov 4, 2026

    @NekoAsakura
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions