8000 Define 'optimised scope' term, review f_locals · python/cpython@b95890d · GitHub
[go: up one dir, main page]

Skip to content

Commit b95890d

Browse files
committed
Define 'optimised scope' term, review f_locals
1 parent 40d0a81 commit b95890d

File tree

6 files changed

+30
-22
lines changed

6 files changed

+30
-22
lines changed

Doc/c-api/frame.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ See also :ref:`Reflection <reflection>`.
121121
.. c:function:: PyObject* PyFrame_GetLocals(PyFrameObject *frame)
122122
123123
Get the *frame*'s :attr:`~frame.f_locals` attribute.
124-
If the frame refers to a function or comprehension, this returns
125-
a write-through proxy object that allows modifying the locals.
124+
If the frame refers to an :term:`optimised scope`, this returns a
125+
write-through proxy object that allows modifying the locals.
126126
In all other cases (classes, modules, :func:`exec`, :func:`eval`) it returns
127127
the mapping representing the frame locals directly (as described for
128128
:func:`locals`).
@@ -132,7 +132,7 @@ See also :ref:`Reflection <reflection>`.
132132
.. versionadded:: 3.11
133133
134134
.. versionchanged:: 3.13
135-
Return a proxy object for functions and comprehensions.
135+
As part of :pep:`667`, return a proxy object for optimised scopes.
136136
137137
138138
.. c:function:: int PyFrame_GetLineNumber(PyFrameObject *frame)

Doc/glossary.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,13 @@ Glossary
889889
(methods). Also the ultimate base class of any :term:`new-style
890890
class`.
891891

892+
optimised scope
893+
A scope where target local variable names are reliably known to the
894+
compiler when the code is compiled, allowing optimisation of read and
895+
write access to these names. The local namespaces for functions,
896+
generators, coroutines, comprehensions, and generator expressions are
897+
optimised.
898+
892899
package
893900
A Python :term:`module` which can contain submodules or recursively,
894901
subpackages. Technically, a package is a Python module with a

Doc/library/functions.rst

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,14 +1080,14 @@ are always available. They are listed here in alphabetical order.
10801080
deleting local variables will immediately affect the contents of the
10811081
returned mapping object.
10821082

1083-
At function scope (including for generators and coroutines), each call to
1084-
``locals()`` instead returns a fresh dictionary containing the current
1085-
bindings of the function's local variables and any nonlocal cell references.
1086-
In this case, name binding changes made via the returned dict are *not*
1087-
written back to the corresponding local variables or nonlocal cell
1088-
references, and binding, rebinding, or deleting local variables and nonlocal
1089-
cell references does *not* affect the contents of previously returned
1090-
dictionaries.
1083+
In an :term:`optimised scope` (including functions, generators, and
1084+
coroutines), each call to ``locals()`` instead returns a fresh dictionary
1085+
containing the current bindings of the function's local variables and any
1086+
nonlocal cell references. In this case, name binding changes made via the
1087+
returned dict are *not* written back to the corresponding local variables
1088+
or nonlocal cell references, and assigning, reassigning, or deleting local
1089+
variables and nonlocal cell references does *not* affect the contents
1090+
of previously returned dictionaries.
10911091

10921092
Calling ``locals()`` as part of a comprehension in a function, generator, or
10931093
coroutine is equivalent to calling it in the containing scope, except that
@@ -1098,7 +1098,6 @@ are always available. They are listed here in alphabetical order.
10981098
Calling ``locals()`` as part of a generator expression is equivalent to
10991099
calling it in a nested generator function.
11001100

1101-
11021101
.. versionchanged:: 3.13
11031102
As part of :pep:`667`, the semantics of mutating the mapping object
11041103
returned from this function are now formally defined. The behavior in

Doc/reference/datamodel.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ Special read-only attributes
13491349
* - .. attribute:: frame.f_locals
13501350
- The dictionary used by the frame to look up
13511351
:ref:`local variables <naming>`.
1352-
If the frame refers to a function or comprehension,
1352+
If the frame refers to an :term:`optimised scope`,
13531353
this may return a write-through proxy object.
13541354

13551355
.. versionchanged:: 3.13

Doc/whatsnew/3.13.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,11 @@ Historically, the semantics for mutating the return value of :func:`locals` have
378378
been left to individual Python implementations to define.
379379

380380
Through :pep:`667`, Python 3.13 standardises the historical behaviour of CPython
381-
for most code execution scopes, but changes optimised scopes (functions,
382-
generators, coroutines, comprehensions, and generator expressions) to explicitly
383-
return independent snapshots of the currently assigned local variables, included
384-
locally referenced nonlocal variables captured in closures.
381+
for most code execution scopes, but changes
382+
:term:`optimised scopes <optimised scope>` (functions, generators, coroutines,
383+
comprehensions, and generator expressions) to explicitly return independent
384+
snapshots of the currently assigned local variables, including locally
385+
referenced nonlocal variables captured in closures.
385386

386387
To ensure debuggers and similar tools can reliably update local variables in
387388
scopes affected by this change, :attr:`FrameType.f_locals <frame.f_locals>` now
@@ -392,8 +393,6 @@ updated shared ``dict`` instance with undefined runtime semantics.
392393
See :pep:`667` for more details, including related C API changes and
393394
deprecations.
394395

395-
.. todo: Add notes to porting guide
396-
397396

398397
Other Language Changes
399398
======================

Lib/pdb.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,12 @@ def setup(self, f, tb):
392392
self.tb_lineno[tb.tb_frame] = lineno
393393
tb = tb.tb_next
394394
self.curframe = self.stack[self.curindex][0]
395-
# The f_locals dictionary is updated from the actual frame
396-
# locals whenever the .f_locals accessor is called, so we
397-
# cache it here to ensure that modifications are not overwritten.
395+
# The f_locals dictionary used to be updated from the actual frame
396+
# locals whenever the .f_locals accessor was called, so it was
397+
# cached here to ensure that modifications were not overwritten. While
398+
# the caching is no longer required now that f_locals is a direct proxy
399+
# on optimised frames, it's also harmless, so the code structure has
400+
# been left unchanged.
398401
self.curframe_locals = self.curframe.f_locals
399402
self.set_convenience_variable(self.curframe, '_frame', self.curframe)
400403

0 commit comments

Comments
 (0)
0