Skip to content

Commit

Permalink
make sure patch.dict() can be applied on async functions
Browse files Browse the repository at this point in the history
Code is from gh#python/cpython@88b101f, it was released
upstream in 3.10.9.

Fixes: gh#python#98086
Patch: 99366-patch.dict-can-decorate-async.patch
  • Loading branch information
mcepl committed Apr 4, 2024
1 parent abf062d commit b53141d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,12 @@ def __init__(self, in_dict, values=(), clear=False, **kwargs):
def __call__(self, f):
if isinstance(f, type):
return self.decorate_class(f)
if inspect.iscoroutinefunction(f):
return self.decorate_async_callable(f)
return self.decorate_callable(f)


def decorate_callable(self, f):
@wraps(f)
def _inner(*args, **kw):
self._patch_dict()
Expand All @@ -1606,6 +1612,18 @@ def _inner(*args, **kw):
return _inner


def decorate_async_callable(self, f):
@wraps(f)
async def _inner(*args, **kw):
self._patch_dict()
try:
return await f(*args, **kw)
finally:
self._unpatch_dict()

return _inner


def decorate_class(self, klass):
for attr in dir(klass):
attr_value = getattr(klass, attr)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make sure ``patch.dict()`` can be applied on async functions.

0 comments on commit b53141d

Please sign in to comment.