python / python/cpython

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

Open
#154,596 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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
  • gh-154597

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the round-trip shown in the issue, then inspect ConfigParser.write() and read_string() to trace how UNNAMED_SECTION items are emitted and parsed. Done means the headerless key remains in cfg2[UNNAMED_SECTION] after writing and reading the configuration; linked PR gh-154597 indicates work is already underway.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.