-
Notifications
You must be signed in to change notification settings - Fork 1
Lazy imports #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Lazy imports #17
Conversation
350c456 to
1bd0372
Compare
17b139d to
f3cf727
Compare
77213f7 to
44b7e9b
Compare
27bd975 to
ae3ff4a
Compare
Misc/stable_abi.toml
Outdated
| [function.PyDict_GetItemWithError] | ||
| added = '3.2' | ||
| [function.PyDict_IsLazyImport] | ||
| added = '3.12' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the new API should be in the Stable ABI, at least initially. The few (hopefully) modules that need these can import and call importlib functions.
Except for PyDict_NextWithError -- that's an improvement, independent of lazy imports. In fact, if you add it now I'd be happy to review the PR.
Also: new items are generally added at the end of stable_abi.toml.
|
This PR is stale because it has been open for 30 days with no activity. |
b7bcf2f to
2dcb749
Compare
48d2d19 to
ed3867c
Compare
e0ade32 to
c4494be
Compare
a678bb4 to
ccb30d7
Compare
|
Rebased on top of CPython 3.12 stable. |
2e1acda to
1d8292c
Compare
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.cPyLazyImportObjectobjects._PyImport_LazyImportName(),_PyImport_EagerImportName(),_PyImport_ImportFrom(),PyImport_LoadLazyImport()and_imp._maybe_set_submodule_attribute()_imp) API:PyImport_SetLazyImports(),PyImport_SetLazyImportsInModule(),PyImport_IsLazyImportsEnabled()Objects/dictobject.cdict.keys(), etc. all maintain lazy values inside a dictionary._Py_dict_lookup()can now returnDKIX_VALUE_ERRORin 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 returning0and 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 passedposis zero. Returns0on 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_importsis 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.cEAGER_IMPORT_NAMEandIMPORT_NAMEto createPyLazyImportObjectobjects 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.cPyLazyImportObjectobject viaPyLazyImport_Type._PyLazyImport_NewModule(),_PyLazyImport_NewObject(),_PyLazyImport_GetName(),PyLazyImport_CheckExact()