This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ConfigParser should be able to write config to a given filename, not only into file object
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: bbayles, berker.peksag, georgefischhof, lukasz.langa, mcepl, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2016-11-24 15:01 by georgefischhof, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 5999 closed python-dev, 2018-03-06 00:07
Messages (5)
msg281636 - (view) Author: George Fischhof (georgefischhof) Date: 2016-11-24 15:01
Hi There,

I started to use ConfigParser, and found that it has no write to file_name option, but xml paarser (ElementTree) has.

This way ConfigParser works different than xml parsers, as when I use elementtree.write it will create a file with full and valid xml content.

But ConfigParser is "able" ;-) to create file with invalid content (for example (this was my findings) creates duplicated sections. Because the handling of the file is in the user's hand.

So it would be good for ConfigParser to handle file writing and the user!s code would became simplier:

Feature request:
ConfigParser should be able to write config to a given filename, not only into file object.
(Like xml parser)

Kind regards,
George Fischhof
msg313597 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-03-11 13:14
What is a problem with writing two lines of code?

    with open(...) as f:
        config.write(f)

If combine ConfigParser.write() with builtin open() you will need to add support of encoding, errors, buffering, newline, and other arguments supported by open(). And don't forgot to update this method after adding new parameters to open().

I'm -1. Not every two lines of code deserve including as a function in the stdlib. This will clutter the user interface, complicate the implementation, and add the burden to core developers for maintaining this code.
msg313598 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-03-11 13:52
George, your third sentence is missing a closing ')' somewhere.  In the first part, I believe you meant that the user is able to create a bad file by directly writing other content or calling cf.write more than once.  I don't get the part about duplicated sections.

I was otherwise +-0 about the feature until Serhiy articulated the negatives.  I am now more inclined to close this.

I just reviewed the PR, and the added complications in the test mirror the added complication in the code. With the patch as is, we should document that if a filename is passed, it would be opened with default args other than the mode.

There are other filename or file APIs, but that does not mean that they are without problems.  I suspect that at least some date to times before the addition of with statements.
msg313601 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-03-11 15:32
I agree with Serhiy and Terry. Also, PR 5999 does not accept pathlib.Path() objects at the moment. This and Serhiy's following comment are good examples of why it shouldn't be added to the standard library in my opinion:

> If combine ConfigParser.write() with builtin open() you will need to
> add support of encoding, errors, buffering, newline, and other
> arguments supported by open(). And don't forgot to update this method
> after adding new parameters to open().
msg313602 - (view) Author: Matej Cepl (mcepl) * Date: 2018-03-11 15:42
OK, Serhiy you have the point: it was just a good exercise for me, but the benefit is doubtful. Could somebody close this ticket then?
History
Date User Action Args
2022-04-11 14:58:40adminsetgithub: 72974
2018-03-11 15:47:25berker.peksagsetstatus: open -> closed
resolution: rejected
stage: patch review -> resolved
2018-03-11 15:42:34mceplsetmessages: + msg313602
2018-03-11 15:32:24berker.peksagsetnosy: + serhiy.storchaka
messages: + msg313601
2018-03-11 15:16:21terry.reedysetnosy: + berker.peksag
2018-03-11 13:52:31terry.reedysetnosy: + terry.reedy, - serhiy.storchaka
messages: + msg313598
versions: + Python 3.8, - Python 3.5
2018-03-11 13:14:01serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg313597
2018-03-11 12:24:30terry.reedysetnosy: + lukasz.langa
2018-03-06 00:07:00python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request5765
2018-03-05 14:15:56bbaylessetnosy: + bbayles
2018-03-05 13:15:19mceplsetnosy: + mcepl
2016-11-25 22:29:48terry.reedysettitle: Feature request: ConfigParser should be able to write config to a given filename, not only into file object -> ConfigParser should be able to write config to a given filename, not only into file object
2016-11-24 15:01:58georgefischhofcreate