Skip to content

Commit cf8f292

Browse files
[3.13] gh-118934: Fix PyEval_GetLocals docs (PEP 667) (GH-119934)
PEP 667's description of the planned changes to PyEval_GetLocals was internally inconsistent when accepted, so the docs added for gh-74929 didn't match either the current behaviour or the intended behaviour once gh-118934 is fixed. This PR updates the documentation and 3.13 What's New to match the intended behaviour (once gh-118934 is fixed). It also tidies up lingering references to `f_locals` always being a dictionary (this hasn't been true since at least when custom namespace support for class statement execution was added) (cherry picked from commit fd6cd62) Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
1 parent 36ca00f commit cf8f292

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

Doc/c-api/reflection.rst

+16-11
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,28 @@ Reflection
1919
2020
.. deprecated:: 3.13
2121
22-
To avoid creating a reference cycle in :term:`optimized scopes <optimized scope>`,
23-
use either :c:func:`PyEval_GetFrameLocals` to obtain the same behaviour as calling
22+
Use either :c:func:`PyEval_GetFrameLocals` to obtain the same behaviour as calling
2423
:func:`locals` in Python code, or else call :c:func:`PyFrame_GetLocals` on the result
25-
of :c:func:`PyEval_GetFrame` to get the same result as this function without having to
26-
cache the proxy instance on the underlying frame.
24+
of :c:func:`PyEval_GetFrame` to access the :attr:`~frame.f_locals` attribute of the
25+
currently executing frame.
2726
28-
Return the :attr:`~frame.f_locals` attribute of the currently executing frame,
27+
Return a mapping providing access to the local variables in the current execution frame,
2928
or ``NULL`` if no frame is currently executing.
3029
31-
If the frame refers to an :term:`optimized scope`, this returns a
32-
write-through proxy object that allows modifying the locals.
33-
In all other cases (classes, modules, :func:`exec`, :func:`eval`) it returns
34-
the mapping representing the frame locals directly (as described for
35-
:func:`locals`).
30+
Refer to :func:`locals` for details of the mapping returned at different scopes.
31+
32+
As this function returns a :term:`borrowed reference`, the dictionary returned for
33+
:term:`optimized scopes <optimized scope>` is cached on the frame object and will remain
34+
alive as long as the frame object does. Unlike :c:func:`PyEval_GetFrameLocals` and
35+
:func:`locals`, subsequent calls to this function in the same frame will update the
36+
contents of the cached dictionary to reflect changes in the state of the local variables
37+
rather than returning a new snapshot.
3638
3739
.. versionchanged:: 3.13
38-
As part of :pep:`667`, return a proxy object for optimized scopes.
40+
As part of :pep:`667`, :c:func:`PyFrame_GetLocals`, :func:`locals`, and
41+
:attr:`FrameType.f_locals <frame.f_locals>` no longer make use of the shared cache
42+
dictionary. Refer to the :ref:`What's New entry <whatsnew313-locals-semantics>` for
43+
additional details.
3944
4045
4146
.. c:function:: PyObject* PyEval_GetGlobals(void)

Doc/reference/datamodel.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1344,13 +1344,13 @@ Special read-only attributes
13441344
``object.__getattr__`` with arguments ``obj`` and ``"f_code"``.
13451345

13461346
* - .. attribute:: frame.f_locals
1347-
- The dictionary used by the frame to look up
1347+
- The mapping used by the frame to look up
13481348
:ref:`local variables <naming>`.
13491349
If the frame refers to an :term:`optimized scope`,
13501350
this may return a write-through proxy object.
13511351

13521352
.. versionchanged:: 3.13
1353-
Return a proxy for functions and comprehensions.
1353+
Return a proxy for optimized scopes.
13541354

13551355
* - .. attribute:: frame.f_globals
13561356
- The dictionary used by the frame to look up

Doc/whatsnew/3.13.rst

+28-8
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,9 @@ returns a write-through proxy to the frame's local and locally referenced
287287
nonlocal variables in these scopes, rather than returning an inconsistently
288288
updated shared ``dict`` instance with undefined runtime semantics.
289289

290-
See :pep:`667` for more details, including related C API changes and
291-
deprecations.
290+
See :pep:`667` for more details, including related C API changes and deprecations. Porting
291+
notes are also provided below for the affected :ref:`Python APIs <pep667-porting-notes-py>`
292+
and :ref:`C APIs <pep667-porting-notes-c>`.
292293

293294
(PEP and implementation contributed by Mark Shannon and Tian Gao in
294295
:gh:`74929`. Documentation updates provided by Guido van Rossum and
@@ -2234,6 +2235,8 @@ Changes in the Python API
22342235
returned by :meth:`zipfile.ZipFile.open` was changed from ``'r'`` to ``'rb'``.
22352236
(Contributed by Serhiy Storchaka in :gh:`115961`.)
22362237

2238+
.. _pep667-porting-notes-py:
2239+
22372240
* Calling :func:`locals` in an :term:`optimized scope` now produces an
22382241
independent snapshot on each call, and hence no longer implicitly updates
22392242
previously returned references. Obtaining the legacy CPython behaviour now
@@ -2329,15 +2332,27 @@ Changes in the C API
23292332
to :c:func:`PyUnstable_Code_GetFirstFree`.
23302333
(Contributed by Bogdan Romanyuk in :gh:`115781`.)
23312334

2332-
* Calling :c:func:`PyFrame_GetLocals` or :c:func:`PyEval_GetLocals` in an
2333-
:term:`optimized scope` now returns a write-through proxy rather than a
2334-
snapshot that gets updated at ill-specified times. If a snapshot is desired,
2335-
it must be created explicitly (e.g. with :c:func:`PyDict_Copy`) or by calling
2336-
the new :c:func:`PyEval_GetFrameLocals` API. (Changed as part of :pep:`667`.)
2335+
.. _pep667-porting-notes-c:
2336+
2337+
* The effects of mutating the dictionary returned from :c:func:`PyEval_GetLocals` in an
2338+
:term:`optimized scope` have changed. New dict entries added this way will now *only* be
2339+
visible to subsequent :c:func:`PyEval_GetLocals` calls in that frame, as
2340+
:c:func:`PyFrame_GetLocals`, :func:`locals`, and
2341+
:attr:`FrameType.f_locals <frame.f_locals>` no longer access the same underlying cached
2342+
dictionary. Changes made to entries for actual variable names and names added via the
2343+
write-through proxy interfaces will be overwritten on subsequent calls to
2344+
:c:func:`PyEval_GetLocals` in that frame. The recommended code update depends on how the
2345+
function was being used, so refer to the deprecation notice on the function for details.
2346+
(Changed as part of :pep:`667`.)
2347+
2348+
* Calling :c:func:`PyFrame_GetLocals` in an :term:`optimized scope` now returns a
2349+
write-through proxy rather than a snapshot that gets updated at ill-specified times.
2350+
If a snapshot is desired, it must be created explicitly (e.g. with :c:func:`PyDict_Copy`)
2351+
or by calling the new :c:func:`PyEval_GetFrameLocals` API. (Changed as part of :pep:`667`.)
23372352

23382353
* :c:func:`!PyFrame_FastToLocals` and :c:func:`!PyFrame_FastToLocalsWithError`
23392354
no longer have any effect. Calling these functions has been redundant since
2340-
Python 3.11, when :c:func:`PyFrame_GetLocals` was first introduced.
2355+
Python 3.11, when :c:func:`PyFrame_GetLocals` was first introduced.
23412356
(Changed as part of :pep:`667`.)
23422357

23432358
* :c:func:`!PyFrame_LocalsToFast` no longer has any effect. Calling this function
@@ -2497,6 +2512,11 @@ Deprecated C APIs
24972512
:c:func:`PyWeakref_GetRef` on Python 3.12 and older.
24982513
(Contributed by Victor Stinner in :gh:`105927`.)
24992514

2515+
* Deprecate the :c:func:`PyEval_GetBuiltins`, :c:func:`PyEval_GetGlobals`, and
2516+
:c:func:`PyEval_GetLocals` functions, which return a :term:`borrowed reference`.
2517+
Refer to the deprecation notices on each function for their recommended replacements.
2518+
(Soft deprecated as part of :pep:`667`.)
2519+
25002520
Pending Removal in Python 3.14
25012521
------------------------------
25022522

0 commit comments

Comments
 (0)