Skip to content

Expose a subset of distutils C-compiler interface as public API #3445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.d/3445.changes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Re-exposed a subset of ``distutils.ccompiler`` and ``distutils.sysconfig``
modules as ``setuptools.ccompiler``.
6 changes: 6 additions & 0 deletions docs/deprecated/distutils-legacy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ As Distutils is deprecated, any usage of functions or objects from distutils is

``distutils.command.{build_clib,build_ext,build_py,sdist}`` → ``setuptools.command.*``

``distutils.ccompiler.CCompiler`` → ``setuptools.ccompiler.CCompiler``

``distutils.ccompiler.new_compiler`` → ``setuptools.ccompiler.new_compiler``

``distutils.sysconfig.customize_compiler`` → ``setuptools.ccompiler.customize_compiler``

``distutils.log`` → :mod:`logging` (standard library)

``distutils.version.*`` → :doc:`packaging.version.* <packaging:version>`
Expand Down
25 changes: 25 additions & 0 deletions setuptools/ccompiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Expose a subset of distutils as public API
# to help with migration to Python 3.12

from distutils.ccompiler import CCompiler
from distutils.ccompiler import new_compiler


__all__ = [
"CCompiler",
"new_compiler",
"customize_compiler",
]


def customize_compiler(compiler: CCompiler):
"""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.
"""
# Importing `distutils.sysconfig` directly may change the global state.
# We adopt a lazy approach instead.
from distutils.sysconfig import customize_compiler as _customize

return _customize(compiler)
25 changes: 25 additions & 0 deletions setuptools/tests/test_ccompiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from setuptools.ccompiler import CCompiler
from setuptools.ccompiler import customize_compiler
from setuptools.ccompiler import new_compiler

import pytest


def test_ccompiler_namespace(_avoid_permanent_changes_in_sysconfig):
ccompiler = new_compiler()
customize_compiler(ccompiler)
assert hasattr(ccompiler, "compile")
assert CCompiler.compiler_type is None


@pytest.fixture
def _avoid_permanent_changes_in_sysconfig(monkeypatch):
import importlib
import sys

# Avoid caching `distutils.sysconfig` and force it to be re-imported later.
# This should "cancel out" any permanent changes that comes as a side-effect of
# import thing `distutils.sysconfig`.
monkeypatch.setattr(sys, "modules", sys.modules.copy())
yield
importlib.invalidate_caches()