You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
configparser.ConfigParser.clean and .update bugs #61024
Thanks for your report, Wolfgang. The clean() method is now fixed. The update() situation is more complicated, however.
The mapping protocol defines that
mapping.update({'a': 'b', 'c': 'd'})
and
mapping.update(a='b', c='d')
are equivalent to
mapping['a'] = 'b'
mapping['c'] = 'd'
For configparser we decided that setting a section on a parser is
a destructive operation (e.g. it overwrites previous options). If the user
finds this undesirable, she can either use parser['section'].update() or parser.read_dict() like you suggested.
The bug here is that __setitem__ for the DEFAULTSECT should also clear options
previously set on this section.
I only noted the discrepancy and did not give it much thought.
I will just implement a merge method on top of read_dict.
That gives me all options that could be desired :).
However, after implementing the entire compatibility layer, I found one more issue:
Using self.remove_section() changes the section order during an update.
I would prefer that the section be cleared instead of removed in order to preserve the section order. Since OrderedDict does indeed preserve the key order during update, I think that this does not violate the Mapping Protocol.
If this is not desired, just go ahead and close the issue.
See also attached example bug_configparser_update_order.py:
OrderedDict does not change the order of keys upon .update():
>>> od = OrderedDict((('section1', {}), ('section2', {})))
New changeset 2f5320497017 by Łukasz Langa in branch '3.3':
Null-merged 3.2 section order preservation fix when using __setitem__ (issue http://hg.python.org/cpython/rev/2f5320497017
New changeset 27b698395d35 by Łukasz Langa in branch 'default':
Null-merged 3.2 section order preservation fix when using __setitem__ (issue http://hg.python.org/cpython/rev/27b698395d35
Breaking section order when using __setitem__ was a bug and as such was fixed in 3.2.4 and 3.3.1.
In the DEFAULTSECT case above someone could potentially use the broken behaviour as a feature so that change was only made for 3.3.1+. In the section order case there is no such controversy.
Activity
wolfmanx commentedon Dec 30, 2012
configparser.ConfigParser.clean() always fails:
>>> cfg = configparser.ConfigParser() >>> if not hasattr(configparser.ConfigParser, 'clear'): ... configparser.ConfigParser.clear = configparser_clear_compat >>> cfg.clear() #doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: Cannot remove the default section.configparser.ConfigParser.update() overwrites all sections except DEFAULT instead of updating them.
See attached test file-
python-dev commentedon Dec 31, 2012
New changeset dc5adc08f1a8 by Łukasz Langa in branch '3.2':
Fixes
parser.clean()reported in issue bpo-16820.http://hg.python.org/cpython/rev/dc5adc08f1a8
New changeset 4fc2fea807e6 by Łukasz Langa in branch '3.3':
Merged
parser.clean()fix (issue bpo-16820) from 3.2.http://hg.python.org/cpython/rev/4fc2fea807e6
New changeset c6f9bc5a0cf1 by Łukasz Langa in branch 'default':
Merged
parser.clean()fix (issue bpo-16820) from 3.2 through 3.3.http://hg.python.org/cpython/rev/c6f9bc5a0cf1
ambv commentedon Dec 31, 2012
Thanks for your report, Wolfgang. The
clean()method is now fixed. Theupdate()situation is more complicated, however.The mapping protocol defines that
and
are equivalent to
For
configparserwe decided that setting a section on a parser isa destructive operation (e.g. it overwrites previous options). If the user
finds this undesirable, she can either use
parser['section'].update()orparser.read_dict()like you suggested.The bug here is that __setitem__ for the DEFAULTSECT should also clear options
previously set on this section.
python-dev commentedon Dec 31, 2012
New changeset 459a23083b66 by Łukasz Langa in branch '3.3':
Fixes
__setitem__on parser['DEFAULT'] reported in issue bpo-16820.http://hg.python.org/cpython/rev/459a23083b66
New changeset f6fb5a5748f0 by Łukasz Langa in branch 'default':
Merged
parser['DEFAULT'].__setitem__fix (issue bpo-16820) from 3.3.http://hg.python.org/cpython/rev/f6fb5a5748f0
ambv commentedon Dec 31, 2012
For the record, the bug that caused the following to be equivalent:
parser['DEFAULT'] = {'option': 'value'}
parser['DEFAULT'].update({'option': 'value'})
has been fixed for 3.3.1+ only. This way it's going to be easier for users to reason about the fix ("it was broken in 3.2.0 - 3.3.0").
Note that the bug only affected __setitem__ on the default section.
wolfmanx commentedon Dec 31, 2012
Thanks, works for me.
I only noted the discrepancy and did not give it much thought.
I will just implement a merge method on top of read_dict.
That gives me all options that could be desired :).
However, after implementing the entire compatibility layer, I found one more issue:
Using self.remove_section() changes the section order during an update.
I would prefer that the section be cleared instead of removed in order to preserve the section order. Since OrderedDict does indeed preserve the key order during update, I think that this does not violate the Mapping Protocol.
If this is not desired, just go ahead and close the issue.
See also attached example bug_configparser_update_order.py:
OrderedDict does not change the order of keys upon .update():
>>> list(od.keys()) ['section1', 'section2']>>> list(od.keys()) ['section1', 'section2', 'section3']But ConfigParser changes the order of sections upon .update():
>>> cfg.sections() ['section1', 'section2']>>> cfg.sections() ['section2', 'section1', 'section3']ambv commentedon Jan 1, 2013
This is a fair point. Stay tuned.
python-dev commentedon Jan 1, 2013
New changeset f580342b63d8 by Łukasz Langa in branch '3.3':
configparser: preserve section order when using
__setitem__(issue bpo-16820)http://hg.python.org/cpython/rev/f580342b63d8
New changeset a758f561a280 by Łukasz Langa in branch 'default':
Merged section order preservation fix when using
__setitem__(issue bpo-16820)http://hg.python.org/cpython/rev/a758f561a280
python-dev commentedon Jan 1, 2013
New changeset 6f0cee62f0c6 by Łukasz Langa in branch '3.2':
configparser: preserve section order when using
__setitem__(issue bpo-16820)http://hg.python.org/cpython/rev/6f0cee62f0c6
New changeset 2f5320497017 by Łukasz Langa in branch '3.3':
Null-merged 3.2 section order preservation fix when using
__setitem__(issuehttp://hg.python.org/cpython/rev/2f5320497017
New changeset 27b698395d35 by Łukasz Langa in branch 'default':
Null-merged 3.2 section order preservation fix when using
__setitem__(issuehttp://hg.python.org/cpython/rev/27b698395d35
ambv commentedon Jan 1, 2013
Breaking section order when using
__setitem__was a bug and as such was fixed in 3.2.4 and 3.3.1.In the DEFAULTSECT case above someone could potentially use the broken behaviour as a feature so that change was only made for 3.3.1+. In the section order case there is no such controversy.
python-dev commentedon Jan 2, 2013
New changeset d5c45089df2d by Łukasz Langa in branch '3.3':
Misc/NEWS updated to tell about bpo-14590 and bpo-16820
http://hg.python.org/cpython/rev/d5c45089df2d
New changeset 7938847b2641 by Łukasz Langa in branch '3.2':
Misc/NEWS updated to tell about bpo-16820
http://hg.python.org/cpython/rev/7938847b2641