80 def decode(arg): |
80 def decode(arg): |
81 if isinstance(arg, bytes): |
81 if isinstance(arg, bytes): |
82 uarg = arg.decode(_encoding) |
82 uarg = arg.decode(_encoding) |
83 if arg == uarg.encode(_encoding): |
83 if arg == uarg.encode(_encoding): |
84 return uarg |
84 return uarg |
85 raise UnicodeError(b"Not local encoding") |
85 raise UnicodeError("Not local encoding") |
86 elif isinstance(arg, tuple): |
86 elif isinstance(arg, tuple): |
87 return tuple(map(decode, arg)) |
87 return tuple(map(decode, arg)) |
88 elif isinstance(arg, list): |
88 elif isinstance(arg, list): |
89 return map(decode, arg) |
89 return map(decode, arg) |
90 elif isinstance(arg, dict): |
90 elif isinstance(arg, dict): |
109 def appendsep(s): |
109 def appendsep(s): |
110 # ensure the path ends with os.sep, appending it if necessary. |
110 # ensure the path ends with os.sep, appending it if necessary. |
111 try: |
111 try: |
112 us = decode(s) |
112 us = decode(s) |
113 except UnicodeError: |
113 except UnicodeError: |
114 us = s |
114 us = s # TODO: how to handle this bytes case?? |
115 if us and us[-1] not in b':/\\': |
115 if us and us[-1] not in ':/\\': |
116 s += pycompat.ossep |
116 s += pycompat.ossep |
117 return s |
117 return s |
118 |
118 |
119 |
119 |
120 def basewrapper(func, argtype, enc, dec, args, kwds): |
120 def basewrapper(func, argtype, enc, dec, args, kwds): |
146 # Ensure 'path' argument ends with os.sep to avoids |
146 # Ensure 'path' argument ends with os.sep to avoids |
147 # misinterpreting last 0x5c of MBCS 2nd byte as path separator. |
147 # misinterpreting last 0x5c of MBCS 2nd byte as path separator. |
148 if args: |
148 if args: |
149 args = list(args) |
149 args = list(args) |
150 args[0] = appendsep(args[0]) |
150 args[0] = appendsep(args[0]) |
151 if b'path' in kwds: |
151 if 'path' in kwds: |
152 kwds[b'path'] = appendsep(kwds[b'path']) |
152 kwds['path'] = appendsep(kwds['path']) |
153 return func(*args, **kwds) |
153 return func(*args, **kwds) |
154 |
154 |
155 |
155 |
156 def wrapname(name, wrapper): |
156 def wrapname(name: str, wrapper): |
157 module, name = name.rsplit(b'.', 1) |
157 module, name = name.rsplit('.', 1) |
158 module = sys.modules[module] |
158 module = sys.modules[module] |
159 func = getattr(module, name) |
159 func = getattr(module, name) |
160 |
160 |
161 def f(*args, **kwds): |
161 def f(*args, **kwds): |
162 return wrapper(func, args, kwds) |
162 return wrapper(func, args, kwds) |
166 |
166 |
167 |
167 |
168 # List of functions to be wrapped. |
168 # List of functions to be wrapped. |
169 # NOTE: os.path.dirname() and os.path.basename() are safe because |
169 # NOTE: os.path.dirname() and os.path.basename() are safe because |
170 # they use result of os.path.split() |
170 # they use result of os.path.split() |
171 funcs = b'''os.path.join os.path.split os.path.splitext |
171 funcs = '''os.path.join os.path.split os.path.splitext |
172 os.path.normpath os.makedirs mercurial.util.endswithsep |
172 os.path.normpath os.makedirs mercurial.util.endswithsep |
173 mercurial.util.splitpath mercurial.util.fscasesensitive |
173 mercurial.util.splitpath mercurial.util.fscasesensitive |
174 mercurial.util.fspath mercurial.util.pconvert mercurial.util.normpath |
174 mercurial.util.fspath mercurial.util.pconvert mercurial.util.normpath |
175 mercurial.util.checkwinfilename mercurial.util.checkosfilename |
175 mercurial.util.checkwinfilename mercurial.util.checkosfilename |
176 mercurial.util.split''' |
176 mercurial.util.split''' |
177 |
177 |
178 # These functions are required to be called with local encoded string |
178 # These functions are required to be called with local encoded string |
179 # because they expects argument is local encoded string and cause |
179 # because they expects argument is local encoded string and cause |
180 # problem with unicode string. |
180 # problem with unicode string. |
181 rfuncs = b'''mercurial.encoding.upper mercurial.encoding.lower |
181 rfuncs = '''mercurial.encoding.upper mercurial.encoding.lower |
182 mercurial.util._filenamebytestr''' |
182 mercurial.util._filenamebytestr''' |
183 |
183 |
184 # List of Windows specific functions to be wrapped. |
184 # List of Windows specific functions to be wrapped. |
185 winfuncs = b'''os.path.splitunc''' |
185 winfuncs = '''os.path.splitunc''' |
186 |
186 |
187 # codec and alias names of sjis and big5 to be faked. |
187 # codec and alias names of sjis and big5 to be faked. |
188 problematic_encodings = b'''big5 big5-tw csbig5 big5hkscs big5-hkscs |
188 problematic_encodings = b'''big5 big5-tw csbig5 big5hkscs big5-hkscs |
189 hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis |
189 hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis |
190 sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004 |
190 sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004 |
206 for f in funcs.split(): |
206 for f in funcs.split(): |
207 wrapname(f, wrapper) |
207 wrapname(f, wrapper) |
208 if pycompat.iswindows: |
208 if pycompat.iswindows: |
209 for f in winfuncs.split(): |
209 for f in winfuncs.split(): |
210 wrapname(f, wrapper) |
210 wrapname(f, wrapper) |
211 wrapname(b"mercurial.util.listdir", wrapperforlistdir) |
211 wrapname("mercurial.util.listdir", wrapperforlistdir) |
212 wrapname(b"mercurial.windows.listdir", wrapperforlistdir) |
212 wrapname("mercurial.windows.listdir", wrapperforlistdir) |
213 # wrap functions to be called with local byte string arguments |
213 # wrap functions to be called with local byte string arguments |
214 for f in rfuncs.split(): |
214 for f in rfuncs.split(): |
215 wrapname(f, reversewrapper) |
215 wrapname(f, reversewrapper) |
216 # Check sys.args manually instead of using ui.debug() because |
216 # Check sys.args manually instead of using ui.debug() because |
217 # command line options is not yet applied when |
217 # command line options is not yet applied when |
218 # extensions.loadall() is called. |
218 # extensions.loadall() is called. |
219 if b'--debug' in sys.argv: |
219 if '--debug' in sys.argv: |
220 ui.writenoi18n( |
220 ui.writenoi18n( |
221 b"[win32mbcs] activated with encoding: %s\n" % _encoding |
221 b"[win32mbcs] activated with encoding: %s\n" % _encoding |
222 ) |
222 ) |