@@ -24,7 +24,9 @@ behaviour at function scope to make it more predictable and independent of the
24
24
presence or absence of tracing functions.
25
25
26
26
In addition, it proposes that the following functions be added to the stable
27
- Python C API/ABI::
27
+ Python C API/ABI:
28
+
29
+ .. code-block :: c
28
30
29
31
typedef enum {
30
32
PyLocals_UNDEFINED = -1,
@@ -145,7 +147,7 @@ builtin to read as follows:
145
147
dictionaries.
146
148
147
149
148
- There would also be a versionchanged note for the release making this change:
150
+ There would also be a `` versionchanged `` note for the release making this change:
149
151
150
152
In prior versions, the semantics of mutating the mapping object returned
151
153
from ``locals() `` were formally undefined. In CPython specifically,
@@ -273,14 +275,20 @@ Summary of proposed implementation-specific changes
273
275
274
276
* Changes are made as necessary to provide the updated Python level semantics
275
277
* Two new functions are added to the stable ABI to replicate the updated
276
- behaviour of the Python ``locals() `` builtin::
278
+ behaviour of the Python ``locals() `` builtin:
279
+
280
+ .. code-block :: c
277
281
278
282
PyObject * PyLocals_Get();
279
283
PyLocals_Kind PyLocals_GetKind();
284
+
280
285
* One new function is added to the stable ABI to efficiently get a snapshot of
281
- the local namespace in the running frame::
286
+ the local namespace in the running frame:
287
+
288
+ .. code-block :: c
282
289
283
290
PyObject * PyLocals_GetCopy();
291
+
284
292
* Corresponding frame accessor functions for these new public APIs are added to
285
293
the CPython frame C API
286
294
* On optimised frames, the Python level ``f_locals `` API will return dynamically
@@ -494,7 +502,9 @@ independent, behaviour. However, it is also desirable to allow C code to
494
502
exactly mimic the behaviour of Python code at the same scope.
495
503
496
504
To enable mimicking the behaviour of Python code, the stable C ABI would gain
497
- the following new functions::
505
+ the following new functions:
506
+
507
+ .. code-block :: c
498
508
499
509
PyObject * PyLocals_Get();
500
510
PyLocals_Kind PyLocals_GetKind();
@@ -526,15 +536,19 @@ information visually through lexical scoping (as covered in the new ``locals()``
526
536
builtin documentation).
527
537
528
538
To allow extension module code to behave consistently regardless of the active
529
- Python scope, the stable C ABI would gain the following new function::
539
+ Python scope, the stable C ABI would gain the following new function:
540
+
541
+ .. code-block :: c
530
542
531
543
PyObject * PyLocals_GetCopy();
532
544
533
545
``PyLocals_GetCopy() `` returns a new dict instance populated from the current
534
546
locals namespace. Roughly equivalent to ``dict(locals()) `` in Python code, but
535
547
avoids the double-copy in the case where ``locals() `` already returns a shallow
536
548
copy. Akin to the following code, but doesn't assume there will only ever be
537
- two kinds of locals result::
549
+ two kinds of locals result:
550
+
551
+ .. code-block :: c
538
552
539
553
locals = PyLocals_Get();
540
554
if (PyLocals_GetKind() == PyLocals_DIRECT_REFERENCE) {
@@ -598,7 +612,9 @@ specifics of when the namespace it returns gets refreshed are still an
598
612
interpreter implementation detail)
599
613
600
614
The additions to the public CPython C API are the frame level enhancements
601
- needed to support the stable C API/ABI updates::
615
+ needed to support the stable C API/ABI updates:
616
+
617
+ .. code-block :: c
602
618
603
619
PyLocals_Kind PyFrame_GetLocalsKind(frame);
604
620
PyObject * PyFrame_GetLocals(frame);
@@ -628,7 +644,9 @@ affected code should be updated to use
628
644
instead.
629
645
630
646
In addition to the above documented interfaces, the draft reference
631
- implementation also exposes the following undocumented interfaces::
647
+ implementation also exposes the following undocumented interfaces:
648
+
649
+ .. code-block :: c
632
650
633
651
PyTypeObject _PyFastLocalsProxy_Type;
634
652
#define _PyFastLocalsProxy_CheckExact(self) Py_IS_TYPE(op, &_PyFastLocalsProxy_Type)
@@ -1016,7 +1034,7 @@ specifically related to the C API:
1016
1034
* :pep: `667 ` still proposes completely unnecessary C API breakage (the programmatic
1017
1035
deprecation and eventual removal of ``PyEval_GetLocals() ``,
1018
1036
``PyFrame_FastToLocalsWithError() ``, and ``PyFrame_FastToLocals() ``) without
1019
- justification, when it is entirely possible to keep these working indefintely
1037
+ justification, when it is entirely possible to keep these working indefinitely
1020
1038
(and interoperably) given a suitably designed fast locals proxy implementation
1021
1039
* the fast locals proxy handling of additional variables is defined in this PEP
1022
1040
in a way that is fully interoperable with the existing ``PyEval_GetLocals() ``
@@ -1029,7 +1047,7 @@ specifically related to the C API:
1029
1047
like a type name than a data access API.
1030
1048
* this PEP adds ``PyLocals_GetCopy() `` and ``PyFrame_GetLocalsCopy() `` APIs to
1031
1049
allow extension modules to easily avoid incurring a double copy operation in
1032
- frames where ``PyLocals_Get() `` alreadys makes a copy
1050
+ frames where ``PyLocals_Get() `` already makes a copy
1033
1051
* this PEP adds ``PyLocals_Kind ``, ``PyLocals_GetKind() ``, and
1034
1052
``PyFrame_GetLocalsKind() `` to allow extension modules to identify when code
1035
1053
is running at function scope without having to inspect non-portable frame and
@@ -1238,7 +1256,7 @@ complexity improvement.
1238
1256
The O(1) nature of the other operations can be restored by adding implementation
1239
1257
code that doesn't rely on the value cache being up to date.
1240
1258
1241
- Keeping the iterator/iterable retrieval methods as `` O(1) `` will involve
1259
+ Keeping the iterator/iterable retrieval methods as O(1) will involve
1242
1260
writing custom replacements for the corresponding builtin dict helper types,
1243
1261
just as proposed in :pep: `667 `. As illustrated above, the implementations would
1244
1262
be similar to the pseudo-code presented in :pep: `667 `, but not identical (due to
@@ -1274,7 +1292,7 @@ Thanks to Nathaniel J. Smith for proposing the write-through proxy idea in
1274
1292
PEP that attempted to avoid introducing such a proxy.
1275
1293
1276
1294
Thanks to Steve Dower and Petr Viktorin for asking that more attention be paid
1277
- to the developer experience of the proposed C API additions [8, 13]_.
1295
+ to the developer experience of the proposed C API additions [8 ]_ [ 13 ]_.
1278
1296
1279
1297
Thanks to Larry Hastings for the suggestion on how to use enums in the stable
1280
1298
ABI while ensuring that they safely support typecasting from arbitrary
@@ -1283,7 +1301,7 @@ integers.
1283
1301
Thanks to Mark Shannon for pushing for further simplification of the C level
1284
1302
API and semantics, as well as significant clarification of the PEP text (and for
1285
1303
restarting discussion on the PEP in early 2021 after a further year of
1286
- inactivity) [10,11, 12]_. Mark's comments that were ultimately published as
1304
+ inactivity) [10 ]_ [ 11 ]_ [ 12 ]_. Mark's comments that were ultimately published as
1287
1305
:pep: `667 ` also directly resulted in several implementation efficiency improvements
1288
1306
that avoid incurring the cost of redundant O(n) mapping refresh operations
1289
1307
when the relevant mappings aren't used, as well as the change to ensure that
@@ -1293,58 +1311,44 @@ the state reported through the Python level ``f_locals`` API is never stale.
1293
1311
References
1294
1312
==========
1295
1313
1296
- .. [1 ] Broken local variable assignment given threads + trace hook + closure
1297
- ( https://bugs. python.org/issue30744)
1314
+ .. [1 ] ` Broken local variable assignment given threads + trace hook + closure
1315
+ < https://github.com/ python/cpython/issues/74929> `_
1298
1316
1299
- .. [2 ] Clarify the required behaviour of `` locals() ``
1300
- ( https://bugs. python.org/issue17960)
1317
+ .. [3 ] ` Updating function local variables from pdb is unreliable
1318
+ < https://github.com/ python/cpython/issues/5384)> `_
1301
1319
1302
- .. [3 ] Updating function local variables from pdb is unreliable
1303
- ( https://bugs .python.org/issue9633)
1320
+ .. [4 ] ` CPython's Python API for installing trace hooks
1321
+ < https://docs .python.org/dev/library/sys.html#sys.settrace> `_
1304
1322
1305
- .. [4 ] CPython's Python API for installing trace hooks
1306
- ( https://docs.python.org/dev/library/sys .html#sys.settrace)
1323
+ .. [5 ] ` CPython's C API for installing trace hooks
1324
+ < https://docs.python.org/3/c-api/init .html#c.PyEval_SetTrace> `_
1307
1325
1308
- .. [5 ] CPython's C API for installing trace hooks
1309
- ( https://docs. python.org/3/c-api/init.html#c.PyEval_SetTrace)
1326
+ .. [6 ] ` PEP 558 reference implementation
1327
+ < https://github.com/ python/cpython/pull/3640/files> `_
1310
1328
1311
- .. [6 ] PEP 558 reference implementation
1312
- ( https://github.com/ python/cpython/pull/3640/files)
1329
+ .. [7 ] ` Nathaniel's review of possible function level semantics for locals()
1330
+ < https://mail. python.org/pipermail/python-dev/2019-May/157738.html> `_
1313
1331
1314
- .. [7 ] Nathaniel's review of possible function level semantics for locals()
1315
- ( https://mail .python.org/pipermail/python-dev/2019-May/157738.html)
1332
+ .. [8 ] ` Discussion of more intentionally designed C API enhancements
1333
+ < https://discuss .python.org/t/pep-558-defined-semantics-for-locals/2936/3> `_
1316
1334
1317
- .. [8 ] Discussion of more intentionally designed C API enhancements
1318
- ( https://discuss. python.org/t/pep-558-defined-semantics-for-locals/2936/3)
1335
+ .. [9 ] ` Disable automatic update of frame locals during tracing
1336
+ < https://github.com/ python/cpython/issues/86363> `_
1319
1337
1320
- .. [9 ] Disable automatic update of frame locals during tracing
1321
- ( https://bugs .python.org/issue42197)
1338
+ .. [10 ] ` python-dev thread: Resurrecting PEP 558 (Defined semantics for locals())
1339
+ < https://mail .python.org/archives/list/python-dev@python.org/thread/TUQOEWQSCQZPUDV2UFFKQ3C3I4WGFPAJ/> `_
1322
1340
1323
- .. [10 ] python-dev thread: Resurrecting PEP 558 (Defined semantics for locals())
1324
- ( https://mail.python.org/archives/list/python-dev@python.org/thread/TUQOEWQSCQZPUDV2UFFKQ3C3I4WGFPAJ/)
1341
+ .. [11 ] ` python-dev thread: Comments on PEP 558
1342
+ < https://mail.python.org/archives/list/python-dev@python.org/thread/A3UN4DGBCOB45STE6AQBITJFW6UZE43O/> `_
1325
1343
1326
- .. [11 ] python-dev thread: Comments on PEP 558
1327
- ( https://mail.python.org/archives/list/python-dev@python.org/thread/A3UN4DGBCOB45STE6AQBITJFW6UZE43O/)
1344
+ .. [12 ] ` python-dev thread: More comments on PEP 558
1345
+ < https://mail.python.org/archives/list/python-dev@python.org/thread/7TKPMD5LHCBXGFUIMKDAUZELRH6EX76S/> `_
1328
1346
1329
- .. [12 ] python-dev thread: More comments on PEP 558
1330
- (https://mail.python.org/archives/list/python-dev@python.org/thread/7TKPMD5LHCBXGFUIMKDAUZELRH6EX76S/)
1331
-
1332
- .. [13 ] Petr Viktorin's suggestion to use an enum for ``PyLocals_Get ``'s behaviour
1333
- (https://mail.python.org/archives/list/python-dev@python.org/message/BTQUBHIVE766RPIWLORC5ZYRCRC4CEBL/)
1347
+ .. [13 ] `Petr Viktorin's suggestion to use an enum for PyLocals_Get's behaviour
1348
+ <https://mail.python.org/archives/list/python-dev@python.org/message/BTQUBHIVE766RPIWLORC5ZYRCRC4CEBL/> `_
1334
1349
1335
1350
Copyright
1336
1351
=========
1337
1352
1338
1353
This document is placed in the public domain or under the
1339
1354
CC0-1.0-Universal license, whichever is more permissive.
1340
-
1341
-
1342
-
1343
- ..
1344
- Local Variables:
1345
- mode: indented-text
1346
- indent-tabs-mode: nil
1347
- sentence-end-double-space: t
1348
- fill-column: 70
1349
- coding: utf-8
1350
- End:
0 commit comments