[utils] Fix intlist_to_bytes in Python 2 (#4181)
authorPhilipp Hagemeister <phihag@phihag.de>
Thu, 13 Nov 2014 14:28:42 +0000 (15:28 +0100)
committerPhilipp Hagemeister <phihag@phihag.de>
Thu, 13 Nov 2014 14:28:42 +0000 (15:28 +0100)
test/test_utils.py
youtube_dl/utils.py

index 0c11d0438d734c907c350db93abebc0b434ea3f3..a9649397f3734db8903c5a943a7470cd6328f271 100644 (file)
@@ -46,6 +46,7 @@ from youtube_dl.utils import (
     escape_url,
     js_to_json,
     get_filesystem_encoding,
+    intlist_to_bytes,
 )
 
 
@@ -350,5 +351,10 @@ class TestUtil(unittest.TestCase):
         self.assertEqual(clean_html('a:\nb'), 'a: b')
         self.assertEqual(clean_html('a:\n   "b"'), 'a:    "b"')
 
+    def test_intlist_to_bytes(self):
+        self.assertEqual(
+            intlist_to_bytes([0, 1, 127, 128, 255]),
+            b'\x00\x01\x7f\x80\xff')
+
 if __name__ == '__main__':
     unittest.main()
index d87e212ae67958cc89e2db7002072fc332477469..0b2ba39e2ec7ab919a2cff022ac50ee9377c05c0 100644 (file)
@@ -843,10 +843,7 @@ def bytes_to_intlist(bs):
 def intlist_to_bytes(xs):
     if not xs:
         return b''
-    if isinstance(chr(0), bytes):  # Python 2
-        return ''.join([chr(x) for x in xs])
-    else:
-        return bytes(xs)
+    return struct.pack('%dB' % len(xs), *xs)
 
 
 # Cross-platform file locking