bpo-39791: Refresh importlib.metadata from importlib_metadata 1.6.1. by jaraco · Pull Request #20656 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Doc/library/importlib.metadata.rst
30 changes: 25 additions & 5 deletions Lib/importlib/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ def load(self):
attrs = filter(None, (match.group('attr') or '').split('.'))
return functools.reduce(getattr, attrs, module)

@property
def module(self):
match = self.pattern.match(self.value)
return match.group('module')

@property
def attr(self):
match = self.pattern.match(self.value)
return match.group('attr')

@property
def extras(self):
match = self.pattern.match(self.value)
Expand Down Expand Up @@ -170,7 +180,7 @@ def from_name(cls, name):
"""
for resolver in cls._discover_resolvers():
dists = resolver(DistributionFinder.Context(name=name))
dist = next(dists, None)
dist = next(iter(dists), None)
if dist is not None:
return dist
else:
Expand Down Expand Up @@ -213,6 +223,17 @@ def _discover_resolvers():
)
return filter(None, declared)

@classmethod
def _local(cls, root='.'):
from pep517 import build, meta
system = build.compat_system(root)
builder = functools.partial(
meta.build,
source_dir=root,
system=system,
)
return PathDistribution(zipfile.Path(meta.build_as_zip(builder)))

@property
def metadata(self):
"""Return the parsed metadata for this Distribution.
Expand Down Expand Up @@ -391,7 +412,7 @@ class FastPath:

def __init__(self, root):
self.root = root
self.base = os.path.basename(root).lower()
self.base = os.path.basename(self.root).lower()

def joinpath(self, child):
return pathlib.Path(self.root, child)
Expand All @@ -408,8 +429,8 @@ def zip_children(self):
names = zip_path.root.namelist()
self.joinpath = zip_path.joinpath

return (
posixpath.split(child)[0]
return dict.fromkeys(
child.split(posixpath.sep, 1)[0]
for child in names
)

Expand Down Expand Up @@ -475,7 +496,6 @@ def _search_paths(cls, name, paths):
)



class PathDistribution(Distribution):
def __init__(self, path):
"""Construct a distribution from a path to the metadata directory.
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_importlib/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ def setUp(self):
build_files(EggInfoFile.files, prefix=self.site_dir)


class LocalPackage:
files = {
"setup.py": """
import setuptools
setuptools.setup(name="local-pkg", version="2.0.1")
""",
}

def setUp(self):
self.fixtures = contextlib.ExitStack()
self.addCleanup(self.fixtures.close)
self.fixtures.enter_context(tempdir_as_cwd())
build_files(self.files)


def build_files(file_defs, prefix=pathlib.Path()):
"""Build a set of files/directories, as described by the

Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_importlib/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,19 @@ def test_json_dump(self):
"""
with self.assertRaises(Exception):
json.dumps(self.ep)

def test_module(self):
assert self.ep.module == 'value'

def test_attr(self):
assert self.ep.attr is None


class FileSystem(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
def test_unicode_dir_on_sys_path(self):
"""
Ensure a Unicode subdirectory of a directory on sys.path
does not crash.
"""
fixtures.build_files({'☃': {}}, prefix=self.site_dir)
list(distributions())
26 changes: 16 additions & 10 deletions Lib/test/test_importlib/test_zip.py