From 1871ac121cebccf4e326b8033a237505636d0d21 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 4 May 2024 13:16:01 -0700 Subject: [PATCH 1/5] Improve wording of PEP 667 news blurm --- .../2024-04-27-21-44-40.gh-issue-74929.C2nESp.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-27-21-44-40.gh-issue-74929.C2nESp.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-27-21-44-40.gh-issue-74929.C2nESp.rst index 46e628f7fa7dbe..07c5a59538b0b0 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-04-27-21-44-40.gh-issue-74929.C2nESp.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-27-21-44-40.gh-issue-74929.C2nESp.rst @@ -1 +1,3 @@ -Implement PEP 667 - converted ``frame.f_locals`` to a write through proxy +Implement PEP 667: converted :attr:`FrameType.f_locals ` +and :c:func:`PyFrame_GetLocals` to return a write-through proxy object +when the frame refers to a function or comprehension. From 192596c1e67a46e731e3f3b92449ea8e7d24c913 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 4 May 2024 13:16:18 -0700 Subject: [PATCH 2/5] Add rudimentary docs for PEP 667 --- Doc/c-api/frame.rst | 9 ++++++++- Doc/reference/datamodel.rst | 5 +++++ Doc/whatsnew/3.13.rst | 5 +++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Doc/c-api/frame.rst b/Doc/c-api/frame.rst index 6bb1e9b5803b58..82e0980ad753d0 100644 --- a/Doc/c-api/frame.rst +++ b/Doc/c-api/frame.rst @@ -120,12 +120,19 @@ See also :ref:`Reflection `. .. c:function:: PyObject* PyFrame_GetLocals(PyFrameObject *frame) - Get the *frame*'s :attr:`~frame.f_locals` attribute (:class:`dict`). + Get the *frame*'s :attr:`~frame.f_locals` attribute. + If the frame refers to a function or comprehension, this returns + a write-through proxy object that allows modifying the locals. + In all other cases (classes, modules) it returns the :class:`dict` + representing the frame locals directly. Return a :term:`strong reference`. .. versionadded:: 3.11 + .. versionchanged:: 3.13 + Return a proxy object for functions and comprehensions. + .. c:function:: int PyFrame_GetLineNumber(PyFrameObject *frame) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 5e1558362ffaa0..311fd22faf2fa8 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1342,6 +1342,11 @@ Special read-only attributes * - .. attribute:: frame.f_locals - The dictionary used by the frame to look up :ref:`local variables ` + (if the frame refers to a function or comprehension, + this may return a write-through proxy object) + + .. versionchanged 3.13 + Return a proxy for functions and comprehensions. * - .. attribute:: frame.f_globals - The dictionary used by the frame to look up diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 269a7cc985ad19..5d703a3dacaa9d 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -87,6 +87,11 @@ Interpreter improvements: Performance improvements are modest -- we expect to be improving this over the next few releases. +* :pep:`667`: :attr:`FrameType.f_locals ` when used in + a function now returns a write-through proxy to the frame's locals, + rather than a ``dict``. See the PEP for corresponding C API changes + and deprecations. + New typing features: * :pep:`696`: Type parameters (:data:`typing.TypeVar`, :data:`typing.ParamSpec`, From 0182e4ba26622eae7da278acf2da6972c5c28872 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 4 May 2024 13:49:22 -0700 Subject: [PATCH 3/5] Fix versionchanged:: markup --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 311fd22faf2fa8..6184c168d9fa0c 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1345,7 +1345,7 @@ Special read-only attributes (if the frame refers to a function or comprehension, this may return a write-through proxy object) - .. versionchanged 3.13 + .. versionchanged:: 3.13 Return a proxy for functions and comprehensions. * - .. attribute:: frame.f_globals From 57e1d5404a3f481da52cf3b3215de1ca589afca5 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 4 May 2024 13:52:43 -0700 Subject: [PATCH 4/5] Improve phrasing in datamodel --- Doc/reference/datamodel.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 6184c168d9fa0c..f9438a141657be 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1341,9 +1341,9 @@ Special read-only attributes * - .. attribute:: frame.f_locals - The dictionary used by the frame to look up - :ref:`local variables ` - (if the frame refers to a function or comprehension, - this may return a write-through proxy object) + :ref:`local variables `. + If the frame refers to a function or comprehension, + this may return a write-through proxy object. .. versionchanged:: 3.13 Return a proxy for functions and comprehensions. From b94c068a577e5b15ec01f1f68bbb6c7bfc9c7642 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sun, 5 May 2024 08:13:45 -0700 Subject: [PATCH 5/5] Fix whitespace --- .../2024-04-27-21-44-40.gh-issue-74929.C2nESp.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-27-21-44-40.gh-issue-74929.C2nESp.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-27-21-44-40.gh-issue-74929.C2nESp.rst index 07c5a59538b0b0..29c7975fa66bc0 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-04-27-21-44-40.gh-issue-74929.C2nESp.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-27-21-44-40.gh-issue-74929.C2nESp.rst @@ -1,3 +1,3 @@ Implement PEP 667: converted :attr:`FrameType.f_locals ` -and :c:func:`PyFrame_GetLocals` to return a write-through proxy object +and :c:func:`PyFrame_GetLocals` to return a write-through proxy object when the frame refers to a function or comprehension.