Skip to content

Commit 0e14162

Browse files
authored
Merge pull request #21061 from anntzer/ud
Remove most visible dependencies on distutils.
2 parents 9bbef1e + b8aa5b2 commit 0e14162

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

setup.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,11 @@
2929
import shutil
3030
import subprocess
3131

32-
from setuptools import setup, find_packages, Extension
32+
from setuptools import setup, find_packages, Distribution, Extension
3333
import setuptools.command.build_ext
3434
import setuptools.command.build_py
3535
import setuptools.command.sdist
3636

37-
from distutils.errors import CompileError
38-
from distutils.dist import Distribution
39-
4037
import setupext
4138
from setupext import print_raw, print_status
4239

@@ -61,7 +58,10 @@ def has_flag(self, flagname):
6158
f.write('int main (int argc, char **argv) { return 0; }')
6259
try:
6360
self.compile([f.name], extra_postargs=[flagname])
64-
except CompileError:
61+
except Exception as exc:
62+
# https://github.com/pypa/setuptools/issues/2698
63+
if type(exc).__name__ != "CompileError":
64+
raise
6565
return False
6666
return True
6767

@@ -263,7 +263,7 @@ def make_release_tree(self, base_dir, files):
263263
package_data.setdefault(key, [])
264264
package_data[key] = list(set(val + package_data[key]))
265265

266-
setup( # Finally, pass this all along to distutils to do the heavy lifting.
266+
setup( # Finally, pass this all along to setuptools to do the heavy lifting.
267267
name="matplotlib",
268268
description="Python plotting package",
269269
author="John D. Hunter, Michael Droettboom",

setupext.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import configparser
2-
from distutils import ccompiler, sysconfig
3-
from distutils.core import Extension
2+
from distutils import sysconfig
43
import functools
54
import hashlib
65
from io import BytesIO
@@ -16,6 +15,8 @@
1615
import textwrap
1716
import urllib.request
1817

18+
from setuptools import Distribution, Extension
19+
1920
_log = logging.getLogger(__name__)
2021

2122

@@ -523,16 +524,37 @@ def add_libagg_flags_and_sources(ext):
523524
os.path.join("extern", "agg24-svn", "src", x) for x in agg_sources)
524525

525526

526-
# First compile checkdep_freetype2.c, which aborts the compilation either
527-
# with "foo.h: No such file or directory" if the header is not found, or an
528-
# appropriate error message if the header indicates a too-old version.
527+
def get_ccompiler():
528+
"""
529+
Return a new CCompiler instance.
530+
531+
CCompiler used to be constructible via `distutils.ccompiler.new_compiler`,
532+
but this API was removed as part of the distutils deprecation. Instead,
533+
we trick setuptools into instantiating it by creating a dummy Distribution
534+
with a list of extension modules that claims to be truthy, but is actually
535+
empty, and then running the Distribution's build_ext command. (If using
536+
a plain empty ext_modules, build_ext would early-return without doing
537+
anything.)
538+
"""
539+
540+
class L(list):
541+
def __bool__(self):
542+
return True
543+
544+
build_ext = Distribution({"ext_modules": L()}).get_command_obj("build_ext")
545+
build_ext.finalize_options()
546+
build_ext.run()
547+
return build_ext.compiler
529548

530549

531550
class FreeType(SetupPackage):
532551
name = "freetype"
533552

534553
@classmethod
535554
def add_flags(cls, ext):
555+
# checkdep_freetype2.c immediately aborts the compilation either with
556+
# "foo.h: No such file or directory" if the header is not found, or an
557+
# appropriate error message if the header indicates a too-old version.
536558
ext.sources.insert(0, 'src/checkdep_freetype2.c')
537559
if options.get('system_freetype'):
538560
pkg_config_setup_extension(
@@ -636,7 +658,7 @@ def do_custom_build(self, env):
636658
f.truncate()
637659
f.write(vcxproj)
638660

639-
cc = ccompiler.new_compiler()
661+
cc = get_ccompiler()
640662
cc.initialize() # Get msbuild in the %PATH% of cc.spawn.
641663
cc.spawn(["msbuild", str(sln_path),
642664
"/t:Clean;Build",

0 commit comments

Comments
 (0)