wrapt 2.0.0rc4__cp313-cp313-musllinux_1_2_riscv64.whl → 2.0.1__cp313-cp313-musllinux_1_2_riscv64.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/__init__.py +2 -1
- wrapt/__init__.pyi +8 -1
- wrapt/__wrapt__.py +4 -0
- wrapt/_wrappers.c +555 -91
- wrapt/_wrappers.cpython-313-riscv64-linux-musl.so +0 -0
- wrapt/proxies.py +105 -20
- wrapt/wrappers.py +70 -25
- wrapt-2.0.1.dist-info/METADATA +216 -0
- wrapt-2.0.1.dist-info/RECORD +18 -0
- wrapt-2.0.0rc4.dist-info/METADATA +0 -198
- wrapt-2.0.0rc4.dist-info/RECORD +0 -18
- {wrapt-2.0.0rc4.dist-info → wrapt-2.0.1.dist-info}/WHEEL +0 -0
- {wrapt-2.0.0rc4.dist-info → wrapt-2.0.1.dist-info}/licenses/LICENSE +0 -0
- {wrapt-2.0.0rc4.dist-info → wrapt-2.0.1.dist-info}/top_level.txt +0 -0
|
Binary file
|
wrapt/proxies.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
"""Variants of ObjectProxy for different use cases."""
|
|
2
2
|
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
from types import ModuleType
|
|
5
|
+
|
|
3
6
|
from .__wrapt__ import BaseObjectProxy
|
|
4
7
|
from .decorators import synchronized
|
|
5
8
|
|
|
@@ -15,6 +18,10 @@ class ObjectProxy(BaseObjectProxy):
|
|
|
15
18
|
support for special dunder methods for callables, iterators, and async,
|
|
16
19
|
then use `AutoObjectProxy`."""
|
|
17
20
|
|
|
21
|
+
@property
|
|
22
|
+
def __object_proxy__(self):
|
|
23
|
+
return ObjectProxy
|
|
24
|
+
|
|
18
25
|
def __new__(cls, *args, **kwargs):
|
|
19
26
|
return super().__new__(cls)
|
|
20
27
|
|
|
@@ -26,7 +33,12 @@ class ObjectProxy(BaseObjectProxy):
|
|
|
26
33
|
# object and add special dunder methods.
|
|
27
34
|
|
|
28
35
|
|
|
29
|
-
def __wrapper_call__(
|
|
36
|
+
def __wrapper_call__(*args, **kwargs):
|
|
37
|
+
def _unpack_self(self, *args):
|
|
38
|
+
return self, args
|
|
39
|
+
|
|
40
|
+
self, args = _unpack_self(*args)
|
|
41
|
+
|
|
30
42
|
return self.__wrapped__(*args, **kwargs)
|
|
31
43
|
|
|
32
44
|
|
|
@@ -46,7 +58,7 @@ async def __wrapper_anext__(self):
|
|
|
46
58
|
return await self.__wrapped__.__anext__()
|
|
47
59
|
|
|
48
60
|
|
|
49
|
-
def
|
|
61
|
+
def __wrapper_length_hint__(self):
|
|
50
62
|
return self.__wrapped__.__length_hint__()
|
|
51
63
|
|
|
52
64
|
|
|
@@ -66,8 +78,8 @@ def __wrapper_delete__(self, instance):
|
|
|
66
78
|
return self.__wrapped__.__delete__(instance)
|
|
67
79
|
|
|
68
80
|
|
|
69
|
-
def __wrapper_set_name__(self, name):
|
|
70
|
-
return self.__wrapped__.__set_name__(name)
|
|
81
|
+
def __wrapper_set_name__(self, owner, name):
|
|
82
|
+
return self.__wrapped__.__set_name__(owner, name)
|
|
71
83
|
|
|
72
84
|
|
|
73
85
|
class AutoObjectProxy(BaseObjectProxy):
|
|
@@ -106,7 +118,7 @@ class AutoObjectProxy(BaseObjectProxy):
|
|
|
106
118
|
namespace["__anext__"] = __wrapper_anext__
|
|
107
119
|
|
|
108
120
|
if "__length_hint__" in wrapped_attrs and "__length_hint__" not in class_attrs:
|
|
109
|
-
namespace["__length_hint__"] =
|
|
121
|
+
namespace["__length_hint__"] = __wrapper_length_hint__
|
|
110
122
|
|
|
111
123
|
# Note that not providing compatibility with generator-based coroutines
|
|
112
124
|
# (PEP 342) here as they are removed in Python 3.11+ and were deprecated
|
|
@@ -132,7 +144,7 @@ class AutoObjectProxy(BaseObjectProxy):
|
|
|
132
144
|
if cls is AutoObjectProxy:
|
|
133
145
|
name = BaseObjectProxy.__name__
|
|
134
146
|
|
|
135
|
-
return super().__new__(type(name, (cls,), namespace))
|
|
147
|
+
return super(AutoObjectProxy, cls).__new__(type(name, (cls,), namespace))
|
|
136
148
|
|
|
137
149
|
def __wrapped_setattr_fixups__(self):
|
|
138
150
|
"""Adjusts special dunder methods on the class as needed based on the
|
|
@@ -174,8 +186,8 @@ class AutoObjectProxy(BaseObjectProxy):
|
|
|
174
186
|
|
|
175
187
|
if hasattr(self.__wrapped__, "__length_hint__"):
|
|
176
188
|
if "__length_hint__" not in class_attrs:
|
|
177
|
-
cls.__length_hint__ =
|
|
178
|
-
elif getattr(cls, "__length_hint__", None) is
|
|
189
|
+
cls.__length_hint__ = __wrapper_length_hint__
|
|
190
|
+
elif getattr(cls, "__length_hint__", None) is __wrapper_length_hint__:
|
|
179
191
|
delattr(cls, "__length_hint__")
|
|
180
192
|
|
|
181
193
|
if hasattr(self.__wrapped__, "__await__"):
|
|
@@ -214,10 +226,64 @@ class LazyObjectProxy(AutoObjectProxy):
|
|
|
214
226
|
when it is first needed.
|
|
215
227
|
"""
|
|
216
228
|
|
|
217
|
-
def __new__(cls, callback=None):
|
|
218
|
-
|
|
229
|
+
def __new__(cls, callback=None, *, interface=...):
|
|
230
|
+
"""Injects special dunder methods into a dynamically created subclass
|
|
231
|
+
as needed based on the wrapped object.
|
|
232
|
+
"""
|
|
233
|
+
|
|
234
|
+
if interface is ...:
|
|
235
|
+
interface = type(None)
|
|
236
|
+
|
|
237
|
+
namespace = {}
|
|
238
|
+
|
|
239
|
+
interface_attrs = dir(interface)
|
|
240
|
+
class_attrs = set(dir(cls))
|
|
241
|
+
|
|
242
|
+
if "__call__" in interface_attrs and "__call__" not in class_attrs:
|
|
243
|
+
namespace["__call__"] = __wrapper_call__
|
|
244
|
+
|
|
245
|
+
if "__iter__" in interface_attrs and "__iter__" not in class_attrs:
|
|
246
|
+
namespace["__iter__"] = __wrapper_iter__
|
|
247
|
+
|
|
248
|
+
if "__next__" in interface_attrs and "__next__" not in class_attrs:
|
|
249
|
+
namespace["__next__"] = __wrapper_next__
|
|
250
|
+
|
|
251
|
+
if "__aiter__" in interface_attrs and "__aiter__" not in class_attrs:
|
|
252
|
+
namespace["__aiter__"] = __wrapper_aiter__
|
|
253
|
+
|
|
254
|
+
if "__anext__" in interface_attrs and "__anext__" not in class_attrs:
|
|
255
|
+
namespace["__anext__"] = __wrapper_anext__
|
|
256
|
+
|
|
257
|
+
if (
|
|
258
|
+
"__length_hint__" in interface_attrs
|
|
259
|
+
and "__length_hint__" not in class_attrs
|
|
260
|
+
):
|
|
261
|
+
namespace["__length_hint__"] = __wrapper_length_hint__
|
|
219
262
|
|
|
220
|
-
|
|
263
|
+
# Note that not providing compatibility with generator-based coroutines
|
|
264
|
+
# (PEP 342) here as they are removed in Python 3.11+ and were deprecated
|
|
265
|
+
# in 3.8.
|
|
266
|
+
|
|
267
|
+
if "__await__" in interface_attrs and "__await__" not in class_attrs:
|
|
268
|
+
namespace["__await__"] = __wrapper_await__
|
|
269
|
+
|
|
270
|
+
if "__get__" in interface_attrs and "__get__" not in class_attrs:
|
|
271
|
+
namespace["__get__"] = __wrapper_get__
|
|
272
|
+
|
|
273
|
+
if "__set__" in interface_attrs and "__set__" not in class_attrs:
|
|
274
|
+
namespace["__set__"] = __wrapper_set__
|
|
275
|
+
|
|
276
|
+
if "__delete__" in interface_attrs and "__delete__" not in class_attrs:
|
|
277
|
+
namespace["__delete__"] = __wrapper_delete__
|
|
278
|
+
|
|
279
|
+
if "__set_name__" in interface_attrs and "__set_name__" not in class_attrs:
|
|
280
|
+
namespace["__set_name__"] = __wrapper_set_name__
|
|
281
|
+
|
|
282
|
+
name = cls.__name__
|
|
283
|
+
|
|
284
|
+
return super(AutoObjectProxy, cls).__new__(type(name, (cls,), namespace))
|
|
285
|
+
|
|
286
|
+
def __init__(self, callback=None, *, interface=...):
|
|
221
287
|
"""Initialize the object proxy with wrapped object as `None` but due
|
|
222
288
|
to presence of special `__wrapped_factory__` attribute addded first,
|
|
223
289
|
this will actually trigger the deferred creation of the wrapped object
|
|
@@ -229,6 +295,8 @@ class LazyObjectProxy(AutoObjectProxy):
|
|
|
229
295
|
|
|
230
296
|
super().__init__(None)
|
|
231
297
|
|
|
298
|
+
__wrapped_initialized__ = False
|
|
299
|
+
|
|
232
300
|
def __wrapped_factory__(self):
|
|
233
301
|
return None
|
|
234
302
|
|
|
@@ -247,20 +315,37 @@ class LazyObjectProxy(AutoObjectProxy):
|
|
|
247
315
|
# If it is then just return it, otherwise call the factory to
|
|
248
316
|
# create it.
|
|
249
317
|
|
|
250
|
-
|
|
251
|
-
return
|
|
252
|
-
except AttributeError:
|
|
253
|
-
pass
|
|
318
|
+
if self.__wrapped_initialized__:
|
|
319
|
+
return self.__wrapped__
|
|
254
320
|
|
|
255
321
|
self.__wrapped__ = self.__wrapped_factory__()
|
|
256
322
|
|
|
323
|
+
self.__wrapped_initialized__ = True
|
|
324
|
+
|
|
257
325
|
return self.__wrapped__
|
|
258
326
|
|
|
259
327
|
|
|
260
|
-
def lazy_import(name):
|
|
261
|
-
"""Lazily imports
|
|
262
|
-
import the module when it is first needed. When name is a dotted name,
|
|
263
|
-
then the full dotted name is imported and the last
|
|
328
|
+
def lazy_import(name, attribute=None, *, interface=...):
|
|
329
|
+
"""Lazily imports the module `name`, returning a `LazyObjectProxy` which
|
|
330
|
+
will import the module when it is first needed. When `name is a dotted name,
|
|
331
|
+
then the full dotted name is imported and the last module is taken as the
|
|
332
|
+
target. If `attribute` is provided then it is used to retrieve an attribute
|
|
333
|
+
from the module.
|
|
264
334
|
"""
|
|
265
335
|
|
|
266
|
-
|
|
336
|
+
if attribute is not None:
|
|
337
|
+
if interface is ...:
|
|
338
|
+
interface = Callable
|
|
339
|
+
else:
|
|
340
|
+
if interface is ...:
|
|
341
|
+
interface = ModuleType
|
|
342
|
+
|
|
343
|
+
def _import():
|
|
344
|
+
module = __import__(name, fromlist=[""])
|
|
345
|
+
|
|
346
|
+
if attribute is not None:
|
|
347
|
+
return getattr(module, attribute)
|
|
348
|
+
|
|
349
|
+
return module
|
|
350
|
+
|
|
351
|
+
return LazyObjectProxy(_import, interface=interface)
|
wrapt/wrappers.py
CHANGED
|
@@ -125,6 +125,10 @@ class ObjectProxy(with_metaclass(_ObjectProxyMetaType)): # type: ignore[misc]
|
|
|
125
125
|
except AttributeError:
|
|
126
126
|
pass
|
|
127
127
|
|
|
128
|
+
@property
|
|
129
|
+
def __object_proxy__(self):
|
|
130
|
+
return ObjectProxy
|
|
131
|
+
|
|
128
132
|
def __self_setattr__(self, name, value):
|
|
129
133
|
object.__setattr__(self, name, value)
|
|
130
134
|
|
|
@@ -364,52 +368,90 @@ class ObjectProxy(with_metaclass(_ObjectProxyMetaType)): # type: ignore[misc]
|
|
|
364
368
|
return other | self.__wrapped__
|
|
365
369
|
|
|
366
370
|
def __iadd__(self, other):
|
|
367
|
-
self.__wrapped__
|
|
368
|
-
|
|
371
|
+
if hasattr(self.__wrapped__, "__iadd__"):
|
|
372
|
+
self.__wrapped__ += other
|
|
373
|
+
return self
|
|
374
|
+
else:
|
|
375
|
+
return self.__object_proxy__(self.__wrapped__ + other)
|
|
369
376
|
|
|
370
377
|
def __isub__(self, other):
|
|
371
|
-
self.__wrapped__
|
|
372
|
-
|
|
378
|
+
if hasattr(self.__wrapped__, "__isub__"):
|
|
379
|
+
self.__wrapped__ -= other
|
|
380
|
+
return self
|
|
381
|
+
else:
|
|
382
|
+
return self.__object_proxy__(self.__wrapped__ - other)
|
|
373
383
|
|
|
374
384
|
def __imul__(self, other):
|
|
375
|
-
self.__wrapped__
|
|
376
|
-
|
|
385
|
+
if hasattr(self.__wrapped__, "__imul__"):
|
|
386
|
+
self.__wrapped__ *= other
|
|
387
|
+
return self
|
|
388
|
+
else:
|
|
389
|
+
return self.__object_proxy__(self.__wrapped__ * other)
|
|
377
390
|
|
|
378
391
|
def __itruediv__(self, other):
|
|
379
|
-
|
|
380
|
-
|
|
392
|
+
if hasattr(self.__wrapped__, "__itruediv__"):
|
|
393
|
+
self.__wrapped__ /= other
|
|
394
|
+
return self
|
|
395
|
+
else:
|
|
396
|
+
return self.__object_proxy__(self.__wrapped__ / other)
|
|
381
397
|
|
|
382
398
|
def __ifloordiv__(self, other):
|
|
383
|
-
self.__wrapped__
|
|
384
|
-
|
|
399
|
+
if hasattr(self.__wrapped__, "__ifloordiv__"):
|
|
400
|
+
self.__wrapped__ //= other
|
|
401
|
+
return self
|
|
402
|
+
else:
|
|
403
|
+
return self.__object_proxy__(self.__wrapped__ // other)
|
|
385
404
|
|
|
386
405
|
def __imod__(self, other):
|
|
387
|
-
self.__wrapped__
|
|
406
|
+
if hasattr(self.__wrapped__, "__imod__"):
|
|
407
|
+
self.__wrapped__ %= other
|
|
408
|
+
return self
|
|
409
|
+
else:
|
|
410
|
+
return self.__object_proxy__(self.__wrapped__ % other)
|
|
411
|
+
|
|
388
412
|
return self
|
|
389
413
|
|
|
390
414
|
def __ipow__(self, other): # type: ignore[misc]
|
|
391
|
-
self.__wrapped__
|
|
392
|
-
|
|
415
|
+
if hasattr(self.__wrapped__, "__ipow__"):
|
|
416
|
+
self.__wrapped__ **= other
|
|
417
|
+
return self
|
|
418
|
+
else:
|
|
419
|
+
return self.__object_proxy__(self.__wrapped__**other)
|
|
393
420
|
|
|
394
421
|
def __ilshift__(self, other):
|
|
395
|
-
self.__wrapped__
|
|
396
|
-
|
|
422
|
+
if hasattr(self.__wrapped__, "__ilshift__"):
|
|
423
|
+
self.__wrapped__ <<= other
|
|
424
|
+
return self
|
|
425
|
+
else:
|
|
426
|
+
return self.__object_proxy__(self.__wrapped__ << other)
|
|
397
427
|
|
|
398
428
|
def __irshift__(self, other):
|
|
399
|
-
self.__wrapped__
|
|
400
|
-
|
|
429
|
+
if hasattr(self.__wrapped__, "__irshift__"):
|
|
430
|
+
self.__wrapped__ >>= other
|
|
431
|
+
return self
|
|
432
|
+
else:
|
|
433
|
+
return self.__object_proxy__(self.__wrapped__ >> other)
|
|
401
434
|
|
|
402
435
|
def __iand__(self, other):
|
|
403
|
-
self.__wrapped__
|
|
404
|
-
|
|
436
|
+
if hasattr(self.__wrapped__, "__iand__"):
|
|
437
|
+
self.__wrapped__ &= other
|
|
438
|
+
return self
|
|
439
|
+
else:
|
|
440
|
+
return self.__object_proxy__(self.__wrapped__ & other)
|
|
405
441
|
|
|
406
442
|
def __ixor__(self, other):
|
|
407
|
-
self.__wrapped__
|
|
408
|
-
|
|
443
|
+
if hasattr(self.__wrapped__, "__ixor__"):
|
|
444
|
+
self.__wrapped__ ^= other
|
|
445
|
+
return self
|
|
446
|
+
else:
|
|
447
|
+
return self.__object_proxy__(self.__wrapped__ ^ other)
|
|
409
448
|
|
|
410
449
|
def __ior__(self, other):
|
|
411
|
-
self.__wrapped__
|
|
412
|
-
|
|
450
|
+
if hasattr(self.__wrapped__, "__ior__"):
|
|
451
|
+
self.__wrapped__ |= other
|
|
452
|
+
return self
|
|
453
|
+
else:
|
|
454
|
+
return self.__object_proxy__(self.__wrapped__ | other)
|
|
413
455
|
|
|
414
456
|
def __neg__(self):
|
|
415
457
|
return -self.__wrapped__
|
|
@@ -448,8 +490,11 @@ class ObjectProxy(with_metaclass(_ObjectProxyMetaType)): # type: ignore[misc]
|
|
|
448
490
|
return other @ self.__wrapped__
|
|
449
491
|
|
|
450
492
|
def __imatmul__(self, other):
|
|
451
|
-
self.__wrapped__
|
|
452
|
-
|
|
493
|
+
if hasattr(self.__wrapped__, "__imatmul__"):
|
|
494
|
+
self.__wrapped__ @= other
|
|
495
|
+
return self
|
|
496
|
+
else:
|
|
497
|
+
return self.__object_proxy__(self.__wrapped__ @ other)
|
|
453
498
|
|
|
454
499
|
def __len__(self):
|
|
455
500
|
return len(self.__wrapped__)
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: wrapt
|
|
3
|
+
Version: 2.0.1
|
|
4
|
+
Summary: Module for decorators, wrappers and monkey patching.
|
|
5
|
+
Home-page: https://github.com/GrahamDumpleton/wrapt
|
|
6
|
+
Author: Graham Dumpleton
|
|
7
|
+
Author-email: Graham Dumpleton <Graham.Dumpleton@gmail.com>
|
|
8
|
+
License: Copyright (c) 2013-2025, Graham Dumpleton
|
|
9
|
+
All rights reserved.
|
|
10
|
+
|
|
11
|
+
Redistribution and use in source and binary forms, with or without
|
|
12
|
+
modification, are permitted provided that the following conditions are met:
|
|
13
|
+
|
|
14
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
15
|
+
list of conditions and the following disclaimer.
|
|
16
|
+
|
|
17
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
18
|
+
this list of conditions and the following disclaimer in the documentation
|
|
19
|
+
and/or other materials provided with the distribution.
|
|
20
|
+
|
|
21
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
22
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
23
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
24
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
25
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
26
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
27
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
28
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
29
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
30
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
31
|
+
POSSIBILITY OF SUCH DAMAGE.
|
|
32
|
+
|
|
33
|
+
Project-URL: Homepage, https://github.com/GrahamDumpleton/wrapt
|
|
34
|
+
Project-URL: Bug Tracker, https://github.com/GrahamDumpleton/wrapt/issues/
|
|
35
|
+
Project-URL: Changelog, https://wrapt.readthedocs.io/en/latest/changes.html
|
|
36
|
+
Project-URL: Documentation, https://wrapt.readthedocs.io/
|
|
37
|
+
Keywords: wrapper,proxy,decorator
|
|
38
|
+
Platform: any
|
|
39
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
40
|
+
Classifier: Programming Language :: Python :: 3
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
42
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
43
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
44
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
45
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
46
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
47
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
48
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
49
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
50
|
+
Requires-Python: >=3.8
|
|
51
|
+
Description-Content-Type: text/x-rst
|
|
52
|
+
License-File: LICENSE
|
|
53
|
+
Provides-Extra: dev
|
|
54
|
+
Requires-Dist: pytest; extra == "dev"
|
|
55
|
+
Requires-Dist: setuptools; extra == "dev"
|
|
56
|
+
Dynamic: license-file
|
|
57
|
+
|
|
58
|
+
wrapt
|
|
59
|
+
=====
|
|
60
|
+
|
|
61
|
+
|PyPI| |Documentation|
|
|
62
|
+
|
|
63
|
+
A Python module for decorators, wrappers and monkey patching.
|
|
64
|
+
|
|
65
|
+
Overview
|
|
66
|
+
--------
|
|
67
|
+
|
|
68
|
+
The **wrapt** module provides a transparent object proxy for Python, which can be used as the basis for the construction of function wrappers and decorator functions.
|
|
69
|
+
|
|
70
|
+
The **wrapt** module focuses very much on correctness. It goes way beyond existing mechanisms such as ``functools.wraps()`` to ensure that decorators preserve introspectability, signatures, type checking abilities etc. The decorators that can be constructed using this module will work in far more scenarios than typical decorators and provide more predictable and consistent behaviour.
|
|
71
|
+
|
|
72
|
+
To ensure that the overhead is as minimal as possible, a C extension module is used for performance critical components. An automatic fallback to a pure Python implementation is also provided where a target system does not have a compiler to allow the C extension to be compiled.
|
|
73
|
+
|
|
74
|
+
Key Features
|
|
75
|
+
------------
|
|
76
|
+
|
|
77
|
+
* **Universal decorators** that work with functions, methods, classmethods, staticmethods, and classes
|
|
78
|
+
* **Transparent object proxies** for advanced wrapping scenarios
|
|
79
|
+
* **Monkey patching utilities** for safe runtime modifications
|
|
80
|
+
* **C extension** for optimal performance with Python fallback
|
|
81
|
+
* **Comprehensive introspection preservation** (signatures, annotations, etc.)
|
|
82
|
+
* **Thread-safe decorator implementations**
|
|
83
|
+
|
|
84
|
+
Installation
|
|
85
|
+
------------
|
|
86
|
+
|
|
87
|
+
Install from PyPI using pip::
|
|
88
|
+
|
|
89
|
+
pip install wrapt
|
|
90
|
+
|
|
91
|
+
Supported Python Versions
|
|
92
|
+
--------------------------
|
|
93
|
+
|
|
94
|
+
* Python 3.8+
|
|
95
|
+
* CPython and PyPy implementations
|
|
96
|
+
|
|
97
|
+
Documentation
|
|
98
|
+
-------------
|
|
99
|
+
|
|
100
|
+
For comprehensive documentation, examples, and advanced usage patterns, visit:
|
|
101
|
+
|
|
102
|
+
* https://wrapt.readthedocs.io/
|
|
103
|
+
|
|
104
|
+
Quick Start
|
|
105
|
+
-----------
|
|
106
|
+
|
|
107
|
+
To implement your decorator you need to first define a wrapper function.
|
|
108
|
+
This will be called each time a decorated function is called. The wrapper
|
|
109
|
+
function needs to take four positional arguments:
|
|
110
|
+
|
|
111
|
+
* ``wrapped`` - The wrapped function which in turns needs to be called by your wrapper function.
|
|
112
|
+
* ``instance`` - The object to which the wrapped function was bound when it was called.
|
|
113
|
+
* ``args`` - The list of positional arguments supplied when the decorated function was called.
|
|
114
|
+
* ``kwargs`` - The dictionary of keyword arguments supplied when the decorated function was called.
|
|
115
|
+
|
|
116
|
+
The wrapper function would do whatever it needs to, but would usually in
|
|
117
|
+
turn call the wrapped function that is passed in via the ``wrapped``
|
|
118
|
+
argument.
|
|
119
|
+
|
|
120
|
+
The decorator ``@wrapt.decorator`` then needs to be applied to the wrapper
|
|
121
|
+
function to convert it into a decorator which can in turn be applied to
|
|
122
|
+
other functions.
|
|
123
|
+
|
|
124
|
+
.. code-block:: python
|
|
125
|
+
|
|
126
|
+
import wrapt
|
|
127
|
+
|
|
128
|
+
@wrapt.decorator
|
|
129
|
+
def pass_through(wrapped, instance, args, kwargs):
|
|
130
|
+
return wrapped(*args, **kwargs)
|
|
131
|
+
|
|
132
|
+
@pass_through
|
|
133
|
+
def function():
|
|
134
|
+
pass
|
|
135
|
+
|
|
136
|
+
If you wish to implement a decorator which accepts arguments, then wrap the
|
|
137
|
+
definition of the decorator in a function closure. Any arguments supplied
|
|
138
|
+
to the outer function when the decorator is applied, will be available to
|
|
139
|
+
the inner wrapper when the wrapped function is called.
|
|
140
|
+
|
|
141
|
+
.. code-block:: python
|
|
142
|
+
|
|
143
|
+
import wrapt
|
|
144
|
+
|
|
145
|
+
def with_arguments(myarg1, myarg2):
|
|
146
|
+
@wrapt.decorator
|
|
147
|
+
def wrapper(wrapped, instance, args, kwargs):
|
|
148
|
+
return wrapped(*args, **kwargs)
|
|
149
|
+
return wrapper
|
|
150
|
+
|
|
151
|
+
@with_arguments(1, 2)
|
|
152
|
+
def function():
|
|
153
|
+
pass
|
|
154
|
+
|
|
155
|
+
When applied to a normal function or static method, the wrapper function
|
|
156
|
+
when called will be passed ``None`` as the ``instance`` argument.
|
|
157
|
+
|
|
158
|
+
When applied to an instance method, the wrapper function when called will
|
|
159
|
+
be passed the instance of the class the method is being called on as the
|
|
160
|
+
``instance`` argument. This will be the case even when the instance method
|
|
161
|
+
was called explicitly via the class and the instance passed as the first
|
|
162
|
+
argument. That is, the instance will never be passed as part of ``args``.
|
|
163
|
+
|
|
164
|
+
When applied to a class method, the wrapper function when called will be
|
|
165
|
+
passed the class type as the ``instance`` argument.
|
|
166
|
+
|
|
167
|
+
When applied to a class, the wrapper function when called will be passed
|
|
168
|
+
``None`` as the ``instance`` argument. The ``wrapped`` argument in this
|
|
169
|
+
case will be the class.
|
|
170
|
+
|
|
171
|
+
The above rules can be summarised with the following example.
|
|
172
|
+
|
|
173
|
+
.. code-block:: python
|
|
174
|
+
|
|
175
|
+
import inspect
|
|
176
|
+
|
|
177
|
+
@wrapt.decorator
|
|
178
|
+
def universal(wrapped, instance, args, kwargs):
|
|
179
|
+
if instance is None:
|
|
180
|
+
if inspect.isclass(wrapped):
|
|
181
|
+
# Decorator was applied to a class.
|
|
182
|
+
return wrapped(*args, **kwargs)
|
|
183
|
+
else:
|
|
184
|
+
# Decorator was applied to a function or staticmethod.
|
|
185
|
+
return wrapped(*args, **kwargs)
|
|
186
|
+
else:
|
|
187
|
+
if inspect.isclass(instance):
|
|
188
|
+
# Decorator was applied to a classmethod.
|
|
189
|
+
return wrapped(*args, **kwargs)
|
|
190
|
+
else:
|
|
191
|
+
# Decorator was applied to an instancemethod.
|
|
192
|
+
return wrapped(*args, **kwargs)
|
|
193
|
+
|
|
194
|
+
Using these checks it is therefore possible to create a universal decorator
|
|
195
|
+
that can be applied in all situations. It is no longer necessary to create
|
|
196
|
+
different variants of decorators for normal functions and instance methods,
|
|
197
|
+
or use additional wrappers to convert a function decorator into one that
|
|
198
|
+
will work for instance methods.
|
|
199
|
+
|
|
200
|
+
In all cases, the wrapped function passed to the wrapper function is called
|
|
201
|
+
in the same way, with ``args`` and ``kwargs`` being passed. The
|
|
202
|
+
``instance`` argument doesn't need to be used in calling the wrapped
|
|
203
|
+
function.
|
|
204
|
+
|
|
205
|
+
Links
|
|
206
|
+
-----
|
|
207
|
+
|
|
208
|
+
* **Documentation**: https://wrapt.readthedocs.io/
|
|
209
|
+
* **Source Code**: https://github.com/GrahamDumpleton/wrapt
|
|
210
|
+
* **Bug Reports**: https://github.com/GrahamDumpleton/wrapt/issues/
|
|
211
|
+
* **Changelog**: https://wrapt.readthedocs.io/en/latest/changes.html
|
|
212
|
+
|
|
213
|
+
.. |PyPI| image:: https://img.shields.io/pypi/v/wrapt.svg?logo=python&cacheSeconds=3600
|
|
214
|
+
:target: https://pypi.python.org/pypi/wrapt
|
|
215
|
+
.. |Documentation| image:: https://img.shields.io/badge/docs-wrapt.readthedocs.io-blue.svg
|
|
216
|
+
:target: https://wrapt.readthedocs.io/
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
wrapt/__init__.py,sha256=bAF7Gzqnf_vUBpf5KNFfIm5P9Drm7M0WYlt22W25Q4Y,1540
|
|
2
|
+
wrapt/__init__.pyi,sha256=d3CBY392zYAgBJoUeaRzdOUDbwlHgCxiLROnd4LdSG8,9402
|
|
3
|
+
wrapt/__wrapt__.py,sha256=E_Yo7fIk51_OjMzlqXKtIhvplbYbJIPcTGD20b0GWM8,1431
|
|
4
|
+
wrapt/_wrappers.c,sha256=ux1b_-Me_8QpJ_QRWvCq9jt_1CZ-9TFA-cVSLqi4_Zg,115800
|
|
5
|
+
wrapt/_wrappers.cpython-313-riscv64-linux-musl.so,sha256=i8fjISsthsG5J7nxSb-X4oOHU7OVGo3ZR4Ehi3QlEGg,215560
|
|
6
|
+
wrapt/arguments.py,sha256=q4bxH7GoCXhTCgxy-AEyvSnOq0ovMSHjN7ru3HWxlhA,2548
|
|
7
|
+
wrapt/decorators.py,sha256=ePWsg43Q5uHYSBGvbybwIZpsYdIhLGX9twbGZ7ygous,21091
|
|
8
|
+
wrapt/importer.py,sha256=E16XxhomZuX5jM_JEH4ZO-MYpNou1t5RZLJHWLCtsgM,12135
|
|
9
|
+
wrapt/patches.py,sha256=WJAKwOEeozpqgLAq_BlEu2HWbjMg9yaR65szi8J4GRQ,9907
|
|
10
|
+
wrapt/proxies.py,sha256=0-N74mBM3tsC_zEQ83to3Y5OMqdxEu4BhXs3Hq1GDCU,12517
|
|
11
|
+
wrapt/py.typed,sha256=la67KBlbjXN-_-DfGNcdOcjYumVpKG_Tkw-8n5dnGB4,8
|
|
12
|
+
wrapt/weakrefs.py,sha256=5HehYcKOKsRIghxLwnCZ4EJ67rhc0z1GH__pRILRLDc,4630
|
|
13
|
+
wrapt/wrappers.py,sha256=btpuNklFXdpnWlQVrgib1cY08M5tm1vTSd_XEjfEgxs,34439
|
|
14
|
+
wrapt-2.0.1.dist-info/METADATA,sha256=nbszIjJc0vsh_DVij7b9ct6R9G2VCzVTTPSke5imlvs,8979
|
|
15
|
+
wrapt-2.0.1.dist-info/WHEEL,sha256=4lZF3a-99kvbRXRFbWdTzxXghO36qYYRQD9SUY4TKXE,113
|
|
16
|
+
wrapt-2.0.1.dist-info/top_level.txt,sha256=Jf7kcuXtwjUJMwOL0QzALDg2WiSiXiH9ThKMjN64DW0,6
|
|
17
|
+
wrapt-2.0.1.dist-info/RECORD,,
|
|
18
|
+
wrapt-2.0.1.dist-info/licenses/LICENSE,sha256=mrxBqmuGkMB-3ptEt8YjPQFCkO0eO1zMN-KSKVtdBY8,1304
|