Mercurial > hg
comparison mercurial/extensions.py @ 28310:01dc11e7191f
extensions: extract function that copies function attributes to wrapper
wrapfunction() will be changed to copy these attributes. See the next patch
for details.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 09 Jan 2016 19:45:10 +0900 |
parents | 7f430b2ac7fd |
children | 1b0ef07ba783 |
comparison
equal
deleted
inserted
replaced
28309:7c22b5bb3a03 | 28310:01dc11e7191f |
---|---|
193 assert callable(func) | 193 assert callable(func) |
194 def closure(*a, **kw): | 194 def closure(*a, **kw): |
195 return func(*(args + a), **kw) | 195 return func(*(args + a), **kw) |
196 return closure | 196 return closure |
197 | 197 |
198 def _updatewrapper(wrap, origfn): | |
199 '''Copy attributes to wrapper function''' | |
200 wrap.__module__ = getattr(origfn, '__module__') | |
201 wrap.__doc__ = getattr(origfn, '__doc__') | |
202 | |
198 def wrapcommand(table, command, wrapper, synopsis=None, docstring=None): | 203 def wrapcommand(table, command, wrapper, synopsis=None, docstring=None): |
199 '''Wrap the command named `command' in table | 204 '''Wrap the command named `command' in table |
200 | 205 |
201 Replace command in the command table with wrapper. The wrapped command will | 206 Replace command in the command table with wrapper. The wrapped command will |
202 be inserted into the command table specified by the table argument. | 207 be inserted into the command table specified by the table argument. |
231 key = alias | 236 key = alias |
232 break | 237 break |
233 | 238 |
234 origfn = entry[0] | 239 origfn = entry[0] |
235 wrap = bind(util.checksignature(wrapper), util.checksignature(origfn)) | 240 wrap = bind(util.checksignature(wrapper), util.checksignature(origfn)) |
236 | 241 _updatewrapper(wrap, origfn) |
237 wrap.__module__ = getattr(origfn, '__module__') | |
238 | |
239 doc = getattr(origfn, '__doc__') | |
240 if docstring is not None: | 242 if docstring is not None: |
241 doc += docstring | 243 wrap.__doc__ += docstring |
242 wrap.__doc__ = doc | |
243 | 244 |
244 newentry = list(entry) | 245 newentry = list(entry) |
245 newentry[0] = wrap | 246 newentry[0] = wrap |
246 if synopsis is not None: | 247 if synopsis is not None: |
247 newentry[2] += synopsis | 248 newentry[2] += synopsis |