##// END OF EJS Templates
wrapfunction: use functools.partial if possible...
Jun Wu -
r34089:5361771f default
parent child Browse files
Show More
@@ -357,7 +357,10 b' def _callcatch(ui, func):'
357 return -1
357 return -1
358
358
359 def aliasargs(fn, givenargs):
359 def aliasargs(fn, givenargs):
360 args = getattr(fn, 'args', [])
360 args = []
361 # only care about alias 'args', ignore 'args' set by extensions.wrapfunction
362 if not util.safehasattr(fn, '_origfunc'):
363 args = getattr(fn, 'args', args)
361 if args:
364 if args:
362 cmd = ' '.join(map(util.shellquote, args))
365 cmd = ' '.join(map(util.shellquote, args))
363
366
@@ -7,6 +7,7 b''
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import functools
10 import imp
11 import imp
11 import inspect
12 import inspect
12 import os
13 import os
@@ -332,6 +333,7 b' def bind(func, *args):'
332
333
333 def _updatewrapper(wrap, origfn, unboundwrapper):
334 def _updatewrapper(wrap, origfn, unboundwrapper):
334 '''Copy and add some useful attributes to wrapper'''
335 '''Copy and add some useful attributes to wrapper'''
336 wrap.__name__ = origfn.__name__
335 wrap.__module__ = getattr(origfn, '__module__')
337 wrap.__module__ = getattr(origfn, '__module__')
336 wrap.__doc__ = getattr(origfn, '__doc__')
338 wrap.__doc__ = getattr(origfn, '__doc__')
337 wrap.__dict__.update(getattr(origfn, '__dict__', {}))
339 wrap.__dict__.update(getattr(origfn, '__dict__', {}))
@@ -459,7 +461,14 b' def wrapfunction(container, funcname, wr'
459
461
460 origfn = getattr(container, funcname)
462 origfn = getattr(container, funcname)
461 assert callable(origfn)
463 assert callable(origfn)
462 wrap = bind(wrapper, origfn)
464 if inspect.ismodule(container):
465 # origfn is not an instance or class method. "partial" can be used.
466 # "partial" won't insert a frame in traceback.
467 wrap = functools.partial(wrapper, origfn)
468 else:
469 # "partial" cannot be safely used. Emulate its effect by using "bind".
470 # The downside is one more frame in traceback.
471 wrap = bind(wrapper, origfn)
463 _updatewrapper(wrap, origfn, wrapper)
472 _updatewrapper(wrap, origfn, wrapper)
464 setattr(container, funcname, wrap)
473 setattr(container, funcname, wrap)
465 return origfn
474 return origfn
General Comments 0
You need to be logged in to leave comments. Login now