equal
deleted
inserted
replaced
618 |
618 |
619 |
619 |
620 class wrappedfunction: |
620 class wrappedfunction: |
621 '''context manager for temporarily wrapping a function''' |
621 '''context manager for temporarily wrapping a function''' |
622 |
622 |
623 def __init__(self, container, funcname, wrapper): |
623 def __init__(self, container, funcname: str, wrapper): |
624 assert callable(wrapper) |
624 assert callable(wrapper) |
625 if not isinstance(funcname, str): |
625 if not isinstance(funcname, str): |
626 msg = b"wrappedfunction target name should be `str`, not `bytes`" |
626 # Keep this compat shim around for older/unmaintained extensions |
627 raise TypeError(msg) |
627 msg = b"pass wrappedfunction target name as `str`, not `bytes`" |
|
628 util.nouideprecwarn(msg, b"6.6", stacklevel=2) |
|
629 funcname = pycompat.sysstr(funcname) |
628 self._container = container |
630 self._container = container |
629 self._funcname = funcname |
631 self._funcname = funcname |
630 self._wrapper = wrapper |
632 self._wrapper = wrapper |
631 |
633 |
632 def __enter__(self): |
634 def __enter__(self): |
634 |
636 |
635 def __exit__(self, exctype, excvalue, traceback): |
637 def __exit__(self, exctype, excvalue, traceback): |
636 unwrapfunction(self._container, self._funcname, self._wrapper) |
638 unwrapfunction(self._container, self._funcname, self._wrapper) |
637 |
639 |
638 |
640 |
639 def wrapfunction(container, funcname, wrapper): |
641 def wrapfunction(container, funcname: str, wrapper): |
640 """Wrap the function named funcname in container |
642 """Wrap the function named funcname in container |
641 |
643 |
642 Replace the funcname member in the given container with the specified |
644 Replace the funcname member in the given container with the specified |
643 wrapper. The container is typically a module, class, or instance. |
645 wrapper. The container is typically a module, class, or instance. |
644 |
646 |
670 subclass trick. |
672 subclass trick. |
671 """ |
673 """ |
672 assert callable(wrapper) |
674 assert callable(wrapper) |
673 |
675 |
674 if not isinstance(funcname, str): |
676 if not isinstance(funcname, str): |
675 msg = b"wrapfunction target name should be `str`, not `bytes`" |
677 # Keep this compat shim around for older/unmaintained extensions |
676 raise TypeError(msg) |
678 msg = b"pass wrapfunction target name as `str`, not `bytes`" |
|
679 util.nouideprecwarn(msg, b"6.6", stacklevel=2) |
|
680 funcname = pycompat.sysstr(funcname) |
677 |
681 |
678 origfn = getattr(container, funcname) |
682 origfn = getattr(container, funcname) |
679 assert callable(origfn) |
683 assert callable(origfn) |
680 if inspect.ismodule(container): |
684 if inspect.ismodule(container): |
681 # origfn is not an instance or class method. "partial" can be used. |
685 # origfn is not an instance or class method. "partial" can be used. |