aboutsummaryrefslogtreecommitdiffstats
path: root/setuptools/distutils_patch.py
blob: 43b64c0fc8aca819522c3baa44b8a3a9b83aac5b (plain)
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
"""
Ensure that the local copy of distutils is preferred over stdlib.

See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401
for more motivation.
"""

import sys
import importlib
import contextlib
from os.path import dirname


@contextlib.contextmanager
def patch_sys_path():
    orig = sys.path[:]
    sys.path[:] = [dirname(dirname(__file__))]
    try:
        yield
    finally:
        sys.path[:] = orig


def ensure_local_distutils():
    if 'distutils' in sys.path:
        raise RuntimeError("Distutils must not be imported before setuptools")
    with patch_sys_path():
        importlib.import_module('distutils')


ensure_local_distutils()