Skip to content

Commit 5b1b511

Browse files
DimitriPapadopoulosmartinblech
authored andcommitted
Get rid of Python 2 basestring and unicode (#346)
In Python 3, it's just str.
1 parent 71b55aa commit 5b1b511

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

xmltodict.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@
1414

1515
from inspect import isgenerator
1616

17-
try: # pragma no cover
18-
_basestring = basestring
19-
except NameError: # pragma no cover
20-
_basestring = str
21-
try: # pragma no cover
22-
_unicode = unicode
23-
except NameError: # pragma no cover
24-
_unicode = str
25-
2617
__author__ = 'Martin Blech'
2718
__version__ = '0.13.0'
2819
__license__ = 'MIT'
@@ -327,9 +318,8 @@ def parse(xml_input, encoding=None, expat=expat, process_namespaces=False,
327318
"""
328319
handler = _DictSAXHandler(namespace_separator=namespace_separator,
329320
**kwargs)
330-
if isinstance(xml_input, _unicode):
331-
if not encoding:
332-
encoding = 'utf-8'
321+
if isinstance(xml_input, str):
322+
encoding = encoding or 'utf-8'
333323
xml_input = xml_input.encode(encoding)
334324
if not process_namespaces:
335325
namespace_separator = None
@@ -404,24 +394,21 @@ def _emit(key, value, content_handler,
404394
if result is None:
405395
return
406396
key, value = result
407-
if not hasattr(value, '__iter__') or isinstance(value, (_basestring, dict)):
397+
if not hasattr(value, '__iter__') or isinstance(value, (str, dict)):
408398
value = [value]
409399
for index, v in enumerate(value):
410400
if full_document and depth == 0 and index > 0:
411401
raise ValueError('document with multiple roots')
412402
if v is None:
413403
v = _dict()
414404
elif isinstance(v, bool):
415-
if v:
416-
v = _unicode('true')
417-
else:
418-
v = _unicode('false')
419-
elif not isinstance(v, dict):
420-
if expand_iter and hasattr(v, '__iter__') and not isinstance(v, _basestring):
405+
v = 'true' if v else 'false'
406+
elif not isinstance(v, (dict, str)):
407+
if expand_iter and hasattr(v, '__iter__'):
421408
v = _dict(((expand_iter, v),))
422409
else:
423-
v = _unicode(v)
424-
if isinstance(v, _basestring):
410+
v = str(v)
411+
if isinstance(v, str):
425412
v = _dict(((cdata_key, v),))
426413
cdata = None
427414
attrs = _dict()
@@ -436,10 +423,10 @@ def _emit(key, value, content_handler,
436423
if ik == '@xmlns' and isinstance(iv, dict):
437424
for k, v in iv.items():
438425
attr = 'xmlns{}'.format(f':{k}' if k else '')
439-
attrs[attr] = _unicode(v)
426+
attrs[attr] = str(v)
440427
continue
441-
if not isinstance(iv, _unicode):
442-
iv = _unicode(iv)
428+
if not isinstance(iv, str):
429+
iv = str(iv)
443430
attrs[ik[len(attr_prefix):]] = iv
444431
continue
445432
children.append((ik, iv))

0 commit comments

Comments
 (0)