Standardize on Python's fsencode for arguments

This can apparently never be `None`, as of Python 3.2.
This commit is contained in:
Adrian Sampson 2022-10-01 16:42:43 -07:00
parent 82d41446a2
commit 086bab55b1
No known key found for this signature in database
GPG key ID: BDB93AB409CC8705

View file

@ -17,7 +17,6 @@
import os
import sys
import errno
import locale
import re
import tempfile
import shutil
@ -332,12 +331,7 @@ def arg_encoding():
"""Get the encoding for command-line arguments (and other OS
locale-sensitive strings).
"""
try:
return locale.getdefaultlocale()[1] or 'utf-8'
except ValueError:
# Invalid locale environment variable setting. To avoid
# failing entirely for no good reason, assume UTF-8.
return 'utf-8'
return sys.getfilesystemencoding()
def _fsencoding():
@ -837,13 +831,14 @@ def cpu_count():
def convert_command_args(args):
"""Convert command arguments to bytestrings on Python 2 and
surrogate-escaped strings on Python 3."""
"""Convert command arguments, which may either be `bytes` or `str`
objects, to uniformly surrogate-escaped strings.
"""
assert isinstance(args, list)
def convert(arg):
if isinstance(arg, bytes):
arg = arg.decode(arg_encoding(), 'surrogateescape')
return os.fsdecode(arg)
return arg
return [convert(a) for a in args]