"""
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
from os.path import dirname


sys.path.insert(0, dirname(dirname(__file__)))
importlib.import_module('distutils')
sys.path.pop(0)

"""
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


if 'distutils' in sys.path:
    raise RuntimeError("Distutils must not be imported before setuptools")


with patch_sys_path():
    importlib.import_module('distutils')

Update distutils patch to monkeypatch all paths from sys.path to ensure that distutils is never imported except from the same path as setuptools. Assert that 'distutils' is not already in sys.modules.
Update distutils patch to monkeypatch all paths from sys.path to ensure that distutils is never imported except from the same path as setuptools. Assert that 'distutils' is not already in sys.modules.
