Skip to content

Commit 88465c1

Browse files
authored
Merge branch 'main' into pth2toml
2 parents cdcd9cd + 2faceee commit 88465c1

71 files changed

Lines changed: 4630 additions & 1894 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Doc/c-api/module.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,12 @@ remove it.
685685
Usually, there is only one variable of this type for each extension module
686686
defined this way.
687687
688+
The struct, including all members, is part of the
689+
:ref:`Stable ABI <stable-abi>` for non-free-threaded builds (``abi3``).
690+
In the Stable ABI for free-threaded builds (``abi3t``),
691+
this struct is opaque, and unusable in practice; see :ref:`pymoduledef_slot`
692+
for a replacement.
693+
688694
.. c:member:: PyModuleDef_Base m_base
689695
690696
Always initialize this member to :c:macro:`PyModuleDef_HEAD_INIT`:
@@ -695,6 +701,11 @@ remove it.
695701
696702
The type of :c:member:`!PyModuleDef.m_base`.
697703

704+
The struct is part of the :ref:`Stable ABI <stable-abi>` for
705+
non-free-threaded builds (``abi3``).
706+
In the Stable ABI for Free-Threaded Builds
707+
(``abi3t``), this struct is opaque, and unusable in practice.
708+
698709
.. c:macro:: PyModuleDef_HEAD_INIT
699710
700711
The required initial value for :c:member:`!PyModuleDef.m_base`.

Doc/c-api/stable.rst

Lines changed: 173 additions & 97 deletions
Large diffs are not rendered by default.

Doc/c-api/structures.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ under :ref:`reference counting <countingrefs>`.
3333
The members must not be accessed directly; instead use macros such as
3434
:c:macro:`Py_REFCNT` and :c:macro:`Py_TYPE`.
3535

36+
In the :ref:`Stable ABI <stable-abi>` for Free-Threaded Builds (``abi3t``),
37+
this struct is opaque; its size and layout may change between
38+
Python versions.
39+
In Stable ABI for non-free-threaded builds (``abi3``), the
40+
:c:member:`!ob_refcnt` and :c:member:`!ob_type` fields are available,
41+
but using them directly is discouraged.
42+
3643
.. c:member:: Py_ssize_t ob_refcnt
3744
3845
The object's reference count, as returned by :c:macro:`Py_REFCNT`.
@@ -72,6 +79,19 @@ under :ref:`reference counting <countingrefs>`.
7279
instead use macros such as :c:macro:`Py_SIZE`, :c:macro:`Py_REFCNT` and
7380
:c:macro:`Py_TYPE`.
7481

82+
In the :ref:`Stable ABI <stable-abi>` for Free-Threaded Builds (``abi3t``),
83+
this struct is opaque; its size and layout may change between
84+
Python versions.
85+
In Stable ABI for non-free-threaded builds (``abi3``), the
86+
:c:member:`!ob_base` and :c:member:`!ob_size` fields are available,
87+
but using them directly is discouraged.
88+
89+
.. c:member:: PyObject ob_base
90+
91+
Common object header.
92+
Typically, this field is not accessed directly; instead
93+
:c:type:`!PyVarObject` can be cast to :c:type:`PyObject`.
94+
7595
.. c:member:: Py_ssize_t ob_size
7696
7797
A size field, whose contents should be considered an object's internal

Doc/c-api/subinterpreters.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,27 @@ High-level APIs
399399
400400
.. versionadded:: 3.9
401401
402+
.. c:function:: void _PyInterpreterState_SetEvalFrameAllowSpecialization(PyInterpreterState *interp, int allow_specialization)
403+
404+
Enables or disables specialization why a custom frame evaluator is in place.
405+
406+
If *allow_specialization* is non-zero, the adaptive specializer will
407+
continue to specialize bytecodes even though a custom eval frame function
408+
is set. When *allow_specialization* is zero, setting a custom eval frame
409+
disables specialization. The standard interpreter loop will continue to deopt
410+
while a frame evaluation API is in place - the frame evaluation function needs
411+
to handle the specialized opcodes to take advantage of this.
412+
413+
.. versionadded:: 3.15
414+
415+
.. c:function:: int _PyInterpreterState_IsSpecializationEnabled(PyInterpreterState *interp)
416+
417+
Return non-zero if adaptive specialization is enabled for the interpreter.
418+
Specialization is enabled when no custom eval frame function is set, or
419+
when one is set with *allow_specialization* enabled.
420+
421+
.. versionadded:: 3.15
422+
402423
403424
Low-level APIs
404425
--------------

Doc/data/stable_abi.dat

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/library/enum.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ Data types
502502
Using :class:`auto` with :class:`Enum` results in integers of increasing value,
503503
starting with ``1``.
504504

505-
.. versionchanged:: 3.12 Added :ref:`enum-dataclass-support`
505+
.. versionchanged:: 3.12 Added :ref:`enum-dataclass-support`.
506506

507507
.. method:: Enum._add_alias_
508508

@@ -977,9 +977,9 @@ Utilities and decorators
977977

978978
*auto* can be used in place of a value. If used, the *Enum* machinery will
979979
call an :class:`Enum`'s :meth:`~Enum._generate_next_value_` to get an appropriate value.
980-
For :class:`Enum` and :class:`IntEnum` that appropriate value will be the last value plus
981-
one; for :class:`Flag` and :class:`IntFlag` it will be the first power-of-two greater
982-
than the highest value; for :class:`StrEnum` it will be the lower-cased version of
980+
For :class:`Enum` and :class:`IntEnum` that appropriate value will be the highest value seen
981+
plus one; for :class:`Flag` and :class:`IntFlag` it will be the first power-of-two greater
982+
than the highest value seen; for :class:`StrEnum` it will be the lower-cased version of
983983
the member's name. Care must be taken if mixing *auto()* with manually
984984
specified values.
985985

@@ -989,8 +989,8 @@ Utilities and decorators
989989
* ``FIRST = auto()`` will work (auto() is replaced with ``1``);
990990
* ``SECOND = auto(), -2`` will work (auto is replaced with ``2``, so ``2, -2`` is
991991
used to create the ``SECOND`` enum member;
992-
* ``THREE = [auto(), -3]`` will *not* work (``[<auto instance>, -3]`` is used to
993-
create the ``THREE`` enum member)
992+
* ``THIRD = [auto(), -3]`` will *not* work (``[<auto instance>, -3]`` is used to
993+
create the ``THIRD`` enum member)
994994

995995
.. versionchanged:: 3.11.1
996996

@@ -1000,7 +1000,7 @@ Utilities and decorators
10001000
``_generate_next_value_`` can be overridden to customize the values used by
10011001
*auto*.
10021002

1003-
.. note:: in 3.13 the default ``_generate_next_value_`` will always return
1003+
.. note:: In version 3.13 the default ``_generate_next_value_`` will always return
10041004
the highest member value incremented by 1, and will fail if any
10051005
member is an incompatible type.
10061006

@@ -1010,7 +1010,7 @@ Utilities and decorators
10101010
enumerations. It allows member attributes to have the same names as members
10111011
themselves.
10121012

1013-
.. note:: the *property* and the member must be defined in separate classes;
1013+
.. note:: The *property* and the member must be defined in separate classes;
10141014
for example, the *value* and *name* attributes are defined in the
10151015
*Enum* class, and *Enum* subclasses can define members with the
10161016
names ``value`` and ``name``.

Doc/tools/extensions/c_annotations.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,17 @@ def _stable_abi_annotation(
249249
reftype="ref",
250250
refexplicit="False",
251251
)
252-
struct_abi_kind = record.struct_abi_kind
253-
if struct_abi_kind in {"opaque", "members"}:
254-
ref_node += nodes.Text(sphinx_gettext("Limited API"))
255-
else:
256-
ref_node += nodes.Text(sphinx_gettext("Stable ABI"))
252+
ref_node += nodes.Text(sphinx_gettext("Stable ABI"))
257253
emph_node += ref_node
254+
struct_abi_kind = record.struct_abi_kind
258255
if struct_abi_kind == "opaque":
259256
emph_node += nodes.Text(" " + sphinx_gettext("(as an opaque struct)"))
260257
elif struct_abi_kind == "full-abi":
261258
emph_node += nodes.Text(
262259
" " + sphinx_gettext("(including all members)")
263260
)
261+
elif struct_abi_kind in {"members", "abi3t-opaque"}:
262+
emph_node += nodes.Text(" " + sphinx_gettext("(see below)"))
264263
if record.ifdef_note:
265264
emph_node += nodes.Text(f" {record.ifdef_note}")
266265
if stable_added == "3.2":
@@ -271,11 +270,7 @@ def _stable_abi_annotation(
271270
" " + sphinx_gettext("since version %s") % stable_added
272271
)
273272
emph_node += nodes.Text(".")
274-
if struct_abi_kind == "members":
275-
msg = " " + sphinx_gettext(
276-
"(Only some members are part of the stable ABI.)"
277-
)
278-
emph_node += nodes.Text(msg)
273+
279274
return emph_node
280275

281276

@@ -378,6 +373,33 @@ def run(self) -> list[nodes.Node]:
378373
return [node]
379374

380375

376+
class VersionHexCheatsheet(SphinxDirective):
377+
"""Show results of Py_PACK_VERSION(3, x) for a few relevant Python versions
378+
379+
This is useful for defining version before Python.h is included.
380+
It should auto-update with the version being documented, so it must be an
381+
extension.
382+
"""
383+
384+
has_content = False
385+
required_arguments = 0
386+
optional_arguments = 0
387+
final_argument_whitespace = True
388+
389+
def run(self) -> list[nodes.Node]:
390+
content = [
391+
".. code-block:: c",
392+
"",
393+
]
394+
current_minor = int(self.config.version.removeprefix('3.'))
395+
for minor in range(current_minor - 5, current_minor + 1):
396+
value = (3 << 24) | (minor << 16)
397+
content.append(f' {value:#x} /* Py_PACK_VERSION(3.{minor}) */')
398+
node = nodes.paragraph()
399+
self.state.nested_parse(StringList(content), 0, node)
400+
return [node]
401+
402+
381403
class CorrespondingTypeSlot(SphinxDirective):
382404
"""Type slot annotations
383405
@@ -443,6 +465,7 @@ def setup(app: Sphinx) -> ExtensionMetadata:
443465
app.add_config_value("stable_abi_file", "", "env", types={str})
444466
app.add_config_value("threadsafety_file", "", "env", types={str})
445467
app.add_directive("limited-api-list", LimitedAPIList)
468+
app.add_directive("version-hex-cheatsheet", VersionHexCheatsheet)
446469
app.add_directive("corresponding-type-slot", CorrespondingTypeSlot)
447470
app.connect("builder-inited", init_annotations)
448471
app.connect("doctree-read", add_annotations)

Doc/whatsnew/3.15.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Summary -- Release highlights
8282
<whatsnew315-typeform>`
8383
* :pep:`782`: :ref:`A new PyBytesWriter C API to create a Python bytes object
8484
<whatsnew315-pybyteswriter>`
85+
* :pep:`803`: :ref:`Stable ABI for Free-Threaded Builds <whatsnew315-abi3t>`
8586
* :ref:`The JIT compiler has been significantly upgraded <whatsnew315-jit>`
8687
* :ref:`Improved error messages <whatsnew315-improved-error-messages>`
8788
* :ref:`The official Windows 64-bit binaries now use the tail-calling interpreter
@@ -381,6 +382,41 @@ agen() for x in a)``.
381382

382383
(Contributed by Adam Hartz in :gh:`143055`.)
383384

385+
.. _whatsnew315-abi3t:
386+
387+
:pep:`803`: ``abi3t`` -- Stable ABI for Free-Threaded Builds
388+
------------------------------------------------------------
389+
390+
C extensions that target the :ref:`Stable ABI <stable-abi>` can now be
391+
compiled for the new *Stable ABI for Free-Threaded Builds* (also known
392+
as ``abi3t``), which makes them compatible with
393+
:term:`free-threaded builds <free-threaded build>` of CPython.
394+
This usually requires some non-trivial changes to the source code;
395+
specifically:
396+
397+
- Switching to API introduced in :pep:`697` (Python 3.12), such as
398+
negative :c:member:`~PyType_Spec.basicsize` and
399+
:c:func:`PyObject_GetTypeData`, rather than making :c:type:`PyObject`
400+
part of the instance struct; and
401+
- Switching from a ``PyInit_`` function to a new export hook,
402+
:c:func:`PyModExport_* <PyModExport_modulename>`, introduced for this
403+
purpose in :pep:`793`.
404+
405+
Note that Stable ABI does not offer all the functionality that CPython
406+
has to offer.
407+
Extensions that cannot switch to ``abi3t`` should continue to build for
408+
the existing Stable ABI (``abi3``) and the version-specific ABI for
409+
free-threading (``cp315t``) separately.
410+
411+
Stable ABI for Free-Threaded Builds should typically
412+
be selected in a build tool (such as, for example, Setuptools, meson-python,
413+
scikit-build-core, or Maturin).
414+
At the time of writing, these tools did **not** support ``abi3t``.
415+
If this is the case for your tool, compile for ``cp315t`` separately.
416+
If not using a build tool -- or when writing such a tool -- you can select
417+
``abi3t`` by setting the macro :c:macro:`!Py_TARGET_ABI3T` as discussed
418+
in :ref:`abi3-compiling`.
419+
384420

385421
.. _whatsnew315-improved-error-messages:
386422

Include/cpython/object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ struct _typeobject {
230230
destructor tp_finalize;
231231
vectorcallfunc tp_vectorcall;
232232

233+
/* Below here all fields are internal to the VM */
234+
233235
/* bitset of which type-watchers care about this type */
234236
unsigned char tp_watched;
235237

@@ -239,6 +241,7 @@ struct _typeobject {
239241
* Otherwise, limited to MAX_VERSIONS_PER_CLASS (defined elsewhere).
240242
*/
241243
uint16_t tp_versions_used;
244+
_Py_iteritemfunc _tp_iteritem; /* Virtual iterator next function */
242245
};
243246

244247
#define _Py_ATTR_CACHE_UNUSED (30000) // (see tp_versions_used)

Include/cpython/pystate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,8 @@ PyAPI_FUNC(_PyFrameEvalFunction) _PyInterpreterState_GetEvalFrameFunc(
319319
PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameFunc(
320320
PyInterpreterState *interp,
321321
_PyFrameEvalFunction eval_frame);
322+
PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameAllowSpecialization(
323+
PyInterpreterState *interp,
324+
int allow_specialization);
325+
PyAPI_FUNC(int) _PyInterpreterState_IsSpecializationEnabled(
326+
PyInterpreterState *interp);

0 commit comments

Comments
 (0)