[PATCH 2 of 2] win32mbcs: add special wrapper for osutil.listdir()

Shun-ichi Goto shunichi.goto at gmail.com
Fri Jul 10 01:57:12 CDT 2009


# HG changeset patch
# User Shun-ichi GOTO <shunichi.goto at gmail.com>
# Date 1247208732 -32400
# Node ID 9c210c066fe1b8a29032b3c6461fccf1ab6f0ee2
# Parent  ef9c065ab5527947eb464ead2b37ef666534c424
win32mbcs: add special wrapper for osutil.listdir().

osutil.listdir() checks the 'path' argument ends with os.sep() to
decide path separator should be added.  This is not good for
problematic encodings.  New wrapper modify the 'path' arugment to
ensure ending with os.sep char to avoid misinterpretation in
osutil.listdir().

diff -r ef9c065ab552 -r 9c210c066fe1 hgext/win32mbcs.py
--- a/hgext/win32mbcs.py	Fri Jul 10 15:52:01 2009 +0900
+++ b/hgext/win32mbcs.py	Fri Jul 10 15:52:12 2009 +0900
@@ -66,6 +66,18 @@
             arg[k] = encode(v)
     return arg
 
+def appendsep(s):
+    # append os.sep if not yet endswith it.
+    try:
+        us = decode(s)
+    except UnicodeError:
+        util.warn(_("[win32mbcs] filename conversion fail with"
+                    " %s encoding\n") % (encoding.encoding))
+        us = s
+    if us and us[-1] not in ':/\\':
+        s += os.sep
+    return s
+
 def wrapper(func, args, kwds):
     # check argument is unicode, then call original
     for arg in args:
@@ -81,7 +93,20 @@
         raise util.Abort(_("[win32mbcs] filename conversion fail with"
                          " %s encoding\n") % (encoding.encoding))
 
-def wrapname(name):
+def wrapperforlistdir(func, args, kwds):
+    # Special wrapper for osutil.listdir() to force first argument and
+    # keyword argument 'path' ends with os.sep.  This avoids
+    # miscomprehending last 0x5c of MBCS 2nd byte as path separator.
+    # This wrapper is different from usual one in point of calling
+    # with str (not unicode) arugment.
+    if args:
+        args = list(args)
+        args[0] = appendsep(args[0])
+    if kwds.has_key('path'):
+        kwds['path'] = appendsep(kwds['path'])
+    return func(*args, **kwds)
+
+def wrapname(name, wrapper):
     module, name = name.rsplit('.', 1)
     module = sys.modules[module]
     func = getattr(module, name)
@@ -116,7 +141,8 @@
     # fake is only for relevant environment.
     if encoding.encoding.lower() in problematic_encodings.split():
         for f in funcs.split():
-            wrapname(f)
+            wrapname(f, wrapper)
+        wrapname("mercurial.osutil.listdir", wrapperforlistdir)
         ui.debug(_("[win32mbcs] activated with encoding: %s\n")
                  % encoding.encoding)
 


More information about the Mercurial-devel mailing list