Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-42382: Make sure each EntryPoint carries it's Distribution information #23334

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

Closed
wants to merge 3 commits into from

Conversation

s0undt3ch
Copy link

@s0undt3ch s0undt3ch commented Nov 17, 2020

This avoids a low performance workaround to get the EntryPoint's Distribution, for example:

USE_IMPORTLIB_METADATA_STDLIB = USE_IMPORTLIB_METADATA = False
try:
    # Py3.8+
    import importlib.metadata

    USE_IMPORTLIB_METADATA_STDLIB = True
except ImportError:
    # < Py3.8 backport package
    import importlib_metadata

    USE_IMPORTLIB_METADATA = True


def get_distribution_from_entry_point(entry_point):
    loaded_entry_point = entry_point.load()
    if isinstance(loaded_entry_point, types.ModuleType):
        module_path = loaded_entry_point.__file__
    else:
        module_path = sys.modules[loaded_entry_point.__module__].__file__
    if USE_IMPORTLIB_METADATA_STDLIB:
        distributions = importlib.metadata.distributions
    else:
        distributions = importlib_metadata.distributions

    for distribution in distributions():
        try:
            relative = pathlib.Path(module_path).relative_to(
                distribution.locate_file("")
            )
        except ValueError:
            pass
        else:
            if relative in distribution.files:
                return distribution

https://bugs.python.org/issue42382

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@s0undt3ch

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@s0undt3ch
Copy link
Author

CLA signed 🎉

Sorry for the direct ping @jaraco but I think you're probably the best person to review?

@s0undt3ch
Copy link
Author

Polite ping.

@asvetlov
Copy link
Contributor

asvetlov commented Dec 6, 2020

@brettcannon you might be interested in review the PR

@jaraco
Copy link
Member

jaraco commented Dec 6, 2020

Always happy to have a direct ping. I missed the earlier notice (or hadn't yet gotten to it), so the bump helped ;)

@jaraco jaraco self-assigned this Dec 6, 2020
Copy link
Member

@jaraco jaraco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this looks good. I think it's on the right track. I'd like to explore the possibility of not storing the 'name' separately from the Distribution object (just store the object). I'm unsure what cases that might break though.

Also, I'll probably want to submit the Python 3.6-compatible backport at importlib_metadata first, and then incorporate those changes into CPython as part of the porting process.

That's not strictly necessary, though. It is faster and easier to test in that project.

@jaraco
Copy link
Member

jaraco commented Dec 14, 2020

Superseded by #23758.

@jaraco jaraco closed this Dec 14, 2020
@s0undt3ch s0undt3ch deleted the issues/bpo-42382 branch December 30, 2020 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants