Skip to content

configparser fails to reject comment-prefixed multiline values that cannot round-trip #158700

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: The public documentation for InvalidWriteError says: "Raised when attempting to write data that the parser would read back differently."

Expected: RawConfigParser.write() raises InvalidWriteError for a value whose continuation line would be discarded as a comment.

Actual: write() succeeds, emitting '[s]\nkey = a\n\t#b\n\n'; an identically configured parser reads the value as 'a'.

import configparser as c
import io

data = {"section": "s", "option": "key", "value": "a\n#b"}
p = c.RawConfigParser()
try:
    # Documented input types: string section, option and value.
    p.add_section(data["section"])
    p.set(data["section"], data["option"], data["value"])
except (TypeError, ValueError, c.Error) as e:
    print("REFUTATION REJECTED:", "invalid input:", repr(e))
else:
    out = io.StringIO()
    try:
        p.write(out)
    except Exception as e:
        print("REFUTATION REJECTED:", "write raised", type(e).__name__)
    else:
        text = out.getvalue()
        q = c.RawConfigParser()
        q.read_string(text)
        restored = q.get(data["section"], data["option"])
        actual = ("write succeeded", text, restored)
        # Independently test the documented round-trip promise.
        if restored != data["value"]:
            print("REFUTATION CONFIRMED:", data, "actual:", actual,
                  "expected:", ("exception", "InvalidWriteError"))
        else:
            print("REFUTATION REJECTED:", "round trip preserved the value")

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library configparser:

REFUTATION CONFIRMED: {'section': 's', 'option': 'key', 'value': 'a\n#b'} actual: ('write succeeded', '[s]\nkey = a\n\t#b\n\n', 'a') expected: ('exception', 'InvalidWriteError')

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/configparser-20261003-195537-c3

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions