Skip to content

Commit 95b4f9c

Browse files
[3.13] GH-120097: Make FrameLocalsProxy a mapping (GH-120101) (GH-120749)
Co-authored-by: Alyssa Coghlan <[email protected]>
1 parent fda8aec commit 95b4f9c

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

Lib/_collections_abc.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ def _f(): pass
8585
dict_items = type({}.items())
8686
## misc ##
8787
mappingproxy = type(type.__dict__)
88+
def _get_framelocalsproxy():
89+
return type(sys._getframe().f_locals)
90+
framelocalsproxy = _get_framelocalsproxy()
91+
del _get_framelocalsproxy
8892
generator = type((lambda: (yield))())
8993
## coroutine ##
9094
async def _coro(): pass
@@ -836,6 +840,7 @@ def __eq__(self, other):
836840
__reversed__ = None
837841

838842
Mapping.register(mappingproxy)
843+
Mapping.register(framelocalsproxy)
839844

840845

841846
class MappingView(Sized):

Lib/test/test_frame.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
except ImportError:
1414
_testcapi = None
1515

16+
from collections.abc import Mapping
1617
from test import support
1718
from test.support import import_helper, threading_helper, Py_GIL_DISABLED
1819
from test.support.script_helper import assert_python_ok
@@ -421,6 +422,17 @@ def test_unsupport(self):
421422
with self.assertRaises(TypeError):
422423
copy.deepcopy(d)
423424

425+
def test_is_mapping(self):
426+
x = 1
427+
d = sys._getframe().f_locals
428+
self.assertIsInstance(d, Mapping)
429+
match d:
430+
case {"x": value}:
431+
self.assertEqual(value, 1)
432+
kind = "mapping"
433+
case _:
434+
kind = "other"
435+
self.assertEqual(kind, "mapping")
424436

425437
def _x_stringlikes(self):
426438
class StringSubclass(str):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``FrameLocalsProxy`` now subclasses ``collections.abc.Mapping`` and can be
2+
matched as a mapping in ``match`` statements

Objects/frameobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ PyTypeObject PyFrameLocalsProxy_Type = {
740740
.tp_as_mapping = &framelocalsproxy_as_mapping,
741741
.tp_getattro = PyObject_GenericGetAttr,
742742
.tp_setattro = PyObject_GenericSetAttr,
743-
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
743+
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_MAPPING,
744744
.tp_traverse = framelocalsproxy_visit,
745745
.tp_clear = framelocalsproxy_tp_clear,
746746
.tp_richcompare = framelocalsproxy_richcompare,

0 commit comments

Comments
 (0)