python / cpython

The Python programming language
https://www.python.org
Other
63.37k stars 30.33k forks source link

configparser accepts keys beginning with comment_chars when writing #65697

Open b8c17aa0-8543-473e-baae-23d368fad117 opened 10 years ago

b8c17aa0-8543-473e-baae-23d368fad117 commented 10 years ago
BPO 21498
Nosy @ambv, @The-Compiler, @isidentical

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = 'https://github.com/ambv' closed_at = None created_at = labels = ['type-bug', 'library'] title = 'configparser accepts keys beginning with comment_chars when writing' updated_at = user = 'https://github.com/The-Compiler' ``` bugs.python.org fields: ```python activity = actor = 'BTaskaya' assignee = 'lukasz.langa' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'The Compiler' dependencies = [] files = [] hgrepos = [] issue_num = 21498 keywords = [] message_count = 2.0 messages = ['218463', '359797'] nosy_count = 3.0 nosy_names = ['lukasz.langa', 'The Compiler', 'BTaskaya'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue21498' versions = [] ```

b8c17aa0-8543-473e-baae-23d368fad117 commented 10 years ago

When adding something to a configparser instance which has a key beginning with a comment char, it writes the data to a file without generating an error, and when reading the file back obviously the data is different as it's a comment:

    >>> cp = configparser.ConfigParser()
    >>> cp.read_dict({'DEFAULT': {';foo': 'bar'}})
    >>> cp.write(sys.stdout)
    [DEFAULT]
    ;foo = bar

This was discussed on python-dev here: https://mail.python.org/pipermail/python-dev/2014-April/134293.html

Of course there are other corner cases as well, like having a key like "[foo]" or "=bar".

I think whatever data I pass into a configparser should also come out again when reading the file back.

Since there's no escaping in configparser, I think the ideal solution would be configparser refusing to write ambigious values.

While this is technically a backwards-incompatible change, applications doing this were broken in the first place, so validation while writing will not break anything.

Validating when setting values would be better of course, but this can potentially break applications where configparser is used without actually writing a file.

isidentical commented 4 years ago

Any update on this? Discussion over Python-Dev looks like finished without a consensus/resolution.