Skip to content

Commit e49b99c

Browse files
committed
Merge remote-tracking branch 'origin/main' into gh-162764-stw-detach-notify
2 parents fa5057b + 6e8fc18 commit e49b99c

32 files changed

Lines changed: 790 additions & 296 deletions

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,10 +1362,22 @@ iterations of the loop.
13621362

13631363
.. opcode:: IMPORT_NAME (namei)
13641364

1365-
Imports the module ``co_names[namei]``. ``STACK[-1]`` and ``STACK[-2]`` are
1366-
popped and provide the *fromlist* and *level* arguments of :func:`__import__`.
1367-
The module object is pushed onto the stack. The current namespace is not affected: for a proper import statement, a subsequent :opcode:`STORE_FAST` instruction
1368-
modifies the namespace.
1365+
Imports the module ``co_names[namei >> 2]``. ``STACK[-1]`` and
1366+
``STACK[-2]`` supply the *fromlist* and *level* arguments of the import.
1367+
The instruction removes both values from the stack.
1368+
The result is a module object or a
1369+
:class:`lazy import proxy <types.LazyImportType>`.
1370+
The instruction pushes the result onto the stack.
1371+
1372+
The instruction does not change the current namespace.
1373+
For ``import spam``, a subsequent store instruction binds the result.
1374+
1375+
The two lowest bits of *namei* control :ref:`lazy imports <lazy-imports>`.
1376+
Bit ``0`` marks an explicit lazy import. Bit ``1`` forces an eager import.
1377+
If both bits are zero, the runtime lazy import controls apply.
1378+
1379+
.. versionchanged:: 3.15
1380+
The two lowest bits encode lazy import flags.
13691381

13701382

13711383
.. opcode:: IMPORT_FROM (namei)
@@ -1374,6 +1386,12 @@ iterations of the loop.
13741386
The resulting object is pushed onto the stack, to be subsequently stored by a
13751387
:opcode:`STORE_FAST` instruction.
13761388

1389+
For a :class:`lazy import proxy <types.LazyImportType>`, the result can
1390+
be another proxy that represents the requested attribute.
1391+
1392+
.. versionchanged:: 3.15
1393+
Added support for lazy import proxies.
1394+
13771395

13781396
.. opcode:: JUMP_FORWARD (delta)
13791397

‎Doc/library/importlib.rst‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,13 @@ To import a Python source file directly from a path, use the following recipe::
14671467
Implementing lazy imports
14681468
'''''''''''''''''''''''''
14691469

1470-
The example below shows how to implement lazy imports::
1470+
Use the :keyword:`lazy` keyword for an explicit :ref:`lazy import <lazy-imports>`.
1471+
Python delays the module search and execution until the first use of the
1472+
imported name.
1473+
1474+
The :class:`importlib.util.LazyLoader` example below does the module search
1475+
immediately and delays only module execution. It creates a module object in
1476+
:data:`sys.modules` before execution::
14711477

14721478
>>> import importlib.util
14731479
>>> import sys

‎Doc/library/sys.rst‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -950,11 +950,11 @@ always available. Unless explicitly noted otherwise, all variables are read-only
950950

951951
Returns the current lazy imports mode as a string.
952952

953-
* ``"normal"``: Only imports explicitly marked with the ``lazy`` keyword
954-
are lazy
955-
* ``"all"``: All top-level imports are potentially lazy
953+
* ``"normal"``: Respect :keyword:`lazy` and
954+
:attr:`module.__lazy_modules__`. This is the default mode.
955+
* ``"all"``: Make all eligible module-scope imports potentially lazy.
956956

957-
See also :func:`set_lazy_imports` and :pep:`810`.
957+
See also :func:`set_lazy_imports` and :ref:`lazy-imports`.
958958

959959
.. versionadded:: 3.15
960960

@@ -1538,7 +1538,7 @@ always available. Unless explicitly noted otherwise, all variables are read-only
15381538
In future versions of Python, these may be removed, and/or additional
15391539
extras may be added.
15401540

1541-
See also :func:`set_lazy_imports` and :pep:`810`.
1541+
See also :func:`set_lazy_imports` and :ref:`lazy-imports`.
15421542

15431543
.. versionadded:: 3.15
15441544

@@ -1829,12 +1829,16 @@ always available. Unless explicitly noted otherwise, all variables are read-only
18291829

18301830
.. function:: set_lazy_imports(mode)
18311831

1832-
Sets the global lazy imports mode. The *mode* parameter must be one of
1833-
the following strings:
1832+
Set the lazy import mode for the current interpreter.
1833+
The *mode* parameter must be one of these strings:
18341834

1835-
* ``"normal"``: Only imports explicitly marked with the ``lazy`` keyword
1836-
are lazy
1837-
* ``"all"``: All top-level imports become potentially lazy
1835+
* ``"normal"``: Respect :keyword:`lazy` and
1836+
:attr:`module.__lazy_modules__`. This is the default mode.
1837+
* ``"all"``: Make all eligible module-scope imports potentially lazy.
1838+
1839+
The :ref:`lazy import restrictions <lazy-imports>` also apply in
1840+
``"all"`` mode. A mode change applies to subsequent import statements.
1841+
It does not resolve lazy import proxies from earlier statements.
18381842

18391843
This function is intended for advanced users who need to control lazy
18401844
imports across their entire application. Library developers should
@@ -1844,7 +1848,7 @@ always available. Unless explicitly noted otherwise, all variables are read-only
18441848
In addition to the mode, lazy imports can be controlled via the filter
18451849
provided by :func:`set_lazy_imports_filter`.
18461850

1847-
See also :func:`get_lazy_imports` and :pep:`810`.
1851+
See also :func:`get_lazy_imports` and :ref:`lazy-imports`.
18481852

18491853
.. versionadded:: 3.15
18501854

@@ -1858,12 +1862,13 @@ always available. Unless explicitly noted otherwise, all variables are read-only
18581862
determine whether it should actually be lazy. It should have the following
18591863
signature::
18601864

1861-
def filter(importing_module: str, imported_module: str,
1865+
def filter(importing_module: str | None, imported_module: str,
18621866
fromlist: tuple[str, ...] | None) -> bool
18631867

18641868
The function is called with three positional arguments:
18651869

1866-
* *importing_module* is the name of the module doing the import
1870+
* *importing_module* is the name of the module that executes the import,
1871+
or ``None`` if its globals do not contain ``__name__``
18671872
* *imported_module* is the resolved name of the module being imported
18681873
(for example, ``lazy from .spam import eggs`` passes
18691874
``package.spam``)
@@ -1873,10 +1878,14 @@ always available. Unless explicitly noted otherwise, all variables are read-only
18731878
The filter should return ``True`` to allow the import to be lazy, or
18741879
``False`` to force an eager import.
18751880

1881+
The filter runs when the import statement executes, before proxy creation.
1882+
If the filter raises an exception, the exception propagates from that
1883+
statement. The filter cannot make an otherwise eager import lazy.
1884+
18761885
This is an advanced feature intended for specialized users who need
18771886
fine-grained control over lazy import behavior.
18781887

1879-
See also :func:`get_lazy_imports_filter` and :pep:`810`.
1888+
See also :func:`get_lazy_imports_filter` and :ref:`lazy-imports`.
18801889

18811890
.. versionadded:: 3.15
18821891

‎Doc/library/types.rst‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,19 +345,38 @@ Standard names are defined for the following types:
345345

346346
.. class:: LazyImportType
347347

348-
The type of lazy import proxy objects. These objects are created when a
349-
module is lazily imported and serve as placeholders until the module is
350-
actually accessed. This type can be used to detect lazy imports
351-
programmatically.
348+
The type of :ref:`lazy import proxies <lazy-imports>`.
349+
A proxy represents a module or an imported attribute until resolution.
350+
You cannot create instances of this type directly.
351+
352+
A direct read of an imported global name resolves its proxy.
353+
Access through :func:`globals` or
354+
:attr:`module.__dict__` returns the proxy itself::
355+
356+
import types
357+
lazy import json
358+
359+
isinstance(globals()["json"], types.LazyImportType) # True
360+
json.dumps({"key": "value"}) # Resolves the import.
361+
isinstance(globals()["json"], types.LazyImportType) # False
352362

353363
.. versionadded:: 3.15
354364

355-
.. seealso:: :pep:`810`
365+
.. seealso:: :ref:`lazy-imports`
356366

357367
.. method:: resolve()
358368

359-
Reify the lazy import and return the "real" object being imported.
369+
Resolve the lazy import and return the imported module or attribute.
370+
This method can leave the proxy in its namespace.
371+
To replace the proxy explicitly, assign the result to the imported name::
372+
373+
lazy import json
374+
json = globals()["json"].resolve()
360375

376+
Each call to :func:`!resolve` call does a fresh import.
377+
The proxy itself does not store :func:`!resolve`'s result,
378+
relying on the import mechanism to use :attr:`sys.modules`
379+
to reuse modules that have already been imported.
361380

362381
.. class:: GetSetDescriptorType
363382

0 commit comments

Comments
 (0)