Skip to content

Commit f60cd62

Browse files
authored
Merge pull request #234 from pypa/quality/231-global-state
Limit mutating global state
2 parents e651e53 + 9e83319 commit f60cd62

File tree

7 files changed

+34
-20
lines changed

7 files changed

+34
-20
lines changed

conftest.py

+7
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,10 @@ def temp_home(tmp_path, monkeypatch):
152152
def fake_home(fs, monkeypatch):
153153
home = fs.create_dir('/fakehome')
154154
return _set_home(monkeypatch, pathlib.Path(home.path))
155+
156+
157+
@pytest.fixture
158+
def disable_macos_customization(monkeypatch):
159+
from distutils import sysconfig
160+
161+
monkeypatch.setattr(sysconfig, '_customize_macos', lambda: None)

distutils/sysconfig.py

+20-16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"""
1111

1212
import os
13+
import functools
1314
import re
1415
import sys
1516
import sysconfig
@@ -266,29 +267,32 @@ def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
266267
)
267268

268269

270+
@functools.lru_cache()
271+
def _customize_macos():
272+
"""
273+
Perform first-time customization of compiler-related
274+
config vars on macOS. Use after a compiler is known
275+
to be needed. This customization exists primarily to support Pythons
276+
from binary installers. The kind and paths to build tools on
277+
the user system may vary significantly from the system
278+
that Python itself was built on. Also the user OS
279+
version and build tools may not support the same set
280+
of CPU architectures for universal builds.
281+
"""
282+
283+
sys.platform == "darwin" and __import__('_osx_support').customize_compiler(
284+
get_config_vars()
285+
)
286+
287+
269288
def customize_compiler(compiler): # noqa: C901
270289
"""Do any platform-specific customization of a CCompiler instance.
271290
272291
Mainly needed on Unix, so we can plug in the information that
273292
varies across Unices and is stored in Python's Makefile.
274293
"""
275294
if compiler.compiler_type == "unix":
276-
if sys.platform == "darwin":
277-
# Perform first-time customization of compiler-related
278-
# config vars on OS X now that we know we need a compiler.
279-
# This is primarily to support Pythons from binary
280-
# installers. The kind and paths to build tools on
281-
# the user system may vary significantly from the system
282-
# that Python itself was built on. Also the user OS
283-
# version and build tools may not support the same set
284-
# of CPU architectures for universal builds.
285-
global _config_vars
286-
# Use get_config_var() to ensure _config_vars is initialized.
287-
if not get_config_var('CUSTOMIZED_OSX_COMPILER'):
288-
import _osx_support
289-
290-
_osx_support.customize_compiler(_config_vars)
291-
_config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
295+
_customize_macos()
292296

293297
(
294298
cc,

distutils/tests/test_sysconfig.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ def set_executables(self, **kw):
9898
'CCSHARED': '--sc-ccshared',
9999
'LDSHARED': 'sc_ldshared',
100100
'SHLIB_SUFFIX': 'sc_shutil_suffix',
101-
# On macOS, disable _osx_support.customize_compiler()
102-
'CUSTOMIZED_OSX_COMPILER': 'True',
103101
}
104102

105103
comp = compiler()
@@ -111,6 +109,7 @@ def set_executables(self, **kw):
111109
return comp
112110

113111
@pytest.mark.skipif("get_default_compiler() != 'unix'")
112+
@pytest.mark.usefixtures('disable_macos_customization')
114113
def test_customize_compiler(self):
115114
# Make sure that sysconfig._config_vars is initialized
116115
sysconfig.get_config_vars()

distutils/tests/test_unixccompiler.py

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ def gcvs(*args, _orig=sysconfig.get_config_vars):
248248
assert self.cc.linker_so[0] == 'my_cc'
249249

250250
@pytest.mark.skipif('platform.system == "Windows"')
251+
@pytest.mark.usefixtures('disable_macos_customization')
251252
def test_cc_overrides_ldshared_for_cxx_correctly(self):
252253
"""
253254
Ensure that setting CC env variable also changes default linker

ruff.toml

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
[lint]
2+
select = [
3+
"C901",
4+
"W",
5+
]
26
ignore = [
37
# https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
48
"W191",

setup.cfg

-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ testing =
4343
docs =
4444
# upstream
4545
sphinx >= 3.5
46-
# workaround for sphinx/sphinx-doc#11662
47-
sphinx < 7.2.5
4846
jaraco.packaging >= 9.3
4947
rst.linker >= 1.9
5048
furo

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extras =
1414
[testenv:diffcov]
1515
description = run tests and check that diff from main is covered
1616
deps =
17+
{[testenv]deps}
1718
diff-cover
1819
commands =
1920
pytest {posargs} --cov-report xml

0 commit comments

Comments
 (0)