- Notifications
You must be signed in to change notification settings - Fork1
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 ourterms of service andprivacy 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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
350c456 to1bd0372Compare17b139d tof3cf727Compare77213f7 to44b7e9bCompare27bd975 toae3ff4aCompareMisc/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 callimportlib functions.
Except forPyDict_NextWithError -- that's an improvement, independent of lazy imports. In fact, if you add itnow I'd be happy to review the PR.
Also: new items are generally added at theend ofstable_abi.toml.
This PR is stale because it has been open for 30 days with no activity. |
b7bcf2f to2dcb749Compare48d2d19 toed3867cComparee0ade32 toc4494beComparea678bb4 toccb30d7CompareKronuz commentedOct 26, 2023
Rebased on top of CPython 3.12 stable. |
2e1acda to1d8292cCompare
Uh oh!
There was an error while loading.Please reload this page.
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()