Skip to content

Support comments as part of multiline values in configparser #130927

@dabuahoid

Description

@dabuahoid

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions