classification
Title: configparser accepts keys beginning with comment_chars when writing
Type: behavior Stage:
Components: Library (Lib) Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: lukasz.langa Nosy List: BTaskaya, The Compiler, lukasz.langa
Priority: normal Keywords:

Created on 2014-05-13 14:47 by The Compiler, last changed 2020-01-11 15:02 by BTaskaya.

Messages (2)
msg218463 - (view) Author: Florian Bruhin (The Compiler) * Date: 2014-05-13 14:47
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.
msg359797 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2020-01-11 15:02
Any update on this? Discussion over Python-Dev looks like finished without a consensus/resolution.
History
Date User Action Args
2020-01-11 15:02:37BTaskayasetnosy: + BTaskaya
messages: + msg359797
2014-05-27 06:56:17lukasz.langasetassignee: lukasz.langa
2014-05-13 14:47:29The Compilercreate