14
14
15
15
from inspect import isgenerator
16
16
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
-
26
17
__author__ = 'Martin Blech'
27
18
__version__ = '0.13.0'
28
19
__license__ = 'MIT'
@@ -327,9 +318,8 @@ def parse(xml_input, encoding=None, expat=expat, process_namespaces=False,
327
318
"""
328
319
handler = _DictSAXHandler (namespace_separator = namespace_separator ,
329
320
** 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'
333
323
xml_input = xml_input .encode (encoding )
334
324
if not process_namespaces :
335
325
namespace_separator = None
@@ -404,24 +394,21 @@ def _emit(key, value, content_handler,
404
394
if result is None :
405
395
return
406
396
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 )):
408
398
value = [value ]
409
399
for index , v in enumerate (value ):
410
400
if full_document and depth == 0 and index > 0 :
411
401
raise ValueError ('document with multiple roots' )
412
402
if v is None :
413
403
v = _dict ()
414
404
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__' ):
421
408
v = _dict (((expand_iter , v ),))
422
409
else :
423
- v = _unicode (v )
424
- if isinstance (v , _basestring ):
410
+ v = str (v )
411
+ if isinstance (v , str ):
425
412
v = _dict (((cdata_key , v ),))
426
413
cdata = None
427
414
attrs = _dict ()
@@ -436,10 +423,10 @@ def _emit(key, value, content_handler,
436
423
if ik == '@xmlns' and isinstance (iv , dict ):
437
424
for k , v in iv .items ():
438
425
attr = 'xmlns{}' .format (':{}' .format (k ) if k else '' )
439
- attrs [attr ] = _unicode (v )
426
+ attrs [attr ] = str (v )
440
427
continue
441
- if not isinstance (iv , _unicode ):
442
- iv = _unicode (iv )
428
+ if not isinstance (iv , str ):
429
+ iv = str (iv )
443
430
attrs [ik [len (attr_prefix ):]] = iv
444
431
continue
445
432
children .append ((ik , iv ))
0 commit comments