[issue22253] ConfigParser does not handle files without sections

Pedro Lacerda report at bugs.python.org
Sat Jun 11 16:59:23 EDT 2016


Pedro Lacerda added the comment:

I also never found a mixture of sectionless options followed by sectioned options. So an unnamed section that is also the DEFAULTSECTION will probably work.

In this patch when `default_section=None` is passed to `RawConfigParser` it will parse top level options into the default section and skip writing its title.

As drawback, default options is not showed in `options()` or `has_section()` reducing it usefulness. It works with `items()` and `keys()` however.

> Using DEFAULTSECT for this purpose is equally wrong since it would
> silently inject default values to every section

I disagree with that because I really *never* found in wild a file where it will happen.

> All in all, it comes down to the question whether the programmer
> expects section-less configuration. If not, the '' section will not be 
> helpful anyway. If yes, then it's desirable to be able to specify a
> section name for global options at *read time*.

Pass a name at read time will improve the API as `sections()` and `has_section()` will work as usual and not like a DEFAULTSECTION.

Please look my patch and tell if it's acceptable, if you prefer that a section name must be given at read and write time we can manage it.

It's my first post in this tracker and I'm very glad that I got it working even if not merged!

----------
keywords: +patch
nosy: +Pedro Lacerda
versions:  -Python 3.5
Added file: http://bugs.python.org/file43345/nosection.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22253>
_______________________________________


More information about the Python-bugs-list mailing list