瀏覽代碼

[compat] compat_etree_fromstring: only decode bytes objects

Jaime Marquínez Ferrándiz 9 年之前
父節點
當前提交
387db16a78
共有 2 個文件被更改,包括 5 次插入4 次删除
  1. 2 1
      test/test_compat.py
  2. 3 3
      youtube_dl/compat.py

+ 2 - 1
test/test_compat.py

@@ -74,9 +74,10 @@ class TestCompat(unittest.TestCase):
         self.assertEqual(compat_shlex_split('-option "one two"'), ['-option', 'one two'])
         self.assertEqual(compat_shlex_split('-option "one two"'), ['-option', 'one two'])
 
 
     def test_compat_etree_fromstring(self):
     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'))
         doc = compat_etree_fromstring(xml.encode('utf-8'))
         self.assertTrue(isinstance(doc.attrib['foo'], compat_str))
         self.assertTrue(isinstance(doc.attrib['foo'], compat_str))
+        self.assertTrue(isinstance(doc.attrib['spam'], compat_str))
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
     unittest.main()
     unittest.main()

+ 3 - 3
youtube_dl/compat.py

@@ -216,8 +216,7 @@ except ImportError:  # Python 2.6
 if sys.version_info[0] >= 3:
 if sys.version_info[0] >= 3:
     compat_etree_fromstring = xml.etree.ElementTree.fromstring
     compat_etree_fromstring = xml.etree.ElementTree.fromstring
 else:
 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
     etree = xml.etree.ElementTree
 
 
     # on 2.6 XML doesn't have a parser argument, function copied from CPython
     # on 2.6 XML doesn't have a parser argument, function copied from CPython
@@ -231,7 +230,8 @@ else:
     def _element_factory(*args, **kwargs):
     def _element_factory(*args, **kwargs):
         el = etree.Element(*args, **kwargs)
         el = etree.Element(*args, **kwargs)
         for k, v in el.items():
         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
         return el
 
 
     def compat_etree_fromstring(text):
     def compat_etree_fromstring(text):