mirror of https://github.com/python/cpython
GH-120097: Make FrameLocalsProxy a mapping (#120101)
* Register FrameLocalsProxy as a subclass of collections.abc.Mapping * Allow FrameLocalsProxy to matching mapping patterns
This commit is contained in:
parent
00257c746c
commit
d1c673b658
|
@ -85,6 +85,10 @@ dict_values = type({}.values())
|
||||||
dict_items = type({}.items())
|
dict_items = type({}.items())
|
||||||
## misc ##
|
## misc ##
|
||||||
mappingproxy = type(type.__dict__)
|
mappingproxy = type(type.__dict__)
|
||||||
|
def _get_framelocalsproxy():
|
||||||
|
return type(sys._getframe().f_locals)
|
||||||
|
framelocalsproxy = _get_framelocalsproxy()
|
||||||
|
del _get_framelocalsproxy
|
||||||
generator = type((lambda: (yield))())
|
generator = type((lambda: (yield))())
|
||||||
## coroutine ##
|
## coroutine ##
|
||||||
async def _coro(): pass
|
async def _coro(): pass
|
||||||
|
@ -836,6 +840,7 @@ class Mapping(Collection):
|
||||||
__reversed__ = None
|
__reversed__ = None
|
||||||
|
|
||||||
Mapping.register(mappingproxy)
|
Mapping.register(mappingproxy)
|
||||||
|
Mapping.register(framelocalsproxy)
|
||||||
|
|
||||||
|
|
||||||
class MappingView(Sized):
|
class MappingView(Sized):
|
||||||
|
|
|
@ -11,6 +11,7 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
_testcapi = None
|
_testcapi = None
|
||||||
|
|
||||||
|
from collections.abc import Mapping
|
||||||
from test import support
|
from test import support
|
||||||
from test.support import import_helper, threading_helper
|
from test.support import import_helper, threading_helper
|
||||||
from test.support.script_helper import assert_python_ok
|
from test.support.script_helper import assert_python_ok
|
||||||
|
@ -418,6 +419,17 @@ class TestFrameLocals(unittest.TestCase):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
copy.deepcopy(d)
|
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")
|
||||||
|
|
||||||
class TestFrameCApi(unittest.TestCase):
|
class TestFrameCApi(unittest.TestCase):
|
||||||
def test_basic(self):
|
def test_basic(self):
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
``FrameLocalsProxy`` now subclasses ``collections.abc.Mapping`` and can be
|
||||||
|
matched as a mapping in ``match`` statements
|
|
@ -720,7 +720,7 @@ PyTypeObject PyFrameLocalsProxy_Type = {
|
||||||
.tp_as_mapping = &framelocalsproxy_as_mapping,
|
.tp_as_mapping = &framelocalsproxy_as_mapping,
|
||||||
.tp_getattro = PyObject_GenericGetAttr,
|
.tp_getattro = PyObject_GenericGetAttr,
|
||||||
.tp_setattro = PyObject_GenericSetAttr,
|
.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_traverse = framelocalsproxy_visit,
|
||||||
.tp_clear = framelocalsproxy_tp_clear,
|
.tp_clear = framelocalsproxy_tp_clear,
|
||||||
.tp_richcompare = framelocalsproxy_richcompare,
|
.tp_richcompare = framelocalsproxy_richcompare,
|
||||||
|
|
Loading…
Reference in New Issue