Skip to content

Commit

Permalink
Small fixes to utils and compat and test
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkf committed Aug 22, 2021
1 parent a803582 commit bc6ad58
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
6 changes: 5 additions & 1 deletion test/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ def test_compat_setenv(self):
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 '')
Expand Down
6 changes: 3 additions & 3 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ def test_smuggle_url(self):
def test_shell_quote(self):
args = ['ffmpeg', '-i', encodeFilename('ñ€ß\'.mp4')]
self.assertEqual(
shell_quote(args),
"""ffmpeg -i 'ñ€ß'"'"'.mp4'""" if compat_os_name != 'nt' else '''ffmpeg -i "ñ€ß'.mp4"''')
shell_quote(args).decode('unicode_escape'),
"""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)
Expand Down Expand Up @@ -1085,7 +1085,7 @@ def test_intlist_to_bytes(self):
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):
Expand Down
5 changes: 2 additions & 3 deletions youtube_dl/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2700,16 +2700,15 @@ def compat_setenv(key, value, env=os.environ):
# 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())
env = env.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
Expand Down
5 changes: 3 additions & 2 deletions youtube_dl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2245,7 +2245,8 @@ def encodeFilename(s, for_subprocess=False):
if sys.platform.startswith('java'):
return s

return s.encode(get_subprocess_encoding(), 'ignore')
# If encoding is (eg) 'ascii', use escape sequences (allows round-trip test)
return s.encode(get_subprocess_encoding(), 'backslashreplace')


def decodeFilename(b, for_subprocess=False):
Expand Down Expand Up @@ -4610,7 +4611,7 @@ def parse_node(node):
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'))
Expand Down

0 comments on commit bc6ad58

Please sign in to comment.