Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/wrapt/wrappers.py: 31%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import inspect
2import operator
3import sys
6def with_metaclass(meta, *bases):
7 """Create a base class with a metaclass."""
8 return meta("NewBase", bases, {})
11class WrapperNotInitializedError(ValueError, AttributeError):
12 """
13 Exception raised when a wrapper is accessed before it has been initialized.
14 To satisfy different situations where this could arise, we inherit from both
15 ValueError and AttributeError.
16 """
18 pass
21class _ObjectProxyMethods:
23 # We use properties to override the values of __module__ and
24 # __doc__. If we add these in ObjectProxy, the derived class
25 # __dict__ will still be setup to have string variants of these
26 # attributes and the rules of descriptors means that they appear to
27 # take precedence over the properties in the base class. To avoid
28 # that, we copy the properties into the derived class type itself
29 # via a meta class. In that way the properties will always take
30 # precedence.
32 @property
33 def __module__(self):
34 return self.__wrapped__.__module__
36 @__module__.setter
37 def __module__(self, value):
38 self.__wrapped__.__module__ = value
40 @property
41 def __doc__(self):
42 return self.__wrapped__.__doc__
44 @__doc__.setter
45 def __doc__(self, value):
46 self.__wrapped__.__doc__ = value
48 # We similar use a property for __dict__. We need __dict__ to be
49 # explicit to ensure that vars() works as expected.
51 @property
52 def __dict__(self):
53 return self.__wrapped__.__dict__
55 # Need to also propagate the special __weakref__ attribute for case
56 # where decorating classes which will define this. If do not define
57 # it and use a function like inspect.getmembers() on a decorator
58 # class it will fail. This can't be in the derived classes.
60 @property
61 def __weakref__(self):
62 return self.__wrapped__.__weakref__
65class _ObjectProxyMetaType(type):
66 def __new__(cls, name, bases, dictionary):
67 # Copy our special properties into the class so that they
68 # always take precedence over attributes of the same name added
69 # during construction of a derived class. This is to save
70 # duplicating the implementation for them in all derived classes.
72 dictionary.update(vars(_ObjectProxyMethods))
74 return type.__new__(cls, name, bases, dictionary)
77# NOTE: Although Python 3+ supports the newer metaclass=MetaClass syntax,
78# we must continue using with_metaclass() for ObjectProxy. The newer syntax
79# changes how __slots__ is handled during class creation, which would break
80# the ability to set _self_* attributes on ObjectProxy instances. The
81# with_metaclass() approach creates an intermediate base class that allows
82# the necessary attribute flexibility while still applying the metaclass.
85class ObjectProxy(with_metaclass(_ObjectProxyMetaType)): # type: ignore[misc]
87 __slots__ = "__wrapped__"
89 def __init__(self, wrapped):
90 """Create an object proxy around the given object."""
92 if wrapped is None:
93 try:
94 callback = object.__getattribute__(self, "__wrapped_factory__")
95 except AttributeError:
96 callback = None
98 if callback is not None:
99 # If wrapped is none and class has a __wrapped_factory__
100 # method, then we don't set __wrapped__ yet and instead will
101 # defer creation of the wrapped object until it is first
102 # needed.
104 pass
106 else:
107 object.__setattr__(self, "__wrapped__", wrapped)
108 else:
109 object.__setattr__(self, "__wrapped__", wrapped)
111 # Python 3.2+ has the __qualname__ attribute, but it does not
112 # allow it to be overridden using a property and it must instead
113 # be an actual string object instead.
115 try:
116 object.__setattr__(self, "__qualname__", wrapped.__qualname__)
117 except AttributeError:
118 pass
120 # Python 3.10 onwards also does not allow itself to be overridden
121 # using a property and it must instead be set explicitly.
123 try:
124 object.__setattr__(self, "__annotations__", wrapped.__annotations__)
125 except AttributeError:
126 pass
128 @property
129 def __object_proxy__(self):
130 return ObjectProxy
132 def __self_setattr__(self, name, value):
133 object.__setattr__(self, name, value)
135 @property
136 def __name__(self):
137 return self.__wrapped__.__name__
139 @__name__.setter
140 def __name__(self, value):
141 self.__wrapped__.__name__ = value
143 @property
144 def __class__(self):
145 return self.__wrapped__.__class__
147 @__class__.setter
148 def __class__(self, value):
149 self.__wrapped__.__class__ = value
151 def __dir__(self):
152 return dir(self.__wrapped__)
154 def __str__(self):
155 return str(self.__wrapped__)
157 def __bytes__(self):
158 return bytes(self.__wrapped__)
160 def __repr__(self):
161 return f"<{type(self).__name__} at 0x{id(self):x} for {type(self.__wrapped__).__name__} at 0x{id(self.__wrapped__):x}>"
163 def __format__(self, format_spec):
164 return format(self.__wrapped__, format_spec)
166 def __reversed__(self):
167 return reversed(self.__wrapped__)
169 def __round__(self, ndigits=None):
170 return round(self.__wrapped__, ndigits)
172 def __mro_entries__(self, bases):
173 if not isinstance(self.__wrapped__, type) and hasattr(
174 self.__wrapped__, "__mro_entries__"
175 ):
176 return self.__wrapped__.__mro_entries__(bases)
177 return (self.__wrapped__,)
179 def __lt__(self, other):
180 return self.__wrapped__ < other
182 def __le__(self, other):
183 return self.__wrapped__ <= other
185 def __eq__(self, other):
186 return self.__wrapped__ == other
188 def __ne__(self, other):
189 return self.__wrapped__ != other
191 def __gt__(self, other):
192 return self.__wrapped__ > other
194 def __ge__(self, other):
195 return self.__wrapped__ >= other
197 def __hash__(self):
198 return hash(self.__wrapped__)
200 def __nonzero__(self):
201 return bool(self.__wrapped__)
203 def __bool__(self):
204 return bool(self.__wrapped__)
206 def __setattr__(self, name, value):
207 if name.startswith("_self_"):
208 object.__setattr__(self, name, value)
210 elif name == "__wrapped__":
211 object.__setattr__(self, name, value)
213 try:
214 object.__delattr__(self, "__qualname__")
215 except AttributeError:
216 pass
217 try:
218 object.__setattr__(self, "__qualname__", value.__qualname__)
219 except AttributeError:
220 pass
221 try:
222 object.__delattr__(self, "__annotations__")
223 except AttributeError:
224 pass
225 try:
226 object.__setattr__(self, "__annotations__", value.__annotations__)
227 except AttributeError:
228 pass
230 __wrapped_setattr_fixups__ = getattr(
231 self, "__wrapped_setattr_fixups__", None
232 )
234 if __wrapped_setattr_fixups__ is not None:
235 __wrapped_setattr_fixups__()
237 elif name == "__qualname__":
238 setattr(self.__wrapped__, name, value)
239 object.__setattr__(self, name, value)
241 elif name == "__annotations__":
242 setattr(self.__wrapped__, name, value)
243 object.__setattr__(self, name, value)
245 elif hasattr(type(self), name):
246 object.__setattr__(self, name, value)
248 else:
249 setattr(self.__wrapped__, name, value)
251 def __getattr__(self, name):
252 # If we need to lookup `__wrapped__` then the `__init__()` method
253 # cannot have been called, or this is a lazy object proxy which is
254 # deferring creation of the wrapped object until it is first needed.
256 if name == "__wrapped__":
257 # Note that we use existance of `__wrapped_factory__` to gate whether
258 # we can attempt to initialize the wrapped object lazily, but it is
259 # `__wrapped_get__` that we actually call to do the initialization.
260 # This is so that we can handle multithreading correctly by having
261 # `__wrapped_get__` use a lock to protect against multiple threads
262 # trying to initialize the wrapped object at the same time.
264 try:
265 object.__getattribute__(self, "__wrapped_factory__")
266 except AttributeError:
267 pass
268 else:
269 return object.__getattribute__(self, "__wrapped_get__")()
271 raise WrapperNotInitializedError("wrapper has not been initialized")
273 return getattr(self.__wrapped__, name)
275 def __delattr__(self, name):
276 if name.startswith("_self_"):
277 object.__delattr__(self, name)
279 elif name == "__wrapped__":
280 raise TypeError("__wrapped__ attribute cannot be deleted")
282 elif name == "__qualname__":
283 object.__delattr__(self, name)
284 delattr(self.__wrapped__, name)
286 elif hasattr(type(self), name):
287 object.__delattr__(self, name)
289 else:
290 delattr(self.__wrapped__, name)
292 def __add__(self, other):
293 return self.__wrapped__ + other
295 def __sub__(self, other):
296 return self.__wrapped__ - other
298 def __mul__(self, other):
299 return self.__wrapped__ * other
301 def __truediv__(self, other):
302 return operator.truediv(self.__wrapped__, other)
304 def __floordiv__(self, other):
305 return self.__wrapped__ // other
307 def __mod__(self, other):
308 return self.__wrapped__ % other
310 def __divmod__(self, other):
311 return divmod(self.__wrapped__, other)
313 def __pow__(self, other, *args):
314 return pow(self.__wrapped__, other, *args)
316 def __lshift__(self, other):
317 return self.__wrapped__ << other
319 def __rshift__(self, other):
320 return self.__wrapped__ >> other
322 def __and__(self, other):
323 return self.__wrapped__ & other
325 def __xor__(self, other):
326 return self.__wrapped__ ^ other
328 def __or__(self, other):
329 return self.__wrapped__ | other
331 def __radd__(self, other):
332 return other + self.__wrapped__
334 def __rsub__(self, other):
335 return other - self.__wrapped__
337 def __rmul__(self, other):
338 return other * self.__wrapped__
340 def __rtruediv__(self, other):
341 return operator.truediv(other, self.__wrapped__)
343 def __rfloordiv__(self, other):
344 return other // self.__wrapped__
346 def __rmod__(self, other):
347 return other % self.__wrapped__
349 def __rdivmod__(self, other):
350 return divmod(other, self.__wrapped__)
352 def __rpow__(self, other, *args):
353 return pow(other, self.__wrapped__, *args)
355 def __rlshift__(self, other):
356 return other << self.__wrapped__
358 def __rrshift__(self, other):
359 return other >> self.__wrapped__
361 def __rand__(self, other):
362 return other & self.__wrapped__
364 def __rxor__(self, other):
365 return other ^ self.__wrapped__
367 def __ror__(self, other):
368 return other | self.__wrapped__
370 def __iadd__(self, other):
371 if hasattr(self.__wrapped__, "__iadd__"):
372 self.__wrapped__ += other
373 return self
374 else:
375 return self.__object_proxy__(self.__wrapped__ + other)
377 def __isub__(self, other):
378 if hasattr(self.__wrapped__, "__isub__"):
379 self.__wrapped__ -= other
380 return self
381 else:
382 return self.__object_proxy__(self.__wrapped__ - other)
384 def __imul__(self, other):
385 if hasattr(self.__wrapped__, "__imul__"):
386 self.__wrapped__ *= other
387 return self
388 else:
389 return self.__object_proxy__(self.__wrapped__ * other)
391 def __itruediv__(self, other):
392 if hasattr(self.__wrapped__, "__itruediv__"):
393 self.__wrapped__ /= other
394 return self
395 else:
396 return self.__object_proxy__(self.__wrapped__ / other)
398 def __ifloordiv__(self, other):
399 if hasattr(self.__wrapped__, "__ifloordiv__"):
400 self.__wrapped__ //= other
401 return self
402 else:
403 return self.__object_proxy__(self.__wrapped__ // other)
405 def __imod__(self, other):
406 if hasattr(self.__wrapped__, "__imod__"):
407 self.__wrapped__ %= other
408 return self
409 else:
410 return self.__object_proxy__(self.__wrapped__ % other)
412 return self
414 def __ipow__(self, other): # type: ignore[misc]
415 if hasattr(self.__wrapped__, "__ipow__"):
416 self.__wrapped__ **= other
417 return self
418 else:
419 return self.__object_proxy__(self.__wrapped__**other)
421 def __ilshift__(self, other):
422 if hasattr(self.__wrapped__, "__ilshift__"):
423 self.__wrapped__ <<= other
424 return self
425 else:
426 return self.__object_proxy__(self.__wrapped__ << other)
428 def __irshift__(self, other):
429 if hasattr(self.__wrapped__, "__irshift__"):
430 self.__wrapped__ >>= other
431 return self
432 else:
433 return self.__object_proxy__(self.__wrapped__ >> other)
435 def __iand__(self, other):
436 if hasattr(self.__wrapped__, "__iand__"):
437 self.__wrapped__ &= other
438 return self
439 else:
440 return self.__object_proxy__(self.__wrapped__ & other)
442 def __ixor__(self, other):
443 if hasattr(self.__wrapped__, "__ixor__"):
444 self.__wrapped__ ^= other
445 return self
446 else:
447 return self.__object_proxy__(self.__wrapped__ ^ other)
449 def __ior__(self, other):
450 if hasattr(self.__wrapped__, "__ior__"):
451 self.__wrapped__ |= other
452 return self
453 else:
454 return self.__object_proxy__(self.__wrapped__ | other)
456 def __neg__(self):
457 return -self.__wrapped__
459 def __pos__(self):
460 return +self.__wrapped__
462 def __abs__(self):
463 return abs(self.__wrapped__)
465 def __invert__(self):
466 return ~self.__wrapped__
468 def __int__(self):
469 return int(self.__wrapped__)
471 def __float__(self):
472 return float(self.__wrapped__)
474 def __complex__(self):
475 return complex(self.__wrapped__)
477 def __oct__(self):
478 return oct(self.__wrapped__)
480 def __hex__(self):
481 return hex(self.__wrapped__)
483 def __index__(self):
484 return operator.index(self.__wrapped__)
486 def __matmul__(self, other):
487 return self.__wrapped__ @ other
489 def __rmatmul__(self, other):
490 return other @ self.__wrapped__
492 def __imatmul__(self, other):
493 if hasattr(self.__wrapped__, "__imatmul__"):
494 self.__wrapped__ @= other
495 return self
496 else:
497 return self.__object_proxy__(self.__wrapped__ @ other)
499 def __len__(self):
500 return len(self.__wrapped__)
502 def __contains__(self, value):
503 return value in self.__wrapped__
505 def __getitem__(self, key):
506 return self.__wrapped__[key]
508 def __setitem__(self, key, value):
509 self.__wrapped__[key] = value
511 def __delitem__(self, key):
512 del self.__wrapped__[key]
514 def __getslice__(self, i, j):
515 return self.__wrapped__[i:j]
517 def __setslice__(self, i, j, value):
518 self.__wrapped__[i:j] = value
520 def __delslice__(self, i, j):
521 del self.__wrapped__[i:j]
523 def __enter__(self):
524 return self.__wrapped__.__enter__()
526 def __exit__(self, *args, **kwargs):
527 return self.__wrapped__.__exit__(*args, **kwargs)
529 def __aenter__(self):
530 return self.__wrapped__.__aenter__()
532 def __aexit__(self, *args, **kwargs):
533 return self.__wrapped__.__aexit__(*args, **kwargs)
535 def __copy__(self):
536 raise NotImplementedError("object proxy must define __copy__()")
538 def __deepcopy__(self, memo):
539 raise NotImplementedError("object proxy must define __deepcopy__()")
541 def __reduce__(self):
542 raise NotImplementedError("object proxy must define __reduce__()")
544 def __reduce_ex__(self, protocol):
545 raise NotImplementedError("object proxy must define __reduce_ex__()")
548class CallableObjectProxy(ObjectProxy):
550 def __call__(*args, **kwargs):
551 def _unpack_self(self, *args):
552 return self, args
554 self, args = _unpack_self(*args)
556 return self.__wrapped__(*args, **kwargs)
559class PartialCallableObjectProxy(ObjectProxy):
560 """A callable object proxy that supports partial application of arguments
561 and keywords.
562 """
564 def __init__(*args, **kwargs):
565 """Create a callable object proxy with partial application of the given
566 arguments and keywords. This behaves the same as `functools.partial`, but
567 implemented using the `ObjectProxy` class to provide better support for
568 introspection.
569 """
571 def _unpack_self(self, *args):
572 return self, args
574 self, args = _unpack_self(*args)
576 if len(args) < 1:
577 raise TypeError("partial type takes at least one argument")
579 wrapped, args = args[0], args[1:]
581 if not callable(wrapped):
582 raise TypeError("the first argument must be callable")
584 super(PartialCallableObjectProxy, self).__init__(wrapped)
586 self._self_args = args
587 self._self_kwargs = kwargs
589 def __call__(*args, **kwargs):
590 def _unpack_self(self, *args):
591 return self, args
593 self, args = _unpack_self(*args)
595 _args = self._self_args + args
597 _kwargs = dict(self._self_kwargs)
598 _kwargs.update(kwargs)
600 return self.__wrapped__(*_args, **_kwargs)
603class _FunctionWrapperBase(ObjectProxy):
605 __slots__ = (
606 "_self_instance",
607 "_self_wrapper",
608 "_self_enabled",
609 "_self_binding",
610 "_self_parent",
611 "_self_owner",
612 )
614 def __init__(
615 self,
616 wrapped,
617 instance,
618 wrapper,
619 enabled=None,
620 binding="callable",
621 parent=None,
622 owner=None,
623 ):
625 super(_FunctionWrapperBase, self).__init__(wrapped)
627 object.__setattr__(self, "_self_instance", instance)
628 object.__setattr__(self, "_self_wrapper", wrapper)
629 object.__setattr__(self, "_self_enabled", enabled)
630 object.__setattr__(self, "_self_binding", binding)
631 object.__setattr__(self, "_self_parent", parent)
632 object.__setattr__(self, "_self_owner", owner)
634 def __get__(self, instance, owner):
635 # This method is actually doing double duty for both unbound and bound
636 # derived wrapper classes. It should possibly be broken up and the
637 # distinct functionality moved into the derived classes. Can't do that
638 # straight away due to some legacy code which is relying on it being
639 # here in this base class.
640 #
641 # The distinguishing attribute which determines whether we are being
642 # called in an unbound or bound wrapper is the parent attribute. If
643 # binding has never occurred, then the parent will be None.
644 #
645 # First therefore, is if we are called in an unbound wrapper. In this
646 # case we perform the binding.
647 #
648 # We have two special cases to worry about here. These are where we are
649 # decorating a class or builtin function as neither provide a __get__()
650 # method to call. In this case we simply return self.
651 #
652 # Note that we otherwise still do binding even if instance is None and
653 # accessing an unbound instance method from a class. This is because we
654 # need to be able to later detect that specific case as we will need to
655 # extract the instance from the first argument of those passed in.
657 if self._self_parent is None:
658 # Technically can probably just check for existence of __get__ on
659 # the wrapped object, but this is more explicit.
661 if self._self_binding == "builtin":
662 return self
664 if self._self_binding == "class":
665 return self
667 binder = getattr(self.__wrapped__, "__get__", None)
669 if binder is None:
670 return self
672 descriptor = binder(instance, owner)
674 return self.__bound_function_wrapper__(
675 descriptor,
676 instance,
677 self._self_wrapper,
678 self._self_enabled,
679 self._self_binding,
680 self,
681 owner,
682 )
684 # Now we have the case of binding occurring a second time on what was
685 # already a bound function. In this case we would usually return
686 # ourselves again. This mirrors what Python does.
687 #
688 # The special case this time is where we were originally bound with an
689 # instance of None and we were likely an instance method. In that case
690 # we rebind against the original wrapped function from the parent again.
692 if self._self_instance is None and self._self_binding in (
693 "function",
694 "instancemethod",
695 "callable",
696 ):
697 descriptor = self._self_parent.__wrapped__.__get__(instance, owner)
699 return self._self_parent.__bound_function_wrapper__(
700 descriptor,
701 instance,
702 self._self_wrapper,
703 self._self_enabled,
704 self._self_binding,
705 self._self_parent,
706 owner,
707 )
709 return self
711 def __call__(*args, **kwargs):
712 def _unpack_self(self, *args):
713 return self, args
715 self, args = _unpack_self(*args)
717 # If enabled has been specified, then evaluate it at this point
718 # and if the wrapper is not to be executed, then simply return
719 # the bound function rather than a bound wrapper for the bound
720 # function. When evaluating enabled, if it is callable we call
721 # it, otherwise we evaluate it as a boolean.
723 if self._self_enabled is not None:
724 if callable(self._self_enabled):
725 if not self._self_enabled():
726 return self.__wrapped__(*args, **kwargs)
727 elif not self._self_enabled:
728 return self.__wrapped__(*args, **kwargs)
730 # This can occur where initial function wrapper was applied to
731 # a function that was already bound to an instance. In that case
732 # we want to extract the instance from the function and use it.
734 if self._self_binding in (
735 "function",
736 "instancemethod",
737 "classmethod",
738 "callable",
739 ):
740 if self._self_instance is None:
741 instance = getattr(self.__wrapped__, "__self__", None)
742 if instance is not None:
743 return self._self_wrapper(self.__wrapped__, instance, args, kwargs)
745 # This is generally invoked when the wrapped function is being
746 # called as a normal function and is not bound to a class as an
747 # instance method. This is also invoked in the case where the
748 # wrapped function was a method, but this wrapper was in turn
749 # wrapped using the staticmethod decorator.
751 return self._self_wrapper(self.__wrapped__, self._self_instance, args, kwargs)
753 def __set_name__(self, owner, name):
754 # This is a special method use to supply information to
755 # descriptors about what the name of variable in a class
756 # definition is. Not wanting to add this to ObjectProxy as not
757 # sure of broader implications of doing that. Thus restrict to
758 # FunctionWrapper used by decorators.
760 if hasattr(self.__wrapped__, "__set_name__"):
761 self.__wrapped__.__set_name__(owner, name)
763 def __instancecheck__(self, instance):
764 # This is a special method used by isinstance() to make checks
765 # instance of the `__wrapped__`.
766 return isinstance(instance, self.__wrapped__)
768 def __subclasscheck__(self, subclass):
769 # This is a special method used by issubclass() to make checks
770 # about inheritance of classes. We need to upwrap any object
771 # proxy. Not wanting to add this to ObjectProxy as not sure of
772 # broader implications of doing that. Thus restrict to
773 # FunctionWrapper used by decorators.
775 if hasattr(subclass, "__wrapped__"):
776 return issubclass(subclass.__wrapped__, self.__wrapped__)
777 else:
778 return issubclass(subclass, self.__wrapped__)
781class BoundFunctionWrapper(_FunctionWrapperBase):
783 def __call__(*args, **kwargs):
784 def _unpack_self(self, *args):
785 return self, args
787 self, args = _unpack_self(*args)
789 # If enabled has been specified, then evaluate it at this point and if
790 # the wrapper is not to be executed, then simply return the bound
791 # function rather than a bound wrapper for the bound function. When
792 # evaluating enabled, if it is callable we call it, otherwise we
793 # evaluate it as a boolean.
795 if self._self_enabled is not None:
796 if callable(self._self_enabled):
797 if not self._self_enabled():
798 return self.__wrapped__(*args, **kwargs)
799 elif not self._self_enabled:
800 return self.__wrapped__(*args, **kwargs)
802 # We need to do things different depending on whether we are likely
803 # wrapping an instance method vs a static method or class method.
805 if self._self_binding == "function":
806 if self._self_instance is None and args:
807 instance, newargs = args[0], args[1:]
808 if isinstance(instance, self._self_owner):
809 wrapped = PartialCallableObjectProxy(self.__wrapped__, instance)
810 return self._self_wrapper(wrapped, instance, newargs, kwargs)
812 return self._self_wrapper(
813 self.__wrapped__, self._self_instance, args, kwargs
814 )
816 elif self._self_binding == "callable":
817 if self._self_instance is None:
818 # This situation can occur where someone is calling the
819 # instancemethod via the class type and passing the instance as
820 # the first argument. We need to shift the args before making
821 # the call to the wrapper and effectively bind the instance to
822 # the wrapped function using a partial so the wrapper doesn't
823 # see anything as being different.
825 if not args:
826 raise TypeError("missing 1 required positional argument")
828 instance, args = args[0], args[1:]
829 wrapped = PartialCallableObjectProxy(self.__wrapped__, instance)
830 return self._self_wrapper(wrapped, instance, args, kwargs)
832 return self._self_wrapper(
833 self.__wrapped__, self._self_instance, args, kwargs
834 )
836 else:
837 # As in this case we would be dealing with a classmethod or
838 # staticmethod, then _self_instance will only tell us whether
839 # when calling the classmethod or staticmethod they did it via an
840 # instance of the class it is bound to and not the case where
841 # done by the class type itself. We thus ignore _self_instance
842 # and use the __self__ attribute of the bound function instead.
843 # For a classmethod, this means instance will be the class type
844 # and for a staticmethod it will be None. This is probably the
845 # more useful thing we can pass through even though we loose
846 # knowledge of whether they were called on the instance vs the
847 # class type, as it reflects what they have available in the
848 # decoratored function.
850 instance = getattr(self.__wrapped__, "__self__", None)
852 return self._self_wrapper(self.__wrapped__, instance, args, kwargs)
855class FunctionWrapper(_FunctionWrapperBase):
856 """
857 A wrapper for callable objects that can be used to apply decorators to
858 functions, methods, classmethods, and staticmethods, or any other callable.
859 It handles binding and unbinding of methods, and allows for the wrapper to
860 be enabled or disabled.
861 """
863 __bound_function_wrapper__ = BoundFunctionWrapper
865 def __init__(self, wrapped, wrapper, enabled=None):
866 """
867 Initialize the `FunctionWrapper` with the `wrapped` callable, the
868 `wrapper` function, and an optional `enabled` argument. The `enabled`
869 argument can be a boolean or a callable that returns a boolean. When a
870 callable is provided, it will be called each time the wrapper is
871 invoked to determine if the wrapper function should be executed or
872 whether the wrapped function should be called directly. If `enabled`
873 is not provided, the wrapper is enabled by default.
874 """
876 # What it is we are wrapping here could be anything. We need to
877 # try and detect specific cases though. In particular, we need
878 # to detect when we are given something that is a method of a
879 # class. Further, we need to know when it is likely an instance
880 # method, as opposed to a class or static method. This can
881 # become problematic though as there isn't strictly a fool proof
882 # method of knowing.
883 #
884 # The situations we could encounter when wrapping a method are:
885 #
886 # 1. The wrapper is being applied as part of a decorator which
887 # is a part of the class definition. In this case what we are
888 # given is the raw unbound function, classmethod or staticmethod
889 # wrapper objects.
890 #
891 # The problem here is that we will not know we are being applied
892 # in the context of the class being set up. This becomes
893 # important later for the case of an instance method, because in
894 # that case we just see it as a raw function and can't
895 # distinguish it from wrapping a normal function outside of
896 # a class context.
897 #
898 # 2. The wrapper is being applied when performing monkey
899 # patching of the class type afterwards and the method to be
900 # wrapped was retrieved direct from the __dict__ of the class
901 # type. This is effectively the same as (1) above.
902 #
903 # 3. The wrapper is being applied when performing monkey
904 # patching of the class type afterwards and the method to be
905 # wrapped was retrieved from the class type. In this case
906 # binding will have been performed where the instance against
907 # which the method is bound will be None at that point.
908 #
909 # This case is a problem because we can no longer tell if the
910 # method was a static method, plus if using Python3, we cannot
911 # tell if it was an instance method as the concept of an
912 # unnbound method no longer exists.
913 #
914 # 4. The wrapper is being applied when performing monkey
915 # patching of an instance of a class. In this case binding will
916 # have been performed where the instance was not None.
917 #
918 # This case is a problem because we can no longer tell if the
919 # method was a static method.
920 #
921 # Overall, the best we can do is look at the original type of the
922 # object which was wrapped prior to any binding being done and
923 # see if it is an instance of classmethod or staticmethod. In
924 # the case where other decorators are between us and them, if
925 # they do not propagate the __class__ attribute so that the
926 # isinstance() checks works, then likely this will do the wrong
927 # thing where classmethod and staticmethod are used.
928 #
929 # Since it is likely to be very rare that anyone even puts
930 # decorators around classmethod and staticmethod, likelihood of
931 # that being an issue is very small, so we accept it and suggest
932 # that those other decorators be fixed. It is also only an issue
933 # if a decorator wants to actually do things with the arguments.
934 #
935 # As to not being able to identify static methods properly, we
936 # just hope that that isn't something people are going to want
937 # to wrap, or if they do suggest they do it the correct way by
938 # ensuring that it is decorated in the class definition itself,
939 # or patch it in the __dict__ of the class type.
940 #
941 # So to get the best outcome we can, whenever we aren't sure what
942 # it is, we label it as a 'callable'. If it was already bound and
943 # that is rebound later, we assume that it will be an instance
944 # method and try and cope with the possibility that the 'self'
945 # argument it being passed as an explicit argument and shuffle
946 # the arguments around to extract 'self' for use as the instance.
948 binding = None
950 if isinstance(wrapped, _FunctionWrapperBase):
951 binding = wrapped._self_binding
953 if not binding:
954 if inspect.isbuiltin(wrapped):
955 binding = "builtin"
957 elif inspect.isfunction(wrapped):
958 binding = "function"
960 elif inspect.isclass(wrapped):
961 binding = "class"
963 elif isinstance(wrapped, classmethod):
964 binding = "classmethod"
966 elif isinstance(wrapped, staticmethod):
967 binding = "staticmethod"
969 elif hasattr(wrapped, "__self__"):
970 if inspect.isclass(wrapped.__self__):
971 binding = "classmethod"
972 elif inspect.ismethod(wrapped):
973 binding = "instancemethod"
974 else:
975 binding = "callable"
977 else:
978 binding = "callable"
980 super(FunctionWrapper, self).__init__(wrapped, None, wrapper, enabled, binding)