Skip to content

Commit cc49d0f

Browse files
authored
Merge branch 'main' into posix-spawn-test
2 parents f25900e + c919d02 commit cc49d0f

File tree

88 files changed

+1481
-371
lines changed

Some content is hidden

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

88 files changed

+1481
-371
lines changed

Doc/c-api/module.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,28 @@ The available slot types are:
388388
389389
.. versionadded:: 3.13
390390
391+
.. c:macro:: Py_mod_abi
392+
393+
A pointer to a :c:struct:`PyABIInfo` structure that describes the ABI that
394+
the extension is using.
395+
396+
When the module is loaded, the :c:struct:`!PyABIInfo` in this slot is checked
397+
using :c:func:`PyABIInfo_Check`.
398+
399+
A suitable :c:struct:`!PyABIInfo` variable can be defined using the
400+
:c:macro:`PyABIInfo_VAR` macro, as in:
401+
402+
.. code-block:: c
403+
404+
PyABIInfo_VAR(abi_info);
405+
406+
static PyModuleDef_Slot mymodule_slots[] = {
407+
{Py_mod_abi, &abi_info},
408+
...
409+
};
410+
411+
.. versionadded:: 3.15
412+
391413
392414
.. _moduledef-dynamic:
393415

Doc/c-api/stable.rst

Lines changed: 159 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
.. _stable:
44

5-
***************
6-
C API Stability
7-
***************
5+
***********************
6+
C API and ABI Stability
7+
***********************
88

99
Unless documented otherwise, Python's C API is covered by the Backwards
1010
Compatibility Policy, :pep:`387`.
@@ -199,6 +199,162 @@ This is the case with Windows and macOS releases from ``python.org`` and many
199199
third-party distributors.
200200

201201

202+
ABI Checking
203+
============
204+
205+
.. versionadded:: next
206+
207+
Python includes a rudimentary check for ABI compatibility.
208+
209+
This check is not comprehensive.
210+
It only guards against common cases of incompatible modules being
211+
installed for the wrong interpreter.
212+
It also does not take :ref:`platform incompatibilities <stable-abi-platform>`
213+
into account.
214+
It can only be done after an extension is successfully loaded.
215+
216+
Despite these limitations, it is recommended that extension modules use this
217+
mechanism, so that detectable incompatibilities raise exceptions rather than
218+
crash.
219+
220+
Most modules can use this check via the :c:data:`Py_mod_abi`
221+
slot and the :c:macro:`PyABIInfo_VAR` macro, for example like this:
222+
223+
.. code-block:: c
224+
225+
PyABIInfo_VAR(abi_info);
226+
227+
static PyModuleDef_Slot mymodule_slots[] = {
228+
{Py_mod_abi, &abi_info},
229+
...
230+
};
231+
232+
233+
The full API is described below for advanced use cases.
234+
235+
.. c:function:: int PyABIInfo_Check(PyABIInfo *info, const char *module_name)
236+
237+
Verify that the given *info* is compatible with the currently running
238+
interpreter.
239+
240+
Return 0 on success. On failure, raise an exception and return -1.
241+
242+
If the ABI is incompatible, the raised exception will be :py:exc:`ImportError`.
243+
244+
The *module_name* argument can be ``NULL``, or point to a NUL-terminated
245+
UTF-8-encoded string used for error messages.
246+
247+
Note that if *info* describes the ABI that the current code uses (as defined
248+
by :c:macro:`PyABIInfo_VAR`, for example), using any other Python C API
249+
may lead to crashes.
250+
In particular, it is not safe to examine the raised exception.
251+
252+
.. versionadded:: next
253+
254+
.. c:macro:: PyABIInfo_VAR(NAME)
255+
256+
Define a static :c:struct:`PyABIInfo` variable with the given *NAME* that
257+
describes the ABI that the current code will use.
258+
This macro expands to:
259+
260+
.. code-block:: c
261+
262+
static PyABIInfo NAME = {
263+
1, 0,
264+
PyABIInfo_DEFAULT_FLAGS,
265+
PY_VERSION_HEX,
266+
PyABIInfo_DEFAULT_ABI_VERSION
267+
}
268+
269+
.. versionadded:: next
270+
271+
.. c:type:: PyABIInfo
272+
273+
.. c:member:: uint8_t abiinfo_major_version
274+
275+
The major version of :c:struct:`PyABIInfo`. Can be set to:
276+
277+
* ``0`` to skip all checking, or
278+
* ``1`` to specify this version of :c:struct:`!PyABIInfo`.
279+
280+
.. c:member:: uint8_t abiinfo_minor_version
281+
282+
The major version of :c:struct:`PyABIInfo`.
283+
Must be set to ``0``; larger values are reserved for backwards-compatible
284+
future versions of :c:struct:`!PyABIInfo`.
285+
286+
.. c:member:: uint16_t flags
287+
288+
.. c:namespace:: NULL
289+
290+
This field is usually set to the following macro:
291+
292+
.. c:macro:: PyABIInfo_DEFAULT_FLAGS
293+
294+
Default flags, based on current values of macros such as
295+
:c:macro:`Py_LIMITED_API` and :c:macro:`Py_GIL_DISABLED`.
296+
297+
Alternately, the field can be set to to the following flags, combined
298+
by bitwise OR.
299+
Unused bits must be set to zero.
300+
301+
ABI variant -- one of:
302+
303+
.. c:macro:: PyABIInfo_STABLE
304+
305+
Specifies that the stable ABI is used.
306+
307+
.. c:macro:: PyABIInfo_INTERNAL
308+
309+
Specifies ABI specific to a particular build of CPython.
310+
Internal use only.
311+
312+
Free-threading compatibility -- one of:
313+
314+
.. c:macro:: PyABIInfo_FREETHREADED
315+
316+
Specifies ABI compatible with free-threading builds of CPython.
317+
(That is, ones compiled with :option:`--disable-gil`; with ``t``
318+
in :py:data:`sys.abiflags`)
319+
320+
.. c:macro:: PyABIInfo_GIL
321+
322+
Specifies ABI compatible with non-free-threading builds of CPython
323+
(ones compiled *without* :option:`--disable-gil`).
324+
325+
.. c:member:: uint32_t build_version
326+
327+
The version of the Python headers used to build the code, in the format
328+
used by :c:macro:`PY_VERSION_HEX`.
329+
330+
This can be set to ``0`` to skip any checks related to this field.
331+
This option is meant mainly for projects that do not use the CPython
332+
headers directly, and do not emulate a specific version of them.
333+
334+
.. c:member:: uint32_t abi_version
335+
336+
The ABI version.
337+
338+
For the Stable ABI, this field should be the value of
339+
:c:macro:`Py_LIMITED_API`
340+
(except if :c:macro:`Py_LIMITED_API` is ``3``; use
341+
:c:expr:`Py_PACK_VERSION(3, 2)` in that case).
342+
343+
Otherwise, it should be set to :c:macro:`PY_VERSION_HEX`.
344+
345+
It can also be set to ``0`` to skip any checks related to this field.
346+
347+
.. c:namespace:: NULL
348+
349+
.. c:macro:: PyABIInfo_DEFAULT_ABI_VERSION
350+
351+
The value that should be used for this field, based on current
352+
values of macros such as :c:macro:`Py_LIMITED_API`,
353+
:c:macro:`PY_VERSION_HEX` and :c:macro:`Py_GIL_DISABLED`.
354+
355+
.. versionadded:: next
356+
357+
202358
.. _limited-api-list:
203359

204360
Contents of Limited API

Doc/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@
245245
('py:attr', '__annotations__'),
246246
('py:meth', '__missing__'),
247247
('py:attr', '__wrapped__'),
248-
('py:meth', 'index'), # list.index, tuple.index, etc.
249248
]
250249

251250
# gh-106948: Copy standard C types declared in the "c:type" domain and C

Doc/data/stable_abi.dat

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

Doc/faq/design.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,9 @@ exhaustive test suites that exercise every line of code in a module.
591591
An appropriate testing discipline can help build large complex applications in
592592
Python as well as having interface specifications would. In fact, it can be
593593
better because an interface specification cannot test certain properties of a
594-
program. For example, the :meth:`!list.append` method is expected to add new elements
594+
program. For example, the :meth:`list.append` method is expected to add new elements
595595
to the end of some internal list; an interface specification cannot test that
596-
your :meth:`!list.append` implementation will actually do this correctly, but it's
596+
your :meth:`list.append` implementation will actually do this correctly, but it's
597597
trivial to check this property in a test suite.
598598

599599
Writing test suites is very helpful, and you might want to design your code to

Doc/faq/programming.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ There are two factors that produce this result:
454454
(the list), and both ``x`` and ``y`` refer to it.
455455
2) Lists are :term:`mutable`, which means that you can change their content.
456456

457-
After the call to :meth:`!append`, the content of the mutable object has
457+
After the call to :meth:`~sequence.append`, the content of the mutable object has
458458
changed from ``[]`` to ``[10]``. Since both the variables refer to the same
459459
object, using either name accesses the modified value ``[10]``.
460460

@@ -1397,9 +1397,9 @@ To see why this happens, you need to know that (a) if an object implements an
13971397
:meth:`~object.__iadd__` magic method, it gets called when the ``+=`` augmented
13981398
assignment
13991399
is executed, and its return value is what gets used in the assignment statement;
1400-
and (b) for lists, :meth:`!__iadd__` is equivalent to calling :meth:`!extend` on the list
1401-
and returning the list. That's why we say that for lists, ``+=`` is a
1402-
"shorthand" for :meth:`!list.extend`::
1400+
and (b) for lists, :meth:`!__iadd__` is equivalent to calling
1401+
:meth:`~sequence.extend` on the list and returning the list.
1402+
That's why we say that for lists, ``+=`` is a "shorthand" for :meth:`list.extend`::
14031403

14041404
>>> a_list = []
14051405
>>> a_list += [1]

Doc/glossary.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,8 +1251,9 @@ Glossary
12511251
The :class:`collections.abc.Sequence` abstract base class
12521252
defines a much richer interface that goes beyond just
12531253
:meth:`~object.__getitem__` and :meth:`~object.__len__`, adding
1254-
:meth:`!count`, :meth:`!index`, :meth:`~object.__contains__`, and
1255-
:meth:`~object.__reversed__`. Types that implement this expanded
1254+
:meth:`~sequence.count`, :meth:`~sequence.index`,
1255+
:meth:`~object.__contains__`, and :meth:`~object.__reversed__`.
1256+
Types that implement this expanded
12561257
interface can be registered explicitly using
12571258
:func:`~abc.ABCMeta.register`. For more documentation on sequence
12581259
methods generally, see

Doc/library/annotationlib.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ and :func:`call_annotate_function`, as well as the
4646
:func:`call_evaluate_function` function for working with
4747
:term:`evaluate functions <evaluate function>`.
4848

49+
.. caution::
50+
51+
Most functionality in this module can execute arbitrary code; see
52+
:ref:`the security section <annotationlib-security>` for more information.
4953

5054
.. seealso::
5155

@@ -604,3 +608,23 @@ Below are a few examples of the behavior with unsupported expressions:
604608
>>> def ifexp(x: 1 if y else 0): ...
605609
>>> get_annotations(ifexp, format=Format.STRING)
606610
{'x': '1'}
611+
612+
.. _annotationlib-security:
613+
614+
Security implications of introspecting annotations
615+
--------------------------------------------------
616+
617+
Much of the functionality in this module involves executing code related to annotations,
618+
which can then do arbitrary things. For example,
619+
:func:`get_annotations` may call an arbitrary :term:`annotate function`, and
620+
:meth:`ForwardRef.evaluate` may call :func:`eval` on an arbitrary string. Code contained
621+
in an annotation might make arbitrary system calls, enter an infinite loop, or perform any
622+
other operation. This is also true for any access of the :attr:`~object.__annotations__` attribute,
623+
and for various functions in the :mod:`typing` module that work with annotations, such as
624+
:func:`typing.get_type_hints`.
625+
626+
Any security issue arising from this also applies immediately after importing
627+
code that may contain untrusted annotations: importing code can always cause arbitrary operations
628+
to be performed. However, it is unsafe to accept strings or other input from an untrusted source and
629+
pass them to any of the APIs for introspecting annotations, for example by editing an
630+
``__annotations__`` dictionary or directly creating a :class:`ForwardRef` object.

Doc/library/bisect.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ The following functions are provided:
8383
Insert *x* in *a* in sorted order.
8484

8585
This function first runs :py:func:`~bisect.bisect_left` to locate an insertion point.
86-
Next, it runs the :meth:`!insert` method on *a* to insert *x* at the
86+
Next, it runs the :meth:`~sequence.insert` method on *a* to insert *x* at the
8787
appropriate position to maintain sort order.
8888

8989
To support inserting records in a table, the *key* function (if any) is
@@ -103,7 +103,7 @@ The following functions are provided:
103103
entries of *x*.
104104

105105
This function first runs :py:func:`~bisect.bisect_right` to locate an insertion point.
106-
Next, it runs the :meth:`!insert` method on *a* to insert *x* at the
106+
Next, it runs the :meth:`~sequence.insert` method on *a* to insert *x* at the
107107
appropriate position to maintain sort order.
108108

109109
To support inserting records in a table, the *key* function (if any) is

Doc/library/collections.abc.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,9 @@ Collections Abstract Base Classes -- Detailed Descriptions
264264
ABCs for read-only and mutable :term:`sequences <sequence>`.
265265

266266
Implementation note: Some of the mixin methods, such as
267-
:meth:`~container.__iter__`, :meth:`~object.__reversed__` and :meth:`index`, make
268-
repeated calls to the underlying :meth:`~object.__getitem__` method.
267+
:meth:`~container.__iter__`, :meth:`~object.__reversed__`,
268+
and :meth:`~sequence.index` make repeated calls to the underlying
269+
:meth:`~object.__getitem__` method.
269270
Consequently, if :meth:`~object.__getitem__` is implemented with constant
270271
access speed, the mixin methods will have linear performance;
271272
however, if the underlying method is linear (as it would be with a
@@ -281,8 +282,8 @@ Collections Abstract Base Classes -- Detailed Descriptions
281282
Supporting the *start* and *stop* arguments is optional, but recommended.
282283

283284
.. versionchanged:: 3.5
284-
The :meth:`!index` method added support for *stop* and *start*
285-
arguments.
285+
The :meth:`~sequence.index` method gained support for
286+
the *stop* and *start* arguments.
286287

287288
.. class:: Set
288289
MutableSet

0 commit comments

Comments
 (0)