From 5b1b5112239440b17c8cbd7133fde849cfd30260 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos Orfanos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Tue, 8 Oct 2024 19:54:07 +0200 Subject: [PATCH] Get rid of Python 2 basestring and unicode (#346) In Python 3, it's just str. --- xmltodict.py | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/xmltodict.py b/xmltodict.py index 6542610..9bd9d73 100755 --- a/xmltodict.py +++ b/xmltodict.py @@ -14,15 +14,6 @@ from inspect import isgenerator -try: # pragma no cover - _basestring = basestring -except NameError: # pragma no cover - _basestring = str -try: # pragma no cover - _unicode = unicode -except NameError: # pragma no cover - _unicode = str - __author__ = 'Martin Blech' __version__ = '0.13.0' __license__ = 'MIT' @@ -327,9 +318,8 @@ def parse(xml_input, encoding=None, expat=expat, process_namespaces=False, """ handler = _DictSAXHandler(namespace_separator=namespace_separator, **kwargs) - if isinstance(xml_input, _unicode): - if not encoding: - encoding = 'utf-8' + if isinstance(xml_input, str): + encoding = encoding or 'utf-8' xml_input = xml_input.encode(encoding) if not process_namespaces: namespace_separator = None @@ -404,7 +394,7 @@ def _emit(key, value, content_handler, if result is None: return key, value = result - if not hasattr(value, '__iter__') or isinstance(value, (_basestring, dict)): + if not hasattr(value, '__iter__') or isinstance(value, (str, dict)): value = [value] for index, v in enumerate(value): if full_document and depth == 0 and index > 0: @@ -412,16 +402,13 @@ def _emit(key, value, content_handler, if v is None: 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 = 'true' if v else 'false' + elif not isinstance(v, (dict, str)): + if expand_iter and hasattr(v, '__iter__'): v = _dict(((expand_iter, v),)) else: - v = _unicode(v) - if isinstance(v, _basestring): + v = str(v) + if isinstance(v, str): v = _dict(((cdata_key, v),)) cdata = None attrs = _dict() @@ -436,10 +423,10 @@ def _emit(key, value, content_handler, if ik == '@xmlns' and isinstance(iv, dict): for k, v in iv.items(): attr = 'xmlns{}'.format(f':{k}' if k else '') - attrs[attr] = _unicode(v) + attrs[attr] = str(v) continue - if not isinstance(iv, _unicode): - iv = _unicode(iv) + if not isinstance(iv, str): + iv = str(iv) attrs[ik[len(attr_prefix):]] = iv continue children.append((ik, iv))