Repository: pypa/setuptools

Issue:
`distutils` submodules being loaded from the stdlib
It seems the issue is that `distutils.sysconfig` is being loaded from the stdlib, even though [the distutils hack has an explicit check that submodules are loaded from the locally-bundled copy](https://github.com/pypa/setuptools/blob/dd5a2cec373ffe7eefc087c1cd06fb4e491a7e88/_distutils_hack/__init__.py#L55-L57).

_Originally posted by @jaraco in https://github.com/pypa/distutils/issues/16#issuecomment-980043534_
`distutils` submodules being loaded from the stdlib
It seems the issue is that `distutils.sysconfig` is being loaded from the stdlib, even though [the distutils hack has an explicit check that submodules are loaded from the locally-bundled copy](https://github.com/pypa/setuptools/blob/dd5a2cec373ffe7eefc087c1cd06fb4e491a7e88/_distutils_hack/__init__.py#L55-L57).

_Originally posted by @jaraco in https://github.com/pypa/distutils/issues/16#issuecomment-980043534_


Discussion:
The issue only occurs when `_distutils_system_mod` is employed and that module itself imports modules from distutils because `sys.modules` won't yet have been [updated to reflect the new base](https://github.com/pypa/setuptools/blob/dd5a2cec373ffe7eefc087c1cd06fb4e491a7e88/_distutils_hack/__init__.py#L52-L53) for `distutils`.
This issue primarily only affects the code path where `ensure_local_distutils` is called.
The issue only occurs when `_distutils_system_mod` is employed and that module itself imports modules from distutils because `sys.modules` won't yet have been [updated to reflect the new base](https://github.com/pypa/setuptools/blob/dd5a2cec373ffe7eefc087c1cd06fb4e491a7e88/_distutils_hack/__init__.py#L52-L53) for `distutils`.
This issue primarily only affects the code path where `ensure_local_distutils` is called.

Resolving patch:
diff --git a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py
--- a/_distutils_hack/__init__.py
+++ b/_distutils_hack/__init__.py
@@ -48,11 +48,15 @@ def enabled():
 
 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
+    # With the DistutilsMetaFinder in place,
+    # perform an import to cause distutils to be
+    # loaded from setuptools._distutils. Ref #2906.
+    add_shim()
+    importlib.import_module('distutils')
+    remove_shim()
+
+    # check that submodules load as expected
     core = importlib.import_module('distutils.core')
     assert '_distutils' in core.__file__, core.__file__
 


Test patch:
diff --git a/setuptools/tests/test_test.py b/setuptools/tests/test_test.py
--- a/setuptools/tests/test_test.py
+++ b/setuptools/tests/test_test.py
@@ -1,5 +1,3 @@
-from distutils import log
-
 import pytest
 from jaraco import path
 
@@ -9,14 +7,7 @@
 from .textwrap import DALS
 
 
-@pytest.fixture
-def quiet_log():
-    # Running some of the other tests will automatically
-    # change the log level to info, messing our output.
-    log.set_verbosity(0)
-
-
-@pytest.mark.usefixtures('tmpdir_cwd', 'quiet_log')
+@pytest.mark.usefixtures('tmpdir_cwd')
 def test_tests_are_run_once(capfd):
     params = dict(
         name='foo',
@@ -46,4 +37,5 @@ def test_test(self):
     cmd.ensure_finalized()
     cmd.run()
     out, err = capfd.readouterr()
-    assert out == 'Foo\n'
+    assert out.endswith('Foo\n')
+    assert len(out.split('Foo')) == 2