Skip to content

Commit

Permalink
Add compatibility shim to prevent exception on select Python versions.
Browse files Browse the repository at this point in the history
…Closes #86.
  • Loading branch information
jaraco committed Sep 16, 2019
1 parent f06bd39 commit 06b8e26
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ def from_name(cls, name):
metadata cannot be found.
"""
for resolver in cls._discover_resolvers():
dists = resolver(DistributionFinder.Context(name=name))
context = DistributionFinder.Context(name=name)
dists = cls._maybe_bind(resolver, context)
dist = next(dists, None)
if dist is not None:
return dist
Expand All @@ -200,10 +201,24 @@ def discover(cls, **kwargs):
raise ValueError("cannot accept context and kwargs")
context = context or DistributionFinder.Context(**kwargs)
return itertools.chain.from_iterable(
resolver(context)
cls._maybe_bind(resolver, context)
for resolver in cls._discover_resolvers()
)

@staticmethod
def _maybe_bind(resolver, context):
"""
Only bind the context to the resolver if as a callable,
the resolver accepts the context parameter.
Workaround for
https://gitlab.com/python-devs/importlib_metadata/issues/86
"""
try: # pragma: nocover
return resolver(context)
except TypeError: # pragma: nocover
return resolver(name=context.name, path=context.path)

@staticmethod
def at(path):
"""Return a Distribution for the indicated metadata path
Expand Down

0 comments on commit 06b8e26

Please sign in to comment.