Skip to content

Commit 3a5b0d8

Browse files
amyreeseambv
authored andcommitted
bpo-33504: Migrate configparser from OrderedDict to dict. (#6819)
With 3.7+, dictionary are ordered by design. Configparser still uses collections.OrderedDict, which is unnecessary. This updates the module to use the standard dict implementation by default, and changes the docs and tests to match.
1 parent 5f3d04f commit 3a5b0d8

File tree

5 files changed

+15
-43
lines changed

5 files changed

+15
-43
lines changed

Doc/library/configparser.rst

+9-41
Original file line numberDiff line numberDiff line change
@@ -445,20 +445,19 @@ the :meth:`__init__` options:
445445
Hint: if you want to specify default values for a specific section, use
446446
:meth:`read_dict` before you read the actual file.
447447

448-
* *dict_type*, default value: :class:`collections.OrderedDict`
448+
* *dict_type*, default value: :class:`dict`
449449

450450
This option has a major impact on how the mapping protocol will behave and how
451-
the written configuration files look. With the default ordered
452-
dictionary, every section is stored in the order they were added to the
453-
parser. Same goes for options within sections.
451+
the written configuration files look. With the standard dictionary, every
452+
section is stored in the order they were added to the parser. Same goes for
453+
options within sections.
454454

455455
An alternative dictionary type can be used for example to sort sections and
456-
options on write-back. You can also use a regular dictionary for performance
457-
reasons.
456+
options on write-back.
458457

459458
Please note: there are ways to add a set of key-value pairs in a single
460459
operation. When you use a regular dictionary in those operations, the order
461-
of the keys may be random. For example:
460+
of the keys will be ordered. For example:
462461

463462
.. doctest::
464463

@@ -474,40 +473,9 @@ the :meth:`__init__` options:
474473
... 'baz': 'z'}
475474
... })
476475
>>> parser.sections() # doctest: +SKIP
477-
['section3', 'section2', 'section1']
478-
>>> [option for option in parser['section3']] # doctest: +SKIP
479-
['baz', 'foo', 'bar']
480-
481-
In these operations you need to use an ordered dictionary as well:
482-
483-
.. doctest::
484-
485-
>>> from collections import OrderedDict
486-
>>> parser = configparser.ConfigParser()
487-
>>> parser.read_dict(
488-
... OrderedDict((
489-
... ('s1',
490-
... OrderedDict((
491-
... ('1', '2'),
492-
... ('3', '4'),
493-
... ('5', '6'),
494-
... ))
495-
... ),
496-
... ('s2',
497-
... OrderedDict((
498-
... ('a', 'b'),
499-
... ('c', 'd'),
500-
... ('e', 'f'),
501-
... ))
502-
... ),
503-
... ))
504-
... )
505-
>>> parser.sections() # doctest: +SKIP
506-
['s1', 's2']
507-
>>> [option for option in parser['s1']] # doctest: +SKIP
508-
['1', '3', '5']
509-
>>> [option for option in parser['s2'].values()] # doctest: +SKIP
510-
['b', 'd', 'f']
476+
['section1', 'section2', 'section3']
477+
>>> [option for option in parser['section3']] # doctest: +SKIP
478+
['foo', 'bar', 'baz']
511479

512480
* *allow_no_value*, default value: ``False``
513481

Lib/configparser.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
"""
140140

141141
from collections.abc import MutableMapping
142-
from collections import OrderedDict as _default_dict, ChainMap as _ChainMap
142+
from collections import ChainMap as _ChainMap
143143
import functools
144144
import io
145145
import itertools
@@ -157,6 +157,7 @@
157157
"LegacyInterpolation", "SectionProxy", "ConverterMapping",
158158
"DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
159159

160+
_default_dict = dict
160161
DEFAULTSECT = "DEFAULT"
161162

162163
MAX_INTERPOLATION_DEPTH = 10

Lib/test/test_configparser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ def test_set_nonstring_types(self):
11091109
self.assertEqual(cf.get(123, 'this is sick'), True)
11101110
if cf._dict is configparser._default_dict:
11111111
# would not work for SortedDict; only checking for the most common
1112-
# default dictionary (OrderedDict)
1112+
# default dictionary (dict)
11131113
cf.optionxform = lambda x: x
11141114
cf.set('non-string', 1, 1)
11151115
self.assertEqual(cf.get('non-string', 1), 1)

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,7 @@ Marc Recht
13151315
John Redford
13161316
Terry J. Reedy
13171317
Gareth Rees
1318+
John Reese
13181319
Steve Reeves
13191320
Lennart Regebro
13201321
John Regehr
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Switch the default dictionary implementation for :mod:`configparser` from
2+
:class:`collections.OrderedDict` to the standard :class:`dict` type.

0 commit comments

Comments
 (0)