FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-44730: Add handling of generator functions by gmjw · Pull Request #27315 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
24 changes: 22 additions & 2 deletions Lib/unittest/mock.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,8 @@ def __call__(self, func):
return self.decorate_class(func)
if inspect.iscoroutinefunction(func):
return self.decorate_async_callable(func)
if inspect.isgeneratorfunction(func):
return self.decorate_generator_callable(func)
return self.decorate_callable(func)


Expand Down Expand Up @@ -1342,8 +1344,26 @@ def decoration_helper(self, patched, args, keywargs):
yield (args, keywargs)


def decorate_generator_callable(self, func):
# NB. Keep the method in sync with decorate_async_callable() and decorate_callable()
if hasattr(func, 'patchings'):
func.patchings.append(self)
return func

@wraps(func)
def patched(*args, **keywargs):
with self.decoration_helper(patched,
args,
keywargs) as (newargs, newkeywargs):
out = yield from func(*newargs, **newkeywargs)
return out

patched.patchings = [self]
return patched


def decorate_callable(self, func):
# NB. Keep the method in sync with decorate_async_callable()
# NB. Keep the method in sync with decorate_async_callable() and decorate_generator_callable()
if hasattr(func, 'patchings'):
func.patchings.append(self)
return func
Expand All @@ -1360,7 +1380,7 @@ def patched(*args, **keywargs):


def decorate_async_callable(self, func):
# NB. Keep the method in sync with decorate_callable()
# NB. Keep the method in sync with decorate_callable() and decorate_generator_callable()
if hasattr(func, 'patchings'):
func.patchings.append(self)
return func
Expand Down
32 changes: 32 additions & 0 deletions Lib/unittest/test/testmock/testpatch.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,38 @@ def test_cant_set_kwargs_when_passing_a_mock(self):
def test(): pass
with self.assertRaises(TypeError):
test()

def test_patch_generator_function(self):
"""
Previously patching a generator function did
not return a generator function, and indeed
the desired patch was not even applied.

This test shows that the behaviour is now
fixed.
"""
list_being_tested = [0, 1]

def alternative_len(l):
return 666

patch_len = patch('%s.len' % builtin_string, alternative_len)

@patch_len
def example_normal_function():
""" A normal function using `return` """
return len(list_being_tested)

@patch_len
def example_generator_function():
""" A generator function """
yield len(list_being_tested)

out_normal = example_normal_function()
out_generator = list(example_generator_function())

self.assertTrue(out_normal == 666)
self.assertTrue(out_generator == [666])


if __name__ == '__main__':
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow unittest.mock.patch to patch generator functions

Back | FazBrowse Home | New Git URL