Skip to content

Commit 9139a23

Browse files
committed
Merge remote-tracking branch 'origin/main' into ac-ssize-t-asssize
# Conflicts: # Modules/clinic/_elementtree.c.h # Modules/clinic/_operator.c.h # Modules/clinic/_testclinic.c.h # Modules/clinic/_testclinic_kwds.c.h # Modules/clinic/arraymodule.c.h
2 parents ca4cba0 + 846acf1 commit 9139a23

139 files changed

Lines changed: 2943 additions & 989 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.

‎.gitignore‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*.orig
1818
*.pyc
1919
*.pyd
20-
*.pyo
2120
*.rej
2221
*.swp
2322
*.pickle

‎Doc/builtins/functions.rst‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,14 +2416,17 @@ are always available. They are listed here in alphabetical order.
24162416
This is an advanced function that is not needed in everyday Python
24172417
programming, unlike :func:`importlib.import_module`.
24182418

2419-
This function is invoked by the :keyword:`import` statement. It can be
2420-
replaced (by importing the :mod:`builtins` module and assigning to
2421-
``builtins.__import__``) in order to change semantics of the
2422-
:keyword:`!import` statement, but doing so is **strongly** discouraged as it
2423-
is usually simpler to use import hooks (see :pep:`302`) to attain the same
2424-
goals and does not cause issues with code which assumes the default import
2425-
implementation is in use. Direct use of :func:`__import__` is also
2426-
discouraged in favor of :func:`importlib.import_module`.
2419+
Eager :keyword:`import` statements call this function.
2420+
For :ref:`lazy imports <lazy-imports>`, Python calls this function at
2421+
resolution.
2422+
2423+
You can replace :func:`__import__` through an assignment to
2424+
``builtins.__import__``. :ref:`Import hooks <import-hooks>` usually provide a
2425+
simpler way to customize imports. A replacement can cause problems for code
2426+
that expects the default import implementation.
2427+
Do not replace :func:`__import__` unless import hooks cannot supply the
2428+
required behavior.
2429+
To import a module by name, use :func:`importlib.import_module`.
24272430

24282431
The function imports the module *name*, potentially using the given *globals*
24292432
and *locals* to determine how to interpret the name in a package context.

‎Doc/c-api/import.rst‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,12 +371,13 @@ Importing Modules
371371
372372
.. c:function:: int PyImport_SetLazyImportsFilter(PyObject *filter)
373373
374-
Sets the current lazy imports filter. The *filter* should be a callable that
375-
will receive ``(importing_module_name, imported_module_name, [fromlist])``
376-
when an import can potentially be lazy. The ``imported_module_name`` value
377-
is the resolved module name, so ``lazy from .spam import eggs`` passes
378-
``package.spam``. The callable must return ``True`` if the import should be
379-
lazy and ``False`` otherwise.
374+
Set the lazy import filter for the current interpreter.
375+
The *filter* must be a callable, ``NULL``, or :c:data:`Py_None`.
376+
Use ``NULL`` or :c:data:`Py_None` to clear the filter.
377+
The callable receives three positional arguments:
378+
``(importing_module_name, imported_module_name, fromlist)``.
379+
See :func:`sys.set_lazy_imports_filter` for the arguments and filter
380+
behavior.
380381
381382
Return ``0`` on success and ``-1`` with an exception set otherwise.
382383
@@ -388,11 +389,13 @@ Importing Modules
388389
389390
.. c:enumerator:: PyImport_LAZY_NORMAL
390391
391-
Respect the ``lazy`` keyword in source code. This is the default mode.
392+
Respect :keyword:`lazy` and :attr:`module.__lazy_modules__`.
393+
This is the default mode.
392394
393395
.. c:enumerator:: PyImport_LAZY_ALL
394396
395-
Make all imports lazy by default.
397+
Make eligible module-scope imports lazy by default.
398+
See the :ref:`lazy import restrictions <lazy-imports>`.
396399
397400
.. versionadded:: 3.15
398401

‎Doc/c-api/init_config.rst‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,9 +1607,11 @@ PyConfig
16071607
16081608
Control :ref:`lazy imports <lazy-imports>`.
16091609
1610-
A value of ``-1`` keeps the default behaviour, where only imports marked
1611-
with the ``lazy`` keyword are lazy. A value of ``1`` makes every import
1612-
lazy. ``0`` is rejected.
1610+
A value of ``-1`` selects the default behavior, which respects
1611+
:keyword:`lazy` and :attr:`module.__lazy_modules__`.
1612+
A value of ``1`` makes eligible module-scope imports lazy by default.
1613+
The :ref:`lazy import restrictions <lazy-imports>` apply in both modes.
1614+
A value of ``0`` is invalid.
16131615
16141616
Configured by the :option:`-X lazy_imports <-X>` command line option or
16151617
the :envvar:`PYTHON_LAZY_IMPORTS` environment variable.

‎Doc/c-api/intro.rst‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,6 @@ System includes
140140
* ``<stdio.h>``
141141
* ``<stdlib.h>``
142142

143-
.. note::
144-
145-
Since Python may define some pre-processor definitions which affect the standard
146-
headers on some systems, you *must* include :file:`Python.h` before any standard
147-
headers are included.
148-
149143

150144
Useful macros
151145
=============

‎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/glossary.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,11 @@ Glossary
929929
which is evaluated when the function is called. The syntax to create
930930
a lambda function is ``lambda [parameters]: expression``
931931

932+
lazy import
933+
An import that delays module search and execution until the first use
934+
of the imported name. A proxy represents the imported object until
935+
resolution. See :ref:`lazy-imports`.
936+
932937
LBYL
933938
Look before you leap. This coding style explicitly tests for
934939
pre-conditions before making calls or lookups. This style contrasts with

‎Doc/library/ast.rst‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,9 +1128,11 @@ described in other sections.
11281128
Imports
11291129
~~~~~~~
11301130

1131-
.. class:: Import(names)
1131+
.. class:: Import(names, is_lazy)
11321132

11331133
An import statement. ``names`` is a list of :class:`alias` nodes.
1134+
``is_lazy`` is ``1`` for ``lazy import`` and ``0`` for ``import``.
1135+
The field records the source syntax and does not reflect runtime controls.
11341136

11351137
.. doctest::
11361138

@@ -1145,12 +1147,22 @@ Imports
11451147
is_lazy=0)])
11461148

11471149

1148-
.. class:: ImportFrom(module, names, level)
1150+
.. versionchanged:: 3.15
1151+
Added ``is_lazy`` for :ref:`lazy imports <lazy-imports>`.
1152+
1153+
1154+
.. class:: ImportFrom(module, names, level, is_lazy)
11491155

11501156
Represents ``from x import y``. ``module`` is a raw string of the 'from' name,
11511157
without any leading dots, or ``None`` for statements such as ``from . import foo``.
11521158
``level`` is an integer holding the level of the relative import (0 means
11531159
absolute import).
1160+
``is_lazy`` is ``1`` for ``lazy from ... import`` and ``0`` for
1161+
``from ... import``.
1162+
1163+
The ``is_lazy`` field records the source syntax. It does not reflect
1164+
runtime controls such as :func:`sys.set_lazy_imports` or
1165+
:attr:`module.__lazy_modules__`.
11541166

11551167
.. doctest::
11561168

@@ -1167,6 +1179,10 @@ Imports
11671179
is_lazy=0)])
11681180

11691181

1182+
.. versionchanged:: 3.15
1183+
Added ``is_lazy`` for :ref:`lazy imports <lazy-imports>`.
1184+
1185+
11701186
.. class:: alias(name, asname)
11711187

11721188
Both parameters are raw strings of the names. ``asname`` can be ``None`` if

‎Doc/library/collections.rst‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,15 +949,17 @@ by the :mod:`csv` or :mod:`sqlite3` modules::
949949
EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')
950950

951951
import csv
952-
for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))):
953-
print(emp.name, emp.title)
952+
with open("employees.csv", newline="") as f:
953+
for emp in map(EmployeeRecord._make, csv.reader(f)):
954+
print(emp.name, emp.title)
954955

955956
import sqlite3
956957
conn = sqlite3.connect('/companydata')
957958
cursor = conn.cursor()
958959
cursor.execute('SELECT name, age, title, department, paygrade FROM employees')
959960
for emp in map(EmployeeRecord._make, cursor.fetchall()):
960961
print(emp.name, emp.title)
962+
conn.close()
961963

962964
In addition to the methods inherited from tuples, named tuples support
963965
three additional methods and two attributes. To prevent conflicts with

0 commit comments

Comments
 (0)