summaryrefslogtreecommitdiff
path: root/Lib/test/test_scope.py
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2021-06-08 16:01:34 -0600
committerGitHub <noreply@github.com>2021-06-08 16:01:34 -0600
commit3e1c7167d86a2a928cdcb659094aa10bb5550c4c (patch)
treeb3b071ff5636c7c5edd8d536b8732c6a2259a44c /Lib/test/test_scope.py
parentab36b9f83424a020fbd672f218612e6f19257a32 (diff)
downloadcpython-git-3e1c7167d86a2a928cdcb659094aa10bb5550c4c.tar.gz
bpo-43693: Un-revert commit f3fa63e. (#26609)
This was reverted in GH-26596 (commit 6d518bb) due to some bad memory accesses. * Add the MAKE_CELL opcode. (gh-26396) The memory accesses have been fixed. https://bugs.python.org/issue43693
Diffstat (limited to 'Lib/test/test_scope.py')
-rw-r--r--Lib/test/test_scope.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/Lib/test/test_scope.py b/Lib/test/test_scope.py
index 4239b26408..29d60ffe53 100644
--- a/Lib/test/test_scope.py
+++ b/Lib/test/test_scope.py
@@ -176,6 +176,57 @@ class ScopeTests(unittest.TestCase):
self.assertEqual(foo(a=42), 50)
self.assertEqual(foo(), 25)
+ def testCellIsArgAndEscapes(self):
+ # We need to be sure that a cell passed in as an arg still
+ # gets wrapped in a new cell if the arg escapes into an
+ # inner function (closure).
+
+ def external():
+ value = 42
+ def inner():
+ return value
+ cell, = inner.__closure__
+ return cell
+ cell_ext = external()
+
+ def spam(arg):
+ def eggs():
+ return arg
+ return eggs
+
+ eggs = spam(cell_ext)
+ cell_closure, = eggs.__closure__
+ cell_eggs = eggs()
+
+ self.assertIs(cell_eggs, cell_ext)
+ self.assertIsNot(cell_eggs, cell_closure)
+
+ def testCellIsLocalAndEscapes(self):
+ # We need to be sure that a cell bound to a local still
+ # gets wrapped in a new cell if the local escapes into an
+ # inner function (closure).
+
+ def external():
+ value = 42
+ def inner():
+ return value
+ cell, = inner.__closure__
+ return cell
+ cell_ext = external()
+
+ def spam(arg):
+ cell = arg
+ def eggs():
+ return cell
+ return eggs
+
+ eggs = spam(cell_ext)
+ cell_closure, = eggs.__closure__
+ cell_eggs = eggs()
+
+ self.assertIs(cell_eggs, cell_ext)
+ self.assertIsNot(cell_eggs, cell_closure)
+
def testRecursion(self):
def f(x):