Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Doc/library/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,16 @@ Standard names are defined for the following types:
actually accessed. This type can be used to detect lazy imports
programmatically.

.. method:: resolve()

Resolve the lazy import and return the imported object. Successful
resolutions are cached.

For a module-level lazy import, ``resolve()`` also updates the original
module global if that name still refers to this proxy. If the name was
deleted or rebound, it is left unchanged; later ``resolve()`` calls return
the cached object without updating that name.

.. versionadded:: 3.15

.. seealso:: :pep:`810`
Expand Down
29 changes: 29 additions & 0 deletions Include/internal/pycore_lazyimportobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,44 @@ typedef struct {
PyObject *lz_builtins;
PyObject *lz_from;
PyObject *lz_attr;
// Protected by the import lock. lz_owner_* records the first module
// global that resolve() may replace. Pending owners are being stored;
// finalized owners cannot be claimed again.
PyObject *lz_owner_globals;
PyObject *lz_owner_key;
PyObject *lz_resolved;
// Frame information for the original import location.
PyCodeObject *lz_code; // Code object where the lazy import was created.
int lz_owner_pending;
int lz_owner_finalized;
int lz_instr_offset; // Instruction offset where the lazy import was created.
} PyLazyImportObject;


PyAPI_FUNC(PyObject *) _PyLazyImport_GetName(PyObject *lazy_import);
PyAPI_FUNC(PyObject *) _PyLazyImport_New(
struct _PyInterpreterFrame *frame, PyObject *import_func, PyObject *from, PyObject *attr);
PyAPI_FUNC(int) _PyLazyImport_BindGlobal(
PyThreadState *tstate,
PyObject *lazy_import,
PyObject *globals,
PyObject *name);
PyAPI_FUNC(void) _PyLazyImport_FinishGlobalBinding(
PyThreadState *tstate,
PyObject *lazy_import,
PyObject *globals,
PyObject *name,
int stored);
PyAPI_FUNC(int) _PyLazyImport_CommitIfCurrent(
PyThreadState *tstate,
PyObject *lazy_import,
PyObject *globals,
PyObject *name,
PyObject *resolved);
PyAPI_FUNC(int) _PyLazyImport_CommitStoredIfCurrent(
PyThreadState *tstate,
PyObject *lazy_import,
PyObject *resolved);

#ifdef __cplusplus
}
Expand Down
Loading
Loading