Skip to content

Commit

Permalink
Merge pull request #233 from pypa/quality/231-global-state
Browse files Browse the repository at this point in the history
Limit mutating global state
  • Loading branch information
jaraco committed Mar 2, 2024
2 parents e651e53 + 9e83319 commit 832bdd9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 20 deletions.
7 changes: 7 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,10 @@ def temp_home(tmp_path, monkeypatch):
def fake_home(fs, monkeypatch):
home = fs.create_dir('/fakehome')
return _set_home(monkeypatch, pathlib.Path(home.path))


@pytest.fixture
def disable_macos_customization(monkeypatch):
from distutils import sysconfig

monkeypatch.setattr(sysconfig, '_customize_macos', lambda: None)
36 changes: 20 additions & 16 deletions distutils/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""

import os
import functools
import re
import sys
import sysconfig
Expand Down Expand Up @@ -266,29 +267,32 @@ def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
)


@functools.lru_cache()
def _customize_macos():
"""
Perform first-time customization of compiler-related
config vars on macOS. Use after a compiler is known
to be needed. This customization exists primarily to support Pythons
from binary installers. The kind and paths to build tools on
the user system may vary significantly from the system
that Python itself was built on. Also the user OS
version and build tools may not support the same set
of CPU architectures for universal builds.
"""

sys.platform == "darwin" and __import__('_osx_support').customize_compiler(
get_config_vars()
)


def customize_compiler(compiler): # noqa: C901
"""Do any platform-specific customization of a CCompiler instance.
Mainly needed on Unix, so we can plug in the information that
varies across Unices and is stored in Python's Makefile.
"""
if compiler.compiler_type == "unix":
if sys.platform == "darwin":
# Perform first-time customization of compiler-related
# config vars on OS X now that we know we need a compiler.
# This is primarily to support Pythons from binary
# installers. The kind and paths to build tools on
# the user system may vary significantly from the system
# that Python itself was built on. Also the user OS
# version and build tools may not support the same set
# of CPU architectures for universal builds.
global _config_vars
# Use get_config_var() to ensure _config_vars is initialized.
if not get_config_var('CUSTOMIZED_OSX_COMPILER'):
import _osx_support

_osx_support.customize_compiler(_config_vars)
_config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
_customize_macos()

(
cc,
Expand Down
3 changes: 1 addition & 2 deletions distutils/tests/test_sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ def set_executables(self, **kw):
'CCSHARED': '--sc-ccshared',
'LDSHARED': 'sc_ldshared',
'SHLIB_SUFFIX': 'sc_shutil_suffix',
# On macOS, disable _osx_support.customize_compiler()
'CUSTOMIZED_OSX_COMPILER': 'True',
}

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

@pytest.mark.skipif("get_default_compiler() != 'unix'")
@pytest.mark.usefixtures('disable_macos_customization')
def test_customize_compiler(self):
# Make sure that sysconfig._config_vars is initialized
sysconfig.get_config_vars()
Expand Down
1 change: 1 addition & 0 deletions distutils/tests/test_unixccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ def gcvs(*args, _orig=sysconfig.get_config_vars):
assert self.cc.linker_so[0] == 'my_cc'

@pytest.mark.skipif('platform.system == "Windows"')
@pytest.mark.usefixtures('disable_macos_customization')
def test_cc_overrides_ldshared_for_cxx_correctly(self):
"""
Ensure that setting CC env variable also changes default linker
Expand Down
4 changes: 4 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
[lint]
select = [
"C901",
"W",
]
ignore = [
# https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
"W191",
Expand Down
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ testing =
docs =
# upstream
sphinx >= 3.5
# workaround for sphinx/sphinx-doc#11662
sphinx < 7.2.5
jaraco.packaging >= 9.3
rst.linker >= 1.9
furo
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extras =
[testenv:diffcov]
description = run tests and check that diff from main is covered
deps =
{[testenv]deps}
diff-cover
commands =
pytest {posargs} --cov-report xml
Expand Down

0 comments on commit 832bdd9

Please sign in to comment.