##// END OF EJS Templates
extensions: extract partial application into a bind() function...
Eric Sumner -
r24734:fb6cb1b8 default
parent child Browse files
Show More
@@ -152,6 +152,18 b' def afterloaded(extension, callback):'
152 else:
152 else:
153 _aftercallbacks.setdefault(extension, []).append(callback)
153 _aftercallbacks.setdefault(extension, []).append(callback)
154
154
155 def bind(func, *args):
156 '''Partial function application
157
158 Returns a new function that is the partial application of args and kwargs
159 to func. For example,
160
161 f(1, 2, bar=3) === bind(f, 1)(2, bar=3)'''
162 assert callable(func)
163 def closure(*a, **kw):
164 return func(*(args + a), **kw)
165 return closure
166
155 def wrapcommand(table, command, wrapper, synopsis=None, docstring=None):
167 def wrapcommand(table, command, wrapper, synopsis=None, docstring=None):
156 '''Wrap the command named `command' in table
168 '''Wrap the command named `command' in table
157
169
@@ -189,9 +201,7 b' def wrapcommand(table, command, wrapper,'
189 break
201 break
190
202
191 origfn = entry[0]
203 origfn = entry[0]
192 def wrap(*args, **kwargs):
204 wrap = bind(util.checksignature(wrapper), util.checksignature(origfn))
193 return util.checksignature(wrapper)(
194 util.checksignature(origfn), *args, **kwargs)
195
205
196 wrap.__module__ = getattr(origfn, '__module__')
206 wrap.__module__ = getattr(origfn, '__module__')
197
207
@@ -241,12 +251,10 b' def wrapfunction(container, funcname, wr'
241 subclass trick.
251 subclass trick.
242 '''
252 '''
243 assert callable(wrapper)
253 assert callable(wrapper)
244 def wrap(*args, **kwargs):
245 return wrapper(origfn, *args, **kwargs)
246
254
247 origfn = getattr(container, funcname)
255 origfn = getattr(container, funcname)
248 assert callable(origfn)
256 assert callable(origfn)
249 setattr(container, funcname, wrap)
257 setattr(container, funcname, bind(wrapper, origfn))
250 return origfn
258 return origfn
251
259
252 def _disabledpaths(strip_init=False):
260 def _disabledpaths(strip_init=False):
General Comments 0
You need to be logged in to leave comments. Login now