-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
bpo-30306: release arguments of contextmanager #1500
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
@tecki, thanks for your PR! By analyzing the history of the files in this pull request, we identified @ncoghlan, @berkerpeksag and @Yhg1s to be potential reviewers. |
Does this change cause problems for recreate_cm?
|
it shouldn't make problems for recreation. At the line you are showing the context manager has not yet been called, so the arguments have not yet been deleted. And once the context manager is actually At least the tests still run, and there are tests for recreating context managers. |
Nice catch, and it makes me pleased I decided exposing To address Raymond's concern, I think a more self-explanatory way of implementing the change would be to explicitly not save the arguments in the cases where we know Suggested setup for that aspect:
And then make clearing the settings in (I'll add some more background and discussion about this on https://bugs.python.org/issue30306 so we have a record of the discussion) |
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.
The request for changes here relates mainly to the structural changes described above, but there's also a minor typo in the new test's helper function name.
Lib/test/test_contextlib.py
Outdated
pass | ||
|
||
@contextmanager | ||
def whohoo(a, b): |
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.
s/whohoo/woohoo/ (Yes, it's just a silly name for a throwaway function, but we may as well spell it correctly and consistently)
I looked at the code more carefully and realized that it is unnecessarily complex. The recreation of the context manager is actually not necessary at all. All we need is to recreate the generator. This can be much more easily done directly in One might notice that I prefixed the variables in |
The generator is created eagerly so that argument/parameter mismatches get reported at the right spot (i.e. when the context manager is created, not when it first gets used). Please don't change that (but feel free to add a comment explaining why it is written that way). |
I changed the implementation to create the generator eagerly as proposed, and added test for it. I think the proposed implementation is pretty clear by now. |
While I haven't tested it, I'm pretty sure the revised implementation of the ContextDecorator support is going to have problems when applied to a recursive function - without an entire new inner CM being created for each call, the exit invocations won't be properly independent. It isn't possible to provide that independence implicitly in If I'm right, that does suggest there's a missing test case for that scenario, though - the current test case just covers reuse without covering recursion. |
So I undid my additional changes, going back to my originally proposed two lines, plus tests added for all the cases that @ncoghlan mentioned. |
The arguments of a function which was declared as a contextmanager are stored inside the context manager, and thus are kept alive. This is a possible unnecessary memory leak.
Branch editing is disabled, so I'll skip the NEWS entry for now, and add it in a follow-up commit. |
NEWS entry PR: #5374 |
The arguments of a function which was declared as a contextmanager
are stored inside the context manager, and thus are kept alive.
This is a possible unnecessary memory leak.
This fixes the problem and adds a test for it. Look at the test to see an example of the problem.
https://bugs.python.org/issue30306