mirror of https://github.com/python/cpython
Refresh from importlib_metadata@cpython (0.15)
This commit is contained in:
parent
a1c3d9c7f5
commit
ccbccced0c
|
@ -221,7 +221,7 @@ interface expected of finders by Python's import system.
|
|||
an iterator over instances of the ``Distribution`` abstract class. This
|
||||
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
|
||||
loading the metadata for packages matching the name
|
||||
(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')):
|
||||
"""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(
|
||||
r'(?P<module>[\w.]+)\s*'
|
||||
|
@ -178,15 +183,6 @@ class Distribution:
|
|||
)
|
||||
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
|
||||
def metadata(self):
|
||||
"""Return the parsed metadata for this Distribution.
|
||||
|
@ -309,8 +305,10 @@ class DistributionFinder(MetaPathFinder):
|
|||
@abc.abstractmethod
|
||||
def find_distributions(self, name=None, path=None):
|
||||
"""
|
||||
Find distributions.
|
||||
|
||||
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
|
||||
of directories ``path`` (defaults to sys.path).
|
||||
"""
|
||||
|
@ -347,14 +345,6 @@ def distributions():
|
|||
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):
|
||||
"""Get the metadata for the package.
|
||||
|
||||
|
|
|
@ -48,23 +48,28 @@ def tempdir_as_cwd():
|
|||
|
||||
|
||||
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):
|
||||
self.fixtures = ExitStack()
|
||||
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 = {
|
||||
"distinfo_pkg-1.0.0.dist-info": {
|
||||
"METADATA": """
|
||||
|
@ -91,7 +96,13 @@ class DistInfoPkg(SiteDir):
|
|||
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 = {
|
||||
"egginfo_pkg.egg-info": {
|
||||
"PKG-INFO": """
|
||||
|
@ -128,7 +139,7 @@ class EggInfoPkg(SiteDir):
|
|||
build_files(EggInfoPkg.files, prefix=self.site_dir)
|
||||
|
||||
|
||||
class EggInfoFile(SiteDir):
|
||||
class EggInfoFile(OnSysPath, SiteDir):
|
||||
files = {
|
||||
"egginfo_file.egg-info": """
|
||||
Metadata-Version: 1.0
|
||||
|
@ -149,14 +160,6 @@ class EggInfoFile(SiteDir):
|
|||
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()):
|
||||
"""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
|
||||
|
||||
|
||||
class NameNormalizationTests(fixtures.SiteDir, unittest.TestCase):
|
||||
class NameNormalizationTests(
|
||||
fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
|
||||
@staticmethod
|
||||
def pkg_with_dashes(site_dir):
|
||||
"""
|
||||
|
@ -95,7 +96,7 @@ class NameNormalizationTests(fixtures.SiteDir, unittest.TestCase):
|
|||
assert version(pkg_name.upper()) == '1.0'
|
||||
|
||||
|
||||
class NonASCIITests(fixtures.SiteDir, unittest.TestCase):
|
||||
class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
|
||||
@staticmethod
|
||||
def pkg_with_non_ascii_description(site_dir):
|
||||
"""
|
||||
|
@ -146,7 +147,7 @@ class DiscoveryTests(fixtures.EggInfoPkg,
|
|||
assert all(
|
||||
isinstance(dist, Distribution)
|
||||
for dist in dists
|
||||
), dists
|
||||
)
|
||||
assert any(
|
||||
dist.metadata['Name'] == 'egginfo-pkg'
|
||||
for dist in dists
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
import re
|
||||
import textwrap
|
||||
import unittest
|
||||
import itertools
|
||||
|
||||
from collections.abc import Iterator
|
||||
|
||||
from . import fixtures
|
||||
from importlib.metadata import (
|
||||
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
|
||||
|
||||
|
||||
class LocalProjectTests(fixtures.LocalPackage, unittest.TestCase):
|
||||
def test_find_local(self):
|
||||
dist = local_distribution()
|
||||
assert dist.metadata['Name'] == 'egginfo-pkg'
|
||||
class OffSysPathTests(fixtures.DistInfoPkgOffPath, unittest.TestCase):
|
||||
def test_find_distributions_specified_path(self):
|
||||
dists = itertools.chain.from_iterable(
|
||||
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(
|
||||
path(self.root, 'example-21.12-py3.6.egg'))
|
||||
sys.path.insert(0, str(egg))
|
||||
print('***', sys.path)
|
||||
self.resources.callback(sys.path.pop, 0)
|
||||
|
||||
def test_files(self):
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Versions/Current/Resources
|
Loading…
Reference in New Issue