Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
forked frompython/cpython

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

Open
Kronuz wants to merge1 commit intomain
base:main
Choose a base branch
Loading
fromlazy_imports
Open

Lazy imports#17

Kronuz wants to merge1 commit intomainfromlazy_imports

Conversation

@Kronuz
Copy link
Owner

@KronuzKronuz commentedSep 6, 2022
edited
Loading

Lazy Imports

This implements Lazy Imports (PEP 690) on CPython main branch.

Most significant changes are in the following files:

Python/import.c - ~800 changes
Objects/dictobject.c - 600 changes
Python/ceval.c - ~300 changes
Objects/lazyimportobject.c - ~200 new lines


Python/import.c

  • Implements the lazy imports basic functions for resolving objects and creatingPyLazyImportObject objects.
  • Implements:_PyImport_LazyImportName(),_PyImport_EagerImportName(),_PyImport_ImportFrom(),PyImport_LoadLazyImport() and_imp._maybe_set_submodule_attribute()
  • Adds the C API and Python (_imp) API:PyImport_SetLazyImports(),PyImport_SetLazyImportsInModule(),PyImport_IsLazyImportsEnabled()

Objects/dictobject.c

  • Makes necessary changes to dictionary internals to support lazy values.
  • Resolved objects from a dictionary immediately and automatically replace the lazy objects that represented them in the dictionary (as long as it's possible). In the case where the value in the dictionary can't be immediately replaced by the resolved object (because maybe the resolution of the object triggered a dictionary mutation), the next time the lazy value it's accessed it'll use an internal cached version of the resolved object and attempt substituting the lazy value with the resolved one again.
  • Values in a dictionary are resolvedonly when is needed, otherwise they remain lazy for as long as possible.
    • Merging, copying,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.
  • Adds*_keep_lazy/*KeepLazy (e.g._Py_dict_lookup_keep_lazy(),_PyDict_GetItemKeepLazy()) to force returning lazy objects without being resolved.
  • AddsPyDict_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.
  • Implements_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.
  • ImplementsPyDict_ResolveLazyImports() to resolve all lazy values in a dictionary.
    • If there are no lazy objects in a dictionary it does nothing, otherwise it walks the whole dictionary trying to resolve each value and retrying if the dictionary mutated halfway.
    • It's used by dictionary views,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.
    • The primary reason we needdk_lazy_imports is to be able to make this call efficient for dictionaries without lazy values.
  • ImplementsPyDict_IsLazyImport() to check if a given key in a dictionary is a lazy object.

Python/ceval.c

  • ImplementsEAGER_IMPORT_NAME andIMPORT_NAME to createPyLazyImportObject objects when the feature is enabled.
  • It also resolves some lazy import objects on specialized opcodes.
  • import_name() andimport_from() were moved toPython/import.c, and mostly only renamed to_PyImport_EagerImportName() and_PyImport_ImportFrom(), respectively.

Objects/lazyimportobject.c

  • Adds the implementation forPyLazyImportObject object viaPyLazyImport_Type.
  • Adds_PyLazyImport_NewModule(),_PyLazyImport_NewObject(),_PyLazyImport_GetName(),PyLazyImport_CheckExact()

@KronuzKronuzforce-pushed thelazy_imports branch 9 times, most recently from350c456 to1bd0372CompareSeptember 26, 2022 21:29
@KronuzKronuzforce-pushed thelazy_imports branch 4 times, most recently from17b139d tof3cf727CompareSeptember 30, 2022 19:39
@KronuzKronuzforce-pushed thelazy_imports branch 5 times, most recently from77213f7 to44b7e9bCompareOctober 14, 2022 18:41
@KronuzKronuzforce-pushed thelazy_imports branch 2 times, most recently from27bd975 toae3ff4aCompareOctober 30, 2022 18:49
[function.PyDict_GetItemWithError]
added ='3.2'
[function.PyDict_IsLazyImport]
added ='3.12'

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.

Kronuz reacted with thumbs up emoji
@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@KronuzKronuzforce-pushed thelazy_imports branch 4 times, most recently fromb7bcf2f to2dcb749CompareMarch 20, 2023 21:27
@KronuzKronuzforce-pushed thelazy_imports branch 2 times, most recently from48d2d19 toed3867cCompareMarch 29, 2023 17:57
@KronuzKronuzforce-pushed thelazy_imports branch 2 times, most recently frome0ade32 toc4494beCompareMay 12, 2023 18:37
@KronuzKronuzforce-pushed thelazy_imports branch 3 times, most recently froma678bb4 toccb30d7CompareOctober 26, 2023 18:58
@Kronuz
Copy link
OwnerAuthor

Rebased on top of CPython 3.12 stable.

@KronuzKronuzforce-pushed thelazy_imports branch 8 times, most recently from2e1acda to1d8292cCompareOctober 27, 2023 20:12
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

1 more reviewer

@encukouencukouencukou left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@Kronuz@encukou

[8]ページ先頭

©2009-2025 Movatter.jp