comparison mercurial/extensions.py @ 34014:47e52f079a57

extensions: add wrappedfunction() context manager Several extensions exist that temporarily want to wrap a function (at least narrowhg, any many of the extensions in hg-experimental). That's why we have the unwrapfunction() that was introduced in 19578bb84731 (extensions: add unwrapfunction to undo wrapfunction, 2016-08-10). This patch adds a simple wrappedfunction() that returns a context manager. Differential Revision: https://phab.mercurial-scm.org/D472
author Martin von Zweigbergk <martinvonz@google.com>
date Mon, 21 Aug 2017 16:46:05 -0700
parents 0646608368a9
children 0e0ac8f09048
comparison
equal deleted inserted replaced
34013:da07367d683b 34014:47e52f079a57
397 397
398 if currcls is object: 398 if currcls is object:
399 raise AttributeError(r"type '%s' has no property '%s'" % ( 399 raise AttributeError(r"type '%s' has no property '%s'" % (
400 cls, propname)) 400 cls, propname))
401 401
402 class wrappedfunction(object):
403 '''context manager for temporarily wrapping a function'''
404
405 def __init__(self, container, funcname, wrapper):
406 assert callable(wrapper)
407 self._container = container
408 self._funcname = funcname
409 self._wrapper = wrapper
410
411 def __enter__(self):
412 wrapfunction(self._container, self._funcname, self._wrapper)
413
414 def __exit__(self, exctype, excvalue, traceback):
415 unwrapfunction(self._container, self._funcname, self._wrapper)
416
402 def wrapfunction(container, funcname, wrapper): 417 def wrapfunction(container, funcname, wrapper):
403 '''Wrap the function named funcname in container 418 '''Wrap the function named funcname in container
404 419
405 Replace the funcname member in the given container with the specified 420 Replace the funcname member in the given container with the specified
406 wrapper. The container is typically a module, class, or instance. 421 wrapper. The container is typically a module, class, or instance.