Skip to content

Commit

Permalink
Drop OrderedDict in Python >= 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
martinblech committed May 8, 2022
1 parent b468b64 commit 231b4d4
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions xmltodict.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
except ImportError:
from io import StringIO

from collections import OrderedDict
_dict = dict
import platform
if tuple(map(int, platform.python_version_tuple()[:2])) < (3, 7):
from collections import OrderedDict as _dict

from inspect import isgenerator

try: # pragma no cover
Expand Down Expand Up @@ -46,7 +50,7 @@ def __init__(self,
force_cdata=False,
cdata_separator='',
postprocessor=None,
dict_constructor=OrderedDict,
dict_constructor=_dict,
strip_whitespace=True,
namespace_separator=':',
namespaces=None,
Expand All @@ -68,7 +72,7 @@ def __init__(self,
self.strip_whitespace = strip_whitespace
self.namespace_separator = namespace_separator
self.namespaces = namespaces
self.namespace_declarations = OrderedDict()
self.namespace_declarations = dict_constructor()
self.force_list = force_list
self.comment_key = comment_key

Expand Down Expand Up @@ -101,7 +105,7 @@ def startElement(self, full_name, attrs):
attrs = self._attrs_to_dict(attrs)
if attrs and self.namespace_declarations:
attrs['xmlns'] = self.namespace_declarations
self.namespace_declarations = OrderedDict()
self.namespace_declarations = self.dict_constructor()
self.path.append((name, attrs or None))
if len(self.path) > self.item_depth:
self.stack.append((self.item, self.data))
Expand Down Expand Up @@ -254,14 +258,14 @@ def parse(xml_input, encoding=None, expat=expat, process_namespaces=False,
... return key, value
>>> xmltodict.parse('<a><b>1</b><b>2</b><b>x</b></a>',
... postprocessor=postprocessor)
OrderedDict([(u'a', OrderedDict([(u'b:int', [1, 2]), (u'b', u'x')]))])
{'a': {'b:int': [1, 2], 'b': 'x'}}
You can pass an alternate version of `expat` (such as `defusedexpat`) by
using the `expat` parameter. E.g:
>>> import defusedexpat
>>> xmltodict.parse('<a>hello</a>', expat=defusedexpat.pyexpat)
OrderedDict([(u'a', u'hello')])
{'a': 'hello'}
You can use the force_list argument to force lists to be created even
when there is only a single child of a given level of hierarchy. The
Expand Down Expand Up @@ -416,21 +420,21 @@ def _emit(key, value, content_handler,
if full_document and depth == 0 and index > 0:
raise ValueError('document with multiple roots')
if v is None:
v = OrderedDict()
v = _dict()
elif isinstance(v, bool):
if v:
v = _unicode('true')
else:
v = _unicode('false')
elif not isinstance(v, dict):
if expand_iter and hasattr(v, '__iter__') and not isinstance(v, _basestring):
v = OrderedDict(((expand_iter, v),))
v = _dict(((expand_iter, v),))
else:
v = _unicode(v)
if isinstance(v, _basestring):
v = OrderedDict(((cdata_key, v),))
v = _dict(((cdata_key, v),))
cdata = None
attrs = OrderedDict()
attrs = _dict()
children = []
for ik, iv in v.items():
if ik == cdata_key:
Expand Down

0 comments on commit 231b4d4

Please sign in to comment.