Skip to content

Commit

Permalink
[compat] compat_etree_fromstring: only decode bytes objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimeMF committed Oct 25, 2015
1 parent 36e6f62 commit 387db16
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion test/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ def test_compat_shlex_split(self):
self.assertEqual(compat_shlex_split('-option "one two"'), ['-option', 'one two'])

def test_compat_etree_fromstring(self):
xml = '<el foo="bar"></el>'
xml = '<el foo="bar" spam="中文"></el>'
doc = compat_etree_fromstring(xml.encode('utf-8'))
self.assertTrue(isinstance(doc.attrib['foo'], compat_str))
self.assertTrue(isinstance(doc.attrib['spam'], compat_str))

if __name__ == '__main__':
unittest.main()
6 changes: 3 additions & 3 deletions youtube_dl/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ def data_open(self, req):
if sys.version_info[0] >= 3:
compat_etree_fromstring = xml.etree.ElementTree.fromstring
else:
# on python 2.x the the attributes of a node are str objects instead of
# unicode
# on python 2.x the the attributes of a node aren't always unicode objects
etree = xml.etree.ElementTree

# on 2.6 XML doesn't have a parser argument, function copied from CPython
Expand All @@ -231,7 +230,8 @@ def _XML(text, parser=None):
def _element_factory(*args, **kwargs):
el = etree.Element(*args, **kwargs)
for k, v in el.items():
el.set(k, v.decode('utf-8'))
if isinstance(v, bytes):
el.set(k, v.decode('utf-8'))
return el

def compat_etree_fromstring(text):
Expand Down

0 comments on commit 387db16

Please sign in to comment.