python / python/cpython

configparser: whitespace-ending delimiter with an empty value cannot be parsed (write() output is unreadable)

未关闭
#157,456 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

The whitespace-in-delimiters feature added in gh-156353 (3.16) cannot parse an option that has an empty value when the delimiter ends in whitespace — including its own write() output.

_read matches option lines against line.clean, which is val.strip() (both sides) with comments removed. For a whitespace delimiter, the delimiter is the trailing whitespace, so strip() erases it and OPTCRE no longer matches.

Reproduction (on main, 3.16.0a0)

import configparser, io

# (1) plain read — rejected, though the option regex itself matches "key ":
cp = configparser.ConfigParser(delimiters=(' ',))
cp.read_string("[s]\nkey \n")          # -> ParsingError   (expected: {'key': ''})

# (2) round-trip — write() emits a line read_string() then refuses to parse:
w = configparser.ConfigParser(delimiters=(' ',)); w['s'] = {'key': ''}
buf = io.StringIO(); w.write(buf)
print(repr(buf.getvalue()))            # '[s]\nkey   \n\n'
configparser.ConfigParser(delimiters=(' ',)).read_string(buf.getvalue())  # ParsingError

A serializer that cannot read its own output is a clear self-inconsistency. Empty values are a supported, first-class feature for every other delimiter (key=, key:, key->, key|| all read back as {'key': ''}), so whitespace delimiters should behave the same.

Bug class

Any delimiter whose string ends in whitespace combined with an empty (or all-whitespace) value: ' ', '\t', ' ', 'x ', '= ' all raise; delimiters without trailing whitespace ('->', ' x') work.

Why it's not caught

The gh-156353 tests (test_space_delimiter, test_any_delimiter) only use non-empty values, so this edge is unpinned.

Fix

Match the option regex against a form that preserves trailing whitespace (the delimiter). optval is already stripped after the match, so ordinary values keep no trailing whitespace and the other uses of line.clean are unaffected. I have a small patch + tests and will open a PR referencing this issue.

Found with AI assistance; I've reproduced and verified the behaviour and fix on a locally built interpreter and understand them.

Linked PRs
  • gh-157457

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 ConfigParser._read 以及 issue 中描述的现有 test_space_delimiter 和 test_any_delimiter 覆盖开始。使用 read_string() 和 write() 重现空值情况,然后扩展测试,使以空白结尾的分隔符和空值能够一致地解析,并且往返过程成功。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
tooling
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。