Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Lazy Imports
This implements Lazy Imports (PEP 690) on CPython main branch.
Most significant changes are in the following files:
Python/import.c
- ~800 changesObjects/dictobject.c
- 600 changesPython/ceval.c
- ~300 changesObjects/lazyimportobject.c
- ~200 new linesPython/import.c
PyLazyImportObject
objects._PyImport_LazyImportName()
,_PyImport_EagerImportName()
,_PyImport_ImportFrom()
,PyImport_LoadLazyImport()
and_imp._maybe_set_submodule_attribute()
_imp
) API:PyImport_SetLazyImports()
,PyImport_SetLazyImportsInModule()
,PyImport_IsLazyImportsEnabled()
Objects/dictobject.c
dict.keys()
, etc. all maintain lazy values inside a dictionary._Py_dict_lookup()
can now returnDKIX_VALUE_ERROR
in case the resolution of a lazy object resulted in an error.*_keep_lazy
/*KeepLazy
(e.g._Py_dict_lookup_keep_lazy()
,_PyDict_GetItemKeepLazy()
) to force returning lazy objects without being resolved.PyDict_NextWithError()
, which works the same way asPyDict_Next()
with the exception it propagates any errors to the caller by returning0
and setting an exception. Caller should useif (PyErr_Ocurred())
to check for any errors._PyDict_HasLazyImports()
PyDict_Next()
andPyDict_NextWithError()
resolve all the lazy objects in the dictionary if it has them and if the passedpos
is zero. Returns0
on errors or if objects can't be resolved.PyDict_ResolveLazyImports()
to resolve all lazy values in a dictionary.PyDict_Next()
,dict.values()
,dict.items()
before starting to iterate through the dictionary, to ensure returned values are all resolved and that the dictionary doesn't mutate midway due to import side effects produced by resolving the values.dk_lazy_imports
is to be able to make this call efficient for dictionaries without lazy values.PyDict_IsLazyImport()
to check if a given key in a dictionary is a lazy object.Python/ceval.c
EAGER_IMPORT_NAME
andIMPORT_NAME
to createPyLazyImportObject
objects when the feature is enabled.import_name()
andimport_from()
were moved toPython/import.c
, and mostly only renamed to_PyImport_EagerImportName()
and_PyImport_ImportFrom()
, respectively.Objects/lazyimportobject.c
PyLazyImportObject
object viaPyLazyImport_Type
._PyLazyImport_NewModule()
,_PyLazyImport_NewObject()
,_PyLazyImport_GetName()
,PyLazyImport_CheckExact()