Skip to content

Commit 37bd4f6

Browse files
committed
Merge remote-tracking branch 'upstream/main' into tachyon-vmremap
2 parents 29a527b + 49a344e commit 37bd4f6

465 files changed

Lines changed: 14542 additions & 4514 deletions

File tree

Some content is hidden

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

‎.github/workflows/build.yml‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,12 @@ jobs:
167167
- true
168168
interpreter:
169169
- switch-case
170+
- tail-call
170171
exclude:
171172
# Skip Win32 on free-threaded builds
172173
- { arch: Win32, free-threading: true }
173-
include:
174-
# msvc::musttail is currently only supported on x64,
175-
# and only supported on 3.15+.
176-
- { arch: x64, free-threading: false, interpreter: tail-call }
177-
- { arch: x64, free-threading: true, interpreter: tail-call }
174+
# Skip Win32 on tail-call interpreter builds
175+
- { arch: Win32, interpreter: tail-call }
178176
uses: $/.github/workflows/reusable-windows.yml
179177
with:
180178
arch: ${{ matrix.arch }}
@@ -549,9 +547,17 @@ jobs:
549547
run: |
550548
"$BUILD_DIR/host-python/bin/python3" -m test test_sysconfig test_site test_embed
551549
- name: Configure cross build
552-
run: ./configure --prefix="$BUILD_DIR/cross-python" --with-build-python="$BUILD_DIR/host-python/bin/python3"
550+
run: |
551+
# Exercise a native build whose builtin modules differ from the build Python.
552+
echo '_bisect _bisectmodule.c' > Modules/Setup.local
553+
./configure --prefix="$BUILD_DIR/cross-python" --with-build-python="$BUILD_DIR/host-python/bin/python3"
553554
- name: Install cross Python
554555
run: make -j8 install
556+
- name: Check native extension modules with a build Python
557+
run: |
558+
"$BUILD_DIR/host-python/bin/python3" -c "import sys; assert '_bisect' not in sys.builtin_module_names"
559+
"$BUILD_DIR/cross-python/bin/python3" -c "import _bisect, sys; assert '_bisect' in sys.builtin_module_names"
560+
PYTHONSTRICTEXTENSIONBUILD=1 make checksharedmods
555561
- name: Display build info
556562
run: |
557563
"$BUILD_DIR/cross-python/bin/python3" -m test.pythoninfo

‎Doc/builtins/functions.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,11 @@ are always available. They are listed here in alphabetical order.
11811181
a :exc:`TypeError` exception is raised. :exc:`TypeError` may not be
11821182
raised for an invalid type if an earlier check succeeds.
11831183

1184+
If *object* has a ``__class__`` attribute that differs from
1185+
:func:`type(object) <type>` (for example a proxy, or a :mod:`unittest.mock`
1186+
object created with a *spec*), ``True`` is also returned when that class
1187+
is a subclass of *classinfo*.
1188+
11841189
.. versionchanged:: 3.10
11851190
*classinfo* can be a :ref:`types-union`.
11861191

‎Doc/c-api/gcsupport.rst‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type:
253253
returned immediately.
254254
Otherwise, return zero.
255255
256+
The garbage collector can call :c:member:`~PyTypeObject.tp_traverse` more
257+
than once for the same object during a collection. If the object's
258+
references do not change, each traversal must visit the same references.
259+
In particular, do not skip a reference because a lock is busy.
260+
If one traversal visits a reference and a later traversal skips it, the
261+
collector can clear an object that is still reachable.
262+
256263
A typical :c:member:`!tp_traverse` function calls the :c:func:`Py_VISIT`
257264
convenience macro on each of the instance's members that are Python
258265
objects that the instance owns.
@@ -318,14 +325,6 @@ The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type:
318325
For a list of safe functions, see a
319326
:ref:`separate section <duringgc-functions>` below.
320327
321-
If an error occurs during traversal, ignore it and return 0.
322-
If ``tp_traverse`` fails to visit some strong references, garbage reference
323-
cycles involving the skipped references will not be reliably broken,
324-
but correctness won't be affected.
325-
For example, if traversal requires a lock that cannot be acquired promptly,
326-
skip the traversal.
327-
The garbage collector will visit your object again in a future pass.
328-
329328
.. note::
330329
331330
The :c:func:`Py_VISIT` call may be skipped for those members that provably

‎Doc/c-api/stable.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ versions of Python, without recompilation.
6464

6565
For simplicity, this document talks about *extensions*, but Stable ABI
6666
works the same way for all uses of the API – for example, embedding Python.
67+
When embedding Python on Linux, see :ref:`embedding-python-versions`
68+
for how to link the application and for limitations.
6769

6870
There are two Stable ABIs:
6971

‎Doc/extending/embedding.rst‎

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,178 @@ combine together. For example:
356356
357357
358358
.. XXX similar documentation for Windows missing
359+
360+
361+
.. _embedding-python-versions:
362+
363+
Linking for one or several Python versions on Linux
364+
===================================================
365+
366+
The flags obtained from ``pythonX.Y-config --ldflags --embed`` link the
367+
application against one minor version of Python.
368+
If that Python was built as a shared library, the executable records a
369+
dependency on the versioned library, such as :file:`libpython3.12.so.1.0`,
370+
and expected that specific minor version at runtime.
371+
This is the recommended setup for most host (embedding) applications.
372+
Build the application for each Python version that it supports,
373+
or distribute it together with the Python that it was built with.
374+
375+
Using the :ref:`Limited API <limited-c-api>` does not change this.
376+
Defining :c:macro:`Py_LIMITED_API` restricts your source code to API that is
377+
compatible across minor versions, but the library dependency is recorded when
378+
linking, so the executable still requires :file:`libpython3.12.so.1.0`.
379+
380+
A binary that needs to run with several minor Python versions must use only
381+
the Limited API and must *also* avoid the versioned dependency.
382+
383+
.. warning::
384+
385+
Host applications that use the :ref:`Limited API <limited-c-api>`
386+
must be aware of the following limitations:
387+
388+
* It can only initialize Python with :c:func:`Py_Initialize` or
389+
:c:func:`Py_InitializeEx`, which set Python up much like the
390+
:program:`python` command does.
391+
They honor :envvar:`PYTHONHOME` and :envvar:`PYTHONPATH`, add the
392+
:data:`user site-packages directory <site.USER_SITE>` to
393+
:data:`sys.path`, and terminate the process if initialization fails.
394+
They set :data:`sys.executable` to the :program:`python3` program found
395+
on :envvar:`!PATH`, which can belong to a different Python installation
396+
than the library that was loaded.
397+
The functions that accept an
398+
:ref:`isolated configuration <init-isolated-conf>`,
399+
:c:func:`Py_InitializeFromConfig` and
400+
:c:func:`Py_InitializeFromInitConfig`, are not part of the Limited API.
401+
* The Python that gets loaded is only known at run-time.
402+
Checking that it is suitable is up to the application.
403+
An incompatible Python, such as a too-old or too-new version,
404+
is not rejected automatically.
405+
* The Stable ABI keeps functions available in later versions, but it does
406+
not guarantee identical behavior.
407+
The default configuration and the way :data:`sys.path` is computed can
408+
change between Python versions, and between installations of the same
409+
version.
410+
Test the application with every Python version and installation that it
411+
is meant to support.
412+
See also :ref:`limited-api-caveats`.
413+
414+
A Python built with :option:`--enable-shared` and without
415+
:option:`--with-pydebug` installs :file:`libpython3.so` next to the versioned
416+
library.
417+
When the dynamic loader loads this library, it also loads the versioned
418+
library that it was built with, and symbols looked up through
419+
:file:`libpython3.so` are found in the versioned library.
420+
An application can therefore load :file:`libpython3.so` with
421+
:manpage:`dlopen(3)` and look up each function that it uses with
422+
:manpage:`dlsym(3)`.
423+
424+
This only works at run time.
425+
When linking an executable, the linker does not use the dependencies of a
426+
library to resolve symbols, and :file:`libpython3.so` defines no Python
427+
symbols of its own, so linking with ``-lpython3`` fails with
428+
``undefined reference`` errors.
429+
430+
For example, with Python 3.12 as the oldest supported version::
431+
432+
#define Py_LIMITED_API 0x030c0000 /* Limited API 3.12 */
433+
#include <Python.h>
434+
#include <dlfcn.h>
435+
#include <stdio.h>
436+
437+
/* The API this application uses, declared as in Python.h.
438+
Everything is reached through these pointers. */
439+
static struct {
440+
const unsigned long *Version;
441+
const char *(*GetVersion)(void);
442+
void (*InitializeEx)(int);
443+
int (*FinalizeEx)(void);
444+
} py;
445+
446+
#define LOAD(lib, pointer, name) \
447+
((*(void **)&(pointer) = dlsym(lib, name)) != NULL)
448+
449+
int
450+
main(int argc, char *argv[])
451+
{
452+
/* For brevity, the path comes from the command line. A real
453+
application reads it from its own configuration. */
454+
if (argc != 2 || argv[1][0] != '/') {
455+
fprintf(stderr, "usage: %s /absolute/path/to/libpython3.so\n",
456+
argv[0]);
457+
return 2;
458+
}
459+
/* RTLD_GLOBAL is required: it lets the extension modules that
460+
Python imports find Python's symbols. */
461+
void *lib = dlopen(argv[1], RTLD_NOW | RTLD_GLOBAL);
462+
if (lib == NULL) {
463+
fprintf(stderr, "Python is not available: %s\n", dlerror());
464+
return 1;
465+
}
466+
if (!LOAD(lib, py.Version, "Py_Version")
467+
|| !LOAD(lib, py.GetVersion, "Py_GetVersion")
468+
|| !LOAD(lib, py.InitializeEx, "Py_InitializeEx")
469+
|| !LOAD(lib, py.FinalizeEx, "Py_FinalizeEx"))
470+
{
471+
fprintf(stderr, "Unsupported Python: %s\n", dlerror());
472+
return 1;
473+
}
474+
if (*py.Version < Py_LIMITED_API) {
475+
fprintf(stderr, "Python %s is older than 3.12\n", py.GetVersion());
476+
return 1;
477+
}
478+
479+
py.InitializeEx(0);
480+
printf("Loaded Python %s\n", py.GetVersion());
481+
return py.FinalizeEx() < 0 ? 120 : 0;
482+
}
483+
484+
Compile the program with Python's headers, but without the output of
485+
``--ldflags``:
486+
487+
.. code-block:: shell-session
488+
489+
$ gcc $(/opt/bin/python3.12-config --includes) -o myapp myapp.c
490+
$ ./myapp /usr/local/lib/libpython3.so
491+
Loaded Python 3.14.10 (main, Nov 5 2026, 09:47:10) [GCC 14.2.0]
492+
493+
No Python library is passed to the linker, so the executable does not depend
494+
on one.
495+
You can check this with ``readelf``:
496+
497+
.. code-block:: shell-session
498+
499+
$ readelf -d myapp | grep NEEDED
500+
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
501+
502+
For the same reason, Python functions can only be called through such
503+
pointers.
504+
This includes the functions that macros in :file:`Python.h` expand to:
505+
for example, call :c:func:`Py_DecRef` through a pointer instead of using
506+
:c:func:`Py_DECREF`.
507+
508+
* The path passed to ``dlopen()`` selects the code that runs in your process.
509+
It should come from a trusted source and name a file that only trusted
510+
users can replace.
511+
* Use an absolute path.
512+
The dynamic loader looks for a name without a slash in its usual search
513+
path, such as :envvar:`!LD_LIBRARY_PATH` and the system library directories.
514+
It looks for the versioned library that :file:`libpython3.so` depends on in
515+
the same way, and not next to :file:`libpython3.so`.
516+
To choose the exact file, pass the absolute path of the versioned library
517+
to ``dlopen()`` instead.
518+
* ``RTLD_GLOBAL`` is required.
519+
Extension modules are generally not linked against a Python library,
520+
so Python can only import them if its symbols are visible globally.
521+
522+
.. rubric:: Unsupported workaround: renaming the dependency after linking
523+
524+
Some applications are instead linked against a versioned library as described
525+
:ref:`above <compiling>`, and the dependency recorded in the executable is
526+
then renamed to :file:`libpython3.so`, for example with ``patchelf``.
527+
528+
.. warning::
529+
530+
Renaming the dependency this way is unsupported.
531+
Nothing checks that such an executable only uses the Stable ABI, and it
532+
can contain copies of Python data, such as ``PyLong_Type``, whose size
533+
differs between Python versions.

‎Doc/howto/abi3t-migration.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ See the :ref:`PyInit documentation <extension-pyinit>` for related information.
256256
The function references a ``PyModuleDef`` object (``<moddef>`` in the code
257257
above).
258258
Its definition should be similar to the following, with different values
259-
and perhaps some fields unnnamed or left out:
259+
and perhaps some fields unnamed or left out:
260260

261261
.. code-block::
262262
:class: bad

0 commit comments

Comments
 (0)