[utils,compat] Move struct_pack and struct_unpack to compat.py
authorYen Chi Hsuan <yan12125@gmail.com>
Sat, 23 Apr 2016 10:28:49 +0000 (18:28 +0800)
committerYen Chi Hsuan <yan12125@gmail.com>
Tue, 10 May 2016 06:51:38 +0000 (14:51 +0800)
test/test_compat.py
test/test_utils.py
youtube_dl/compat.py
youtube_dl/downloader/f4m.py
youtube_dl/extractor/rtve.py
youtube_dl/swfinterp.py
youtube_dl/utils.py

index 9adf757631ce0df26295979af10cd2fac7dcc9fd..dd62a5d6b7304a1230f6967c3d66a3c7fa89ee1f 100644 (file)
@@ -20,6 +20,7 @@ from youtube_dl.compat import (
     compat_urllib_parse_unquote,
     compat_urllib_parse_unquote_plus,
     compat_urllib_parse_urlencode,
+    struct_unpack,
 )
 
 
@@ -102,5 +103,9 @@ class TestCompat(unittest.TestCase):
         self.assertTrue(isinstance(doc.find('chinese').text, compat_str))
         self.assertTrue(isinstance(doc.find('foo/bar').text, compat_str))
 
+    def test_struct_unpack(self):
+        self.assertEqual(struct_unpack('!B', b'\x00'), (0,))
+
+
 if __name__ == '__main__':
     unittest.main()
index 00ada95ecabf05ed761f3e6a5bff1c810bc5a00c..5702ffa97fd51fb5633030f83514b27308ae69b6 100644 (file)
@@ -55,7 +55,6 @@ from youtube_dl.utils import (
     smuggle_url,
     str_to_int,
     strip_jsonp,
-    struct_unpack,
     timeconvert,
     unescapeHTML,
     unified_strdate,
@@ -457,9 +456,6 @@ class TestUtil(unittest.TestCase):
         testPL(5, 2, (2, 99), [2, 3, 4])
         testPL(5, 2, (20, 99), [])
 
-    def test_struct_unpack(self):
-        self.assertEqual(struct_unpack('!B', b'\x00'), (0,))
-
     def test_read_batch_urls(self):
         f = io.StringIO('''\xef\xbb\xbf foo
             bar\r
index 12b53cdc88f8de7e0d37a46cc9804a193463db1e..f697bee7ec3daaccb28345c56a3a4acfc60f7b11 100644 (file)
@@ -11,6 +11,7 @@ import re
 import shlex
 import shutil
 import socket
+import struct
 import subprocess
 import sys
 import itertools
@@ -592,6 +593,26 @@ if sys.version_info >= (3, 0):
 else:
     from tokenize import generate_tokens as compat_tokenize_tokenize
 
+
+try:
+    struct.pack('!I', 0)
+except TypeError:
+    # In Python 2.6 and 2.7.x < 2.7.7, struct requires a bytes argument
+    # See https://bugs.python.org/issue19099
+    def struct_pack(spec, *args):
+        if isinstance(spec, compat_str):
+            spec = spec.encode('ascii')
+        return struct.pack(spec, *args)
+
+    def struct_unpack(spec, *args):
+        if isinstance(spec, compat_str):
+            spec = spec.encode('ascii')
+        return struct.unpack(spec, *args)
+else:
+    struct_pack = struct.pack
+    struct_unpack = struct.unpack
+
+
 __all__ = [
     'compat_HTMLParser',
     'compat_HTTPError',
@@ -634,6 +655,8 @@ __all__ = [
     'compat_xml_parse_error',
     'compat_xpath',
     'shlex_quote',
+    'struct_pack',
+    'struct_unpack',
     'subprocess_check_output',
     'workaround_optparse_bug9161',
 ]
index 664d87543d07f7c357b803e0a0058034b71276a6..b282fe3d63b9c6b77509878ff47564457c830efc 100644 (file)
@@ -12,13 +12,13 @@ from ..compat import (
     compat_urlparse,
     compat_urllib_error,
     compat_urllib_parse_urlparse,
+    struct_pack,
+    struct_unpack,
 )
 from ..utils import (
     encodeFilename,
     fix_xml_ampersands,
     sanitize_open,
-    struct_pack,
-    struct_unpack,
     xpath_text,
 )
 
index 79af477158630503078d86b117f960a36f5f1f73..f590408775d0c418c26a58298607cf3fc578af68 100644 (file)
@@ -6,6 +6,9 @@ import re
 import time
 
 from .common import InfoExtractor
+from ..compat import (
+    struct_unpack,
+)
 from ..utils import (
     ExtractorError,
     float_or_none,
@@ -13,7 +16,6 @@ from ..utils import (
     remove_start,
     sanitized_Request,
     std_headers,
-    struct_unpack,
 )
 
 
index 06c1d6cc1755ef022aa78967d4b651e21fd66618..86b28716ce91e2b38e117745107a128a8e95081a 100644 (file)
@@ -4,10 +4,12 @@ import collections
 import io
 import zlib
 
-from .compat import compat_str
+from .compat import (
+    compat_str,
+    struct_unpack,
+)
 from .utils import (
     ExtractorError,
-    struct_unpack,
 )
 
 
index 6e45737843fbc2594227ac4699a3fce549b81e32..fa16a42ad4940bd28373713d27c0becd422ec29c 100644 (file)
@@ -26,7 +26,6 @@ import platform
 import re
 import socket
 import ssl
-import struct
 import subprocess
 import sys
 import tempfile
@@ -53,6 +52,7 @@ from .compat import (
     compat_urlparse,
     compat_xpath,
     shlex_quote,
+    struct_pack,
 )
 
 
@@ -1761,24 +1761,6 @@ def escape_url(url):
         fragment=escape_rfc3986(url_parsed.fragment)
     ).geturl()
 
-try:
-    struct.pack('!I', 0)
-except TypeError:
-    # In Python 2.6 and 2.7.x < 2.7.7, struct requires a bytes argument
-    # See https://bugs.python.org/issue19099
-    def struct_pack(spec, *args):
-        if isinstance(spec, compat_str):
-            spec = spec.encode('ascii')
-        return struct.pack(spec, *args)
-
-    def struct_unpack(spec, *args):
-        if isinstance(spec, compat_str):
-            spec = spec.encode('ascii')
-        return struct.unpack(spec, *args)
-else:
-    struct_pack = struct.pack
-    struct_unpack = struct.unpack
-
 
 def read_batch_urls(batch_fd):
     def fixup(url):