mirror of
https://github.com/python/cpython.git
synced 2025-08-06 18:08:48 +00:00
Refresh from importlib_metadata@cpython (0.15)
This commit is contained in:
parent
a1c3d9c7f5
commit
ccbccced0c
7 changed files with 52 additions and 51 deletions
|
@ -221,7 +221,7 @@ interface expected of finders by Python's import system.
|
||||||
an iterator over instances of the ``Distribution`` abstract class. This
|
an iterator over instances of the ``Distribution`` abstract class. This
|
||||||
method must have the signature::
|
method must have the signature::
|
||||||
|
|
||||||
def find_distributions(name=None, path=sys.path):
|
def find_distributions(name=None, path=None):
|
||||||
"""Return an iterable of all Distribution instances capable of
|
"""Return an iterable of all Distribution instances capable of
|
||||||
loading the metadata for packages matching the name
|
loading the metadata for packages matching the name
|
||||||
(or all names if not supplied) along the paths in the list
|
(or all names if not supplied) along the paths in the list
|
||||||
|
|
|
@ -35,7 +35,12 @@ class PackageNotFoundError(ModuleNotFoundError):
|
||||||
|
|
||||||
|
|
||||||
class EntryPoint(collections.namedtuple('EntryPointBase', 'name value group')):
|
class EntryPoint(collections.namedtuple('EntryPointBase', 'name value group')):
|
||||||
"""An entry point as defined by Python packaging conventions."""
|
"""An entry point as defined by Python packaging conventions.
|
||||||
|
|
||||||
|
See `the packaging docs on entry points
|
||||||
|
<https://packaging.python.org/specifications/entry-points/>`_
|
||||||
|
for more information.
|
||||||
|
"""
|
||||||
|
|
||||||
pattern = re.compile(
|
pattern = re.compile(
|
||||||
r'(?P<module>[\w.]+)\s*'
|
r'(?P<module>[\w.]+)\s*'
|
||||||
|
@ -178,15 +183,6 @@ class Distribution:
|
||||||
)
|
)
|
||||||
return filter(None, declared)
|
return filter(None, declared)
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def find_local(cls):
|
|
||||||
dists = itertools.chain.from_iterable(
|
|
||||||
resolver(path=['.'])
|
|
||||||
for resolver in cls._discover_resolvers()
|
|
||||||
)
|
|
||||||
dist, = dists
|
|
||||||
return dist
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def metadata(self):
|
def metadata(self):
|
||||||
"""Return the parsed metadata for this Distribution.
|
"""Return the parsed metadata for this Distribution.
|
||||||
|
@ -309,8 +305,10 @@ class DistributionFinder(MetaPathFinder):
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def find_distributions(self, name=None, path=None):
|
def find_distributions(self, name=None, path=None):
|
||||||
"""
|
"""
|
||||||
|
Find distributions.
|
||||||
|
|
||||||
Return an iterable of all Distribution instances capable of
|
Return an iterable of all Distribution instances capable of
|
||||||
loading the metadata for packages matching the name
|
loading the metadata for packages matching the ``name``
|
||||||
(or all names if not supplied) along the paths in the list
|
(or all names if not supplied) along the paths in the list
|
||||||
of directories ``path`` (defaults to sys.path).
|
of directories ``path`` (defaults to sys.path).
|
||||||
"""
|
"""
|
||||||
|
@ -347,14 +345,6 @@ def distributions():
|
||||||
return Distribution.discover()
|
return Distribution.discover()
|
||||||
|
|
||||||
|
|
||||||
def local_distribution():
|
|
||||||
"""Get the ``Distribution`` instance for the package in CWD.
|
|
||||||
|
|
||||||
:return: A ``Distribution`` instance (or subclass thereof).
|
|
||||||
"""
|
|
||||||
return Distribution.find_local()
|
|
||||||
|
|
||||||
|
|
||||||
def metadata(package):
|
def metadata(package):
|
||||||
"""Get the metadata for the package.
|
"""Get the metadata for the package.
|
||||||
|
|
||||||
|
|
|
@ -48,23 +48,28 @@ def tempdir_as_cwd():
|
||||||
|
|
||||||
|
|
||||||
class SiteDir:
|
class SiteDir:
|
||||||
@staticmethod
|
|
||||||
@contextlib.contextmanager
|
|
||||||
def site_dir():
|
|
||||||
with tempdir() as tmp:
|
|
||||||
sys.path[:0] = [str(tmp)]
|
|
||||||
try:
|
|
||||||
yield tmp
|
|
||||||
finally:
|
|
||||||
sys.path.remove(str(tmp))
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.fixtures = ExitStack()
|
self.fixtures = ExitStack()
|
||||||
self.addCleanup(self.fixtures.close)
|
self.addCleanup(self.fixtures.close)
|
||||||
self.site_dir = self.fixtures.enter_context(self.site_dir())
|
self.site_dir = self.fixtures.enter_context(tempdir())
|
||||||
|
|
||||||
|
|
||||||
class DistInfoPkg(SiteDir):
|
class OnSysPath:
|
||||||
|
@staticmethod
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def add_sys_path(dir):
|
||||||
|
sys.path[:0] = [str(dir)]
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
sys.path.remove(str(dir))
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(OnSysPath, self).setUp()
|
||||||
|
self.fixtures.enter_context(self.add_sys_path(self.site_dir))
|
||||||
|
|
||||||
|
|
||||||
|
class DistInfoPkg(OnSysPath, SiteDir):
|
||||||
files = {
|
files = {
|
||||||
"distinfo_pkg-1.0.0.dist-info": {
|
"distinfo_pkg-1.0.0.dist-info": {
|
||||||
"METADATA": """
|
"METADATA": """
|
||||||
|
@ -91,7 +96,13 @@ class DistInfoPkg(SiteDir):
|
||||||
build_files(DistInfoPkg.files, self.site_dir)
|
build_files(DistInfoPkg.files, self.site_dir)
|
||||||
|
|
||||||
|
|
||||||
class EggInfoPkg(SiteDir):
|
class DistInfoPkgOffPath(SiteDir):
|
||||||
|
def setUp(self):
|
||||||
|
super(DistInfoPkgOffPath, self).setUp()
|
||||||
|
build_files(DistInfoPkg.files, self.site_dir)
|
||||||
|
|
||||||
|
|
||||||
|
class EggInfoPkg(OnSysPath, SiteDir):
|
||||||
files = {
|
files = {
|
||||||
"egginfo_pkg.egg-info": {
|
"egginfo_pkg.egg-info": {
|
||||||
"PKG-INFO": """
|
"PKG-INFO": """
|
||||||
|
@ -128,7 +139,7 @@ class EggInfoPkg(SiteDir):
|
||||||
build_files(EggInfoPkg.files, prefix=self.site_dir)
|
build_files(EggInfoPkg.files, prefix=self.site_dir)
|
||||||
|
|
||||||
|
|
||||||
class EggInfoFile(SiteDir):
|
class EggInfoFile(OnSysPath, SiteDir):
|
||||||
files = {
|
files = {
|
||||||
"egginfo_file.egg-info": """
|
"egginfo_file.egg-info": """
|
||||||
Metadata-Version: 1.0
|
Metadata-Version: 1.0
|
||||||
|
@ -149,14 +160,6 @@ class EggInfoFile(SiteDir):
|
||||||
build_files(EggInfoFile.files, prefix=self.site_dir)
|
build_files(EggInfoFile.files, prefix=self.site_dir)
|
||||||
|
|
||||||
|
|
||||||
class LocalPackage:
|
|
||||||
def setUp(self):
|
|
||||||
self.fixtures = ExitStack()
|
|
||||||
self.addCleanup(self.fixtures.close)
|
|
||||||
self.fixtures.enter_context(tempdir_as_cwd())
|
|
||||||
build_files(EggInfoPkg.files)
|
|
||||||
|
|
||||||
|
|
||||||
def build_files(file_defs, prefix=pathlib.Path()):
|
def build_files(file_defs, prefix=pathlib.Path()):
|
||||||
"""Build a set of files/directories, as described by the
|
"""Build a set of files/directories, as described by the
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,8 @@ class ImportTests(fixtures.DistInfoPkg, unittest.TestCase):
|
||||||
assert ep.load() is importlib.metadata
|
assert ep.load() is importlib.metadata
|
||||||
|
|
||||||
|
|
||||||
class NameNormalizationTests(fixtures.SiteDir, unittest.TestCase):
|
class NameNormalizationTests(
|
||||||
|
fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def pkg_with_dashes(site_dir):
|
def pkg_with_dashes(site_dir):
|
||||||
"""
|
"""
|
||||||
|
@ -95,7 +96,7 @@ class NameNormalizationTests(fixtures.SiteDir, unittest.TestCase):
|
||||||
assert version(pkg_name.upper()) == '1.0'
|
assert version(pkg_name.upper()) == '1.0'
|
||||||
|
|
||||||
|
|
||||||
class NonASCIITests(fixtures.SiteDir, unittest.TestCase):
|
class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def pkg_with_non_ascii_description(site_dir):
|
def pkg_with_non_ascii_description(site_dir):
|
||||||
"""
|
"""
|
||||||
|
@ -146,7 +147,7 @@ class DiscoveryTests(fixtures.EggInfoPkg,
|
||||||
assert all(
|
assert all(
|
||||||
isinstance(dist, Distribution)
|
isinstance(dist, Distribution)
|
||||||
for dist in dists
|
for dist in dists
|
||||||
), dists
|
)
|
||||||
assert any(
|
assert any(
|
||||||
dist.metadata['Name'] == 'egginfo-pkg'
|
dist.metadata['Name'] == 'egginfo-pkg'
|
||||||
for dist in dists
|
for dist in dists
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
import re
|
import re
|
||||||
import textwrap
|
import textwrap
|
||||||
import unittest
|
import unittest
|
||||||
|
import itertools
|
||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
|
|
||||||
from . import fixtures
|
from . import fixtures
|
||||||
from importlib.metadata import (
|
from importlib.metadata import (
|
||||||
Distribution, PackageNotFoundError, distribution,
|
Distribution, PackageNotFoundError, distribution,
|
||||||
entry_points, files, local_distribution, metadata, requires, version,
|
entry_points, files, metadata, requires, version,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -138,7 +139,13 @@ class APITests(
|
||||||
assert deps == expected
|
assert deps == expected
|
||||||
|
|
||||||
|
|
||||||
class LocalProjectTests(fixtures.LocalPackage, unittest.TestCase):
|
class OffSysPathTests(fixtures.DistInfoPkgOffPath, unittest.TestCase):
|
||||||
def test_find_local(self):
|
def test_find_distributions_specified_path(self):
|
||||||
dist = local_distribution()
|
dists = itertools.chain.from_iterable(
|
||||||
assert dist.metadata['Name'] == 'egginfo-pkg'
|
resolver(path=[str(self.site_dir)])
|
||||||
|
for resolver in Distribution._discover_resolvers()
|
||||||
|
)
|
||||||
|
assert any(
|
||||||
|
dist.metadata['Name'] == 'distinfo-pkg'
|
||||||
|
for dist in dists
|
||||||
|
)
|
||||||
|
|
|
@ -48,7 +48,6 @@ class TestEgg(TestZip):
|
||||||
egg = self.resources.enter_context(
|
egg = self.resources.enter_context(
|
||||||
path(self.root, 'example-21.12-py3.6.egg'))
|
path(self.root, 'example-21.12-py3.6.egg'))
|
||||||
sys.path.insert(0, str(egg))
|
sys.path.insert(0, str(egg))
|
||||||
print('***', sys.path)
|
|
||||||
self.resources.callback(sys.path.pop, 0)
|
self.resources.callback(sys.path.pop, 0)
|
||||||
|
|
||||||
def test_files(self):
|
def test_files(self):
|
||||||
|
|
1
Python.framework/Resources
Symbolic link
1
Python.framework/Resources
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
Versions/Current/Resources
|
Loading…
Add table
Add a link
Reference in a new issue