@@ -5,6 +5,7 @@ Status: Draft
5
5
Type: Standards Track
6
6
Content-Type: text/x-rst
7
7
Created: 30-Jul-2021
8
+ Python-Version: 3.13
8
9
Post-History: 20-Aug-2021
9
10
10
11
@@ -118,11 +119,16 @@ For example::
118
119
y
119
120
print(locals(), x)
120
121
121
- ``test() `` will print ``{'x': 2, 'y': 4, 'z': 5} 2 ``
122
+ ``test() `` will print ``{'x': 2, 'y': 4, 'z': 5} 2 ``.
122
123
123
- In Python 3.10, the above will fail with a ``NameError ``,
124
+ In Python 3.10, the above will fail with an ``UnboundLocalError ``,
124
125
as the definition of ``y `` by ``l()['y'] = 4 `` is lost.
125
126
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
+
126
132
C-API
127
133
-----
128
134
@@ -152,7 +158,7 @@ The following C-API functions will be deprecated, as they return borrowed refere
152
158
PyEval_GetGlobals()
153
159
PyEval_GetBuiltins()
154
160
155
- They will be will be removed in 3.14 .
161
+ They will be removed in 3.15 .
156
162
157
163
The following functions should be used instead::
158
164
@@ -171,7 +177,7 @@ The following three functions will become no-ops, and will be deprecated::
171
177
PyFrame_FastToLocals()
172
178
PyFrame_LocalsToFast()
173
179
174
- They will be will be removed in 3.14 .
180
+ They will be removed in 3.15 .
175
181
176
182
Behavior of f_locals for optimized functions
177
183
--------------------------------------------
@@ -225,7 +231,7 @@ should be replaced with::
225
231
PyFrame_FastToLocals, etc.
226
232
''''''''''''''''''''''''''
227
233
228
- These functions were designed to convert the internal "fast" representation
234
+ These functions were designed to convert the internal "fast" representation
229
235
of the locals variables of a function to a dictionary, and vice versa.
230
236
231
237
Calls to them are no longer required. C code that directly accesses the
@@ -387,6 +393,21 @@ C API
387
393
As with all functions that return a borrowed reference, care must be taken to
388
394
ensure that the reference is not used beyond the lifetime of the object.
389
395
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
+
390
411
Comparison with PEP 558
391
412
=======================
392
413
@@ -414,8 +435,8 @@ An alternative way to define ``locals()`` would be simply as::
414
435
return sys._getframe(1).f_locals
415
436
416
437
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 ``.
419
440
420
441
Lifetime of the mapping proxy
421
442
-----------------------------
0 commit comments