wrapt 2.1.0.dev1__cp313-cp313t-musllinux_1_2_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
wrapt/wrappers.py ADDED
@@ -0,0 +1,980 @@
1
+ import inspect
2
+ import operator
3
+ import sys
4
+
5
+
6
+ def with_metaclass(meta, *bases):
7
+ """Create a base class with a metaclass."""
8
+ return meta("NewBase", bases, {})
9
+
10
+
11
+ class 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
+ """
17
+
18
+ pass
19
+
20
+
21
+ class _ObjectProxyMethods:
22
+
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.
31
+
32
+ @property
33
+ def __module__(self):
34
+ return self.__wrapped__.__module__
35
+
36
+ @__module__.setter
37
+ def __module__(self, value):
38
+ self.__wrapped__.__module__ = value
39
+
40
+ @property
41
+ def __doc__(self):
42
+ return self.__wrapped__.__doc__
43
+
44
+ @__doc__.setter
45
+ def __doc__(self, value):
46
+ self.__wrapped__.__doc__ = value
47
+
48
+ # We similar use a property for __dict__. We need __dict__ to be
49
+ # explicit to ensure that vars() works as expected.
50
+
51
+ @property
52
+ def __dict__(self):
53
+ return self.__wrapped__.__dict__
54
+
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.
59
+
60
+ @property
61
+ def __weakref__(self):
62
+ return self.__wrapped__.__weakref__
63
+
64
+
65
+ class _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.
71
+
72
+ dictionary.update(vars(_ObjectProxyMethods))
73
+
74
+ return type.__new__(cls, name, bases, dictionary)
75
+
76
+
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.
83
+
84
+
85
+ class ObjectProxy(with_metaclass(_ObjectProxyMetaType)): # type: ignore[misc]
86
+
87
+ __slots__ = "__wrapped__"
88
+
89
+ def __init__(self, wrapped):
90
+ """Create an object proxy around the given object."""
91
+
92
+ if wrapped is None:
93
+ try:
94
+ callback = object.__getattribute__(self, "__wrapped_factory__")
95
+ except AttributeError:
96
+ callback = None
97
+
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.
103
+
104
+ pass
105
+
106
+ else:
107
+ object.__setattr__(self, "__wrapped__", wrapped)
108
+ else:
109
+ object.__setattr__(self, "__wrapped__", wrapped)
110
+
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.
114
+
115
+ try:
116
+ object.__setattr__(self, "__qualname__", wrapped.__qualname__)
117
+ except AttributeError:
118
+ pass
119
+
120
+ # Python 3.10 onwards also does not allow itself to be overridden
121
+ # using a property and it must instead be set explicitly.
122
+
123
+ try:
124
+ object.__setattr__(self, "__annotations__", wrapped.__annotations__)
125
+ except AttributeError:
126
+ pass
127
+
128
+ @property
129
+ def __object_proxy__(self):
130
+ return ObjectProxy
131
+
132
+ def __self_setattr__(self, name, value):
133
+ object.__setattr__(self, name, value)
134
+
135
+ @property
136
+ def __name__(self):
137
+ return self.__wrapped__.__name__
138
+
139
+ @__name__.setter
140
+ def __name__(self, value):
141
+ self.__wrapped__.__name__ = value
142
+
143
+ @property
144
+ def __class__(self):
145
+ return self.__wrapped__.__class__
146
+
147
+ @__class__.setter
148
+ def __class__(self, value):
149
+ self.__wrapped__.__class__ = value
150
+
151
+ def __dir__(self):
152
+ return dir(self.__wrapped__)
153
+
154
+ def __str__(self):
155
+ return str(self.__wrapped__)
156
+
157
+ def __bytes__(self):
158
+ return bytes(self.__wrapped__)
159
+
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}>"
162
+
163
+ def __format__(self, format_spec):
164
+ return format(self.__wrapped__, format_spec)
165
+
166
+ def __reversed__(self):
167
+ return reversed(self.__wrapped__)
168
+
169
+ def __round__(self, ndigits=None):
170
+ return round(self.__wrapped__, ndigits)
171
+
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__,)
178
+
179
+ def __lt__(self, other):
180
+ return self.__wrapped__ < other
181
+
182
+ def __le__(self, other):
183
+ return self.__wrapped__ <= other
184
+
185
+ def __eq__(self, other):
186
+ return self.__wrapped__ == other
187
+
188
+ def __ne__(self, other):
189
+ return self.__wrapped__ != other
190
+
191
+ def __gt__(self, other):
192
+ return self.__wrapped__ > other
193
+
194
+ def __ge__(self, other):
195
+ return self.__wrapped__ >= other
196
+
197
+ def __hash__(self):
198
+ return hash(self.__wrapped__)
199
+
200
+ def __nonzero__(self):
201
+ return bool(self.__wrapped__)
202
+
203
+ def __bool__(self):
204
+ return bool(self.__wrapped__)
205
+
206
+ def __setattr__(self, name, value):
207
+ if name.startswith("_self_"):
208
+ object.__setattr__(self, name, value)
209
+
210
+ elif name == "__wrapped__":
211
+ object.__setattr__(self, name, value)
212
+
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
229
+
230
+ __wrapped_setattr_fixups__ = getattr(
231
+ self, "__wrapped_setattr_fixups__", None
232
+ )
233
+
234
+ if __wrapped_setattr_fixups__ is not None:
235
+ __wrapped_setattr_fixups__()
236
+
237
+ elif name == "__qualname__":
238
+ setattr(self.__wrapped__, name, value)
239
+ object.__setattr__(self, name, value)
240
+
241
+ elif name == "__annotations__":
242
+ setattr(self.__wrapped__, name, value)
243
+ object.__setattr__(self, name, value)
244
+
245
+ elif hasattr(type(self), name):
246
+ object.__setattr__(self, name, value)
247
+
248
+ else:
249
+ setattr(self.__wrapped__, name, value)
250
+
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.
255
+
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.
263
+
264
+ try:
265
+ object.__getattribute__(self, "__wrapped_factory__")
266
+ except AttributeError:
267
+ pass
268
+ else:
269
+ return object.__getattribute__(self, "__wrapped_get__")()
270
+
271
+ raise WrapperNotInitializedError("wrapper has not been initialized")
272
+
273
+ return getattr(self.__wrapped__, name)
274
+
275
+ def __delattr__(self, name):
276
+ if name.startswith("_self_"):
277
+ object.__delattr__(self, name)
278
+
279
+ elif name == "__wrapped__":
280
+ raise TypeError("__wrapped__ attribute cannot be deleted")
281
+
282
+ elif name == "__qualname__":
283
+ object.__delattr__(self, name)
284
+ delattr(self.__wrapped__, name)
285
+
286
+ elif hasattr(type(self), name):
287
+ object.__delattr__(self, name)
288
+
289
+ else:
290
+ delattr(self.__wrapped__, name)
291
+
292
+ def __add__(self, other):
293
+ return self.__wrapped__ + other
294
+
295
+ def __sub__(self, other):
296
+ return self.__wrapped__ - other
297
+
298
+ def __mul__(self, other):
299
+ return self.__wrapped__ * other
300
+
301
+ def __truediv__(self, other):
302
+ return operator.truediv(self.__wrapped__, other)
303
+
304
+ def __floordiv__(self, other):
305
+ return self.__wrapped__ // other
306
+
307
+ def __mod__(self, other):
308
+ return self.__wrapped__ % other
309
+
310
+ def __divmod__(self, other):
311
+ return divmod(self.__wrapped__, other)
312
+
313
+ def __pow__(self, other, *args):
314
+ return pow(self.__wrapped__, other, *args)
315
+
316
+ def __lshift__(self, other):
317
+ return self.__wrapped__ << other
318
+
319
+ def __rshift__(self, other):
320
+ return self.__wrapped__ >> other
321
+
322
+ def __and__(self, other):
323
+ return self.__wrapped__ & other
324
+
325
+ def __xor__(self, other):
326
+ return self.__wrapped__ ^ other
327
+
328
+ def __or__(self, other):
329
+ return self.__wrapped__ | other
330
+
331
+ def __radd__(self, other):
332
+ return other + self.__wrapped__
333
+
334
+ def __rsub__(self, other):
335
+ return other - self.__wrapped__
336
+
337
+ def __rmul__(self, other):
338
+ return other * self.__wrapped__
339
+
340
+ def __rtruediv__(self, other):
341
+ return operator.truediv(other, self.__wrapped__)
342
+
343
+ def __rfloordiv__(self, other):
344
+ return other // self.__wrapped__
345
+
346
+ def __rmod__(self, other):
347
+ return other % self.__wrapped__
348
+
349
+ def __rdivmod__(self, other):
350
+ return divmod(other, self.__wrapped__)
351
+
352
+ def __rpow__(self, other, *args):
353
+ return pow(other, self.__wrapped__, *args)
354
+
355
+ def __rlshift__(self, other):
356
+ return other << self.__wrapped__
357
+
358
+ def __rrshift__(self, other):
359
+ return other >> self.__wrapped__
360
+
361
+ def __rand__(self, other):
362
+ return other & self.__wrapped__
363
+
364
+ def __rxor__(self, other):
365
+ return other ^ self.__wrapped__
366
+
367
+ def __ror__(self, other):
368
+ return other | self.__wrapped__
369
+
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)
376
+
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)
383
+
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)
390
+
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)
397
+
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)
404
+
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)
411
+
412
+ return self
413
+
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)
420
+
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)
427
+
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)
434
+
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)
441
+
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)
448
+
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)
455
+
456
+ def __neg__(self):
457
+ return -self.__wrapped__
458
+
459
+ def __pos__(self):
460
+ return +self.__wrapped__
461
+
462
+ def __abs__(self):
463
+ return abs(self.__wrapped__)
464
+
465
+ def __invert__(self):
466
+ return ~self.__wrapped__
467
+
468
+ def __int__(self):
469
+ return int(self.__wrapped__)
470
+
471
+ def __float__(self):
472
+ return float(self.__wrapped__)
473
+
474
+ def __complex__(self):
475
+ return complex(self.__wrapped__)
476
+
477
+ def __oct__(self):
478
+ return oct(self.__wrapped__)
479
+
480
+ def __hex__(self):
481
+ return hex(self.__wrapped__)
482
+
483
+ def __index__(self):
484
+ return operator.index(self.__wrapped__)
485
+
486
+ def __matmul__(self, other):
487
+ return self.__wrapped__ @ other
488
+
489
+ def __rmatmul__(self, other):
490
+ return other @ self.__wrapped__
491
+
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)
498
+
499
+ def __len__(self):
500
+ return len(self.__wrapped__)
501
+
502
+ def __contains__(self, value):
503
+ return value in self.__wrapped__
504
+
505
+ def __getitem__(self, key):
506
+ return self.__wrapped__[key]
507
+
508
+ def __setitem__(self, key, value):
509
+ self.__wrapped__[key] = value
510
+
511
+ def __delitem__(self, key):
512
+ del self.__wrapped__[key]
513
+
514
+ def __getslice__(self, i, j):
515
+ return self.__wrapped__[i:j]
516
+
517
+ def __setslice__(self, i, j, value):
518
+ self.__wrapped__[i:j] = value
519
+
520
+ def __delslice__(self, i, j):
521
+ del self.__wrapped__[i:j]
522
+
523
+ def __enter__(self):
524
+ return self.__wrapped__.__enter__()
525
+
526
+ def __exit__(self, *args, **kwargs):
527
+ return self.__wrapped__.__exit__(*args, **kwargs)
528
+
529
+ def __aenter__(self):
530
+ return self.__wrapped__.__aenter__()
531
+
532
+ def __aexit__(self, *args, **kwargs):
533
+ return self.__wrapped__.__aexit__(*args, **kwargs)
534
+
535
+ def __copy__(self):
536
+ raise NotImplementedError("object proxy must define __copy__()")
537
+
538
+ def __deepcopy__(self, memo):
539
+ raise NotImplementedError("object proxy must define __deepcopy__()")
540
+
541
+ def __reduce__(self):
542
+ raise NotImplementedError("object proxy must define __reduce__()")
543
+
544
+ def __reduce_ex__(self, protocol):
545
+ raise NotImplementedError("object proxy must define __reduce_ex__()")
546
+
547
+
548
+ class CallableObjectProxy(ObjectProxy):
549
+
550
+ def __call__(*args, **kwargs):
551
+ def _unpack_self(self, *args):
552
+ return self, args
553
+
554
+ self, args = _unpack_self(*args)
555
+
556
+ return self.__wrapped__(*args, **kwargs)
557
+
558
+
559
+ class PartialCallableObjectProxy(ObjectProxy):
560
+ """A callable object proxy that supports partial application of arguments
561
+ and keywords.
562
+ """
563
+
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
+ """
570
+
571
+ def _unpack_self(self, *args):
572
+ return self, args
573
+
574
+ self, args = _unpack_self(*args)
575
+
576
+ if len(args) < 1:
577
+ raise TypeError("partial type takes at least one argument")
578
+
579
+ wrapped, args = args[0], args[1:]
580
+
581
+ if not callable(wrapped):
582
+ raise TypeError("the first argument must be callable")
583
+
584
+ super(PartialCallableObjectProxy, self).__init__(wrapped)
585
+
586
+ self._self_args = args
587
+ self._self_kwargs = kwargs
588
+
589
+ def __call__(*args, **kwargs):
590
+ def _unpack_self(self, *args):
591
+ return self, args
592
+
593
+ self, args = _unpack_self(*args)
594
+
595
+ _args = self._self_args + args
596
+
597
+ _kwargs = dict(self._self_kwargs)
598
+ _kwargs.update(kwargs)
599
+
600
+ return self.__wrapped__(*_args, **_kwargs)
601
+
602
+
603
+ class _FunctionWrapperBase(ObjectProxy):
604
+
605
+ __slots__ = (
606
+ "_self_instance",
607
+ "_self_wrapper",
608
+ "_self_enabled",
609
+ "_self_binding",
610
+ "_self_parent",
611
+ "_self_owner",
612
+ )
613
+
614
+ def __init__(
615
+ self,
616
+ wrapped,
617
+ instance,
618
+ wrapper,
619
+ enabled=None,
620
+ binding="callable",
621
+ parent=None,
622
+ owner=None,
623
+ ):
624
+
625
+ super(_FunctionWrapperBase, self).__init__(wrapped)
626
+
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)
633
+
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.
656
+
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.
660
+
661
+ if self._self_binding == "builtin":
662
+ return self
663
+
664
+ if self._self_binding == "class":
665
+ return self
666
+
667
+ binder = getattr(self.__wrapped__, "__get__", None)
668
+
669
+ if binder is None:
670
+ return self
671
+
672
+ descriptor = binder(instance, owner)
673
+
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
+ )
683
+
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.
691
+
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)
698
+
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
+ )
708
+
709
+ return self
710
+
711
+ def __call__(*args, **kwargs):
712
+ def _unpack_self(self, *args):
713
+ return self, args
714
+
715
+ self, args = _unpack_self(*args)
716
+
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.
722
+
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)
729
+
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.
733
+
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)
744
+
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.
750
+
751
+ return self._self_wrapper(self.__wrapped__, self._self_instance, args, kwargs)
752
+
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.
759
+
760
+ if hasattr(self.__wrapped__, "__set_name__"):
761
+ self.__wrapped__.__set_name__(owner, name)
762
+
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__)
767
+
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.
774
+
775
+ if hasattr(subclass, "__wrapped__"):
776
+ return issubclass(subclass.__wrapped__, self.__wrapped__)
777
+ else:
778
+ return issubclass(subclass, self.__wrapped__)
779
+
780
+
781
+ class BoundFunctionWrapper(_FunctionWrapperBase):
782
+
783
+ def __call__(*args, **kwargs):
784
+ def _unpack_self(self, *args):
785
+ return self, args
786
+
787
+ self, args = _unpack_self(*args)
788
+
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.
794
+
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)
801
+
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.
804
+
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)
811
+
812
+ return self._self_wrapper(
813
+ self.__wrapped__, self._self_instance, args, kwargs
814
+ )
815
+
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.
824
+
825
+ if not args:
826
+ raise TypeError("missing 1 required positional argument")
827
+
828
+ instance, args = args[0], args[1:]
829
+ wrapped = PartialCallableObjectProxy(self.__wrapped__, instance)
830
+ return self._self_wrapper(wrapped, instance, args, kwargs)
831
+
832
+ return self._self_wrapper(
833
+ self.__wrapped__, self._self_instance, args, kwargs
834
+ )
835
+
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.
849
+
850
+ instance = getattr(self.__wrapped__, "__self__", None)
851
+
852
+ return self._self_wrapper(self.__wrapped__, instance, args, kwargs)
853
+
854
+
855
+ class 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
+ """
862
+
863
+ __bound_function_wrapper__ = BoundFunctionWrapper
864
+
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
+ """
875
+
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.
947
+
948
+ binding = None
949
+
950
+ if isinstance(wrapped, _FunctionWrapperBase):
951
+ binding = wrapped._self_binding
952
+
953
+ if not binding:
954
+ if inspect.isbuiltin(wrapped):
955
+ binding = "builtin"
956
+
957
+ elif inspect.isfunction(wrapped):
958
+ binding = "function"
959
+
960
+ elif inspect.isclass(wrapped):
961
+ binding = "class"
962
+
963
+ elif isinstance(wrapped, classmethod):
964
+ binding = "classmethod"
965
+
966
+ elif isinstance(wrapped, staticmethod):
967
+ binding = "staticmethod"
968
+
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"
976
+
977
+ else:
978
+ binding = "callable"
979
+
980
+ super(FunctionWrapper, self).__init__(wrapped, None, wrapper, enabled, binding)