From 06d489c5228d33e673c3f9c9551ae105b5973ce7 Mon Sep 17 00:00:00 2001 From: df Date: Sun, 22 Aug 2021 02:57:02 +0100 Subject: [PATCH] [utils] Small fixes to utils and compat and test --- test/test_compat.py | 6 +++++- test/test_utils.py | 4 ++-- youtube_dl/compat.py | 9 ++++++--- youtube_dl/utils.py | 10 +++++++--- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/test/test_compat.py b/test/test_compat.py index b83c8cb41..7606d022f 100644 --- a/test/test_compat.py +++ b/test/test_compat.py @@ -41,8 +41,12 @@ class TestCompat(unittest.TestCase): self.assertEqual(compat_getenv(test_var), test_str) def test_compat_expanduser(self): + from youtube_dl.compat import compat_os_name old_home = os.environ.get('HOME') - test_str = r'C:\Documents and Settings\тест\Application Data' + if compat_os_name in ('nt', 'ce'): + test_str = r'C:\Documents and Settings\тест\Application Data' + else: + test_str = '/home/тест' compat_setenv('HOME', test_str) self.assertEqual(compat_expanduser('~'), test_str) compat_setenv('HOME', old_home or '') diff --git a/test/test_utils.py b/test/test_utils.py index ca36909a8..870032d6c 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -514,7 +514,7 @@ class TestUtil(unittest.TestCase): args = ['ffmpeg', '-i', encodeFilename('ñ€ß\'.mp4')] self.assertEqual( shell_quote(args), - """ffmpeg -i 'ñ€ß'"'"'.mp4'""" if compat_os_name != 'nt' else '''ffmpeg -i "ñ€ß'.mp4"''') + """ffmpeg -i 'ñ€ß'"'"'.mp4'""" if not (compat_os_name in ('nt', 'ce')) else '''ffmpeg -i "ñ€ß'.mp4"''') def test_float_or_none(self): self.assertEqual(float_or_none('42.42'), 42.42) @@ -1241,7 +1241,7 @@ class TestUtil(unittest.TestCase): def test_args_to_str(self): self.assertEqual( args_to_str(['foo', 'ba/r', '-baz', '2 be', '']), - 'foo ba/r -baz \'2 be\' \'\'' if compat_os_name != 'nt' else 'foo ba/r -baz "2 be" ""' + 'foo ba/r -baz \'2 be\' \'\'' if not(compat_os_name in ('nt', 'ce')) else 'foo ba/r -baz "2 be" ""' ) def test_parse_filesize(self): diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py index 818ccebd0..cc43172e2 100644 --- a/youtube_dl/compat.py +++ b/youtube_dl/compat.py @@ -2776,16 +2776,19 @@ else: # Otherwise it will fail if any non-ASCII characters present (see #3854 #3217 #2918) def compat_getenv(key, default=None): - from .utils import get_filesystem_encoding env = os.getenv(key, default) if env: - env = env.decode(get_filesystem_encoding()) + from .utils import get_filesystem_encoding + encoding = get_filesystem_encoding() + env = env.decode(encoding) + if not encoding.lower().startswith('ut'): + env = env.encode('utf-8').decode('unicode-escape') return env def compat_setenv(key, value, env=os.environ): def encode(v): from .utils import get_filesystem_encoding - return v.encode(get_filesystem_encoding()) if isinstance(v, compat_str) else v + return v.encode(get_filesystem_encoding(), 'backslashreplace') if isinstance(v, compat_str) else v env[encode(key)] = encode(value) # HACK: The default implementations of os.path.expanduser from cpython do not decode diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 03c73dff3..970bc591a 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -3566,8 +3566,7 @@ class locked_file(object): def get_filesystem_encoding(): - encoding = sys.getfilesystemencoding() - return encoding if encoding is not None else 'utf-8' + return sys.getfilesystemencoding() or sys.getdefaultencoding() or 'utf-8' def shell_quote(args): @@ -3576,6 +3575,11 @@ def shell_quote(args): for a in args: # We may get a filename encoded with 'encodeFilename' a = _decode_compat_str(a, encoding) + if isinstance(a, bytes): + # We may get a filename encoded with 'encodeFilename' + a = a.decode(encoding) + if not encoding.lower().startswith('ut'): + a = a.encode('utf-8').decode('unicode-escape') quoted_args.append(compat_shlex_quote(a)) return ' '.join(quoted_args) @@ -5065,7 +5069,7 @@ def dfxp2srt(dfxp_data): continue default_style.update(style) - for para, index in zip(paras, itertools.count(1)): + for index, para in enumerate(paras, 1): begin_time = parse_dfxp_time_expr(para.attrib.get('begin')) end_time = parse_dfxp_time_expr(para.attrib.get('end')) dur = parse_dfxp_time_expr(para.attrib.get('dur'))