Skip to content

configparser.write() loses UNNAMED_SECTION items to DEFAULT on round-trip #154596

Description

@tonghuaroot

Bug description

ConfigParser.write() outputs UNNAMED_SECTION items after the [DEFAULT] header. When the output is read back, the headerless items appear under [DEFAULT] and are lost from UNNAMED_SECTION.

import configparser, io

US = configparser.UNNAMED_SECTION
cfg = configparser.ConfigParser(allow_unnamed_section=True)
cfg.read_string('key1 = val1\n[DEFAULT]\ndkey = dval\n[sect1]\nkey2 = val2\n')

out = io.StringIO()
cfg.write(out)

cfg2 = configparser.ConfigParser(allow_unnamed_section=True)
cfg2.read_string(out.getvalue())
print('key1' in cfg2[US])       # False -- lost
print('key1' in cfg2.defaults())  # True -- migrated to DEFAULT

CPython versions tested on

3.14, main

Operating systems tested on

macOS

Linked PRs

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