]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] GH-120097: Make FrameLocalsProxy a mapping (GH-120101) (GH-120749)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 23 Aug 2024 09:26:03 +0000 (11:26 +0200)
committerGitHub <noreply@github.com>
Fri, 23 Aug 2024 09:26:03 +0000 (10:26 +0100)
Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
Lib/_collections_abc.py
Lib/test/test_frame.py
Misc/NEWS.d/next/Core and Builtins/2024-06-05-10-32-44.gh-issue-120097.9S2klk.rst [new file with mode: 0644]
Objects/frameobject.c

index 601107d2d867718fb3dd44f409a3db07edc515bd..036254869d52c3052b6b1c47e036caf65067231d 100644 (file)
@@ -85,6 +85,10 @@ dict_values = type({}.values())
 dict_items = type({}.items())
 ## misc ##
 mappingproxy = type(type.__dict__)
+def _get_framelocalsproxy():
+    return type(sys._getframe().f_locals)
+framelocalsproxy = _get_framelocalsproxy()
+del _get_framelocalsproxy
 generator = type((lambda: (yield))())
 ## coroutine ##
 async def _coro(): pass
@@ -836,6 +840,7 @@ class Mapping(Collection):
     __reversed__ = None
 
 Mapping.register(mappingproxy)
+Mapping.register(framelocalsproxy)
 
 
 class MappingView(Sized):
index 55b1597e333fedfd619713dfa96f2b43dde7c091..4c79813f12ada28de6c6ec03144ed066799f5028 100644 (file)
@@ -13,6 +13,7 @@ try:
 except ImportError:
     _testcapi = None
 
+from collections.abc import Mapping
 from test import support
 from test.support import import_helper, threading_helper, Py_GIL_DISABLED
 from test.support.script_helper import assert_python_ok
@@ -421,6 +422,17 @@ class TestFrameLocals(unittest.TestCase):
         with self.assertRaises(TypeError):
             copy.deepcopy(d)
 
+    def test_is_mapping(self):
+        x = 1
+        d = sys._getframe().f_locals
+        self.assertIsInstance(d, Mapping)
+        match d:
+            case {"x": value}:
+                self.assertEqual(value, 1)
+                kind = "mapping"
+            case _:
+                kind = "other"
+        self.assertEqual(kind, "mapping")
 
     def _x_stringlikes(self):
         class StringSubclass(str):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-06-05-10-32-44.gh-issue-120097.9S2klk.rst b/Misc/NEWS.d/next/Core and Builtins/2024-06-05-10-32-44.gh-issue-120097.9S2klk.rst
new file mode 100644 (file)
index 0000000..39d829b
--- /dev/null
@@ -0,0 +1,2 @@
+``FrameLocalsProxy`` now subclasses ``collections.abc.Mapping`` and can be
+matched as a mapping in ``match`` statements
index 6ed56bf270723f1da502584cbd29cff23bd4be4d..90b975b3f43fdb1ed32bb6c13283d8e6d9e420db 100644 (file)
@@ -740,7 +740,7 @@ PyTypeObject PyFrameLocalsProxy_Type = {
     .tp_as_mapping = &framelocalsproxy_as_mapping,
     .tp_getattro = PyObject_GenericGetAttr,
     .tp_setattro = PyObject_GenericSetAttr,
-    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
+    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_MAPPING,
     .tp_traverse = framelocalsproxy_visit,
     .tp_clear = framelocalsproxy_tp_clear,
     .tp_richcompare = framelocalsproxy_richcompare,