aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/importlib-metadata/py3/importlib_metadata/_compat.py
blob: 1a3578e60fb168fcffff659245ec1ae42cbfcb13 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import sys
import platform


__all__ = ['install', 'NullFinder']


def install(flag):
    def dec_install(cls):
        """
        Class decorator for installation on sys.meta_path.

        Adds the backport DistributionFinder to sys.meta_path and
        attempts to disable the finder functionality of the stdlib
        DistributionFinder.
        """
        if flag:
            sys.meta_path.append(cls())
            disable_stdlib_finder()
        return cls
    return dec_install


def disable_stdlib_finder():
    """
    Give the backport primacy for discovering path-based distributions
    by monkey-patching the stdlib O_O.

    See #91 for more background for rationale on this sketchy
    behavior.
    """

    def matches(finder):
        return getattr(
            finder, '__module__', None
        ) == '_frozen_importlib_external' and hasattr(finder, 'find_distributions')

    for finder in filter(matches, sys.meta_path):  # pragma: nocover
        del finder.find_distributions


class NullFinder:
    """
    A "Finder" (aka "MetaPathFinder") that never finds any modules,
    but may find distributions.
    """

    @staticmethod
    def find_spec(*args, **kwargs):
        return None


def pypy_partial(val):
    """
    Adjust for variable stacklevel on partial under PyPy.

    Workaround for #327.
    """
    is_pypy = platform.python_implementation() == 'PyPy'
    return val + is_pypy