aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--setuptools/distutils_patch.py20
1 files changed, 17 insertions, 3 deletions
diff --git a/setuptools/distutils_patch.py b/setuptools/distutils_patch.py
index a2fc1a8c..e0abbd77 100644
--- a/setuptools/distutils_patch.py
+++ b/setuptools/distutils_patch.py
@@ -7,9 +7,23 @@ for more motivation.
import sys
import importlib
+import contextlib
from os.path import dirname
-sys.path.insert(0, dirname(dirname(__file__)))
-importlib.import_module('distutils')
-sys.path.pop(0)
+@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')