Skip to content

Commit d5e12f5

Browse files
authored
PEP 667: add 709 impact, other tweaks (#3196)
1 parent d9e66e6 commit d5e12f5

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

pep-0667.rst

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Status: Draft
55
Type: Standards Track
66
Content-Type: text/x-rst
77
Created: 30-Jul-2021
8+
Python-Version: 3.13
89
Post-History: 20-Aug-2021
910

1011

@@ -118,11 +119,16 @@ For example::
118119
y
119120
print(locals(), x)
120121

121-
``test()`` will print ``{'x': 2, 'y': 4, 'z': 5} 2``
122+
``test()`` will print ``{'x': 2, 'y': 4, 'z': 5} 2``.
122123

123-
In Python 3.10, the above will fail with a ``NameError``,
124+
In Python 3.10, the above will fail with an ``UnboundLocalError``,
124125
as the definition of ``y`` by ``l()['y'] = 4`` is lost.
125126

127+
If the second-to-last line were changed from ``y`` to ``z``, this would be a
128+
``NameError``, as it is today. Keys added to ``frame.f_locals`` that are not
129+
lexically local variables remain visible in ``frame.f_locals``, but do not
130+
dynamically become local variables.
131+
126132
C-API
127133
-----
128134

@@ -152,7 +158,7 @@ The following C-API functions will be deprecated, as they return borrowed refere
152158
PyEval_GetGlobals()
153159
PyEval_GetBuiltins()
154160

155-
They will be will be removed in 3.14.
161+
They will be removed in 3.15.
156162

157163
The following functions should be used instead::
158164

@@ -171,7 +177,7 @@ The following three functions will become no-ops, and will be deprecated::
171177
PyFrame_FastToLocals()
172178
PyFrame_LocalsToFast()
173179

174-
They will be will be removed in 3.14.
180+
They will be removed in 3.15.
175181

176182
Behavior of f_locals for optimized functions
177183
--------------------------------------------
@@ -225,7 +231,7 @@ should be replaced with::
225231
PyFrame_FastToLocals, etc.
226232
''''''''''''''''''''''''''
227233

228-
These functions were designed to convert the internal "fast" representation
234+
These functions were designed to convert the internal "fast" representation
229235
of the locals variables of a function to a dictionary, and vice versa.
230236

231237
Calls to them are no longer required. C code that directly accesses the
@@ -387,6 +393,21 @@ C API
387393
As with all functions that return a borrowed reference, care must be taken to
388394
ensure that the reference is not used beyond the lifetime of the object.
389395

396+
Impact on PEP 709 inlined comprehensions
397+
========================================
398+
399+
For inlined comprehensions within a function, ``locals()`` currently behaves the
400+
same inside or outside of the comprehension, and this will not change. The
401+
behavior of ``locals()`` inside functions will generally change as specified in
402+
the rest of this PEP.
403+
404+
For inlined comprehensions at module or class scope, currently calling
405+
``locals()`` within the inlined comprehension returns a new dictionary for each
406+
call. This PEP will make ``locals()`` within a function also always return a new
407+
dictionary for each call, improving consistency; class or module scope inlined
408+
comprehensions will appear to behave as if the inlined comprehension is still a
409+
distinct function.
410+
390411
Comparison with PEP 558
391412
=======================
392413

@@ -414,8 +435,8 @@ An alternative way to define ``locals()`` would be simply as::
414435
return sys._getframe(1).f_locals
415436

416437
This would be simpler and easier to understand. However,
417-
there would be backwards compatibility issues when ``locals`` is assigned
418-
to a local variable or when passed to ``eval`` or ``exec``.
438+
there would be backwards compatibility issues when ``locals()`` is assigned
439+
to a local variable or passed to ``eval`` or ``exec``.
419440

420441
Lifetime of the mapping proxy
421442
-----------------------------

0 commit comments

Comments
 (0)