Skip to content

Commit c854e9a

Browse files
committed
Merge branch 'feature/91-override-stdlib' into 'master'
Disable the stdlib distribution finder when backport is present Closes #100 and #91 See merge request python-devs/importlib_metadata!98
2 parents c471831 + 48a4927 commit c854e9a

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

importlib_metadata/_compat.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,35 @@ class MetaPathFinder(object):
5151

5252

5353
def install(cls):
54-
"""Class decorator for installation on sys.meta_path."""
54+
"""
55+
Class decorator for installation on sys.meta_path.
56+
57+
Adds the backport DistributionFinder to sys.meta_path and
58+
attempts to disable the finder functionality of the stdlib
59+
DistributionFinder.
60+
"""
5561
sys.meta_path.append(cls())
62+
disable_stdlib_finder()
5663
return cls
5764

5865

66+
def disable_stdlib_finder():
67+
"""
68+
Give the backport primacy for discovering path-based distributions
69+
by monkey-patching the stdlib O_O.
70+
71+
See #91 for more background for rationale on this sketchy
72+
behavior.
73+
"""
74+
def matches(finder):
75+
return (
76+
finder.__module__ == '_frozen_importlib_external'
77+
and hasattr(finder, 'find_distributions')
78+
)
79+
for finder in filter(matches, sys.meta_path): # pragma: nocover
80+
del finder.find_distributions
81+
82+
5983
class NullFinder:
6084
"""
6185
A "Finder" (aka "MetaClassFinder") that never finds any modules,

importlib_metadata/docs/changelog.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
importlib_metadata NEWS
33
=========================
44

5+
v1.0.0
6+
======
7+
* For better compatibility with the stdlib implementation and to
8+
avoid the same distributions being discovered by the stdlib and
9+
backport implementations, the backport now disables the
10+
stdlib DistributionFinder during initialization (import time).
11+
Closes #91 and closes #100.
12+
513
0.23
614
====
715
* Added a compatibility shim to prevent failures on beta releases

importlib_metadata/docs/index.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ efficient ``pkg_resources`` package.
1212

1313
``importlib_metadata`` is a backport of Python 3.8's standard library
1414
`importlib.metadata`_ module for Python 2.7, and 3.4 through 3.7. Users of
15-
Python 3.8 and beyond are encouraged to use the standard library module, and
16-
in fact for these versions, ``importlib_metadata`` just shadows that module.
15+
Python 3.8 and beyond are encouraged to use the standard library module.
16+
When imported on Python 3.8 and later, ``importlib_metadata`` replaces the
17+
DistributionFinder behavior from the stdlib, but leaves the API in tact.
1718
Developers looking for detailed API descriptions should refer to the Python
1819
3.8 standard library documentation.
1920

0 commit comments

Comments
 (0)