Skip to content

Commit 9c49e70

Browse files
committed
Merge branch 'main' into pythongh-127411-add-cast
2 parents 69dd507 + 04673d2 commit 9c49e70

25 files changed

+303
-229
lines changed

Doc/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pydoc-topics: build
144144

145145
.PHONY: gettext
146146
gettext: BUILDER = gettext
147-
gettext: SPHINXOPTS += -d build/doctrees-gettext
147+
gettext: override SPHINXOPTS := -d build/doctrees-gettext $(SPHINXOPTS)
148148
gettext: build
149149

150150
.PHONY: htmlview

Doc/c-api/type.rst

+23-25
Original file line numberDiff line numberDiff line change
@@ -529,19 +529,19 @@ The following functions and structs are used to create
529529
530530
The following “offset” fields cannot be set using :c:type:`PyType_Slot`:
531531
532-
* :c:member:`~PyTypeObject.tp_weaklistoffset`
533-
(use :c:macro:`Py_TPFLAGS_MANAGED_WEAKREF` instead if possible)
534-
* :c:member:`~PyTypeObject.tp_dictoffset`
535-
(use :c:macro:`Py_TPFLAGS_MANAGED_DICT` instead if possible)
536-
* :c:member:`~PyTypeObject.tp_vectorcall_offset`
537-
(use ``"__vectorcalloffset__"`` in
538-
:ref:`PyMemberDef <pymemberdef-offsets>`)
539-
540-
If it is not possible to switch to a ``MANAGED`` flag (for example,
541-
for vectorcall or to support Python older than 3.12), specify the
542-
offset in :c:member:`Py_tp_members <PyTypeObject.tp_members>`.
543-
See :ref:`PyMemberDef documentation <pymemberdef-offsets>`
544-
for details.
532+
* :c:member:`~PyTypeObject.tp_weaklistoffset`
533+
(use :c:macro:`Py_TPFLAGS_MANAGED_WEAKREF` instead if possible)
534+
* :c:member:`~PyTypeObject.tp_dictoffset`
535+
(use :c:macro:`Py_TPFLAGS_MANAGED_DICT` instead if possible)
536+
* :c:member:`~PyTypeObject.tp_vectorcall_offset`
537+
(use ``"__vectorcalloffset__"`` in
538+
:ref:`PyMemberDef <pymemberdef-offsets>`)
539+
540+
If it is not possible to switch to a ``MANAGED`` flag (for example,
541+
for vectorcall or to support Python older than 3.12), specify the
542+
offset in :c:member:`Py_tp_members <PyTypeObject.tp_members>`.
543+
See :ref:`PyMemberDef documentation <pymemberdef-offsets>`
544+
for details.
545545
546546
The following internal fields cannot be set at all when creating a heap
547547
type:
@@ -557,20 +557,18 @@ The following functions and structs are used to create
557557
To avoid issues, use the *bases* argument of
558558
:c:func:`PyType_FromSpecWithBases` instead.
559559
560-
.. versionchanged:: 3.9
561-
562-
Slots in :c:type:`PyBufferProcs` may be set in the unlimited API.
560+
.. versionchanged:: 3.9
561+
Slots in :c:type:`PyBufferProcs` may be set in the unlimited API.
563562
564-
.. versionchanged:: 3.11
565-
:c:member:`~PyBufferProcs.bf_getbuffer` and
566-
:c:member:`~PyBufferProcs.bf_releasebuffer` are now available
567-
under the :ref:`limited API <limited-c-api>`.
563+
.. versionchanged:: 3.11
564+
:c:member:`~PyBufferProcs.bf_getbuffer` and
565+
:c:member:`~PyBufferProcs.bf_releasebuffer` are now available
566+
under the :ref:`limited API <limited-c-api>`.
568567
569-
.. versionchanged:: 3.14
570-
571-
The field :c:member:`~PyTypeObject.tp_vectorcall` can now set
572-
using ``Py_tp_vectorcall``. See the field's documentation
573-
for details.
568+
.. versionchanged:: 3.14
569+
The field :c:member:`~PyTypeObject.tp_vectorcall` can now set
570+
using ``Py_tp_vectorcall``. See the field's documentation
571+
for details.
574572
575573
.. c:member:: void *pfunc
576574

InternalDocs/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# CPython Internals Documentation
32

43
The documentation in this folder is intended for CPython maintainers.

InternalDocs/adaptive.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ quality of specialization and keeping the overhead of specialization low.
9696
Specialized instructions must be fast. In order to be fast,
9797
specialized instructions should be tailored for a particular
9898
set of values that allows them to:
99+
99100
1. Verify that incoming value is part of that set with low overhead.
100101
2. Perform the operation quickly.
101102

@@ -107,9 +108,11 @@ For example, `LOAD_GLOBAL_MODULE` is specialized for `globals()`
107108
dictionaries that have a keys with the expected version.
108109

109110
This can be tested quickly:
111+
110112
* `globals->keys->dk_version == expected_version`
111113

112114
and the operation can be performed quickly:
115+
113116
* `value = entries[cache->index].me_value;`.
114117

115118
Because it is impossible to measure the performance of an instruction without
@@ -122,10 +125,11 @@ base instruction.
122125
### Implementation of specialized instructions
123126

124127
In general, specialized instructions should be implemented in two parts:
128+
125129
1. A sequence of guards, each of the form
126-
`DEOPT_IF(guard-condition-is-false, BASE_NAME)`.
130+
`DEOPT_IF(guard-condition-is-false, BASE_NAME)`.
127131
2. The operation, which should ideally have no branches and
128-
a minimum number of dependent memory accesses.
132+
a minimum number of dependent memory accesses.
129133

130134
In practice, the parts may overlap, as data required for guards
131135
can be re-used in the operation.

InternalDocs/changing_grammar.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Below is a checklist of things that may need to change.
3232
[`Include/internal/pycore_ast.h`](../Include/internal/pycore_ast.h) and
3333
[`Python/Python-ast.c`](../Python/Python-ast.c).
3434

35-
* [`Parser/lexer/`](../Parser/lexer/) contains the tokenization code.
35+
* [`Parser/lexer/`](../Parser/lexer) contains the tokenization code.
3636
This is where you would add a new type of comment or string literal, for example.
3737

3838
* [`Python/ast.c`](../Python/ast.c) will need changes to validate AST objects
@@ -60,4 +60,4 @@ Below is a checklist of things that may need to change.
6060
to the tokenizer.
6161

6262
* Documentation must be written! Specifically, one or more of the pages in
63-
[`Doc/reference/`](../Doc/reference/) will need to be updated.
63+
[`Doc/reference/`](../Doc/reference) will need to be updated.

InternalDocs/code_objects.md

+61-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Code objects
32

43
A `CodeObject` is a builtin Python type that represents a compiled executable,
@@ -43,7 +42,7 @@ so a compact format is very important.
4342
Note that traceback objects don't store all this information -- they store the start line
4443
number, for backward compatibility, and the "last instruction" value.
4544
The rest can be computed from the last instruction (`tb_lasti`) with the help of the
46-
locations table. For Python code, there is a convenience method
45+
locations table. For Python code, there is a convenience method
4746
(`codeobject.co_positions`)[https://docs.python.org/dev/reference/datamodel.html#codeobject.co_positions]
4847
which returns an iterator of `({line}, {endline}, {column}, {endcolumn})` tuples,
4948
one per instruction.
@@ -75,9 +74,11 @@ returned by the `co_positions()` iterator.
7574
> See [`Objects/lnotab_notes.txt`](../Objects/lnotab_notes.txt) for more details.
7675
7776
`co_linetable` consists of a sequence of location entries.
78-
Each entry starts with a byte with the most significant bit set, followed by zero or more bytes with the most significant bit unset.
77+
Each entry starts with a byte with the most significant bit set, followed by
78+
zero or more bytes with the most significant bit unset.
7979

8080
Each entry contains the following information:
81+
8182
* The number of code units covered by this entry (length)
8283
* The start line
8384
* The end line
@@ -86,54 +87,88 @@ Each entry contains the following information:
8687

8788
The first byte has the following format:
8889

89-
Bit 7 | Bits 3-6 | Bits 0-2
90-
---- | ---- | ----
91-
1 | Code | Length (in code units) - 1
90+
| Bit 7 | Bits 3-6 | Bits 0-2 |
91+
|-------|----------|----------------------------|
92+
| 1 | Code | Length (in code units) - 1 |
9293

9394
The codes are enumerated in the `_PyCodeLocationInfoKind` enum.
9495

95-
## Variable-length integer encodings
96+
### Variable-length integer encodings
9697

97-
Integers are often encoded using a variable-length integer encoding
98+
Integers are often encoded using a variable length integer encoding
9899

99-
### Unsigned integers (`varint`)
100+
#### Unsigned integers (`varint`)
100101

101102
Unsigned integers are encoded in 6-bit chunks, least significant first.
102103
Each chunk but the last has bit 6 set.
103104
For example:
104105

105106
* 63 is encoded as `0x3f`
106-
* 200 is encoded as `0x48`, `0x03`
107+
* 200 is encoded as `0x48`, `0x03` since ``200 = (0x03 << 6) | 0x48``.
108+
109+
The following helper can be used to convert an integer into a `varint`:
110+
111+
```py
112+
def encode_varint(s):
113+
ret = []
114+
while s >= 64:
115+
ret.append(((s & 0x3F) | 0x40) & 0x3F)
116+
s >>= 6
117+
ret.append(s & 0x3F)
118+
return bytes(ret)
119+
```
120+
121+
To convert a `varint` into an unsigned integer:
122+
123+
```py
124+
def decode_varint(chunks):
125+
ret = 0
126+
for chunk in reversed(chunks):
127+
ret = (ret << 6) | chunk
128+
return ret
129+
```
107130

108-
### Signed integers (`svarint`)
131+
#### Signed integers (`svarint`)
109132

110133
Signed integers are encoded by converting them to unsigned integers, using the following function:
111-
```Python
112-
def convert(s):
134+
135+
```py
136+
def svarint_to_varint(s):
113137
if s < 0:
114-
return ((-s)<<1) | 1
138+
return ((-s) << 1) | 1
115139
else:
116-
return (s<<1)
140+
return s << 1
141+
```
142+
143+
To convert a `varint` into a signed integer:
144+
145+
```py
146+
def varint_to_svarint(uval):
147+
return -(uval >> 1) if uval & 1 else (uval >> 1)
117148
```
118149

119-
*Location entries*
150+
### Location entries
120151

121152
The meaning of the codes and the following bytes are as follows:
122153

123-
Code | Meaning | Start line | End line | Start column | End column
124-
---- | ---- | ---- | ---- | ---- | ----
125-
0-9 | Short form | Δ 0 | Δ 0 | See below | See below
126-
10-12 | One line form | Δ (code - 10) | Δ 0 | unsigned byte | unsigned byte
127-
13 | No column info | Δ svarint | Δ 0 | None | None
128-
14 | Long form | Δ svarint | Δ varint | varint | varint
129-
15 | No location | None | None | None | None
154+
| Code | Meaning | Start line | End line | Start column | End column |
155+
|-------|----------------|---------------|----------|---------------|---------------|
156+
| 0-9 | Short form | Δ 0 | Δ 0 | See below | See below |
157+
| 10-12 | One line form | Δ (code - 10) | Δ 0 | unsigned byte | unsigned byte |
158+
| 13 | No column info | Δ svarint | Δ 0 | None | None |
159+
| 14 | Long form | Δ svarint | Δ varint | varint | varint |
160+
| 15 | No location | None | None | None | None |
130161

131162
The Δ means the value is encoded as a delta from another value:
163+
132164
* Start line: Delta from the previous start line, or `co_firstlineno` for the first entry.
133-
* End line: Delta from the start line
165+
* End line: Delta from the start line.
166+
167+
### The short forms
134168

135-
*The short forms*
169+
Codes 0-9 are the short forms. The short form consists of two bytes,
170+
the second byte holding additional column information. The code is the
171+
start column divided by 8 (and rounded down).
136172

137-
Codes 0-9 are the short forms. The short form consists of two bytes, the second byte holding additional column information. The code is the start column divided by 8 (and rounded down).
138173
* Start column: `(code*8) + ((second_byte>>4)&7)`
139174
* End column: `start_column + (second_byte&15)`

0 commit comments

Comments
 (0)