[Python-checkins] bpo-30306: release arguments of contextmanager (GH-1500)

Nick Coghlan webhook-mailer at python.org
Sat Jan 27 23:17:49 EST 2018


https://github.com/python/cpython/commit/dd0e087edc8f1e4d2c0913236b1a62a77d9db6d8
commit: dd0e087edc8f1e4d2c0913236b1a62a77d9db6d8
branch: master
author: Martin Teichmann <lkb.teichmann at gmail.com>
committer: Nick Coghlan <ncoghlan at gmail.com>
date: 2018-01-28T14:17:46+10:00
summary:

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.

files:
M Lib/contextlib.py
M Lib/test/test_contextlib.py

diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index ef8f8c9f55bc..1ff8cdf1cecf 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -105,6 +105,9 @@ def _recreate_cm(self):
         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:
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index 96ceaa7f0600..2a44404a603e 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -8,6 +8,7 @@
 import unittest
 from contextlib import *  # Tests __all__
 from test import support
+import weakref
 
 
 class TestAbstractContextManager(unittest.TestCase):
@@ -219,6 +220,52 @@ def woohoo(self, func, args, kwds):
         with woohoo(self=11, func=22, args=33, kwds=44) as target:
             self.assertEqual(target, (11, 22, 33, 44))
 
+    def test_nokeepref(self):
+        class A:
+            pass
+
+        @contextmanager
+        def woohoo(a, b):
+            a = weakref.ref(a)
+            b = weakref.ref(b)
+            self.assertIsNone(a())
+            self.assertIsNone(b())
+            yield
+
+        with woohoo(A(), b=A()):
+            pass
+
+    def test_param_errors(self):
+        @contextmanager
+        def woohoo(a, *, b):
+            yield
+
+        with self.assertRaises(TypeError):
+            woohoo()
+        with self.assertRaises(TypeError):
+            woohoo(3, 5)
+        with self.assertRaises(TypeError):
+            woohoo(b=3)
+
+    def test_recursive(self):
+        depth = 0
+        @contextmanager
+        def woohoo():
+            nonlocal depth
+            before = depth
+            depth += 1
+            yield
+            depth -= 1
+            self.assertEqual(depth, before)
+
+        @woohoo()
+        def recursive():
+            if depth < 10:
+                recursive()
+
+        recursive()
+        self.assertEqual(depth, 0)
+
 
 class ClosingTestCase(unittest.TestCase):
 



More information about the Python-checkins mailing list