Hear no evil, see no evil, patch no evil: Or, how to monkey-patch safely.
summary
Claims that the wrapt library fixes decorator and monkey‑patching pitfalls by preserving introspection, honoring the descriptor protocol and offering a C-backed fast path; demonstrates a synchronized universal decorator, transparent object proxy for per‑instance overrides, transient patches for tests, and a post-import hook system plus an auto-wrapped bootstrap (via setuptools entry points and a .pth hack) to apply patches before modules are used — code exists, is used for instrumentation, and the library works in production.
AI-written summary · about
chapters (21)
- 0:00Intro
- 1:18Typical decorator
- 2:38Complicated details removed ....
- 3:23Basic decorator.
- 3:57Universal decorator.
- 5:18Synchronise function calls.
- 6:05Methods of classes as well.
- 6:22Synchronise block of code.
- 7:53Before decorators.
- 9:10Why monkey patch?
- 10:21Monkey patching with wrapt.
- 10:45Don't patch it yourself.
- 11:46Avoiding imports.
- 13:06Mock alternative.
- 14:11Transparent object proxy.
- 15:46Import from module.
- 18:19Post import hooks (PEP 369).
- 20:02The autowrapt package.
- 21:02Patch to time functions.
- 22:01Enabling patches.
- 25:48Reasons to use wrapt.
related talks
03mLightning Talk: Python Decorators
27mI Dance with Your Shadow: Patch, Stub, Mock
45mDon't Live Patch Your CPython Interpreter
20mMock, patch - when and how to use
31mWhat to Do When the Bug Is in Someone Else's Code
37mDemystifying the Patch Function
24m"A Confession of My Python Sins"
30mMonkey patching
