Skip to content

Commit

Permalink
Merge pull request #2907 from pypa/bugfix/2906-distutils-race
Browse files Browse the repository at this point in the history
In ensure_local_distutils, re-use DistutilsMetaFinder
  • Loading branch information
jaraco authored Nov 26, 2021
2 parents 4c57dfb + 71c2891 commit e7a67a3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
12 changes: 8 additions & 4 deletions _distutils_hack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__

Expand Down
1 change: 1 addition & 0 deletions changelog.d/2906.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In ensure_local_distutils, re-use DistutilsMetaFinder to load the module. Avoids race conditions when _distutils_system_mod is employed.
14 changes: 3 additions & 11 deletions setuptools/tests/test_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from distutils import log

import pytest
from jaraco import path

Expand All @@ -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',
Expand Down Expand Up @@ -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

0 comments on commit e7a67a3

Please sign in to comment.