python / python/cpython

Support comments as part of multiline values in configparser

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

还没有人认领这个 Issue。

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

描述

Feature or enhancement

Proposal:

Today's configparser module does not consider lines being properly indented and starting with a comment_prefixes to be part of a multiline value. This can lead to unintended data loss during reading an INI file as:
config.ini

# I am a comment
    # I am also a comment
[dummy]
# I am a comment
key = I am a multiline
# I am a comment
    # multiline part which is not considered but should be
    being again part
# I am a comment

test.py

import configparser
config = configparser.ConfigParser(comment_prefixes='#', interpolation=None, inline_comment_prefixes=None)
config.read('config.ini')
print(config['dummy']['key'])
# output: 'I am a multiline\nbeing again part'
# expected output:  'I am a multiline\n# multiline part which is not considered but should be\nbeing again part''

Therefore, I would propose to add a new parameter allow_comment_in_value (default False) in the ConfigParser class to support such scenarios. Please note that in this scenario the comment_prefixes is set to '#' and any interpolation is disabled!

Thanks and best regards!

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 ConfigParser 入口点开始,使用 issue 中的 config.ini 和 test.py 示例复现该行为。跟踪 comment_prefixes 和多行值的处理方式,然后为提议的选项添加测试覆盖。完成标准是:缩进的注释会保留在多行值中,同时现有行为在默认情况下保持不变。

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

评估

技术栈
python
领域
tooling
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

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