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
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
@@ -266,6 +266,21 @@ comprehensions, and generator expressions) to explicitly return independent
snapshots of the currently assigned local variables, including locally
referenced nonlocal variables captured in closures.
This change to the semantics of :func:`locals` in optimized scopes also affects the default
behaviour of code execution functions that implicitly target ``locals()`` if no explicit
namespace is provided (such as :func:`exec` and :func:`eval`). In previous versions, whether
or not changes could be accessed by calling ``locals()`` after calling the code execution
function was implementation dependent. In CPython specifically, such code would typically
appear to work as desired, but could sometimes fail in optimized scopes based on other code
(including debuggers and code execution tracing tools) potentially resetting the shared
snapshot in that scope. Now, the code will always run against an independent snapshot of the
local variables in optimized scopes, and hence the changes will never be visible in
subsequent calls to ``locals()``. To access the changes made in these cases, an explicit
namespace reference must now be passed to the relevant function. Alternatively, it may make
sense to update affected code to use a higher level code execution API that returns the
resulting code execution namespace (e.g. :func:`runpy.run_path` when executing Python
files from disk).
To ensure debuggers and similar tools can reliably update local variables in
scopes affected by this change, :attr:`FrameType.f_locals <frame.f_locals>` now
returns a write-through proxy to the frame's local and locally referenced
Expand Down
Expand Up
@@ -2223,7 +2238,10 @@ Changes in the Python API
independent snapshot on each call, and hence no longer implicitly updates
previously returned references. Obtaining the legacy CPython behaviour now
requires explicit calls to update the initially returned dictionary with the
results of subsequent calls to ``locals()``. (Changed as part of :pep:`667`.)
results of subsequent calls to ``locals()``. Code execution functions that
implicitly target ``locals()`` (such as ``exec`` and ``eval``) must be
passed an explicit namespace to access their results in an optimized scope.
(Changed as part of :pep:`667`.)
* Calling :func:`locals` from a comprehension at module or class scope
(including via ``exec`` or ``eval``) once more behaves as if the comprehension
Expand Down
Expand Up
@@ -2311,6 +2329,12 @@ Changes in the C API
to :c:func:`PyUnstable_Code_GetFirstFree`.
(Contributed by Bogdan Romanyuk in :gh:`115781`.)
* Calling :c:func:`PyFrame_GetLocals` or :c:func:`PyEval_GetLocals` in an
:term:`optimized scope` now returns a write-through proxy rather than a
snapshot that gets updated at ill-specified times. If a snapshot is desired,
it must be created explicitly (e.g. with :c:func:`PyDict_Copy`) or by calling
the new :c:func:`PyEval_GetFrameLocals` API. (Changed as part of :pep:`667`.)
* :c:func:`!PyFrame_FastToLocals` and :c:func:`!PyFrame_FastToLocalsWithError`
no longer have any effect. Calling these functions has been redundant since
Python 3.11, when :c:func:`PyFrame_GetLocals` was first introduced.
Expand Down
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[3.13] gh-118888: Further PEP 667 docs updates (gh-119893) #119894
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
[3.13] gh-118888: Further PEP 667 docs updates (gh-119893) #119894
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing