-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-44730: Add handling of generator functions #27315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Previously generator functions were changed by patches applied as decorators, ending up as non-generator functions (co_flags changed from 31 to 99). Patches were not applied properly in these generator functions. Making the change here seems to address this.
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA). CLA MissingOur records indicate the following people have not signed the CLA: For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. If you have recently signed the CLA, please wait at least one business day You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
I would be happy to flesh these changes out with a test or two, of course - there is an example associated with the bug report which could easily be made into a simple unit test. I've never contributed to the repo before so I'll await some general feedback before proceeding with anything. |
This PR is stale because it has been open for 30 days with no activity. |
Lib/unittest/mock.py
Outdated
with self.decoration_helper(patched, | ||
args, | ||
keywargs) as (newargs, newkeywargs): | ||
yield from func(*newargs, **newkeywargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't preserve the original characteristics. For example on the following function
def gen_1():
yield 1
return 2
this will simply raise StopIteration(None)
instead of StopIteration(2)
at the end. You should return the result of the generator (see PEP 380)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're absolutely right. Thanks, I forgot that generator functions could also return values.
I can't get this to run correctly: ./python -m unittest Lib/unittest/test/testmock/testpatch.py During handling of the above exception, another exception occurred: Traceback (most recent call last): cpython on gmjw-patch-generator-functions-1 [$?] via 🐍 v3.11.0a6+ How to you test testpatch? |
Previously generator functions were changed by patches applied as decorators, ending up as non-generator functions (co_flags changed from 99 to 31). Patches were not applied properly in these generator functions.
This PR addresses this by adding a simple switch in mock.py to handle generator functions cleanly.
https://bugs.python.org/issue44730