summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2014-07-05 00:26:22 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2014-07-05 00:26:22 (GMT)
commita93342b8b2aeb0332de9ea0880d4592824467f3f (patch)
tree189d7e7f1120d30ab29c82bc01b9c95ca73993c0 /Lib/test
parent92e8deebecd5e5d754a523864b2cb2c70fb87866 (diff)
parentacc8cf2cfa18c445e8c9b324206eb7c5e392bcb7 (diff)
downloadcpython-a93342b8b2aeb0332de9ea0880d4592824467f3f.zip
cpython-a93342b8b2aeb0332de9ea0880d4592824467f3f.tar.gz
cpython-a93342b8b2aeb0332de9ea0880d4592824467f3f.tar.bz2
Issue #21897: Fix a crash with the f_locals attribute with closure variables when frame.clear() has been called.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_frame.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/Lib/test/test_frame.py b/Lib/test/test_frame.py
index 2dd5780..c402ec3 100644
--- a/Lib/test/test_frame.py
+++ b/Lib/test/test_frame.py
@@ -1,5 +1,6 @@
import gc
import sys
+import types
import unittest
import weakref
@@ -109,6 +110,57 @@ class ClearTest(unittest.TestCase):
self.assertIs(None, wr())
+class FrameLocalsTest(unittest.TestCase):
+ """
+ Tests for the .f_locals attribute.
+ """
+
+ def make_frames(self):
+ def outer():
+ x = 5
+ y = 6
+ def inner():
+ z = x + 2
+ 1/0
+ t = 9
+ return inner()
+ try:
+ outer()
+ except ZeroDivisionError as e:
+ tb = e.__traceback__
+ frames = []
+ while tb:
+ frames.append(tb.tb_frame)
+ tb = tb.tb_next
+ return frames
+
+ def test_locals(self):
+ f, outer, inner = self.make_frames()
+ outer_locals = outer.f_locals
+ self.assertIsInstance(outer_locals.pop('inner'), types.FunctionType)
+ self.assertEqual(outer_locals, {'x': 5, 'y': 6})
+ inner_locals = inner.f_locals
+ self.assertEqual(inner_locals, {'x': 5, 'z': 7})
+
+ def test_clear_locals(self):
+ # Test f_locals after clear() (issue #21897)
+ f, outer, inner = self.make_frames()
+ outer.clear()
+ inner.clear()
+ self.assertEqual(outer.f_locals, {})
+ self.assertEqual(inner.f_locals, {})
+
+ def test_locals_clear_locals(self):
+ # Test f_locals before and after clear() (to exercise caching)
+ f, outer, inner = self.make_frames()
+ outer.f_locals
+ inner.f_locals
+ outer.clear()
+ inner.clear()
+ self.assertEqual(outer.f_locals, {})
+ self.assertEqual(inner.f_locals, {})
+
+
def test_main():
support.run_unittest(__name__)