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 23, 2021
1 parent a803582 commit 1972157
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 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
4 changes: 2 additions & 2 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ 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"''')
"""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
9 changes: 6 additions & 3 deletions youtube_dl/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2700,16 +2700,19 @@ 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())
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
Expand Down
10 changes: 6 additions & 4 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 @@ -3354,8 +3355,7 @@ def read(self, *args):


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):
Expand All @@ -3365,6 +3365,8 @@ def shell_quote(args):
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)

Expand Down Expand Up @@ -4610,7 +4612,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 1972157

Please sign in to comment.