Skip to content

Commit 3e206f4

Browse files
committed
[3.8] bpo-38121: Sync importlib.metadata with 0.22 backport (GH-15993)
* bpo-38121: Sync importlib.metadata with 0.22 backport * πŸ“œπŸ€– Added by blurb_it.. (cherry picked from commit 8ed6503) Co-authored-by: Jason R. Coombs <[email protected]>
1 parent 36c29e4 commit 3e206f4

File tree

4 files changed

+979
-1084
lines changed

4 files changed

+979
-1084
lines changed

β€ŽLib/importlib/_bootstrap_external.py

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ def find_module(cls, fullname, path=None):
13691369
return spec.loader
13701370

13711371
@classmethod
1372-
def find_distributions(self, context=None):
1372+
def find_distributions(cls, *args, **kwargs):
13731373
"""
13741374
Find distributions.
13751375
@@ -1378,54 +1378,8 @@ def find_distributions(self, context=None):
13781378
(or all names if ``None`` indicated) along the paths in the list
13791379
of directories ``context.path``.
13801380
"""
1381-
from importlib.metadata import PathDistribution, DistributionFinder
1382-
if context is None:
1383-
context = DistributionFinder.Context()
1384-
found = self._search_paths(context.pattern, context.path)
1385-
return map(PathDistribution, found)
1386-
1387-
@classmethod
1388-
def _search_paths(cls, pattern, paths):
1389-
"""Find metadata directories in paths heuristically."""
1390-
import itertools
1391-
return itertools.chain.from_iterable(
1392-
cls._search_path(path, pattern)
1393-
for path in map(cls._switch_path, paths)
1394-
)
1395-
1396-
@staticmethod
1397-
def _switch_path(path):
1398-
from contextlib import suppress
1399-
import zipfile
1400-
import pathlib
1401-
PYPY_OPEN_BUG = False
1402-
if not PYPY_OPEN_BUG or os.path.isfile(path): # pragma: no branch
1403-
with suppress(Exception):
1404-
return zipfile.Path(path)
1405-
return pathlib.Path(path)
1406-
1407-
@classmethod
1408-
def _matches_info(cls, normalized, item):
1409-
import re
1410-
template = r'{pattern}(-.*)?\.(dist|egg)-info'
1411-
manifest = template.format(pattern=normalized)
1412-
return re.match(manifest, item.name, flags=re.IGNORECASE)
1413-
1414-
@classmethod
1415-
def _matches_legacy(cls, normalized, item):
1416-
import re
1417-
template = r'{pattern}-.*\.egg[\\/]EGG-INFO'
1418-
manifest = template.format(pattern=normalized)
1419-
return re.search(manifest, str(item), flags=re.IGNORECASE)
1420-
1421-
@classmethod
1422-
def _search_path(cls, root, pattern):
1423-
if not root.is_dir():
1424-
return ()
1425-
normalized = pattern.replace('-', '_')
1426-
return (item for item in root.iterdir()
1427-
if cls._matches_info(normalized, item)
1428-
or cls._matches_legacy(normalized, item))
1381+
from importlib.metadata import MetadataPathFinder
1382+
return MetadataPathFinder.find_distributions(*args, **kwargs)
14291383

14301384

14311385
class FileFinder:

β€ŽLib/importlib/metadata.py

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import io
2+
import os
23
import re
34
import abc
45
import csv
56
import sys
67
import email
78
import pathlib
9+
import zipfile
810
import operator
911
import functools
1012
import itertools
@@ -363,6 +365,58 @@ def find_distributions(self, context=Context()):
363365
"""
364366

365367

368+
class MetadataPathFinder(DistributionFinder):
369+
@classmethod
370+
def find_distributions(cls, context=DistributionFinder.Context()):
371+
"""
372+
Find distributions.
373+
374+
Return an iterable of all Distribution instances capable of
375+
loading the metadata for packages matching ``context.name``
376+
(or all names if ``None`` indicated) along the paths in the list
377+
of directories ``context.path``.
378+
"""
379+
found = cls._search_paths(context.pattern, context.path)
380+
return map(PathDistribution, found)
381+
382+
@classmethod
383+
def _search_paths(cls, pattern, paths):
384+
"""Find metadata directories in paths heuristically."""
385+
return itertools.chain.from_iterable(
386+
cls._search_path(path, pattern)
387+
for path in map(cls._switch_path, paths)
388+
)
389+
390+
@staticmethod
391+
def _switch_path(path):
392+
PYPY_OPEN_BUG = False
393+
if not PYPY_OPEN_BUG or os.path.isfile(path): # pragma: no branch
394+
with suppress(Exception):
395+
return zipfile.Path(path)
396+
return pathlib.Path(path)
397+
398+
@classmethod
399+
def _matches_info(cls, normalized, item):
400+
template = r'{pattern}(-.*)?\.(dist|egg)-info'
401+
manifest = template.format(pattern=normalized)
402+
return re.match(manifest, item.name, flags=re.IGNORECASE)
403+
404+
@classmethod
405+
def _matches_legacy(cls, normalized, item):
406+
template = r'{pattern}-.*\.egg[\\/]EGG-INFO'
407+
manifest = template.format(pattern=normalized)
408+
return re.search(manifest, str(item), flags=re.IGNORECASE)
409+
410+
@classmethod
411+
def _search_path(cls, root, pattern):
412+
if not root.is_dir():
413+
return ()
414+
normalized = pattern.replace('-', '_')
415+
return (item for item in root.iterdir()
416+
if cls._matches_info(normalized, item)
417+
or cls._matches_legacy(normalized, item))
418+
419+
366420
class PathDistribution(Distribution):
367421
def __init__(self, path):
368422
"""Construct a distribution from a path to the metadata directory.
@@ -382,13 +436,13 @@ def locate_file(self, path):
382436
return self._path.parent / path
383437

384438

385-
def distribution(package):
386-
"""Get the ``Distribution`` instance for the given package.
439+
def distribution(distribution_name):
440+
"""Get the ``Distribution`` instance for the named package.
387441
388-
:param package: The name of the package as a string.
442+
:param distribution_name: The name of the distribution package as a string.
389443
:return: A ``Distribution`` instance (or subclass thereof).
390444
"""
391-
return Distribution.from_name(package)
445+
return Distribution.from_name(distribution_name)
392446

393447

394448
def distributions(**kwargs):
@@ -399,23 +453,23 @@ def distributions(**kwargs):
399453
return Distribution.discover(**kwargs)
400454

401455

402-
def metadata(package):
403-
"""Get the metadata for the package.
456+
def metadata(distribution_name):
457+
"""Get the metadata for the named package.
404458
405-
:param package: The name of the distribution package to query.
459+
:param distribution_name: The name of the distribution package to query.
406460
:return: An email.Message containing the parsed metadata.
407461
"""
408-
return Distribution.from_name(package).metadata
462+
return Distribution.from_name(distribution_name).metadata
409463

410464

411-
def version(package):
465+
def version(distribution_name):
412466
"""Get the version string for the named package.
413467
414-
:param package: The name of the distribution package to query.
468+
:param distribution_name: The name of the distribution package to query.
415469
:return: The version string for the package as defined in the package's
416470
"Version" metadata key.
417471
"""
418-
return distribution(package).version
472+
return distribution(distribution_name).version
419473

420474

421475
def entry_points():
@@ -434,15 +488,20 @@ def entry_points():
434488
}
435489

436490

437-
def files(package):
438-
return distribution(package).files
491+
def files(distribution_name):
492+
"""Return a list of files for the named package.
493+
494+
:param distribution_name: The name of the distribution package to query.
495+
:return: List of files composing the distribution.
496+
"""
497+
return distribution(distribution_name).files
439498

440499

441-
def requires(package):
500+
def requires(distribution_name):
442501
"""
443-
Return a list of requirements for the indicated distribution.
502+
Return a list of requirements for the named package.
444503
445504
:return: An iterator of requirements, suitable for
446505
packaging.requirement.Requirement.
447506
"""
448-
return distribution(package).requires
507+
return distribution(distribution_name).requires
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update parameter names on functions in importlib.metadata matching the changes in the 0.22 release of importlib_metadata.

0 commit comments

Comments
Β (0)