diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2021-06-07 22:52:00 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-07 22:52:00 (GMT) |
commit | 631f9938b1604d4f893417ec339b9e0fa9196fb1 (patch) | |
tree | 6a72bf35dafb42ad3d00e153e244538db4a646ee /Lib/test/test_scope.py | |
parent | e915db3e9e512249a6f494c0b331db2d021e1f56 (diff) | |
download | cpython-631f9938b1604d4f893417ec339b9e0fa9196fb1.zip cpython-631f9938b1604d4f893417ec339b9e0fa9196fb1.tar.gz cpython-631f9938b1604d4f893417ec339b9e0fa9196fb1.tar.bz2 |
bpo-43693: Add the MAKE_CELL opcode and interleave fast locals offsets. (gh-26396)
This moves logic out of the frame initialization code and into the compiler and eval loop. Doing so simplifies the runtime code and allows us to optimize it better.
https://bugs.python.org/issue43693
Diffstat (limited to 'Lib/test/test_scope.py')
-rw-r--r-- | Lib/test/test_scope.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/Lib/test/test_scope.py b/Lib/test/test_scope.py index 4239b26..29d60ff 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): |