From b678ce30a356abb36cd49d523731a9f978fce0bf Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 25 May 2020 13:13:44 -0400 Subject: Move distutils import to a separate file to avoid linter errors. --- setuptools/distutils_patch.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 setuptools/distutils_patch.py (limited to 'setuptools/distutils_patch.py') diff --git a/setuptools/distutils_patch.py b/setuptools/distutils_patch.py new file mode 100644 index 00000000..a2fc1a8c --- /dev/null +++ b/setuptools/distutils_patch.py @@ -0,0 +1,15 @@ +""" +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) -- cgit v1.2.3 From 151599602b9d626ebcfe5ae6960ea216b767fec2 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 25 May 2020 13:38:15 -0400 Subject: 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. --- setuptools/distutils_patch.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'setuptools/distutils_patch.py') 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') -- cgit v1.2.3 From 9962c0af814ccd13c370ce8b04742a52f77025c6 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 31 May 2020 08:50:31 -0400 Subject: Extract function for ensure_local_distuils --- setuptools/distutils_patch.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'setuptools/distutils_patch.py') diff --git a/setuptools/distutils_patch.py b/setuptools/distutils_patch.py index e0abbd77..43b64c0f 100644 --- a/setuptools/distutils_patch.py +++ b/setuptools/distutils_patch.py @@ -21,9 +21,11 @@ def patch_sys_path(): sys.path[:] = orig -if 'distutils' in sys.path: - raise RuntimeError("Distutils must not be imported before setuptools") +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') -with patch_sys_path(): - importlib.import_module('distutils') +ensure_local_distutils() -- cgit v1.2.3 From a8081a19cc48fc285dfd50953bb2c522cb4b8b02 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 21 Jun 2020 12:09:39 -0400 Subject: Update setuptools/distutils_patch.py Co-authored-by: Steve Dower --- setuptools/distutils_patch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setuptools/distutils_patch.py') diff --git a/setuptools/distutils_patch.py b/setuptools/distutils_patch.py index 43b64c0f..1416a7a3 100644 --- a/setuptools/distutils_patch.py +++ b/setuptools/distutils_patch.py @@ -22,7 +22,7 @@ def patch_sys_path(): def ensure_local_distutils(): - if 'distutils' in sys.path: + if 'distutils' in sys.modules: raise RuntimeError("Distutils must not be imported before setuptools") with patch_sys_path(): importlib.import_module('distutils') -- cgit v1.2.3 From b596e4b0f684f5ac11673598e1de3aaa5bc74162 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 21 Jun 2020 14:47:11 -0400 Subject: Replace distutils rather than requiring it to be present in advanec. Instead of crashing, issue a warning when Setuptools is replacing distutils. --- setuptools/distutils_patch.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'setuptools/distutils_patch.py') diff --git a/setuptools/distutils_patch.py b/setuptools/distutils_patch.py index 1416a7a3..f9e63798 100644 --- a/setuptools/distutils_patch.py +++ b/setuptools/distutils_patch.py @@ -6,8 +6,10 @@ for more motivation. """ import sys +import re import importlib import contextlib +import warnings from os.path import dirname @@ -21,11 +23,20 @@ def patch_sys_path(): sys.path[:] = orig +def clear_distutils(): + if 'distutils' not in sys.modules: + return + warnings.warn("Setuptools is replacing distutils") + mods = [name for name in sys.modules if re.match(r'distutils\b', name)] + for name in mods: + del sys.modules[name] + + def ensure_local_distutils(): - if 'distutils' in sys.modules: - raise RuntimeError("Distutils must not be imported before setuptools") + clear_distutils() with patch_sys_path(): importlib.import_module('distutils') + assert sys.modules['distutils'].local ensure_local_distutils() -- cgit v1.2.3 From bb9fb1fcfe37c1ef1e29e1e6d1fc4e483c743380 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 3 Jul 2020 03:53:46 -0400 Subject: Move distutils into a submodule of setuptools. --- setuptools/distutils_patch.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) (limited to 'setuptools/distutils_patch.py') diff --git a/setuptools/distutils_patch.py b/setuptools/distutils_patch.py index f9e63798..06eed82f 100644 --- a/setuptools/distutils_patch.py +++ b/setuptools/distutils_patch.py @@ -8,19 +8,7 @@ for more motivation. import sys import re import importlib -import contextlib import warnings -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 clear_distutils(): @@ -34,9 +22,12 @@ def clear_distutils(): def ensure_local_distutils(): clear_distutils() - with patch_sys_path(): - importlib.import_module('distutils') - assert sys.modules['distutils'].local + distutils = importlib.import_module('setuptools._distutils') + sys.modules['distutils'] = distutils + + # sanity check that submodules load as expected + core = importlib.import_module('distutils.core') + assert '_distutils' in core.__file__, core.__file__ ensure_local_distutils() -- cgit v1.2.3 From 97192962e89a24a02effd1f7a541108335517253 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 3 Jul 2020 04:54:40 -0400 Subject: Ensure the module is named 'distutils'. Avoids errors when distutils.log and setuptools._distutils.log are two separate modules with separate state. --- setuptools/distutils_patch.py | 1 + 1 file changed, 1 insertion(+) (limited to 'setuptools/distutils_patch.py') diff --git a/setuptools/distutils_patch.py b/setuptools/distutils_patch.py index 06eed82f..b2095fba 100644 --- a/setuptools/distutils_patch.py +++ b/setuptools/distutils_patch.py @@ -23,6 +23,7 @@ def clear_distutils(): def ensure_local_distutils(): clear_distutils() distutils = importlib.import_module('setuptools._distutils') + distutils.__name__ = 'distutils' sys.modules['distutils'] = distutils # sanity check that submodules load as expected -- cgit v1.2.3