8000 Merge pull request #21071 from meeseeksmachine/auto-backport-of-pr-21… · matplotlib/matplotlib@2eae3a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2eae3a3

Browse files
authored
Merge pull request #21071 from meeseeksmachine/auto-backport-of-pr-21061-on-v3.5.x
Backport PR #21061 on branch v3.5.x (Remove most visible dependencies on distutils.)
2 parents 97db802 + 1437848 commit 2eae3a3

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,15 +29,12 @@
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.test
3636
import setuptools.command.sdist
3737

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

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

@@ -270,7 +270,7 @@ def make_release_tree(self, base_dir, files):
270270
package_data.setdefault(key, [])
271271
package_data[key] = list(set(val + package_data[key]))
272272

273-
setup( # Finally, pass this all along to distutils to do the heavy lifting.
273+
setup( # Finally, pass this all along to setuptools to do the heavy lifting.
274274
name="matplotlib",
275275
description="Python plotting package",
276276
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)
0