[utils] Small fixes to utils and compat and test

pull/29845/head
df 3 years ago committed by dirkf
parent 820fae3b3a
commit 06d489c522

@ -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 '')

@ -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):

@ -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

@ -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'))

Loading…
Cancel
Save