Skip to content

configparser.write fails to reject comment-prefixed option names #158699

Description

@augusto-rehfeldt

Bug report

Bug description:

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

Expected: RawConfigParser.write() raises InvalidWriteError for an option name such as '#a' or ';a' that would be omitted on readback.

Actual: write() succeeds and emits '[s]\n#a = v\n\n'; a fresh default parser reads section 's' with no options.

import configparser as c
import io

inp = {"section": "s", "option": "#a", "value": "v"}
try:
    # Documented API: string section/option/value; no forbidden option names.
    p = c.RawConfigParser()
    p.add_section(inp["section"])
    p.set(inp["section"], inp["option"], inp["value"])
    original = p.items("s", raw=True)
    if original != [("#a", "v")]:
        raise ValueError("input was not stored as reported")

    out = io.StringIO()
    try:
        p.write(out)
    except Exception as e:
        if isinstance(e, getattr(c, "InvalidWriteError", ())):
            print("REFUTATION REJECTED:", "write raised InvalidWriteError")
        else:
            print("REFUTATION REJECTED:", repr(e))
    else:
        q = c.RawConfigParser()
        q.read_string(out.getvalue())
        readback = q.items("s", raw=True) if q.has_section("s") else []
        actual = {
            "status": "written",
            "output": out.getvalue(),
            "readback_items": readback,
        }
        expected = {
            "must_raise_InvalidWriteError": readback != original,
            "original_items": original,
            "readback_items": readback,
        }
        if expected["must_raise_InvalidWriteError"]:
            print("REFUTATION CONFIRMED:", inp, actual, expected)
        else:
            print("REFUTATION REJECTED:", "output preserves the input", actual)
except Exception as e:
    print("REFUTATION REJECTED:", "validation/readback failed:", repr(e))

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

REFUTATION CONFIRMED: {'section': 's', 'option': '#a', 'value': 'v'} {'status': 'written', 'output': '[s]\n#a = v\n\n', 'readback_items': []} {'must_raise_InvalidWriteError': True, 'original_items': [('#a', 'v')], 'readback_items': []}

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-021945-c2

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