##// END OF EJS Templates
pycompat: use os.fsencode() to re-encode sys.argv...
Manuel Jacob -
r45533:f2de8f31 default
parent child Browse files
Show More
@@ -98,7 +98,6 b' if ispy3:'
98 98 import codecs
99 99 import functools
100 100 import io
101 import locale
102 101 import struct
103 102
104 103 if os.name == r'nt' and sys.version_info >= (3, 6):
@@ -156,16 +155,10 b' if ispy3:'
156 155
157 156 if getattr(sys, 'argv', None) is not None:
158 157 # On POSIX, the char** argv array is converted to Python str using
159 # Py_DecodeLocale(). The inverse of this is Py_EncodeLocale(), which isn't
160 # directly callable from Python code. So, we need to emulate it.
161 # Py_DecodeLocale() calls mbstowcs() and falls back to mbrtowc() with
162 # surrogateescape error handling on failure. These functions take the
163 # current system locale into account. So, the inverse operation is to
164 # .encode() using the system locale's encoding and using the
165 # surrogateescape error handler. The only tricky part here is getting
166 # the system encoding correct, since `locale.getlocale()` can return
167 # None. We fall back to the filesystem encoding if lookups via `locale`
168 # fail, as this seems like a reasonable thing to do.
158 # Py_DecodeLocale(). The inverse of this is Py_EncodeLocale(), which
159 # isn't directly callable from Python code. In practice, os.fsencode()
160 # can be used instead (this is recommended by Python's documentation
161 # for sys.argv).
169 162 #
170 163 # On Windows, the wchar_t **argv is passed into the interpreter as-is.
171 164 # Like POSIX, we need to emulate what Py_EncodeLocale() would do. But
@@ -178,19 +171,7 b' if ispy3:'
178 171 if os.name == r'nt':
179 172 sysargv = [a.encode("mbcs", "ignore") for a in sys.argv]
180 173 else:
181
182 def getdefaultlocale_if_known():
183 try:
184 return locale.getdefaultlocale()
185 except ValueError:
186 return None, None
187
188 encoding = (
189 locale.getlocale()[1]
190 or getdefaultlocale_if_known()[1]
191 or sys.getfilesystemencoding()
192 )
193 sysargv = [a.encode(encoding, "surrogateescape") for a in sys.argv]
174 sysargv = [fsencode(a) for a in sys.argv]
194 175
195 176 bytechr = struct.Struct('>B').pack
196 177 byterepr = b'%r'.__mod__
General Comments 0
You need to be logged in to leave comments. Login now