Mercurial > hg
comparison hgext/win32mbcs.py @ 9131:2bbb8419720d
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.
author | Shun-ichi GOTO <shunichi.goto@gmail.com> |
---|---|
date | Fri, 10 Jul 2009 15:52:01 +0900 |
parents | 623f96ae3a26 |
children | b47d7b440c5c |
comparison
equal
deleted
inserted
replaced
9130:335f749cc369 | 9131:2bbb8419720d |
---|---|
50 raise UnicodeError("Not local encoding") | 50 raise UnicodeError("Not local encoding") |
51 elif isinstance(arg, tuple): | 51 elif isinstance(arg, tuple): |
52 return tuple(map(decode, arg)) | 52 return tuple(map(decode, arg)) |
53 elif isinstance(arg, list): | 53 elif isinstance(arg, list): |
54 return map(decode, arg) | 54 return map(decode, arg) |
55 elif isinstance(arg, dict): | |
56 for k, v in arg.items(): | |
57 arg[k] = decode(v) | |
55 return arg | 58 return arg |
56 | 59 |
57 def encode(arg): | 60 def encode(arg): |
58 if isinstance(arg, unicode): | 61 if isinstance(arg, unicode): |
59 return arg.encode(encoding.encoding) | 62 return arg.encode(encoding.encoding) |
60 elif isinstance(arg, tuple): | 63 elif isinstance(arg, tuple): |
61 return tuple(map(encode, arg)) | 64 return tuple(map(encode, arg)) |
62 elif isinstance(arg, list): | 65 elif isinstance(arg, list): |
63 return map(encode, arg) | 66 return map(encode, arg) |
67 elif isinstance(arg, dict): | |
68 for k, v in arg.items(): | |
69 arg[k] = encode(v) | |
64 return arg | 70 return arg |
65 | 71 |
66 def wrapper(func, args): | 72 def wrapper(func, args, kwds): |
67 # check argument is unicode, then call original | 73 # check argument is unicode, then call original |
68 for arg in args: | 74 for arg in args: |
69 if isinstance(arg, unicode): | 75 if isinstance(arg, unicode): |
70 return func(*args) | 76 return func(*args, **kwds) |
71 | 77 |
72 try: | 78 try: |
73 # convert arguments to unicode, call func, then convert back | 79 # convert arguments to unicode, call func, then convert back |
74 return encode(func(*decode(args))) | 80 return encode(func(*decode(args), **decode(kwds))) |
75 except UnicodeError: | 81 except UnicodeError: |
76 # If not encoded with encoding.encoding, report it then | 82 # If not encoded with encoding.encoding, report it then |
77 # continue with calling original function. | 83 # continue with calling original function. |
78 raise util.Abort(_("[win32mbcs] filename conversion fail with" | 84 raise util.Abort(_("[win32mbcs] filename conversion fail with" |
79 " %s encoding\n") % (encoding.encoding)) | 85 " %s encoding\n") % (encoding.encoding)) |
80 | 86 |
81 def wrapname(name): | 87 def wrapname(name): |
82 module, name = name.rsplit('.', 1) | 88 module, name = name.rsplit('.', 1) |
83 module = sys.modules[module] | 89 module = sys.modules[module] |
84 func = getattr(module, name) | 90 func = getattr(module, name) |
85 def f(*args): | 91 def f(*args, **kwds): |
86 return wrapper(func, args) | 92 return wrapper(func, args, kwds) |
87 try: | 93 try: |
88 f.__name__ = func.__name__ # fail with python23 | 94 f.__name__ = func.__name__ # fail with python23 |
89 except Exception: | 95 except Exception: |
90 pass | 96 pass |
91 setattr(module, name, f) | 97 setattr(module, name, f) |