[PATCH 1 of 2] win32mbcs: wrapper supports keyword arguments and dict result

Shun-ichi Goto shunichi.goto at gmail.com
Fri Jul 10 05:34:56 CDT 2009


# HG changeset patch
# User Shun-ichi GOTO <shunichi.goto at gmail.com>
# Date 1247208721 -32400
# Node ID ef9c065ab5527947eb464ead2b37ef666534c424
# Parent  2f14045d3ed27ccedbabb14fa349fa63cd331d4a
win32mbcs: wrapper supports keyword arguments and dict result.

The keyword arguments are also decoded before calling original.
And dict of return value is also encoded back.

diff -r 2f14045d3ed2 -r ef9c065ab552 hgext/win32mbcs.py
--- a/hgext/win32mbcs.py	Fri Jul 10 15:36:28 2009 +0900
+++ b/hgext/win32mbcs.py	Fri Jul 10 15:52:01 2009 +0900
@@ -49,6 +49,9 @@
         return tuple(map(decode, arg))
     elif isinstance(arg, list):
         return map(decode, arg)
+    elif isinstance(arg, dict):
+        for k, v in arg.items():
+            arg[k] = decode(v)
     return arg
 
 def encode(arg):
@@ -58,17 +61,20 @@
         return tuple(map(encode, arg))
     elif isinstance(arg, list):
         return map(encode, arg)
+    elif isinstance(arg, dict):
+        for k, v in arg.items():
+            arg[k] = encode(v)
     return arg
 
-def wrapper(func, args):
+def wrapper(func, args, kwds):
     # check argument is unicode, then call original
     for arg in args:
         if isinstance(arg, unicode):
-            return func(*args)
+            return func(*args, **kwds)
 
     try:
         # convert arguments to unicode, call func, then convert back
-        return encode(func(*decode(args)))
+        return encode(func(*decode(args), **decode(kwds)))
     except UnicodeError:
         # If not encoded with encoding.encoding, report it then
         # continue with calling original function.
@@ -79,8 +85,8 @@
     module, name = name.rsplit('.', 1)
     module = sys.modules[module]
     func = getattr(module, name)
-    def f(*args):
-        return wrapper(func, args)
+    def f(*args, **kwds):
+        return wrapper(func, args, kwds)
     try:
         f.__name__ = func.__name__                # fail with python23
     except Exception:


More information about the Mercurial-devel mailing list