FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-120097: Make FrameLocalsProxy a mapping by markshannon · Pull Request #120101 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (2) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 5 additions & 0 deletions Lib/_collections_abc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def _f(): pass
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
Expand Down Expand Up @@ -836,6 +840,7 @@ def __eq__(self, other):
__reversed__ = None

Mapping.register(mappingproxy)
Mapping.register(framelocalsproxy)


class MappingView(Sized):
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_frame.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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
Expand Down Expand Up @@ -420,6 +421,17 @@ def test_unsupport(self):
with self.assertRaises(TypeError):
copy.deepcopy(d)

def test_is_mapping(self):
x = 1

sobolevn Jun 5, 2024 •
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why do you need x? To fill f_locals? In this case I propose to use a inner function. Or maybe two: with and without locals.

markshannon Jun 7, 2024 •
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The contents are irrelevant, but we want something just to make sure we are testing the right thing.
Without x = 1, the locals would just be {} and it would be too easy to mistakenly test the wrong mapping.

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):
def test_basic(self):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``FrameLocalsProxy`` now subclasses ``collections.abc.Mapping`` and can be
matched as a mapping in ``match`` statements
2 changes: 1 addition & 1 deletion Objects/frameobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,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,
Expand Down

Back | FazBrowse Home | New Git URL