aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Teichmann <lkb.teichmann@gmail.com>2018-01-28 05:17:46 +0100
committerNick Coghlan <ncoghlan@gmail.com>2018-01-28 14:17:46 +1000
commitdd0e087edc8f1e4d2c0913236b1a62a77d9db6d8 (patch)
treeada932bb080ed3a999989e2055e66df581291d4b /Lib/contextlib.py
parentPep 557 What's New (GH-5371) (diff)
downloadcpython-dd0e087edc8f1e4d2c0913236b1a62a77d9db6d8.tar.gz
cpython-dd0e087edc8f1e4d2c0913236b1a62a77d9db6d8.tar.bz2
cpython-dd0e087edc8f1e4d2c0913236b1a62a77d9db6d8.zip
bpo-30306: release arguments of contextmanager (GH-1500)
The arguments to a generator function which is declared as a contextmanager are stored inside the context manager, and thus are kept alive, even when it is used as a regular context manager, and not as a function decorator (where it needs the original arguments to recreate the generator on each call). This is a possible unnecessary memory leak, so this changes contextmanager.__enter__ to release the saved arguments, as that method being called means that particular CM instance isn't going to need to recreate the underlying generator. Patch by Martin Teichmann.
Diffstat (limited to 'Lib/contextlib.py')
-rw-r--r--Lib/contextlib.py3
1 files changed, 3 insertions, 0 deletions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index ef8f8c9f55b..1ff8cdf1cec 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -105,6 +105,9 @@ class _GeneratorContextManager(_GeneratorContextManagerBase,
return self.__class__(self.func, self.args, self.kwds)
def __enter__(self):
+ # do not keep args and kwds alive unnecessarily
+ # they are only needed for recreation, which is not possible anymore
+ del self.args, self.kwds, self.func
try:
return next(self.gen)
except StopIteration: