[3.13] gh-125590: Allow FrameLocalsProxy to delete and pop keys from extra locals (GH-125616) (#125797)

gh-125590: Allow FrameLocalsProxy to delete and pop keys from extra locals (GH-125616)
(cherry picked from commit 5b7a872b26)

Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
This commit is contained in:
Miss Islington (bot) 2024-10-21 19:06:27 +02:00 committed by GitHub
parent ace5478673
commit 829d650ccb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 99 additions and 8 deletions

View file

@ -399,15 +399,41 @@ class TestFrameLocals(unittest.TestCase):
def test_delete(self):
x = 1
d = sys._getframe().f_locals
with self.assertRaises(TypeError):
# This needs to be tested before f_extra_locals is created
with self.assertRaisesRegex(KeyError, 'non_exist'):
del d['non_exist']
with self.assertRaises(KeyError):
d.pop('non_exist')
with self.assertRaisesRegex(ValueError, 'local variables'):
del d['x']
with self.assertRaises(AttributeError):
d.clear()
with self.assertRaises(AttributeError):
with self.assertRaises(ValueError):
d.pop('x')
with self.assertRaises(ValueError):
d.pop('x', None)
# 'm', 'n' is stored in f_extra_locals
d['m'] = 1
d['n'] = 1
with self.assertRaises(KeyError):
d.pop('non_exist')
del d['m']
self.assertEqual(d.pop('n'), 1)
self.assertNotIn('m', d)
self.assertNotIn('n', d)
self.assertEqual(d.pop('n', 2), 2)
@support.cpython_only
def test_sizeof(self):
proxy = sys._getframe().f_locals