summaryrefslogtreecommitdiff
path: root/setuptools/_distutils
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2023-01-14 11:13:55 -0500
committerJason R. Coombs <jaraco@jaraco.com>2023-01-14 11:13:55 -0500
commit245da5441248eeb2d575034d04cbc241bf545161 (patch)
treed76526e1461252cc1119cd9482a64ef1e75f7838 /setuptools/_distutils
parentd7b783a4b8b01e58135e40bd9a1db8a82c090982 (diff)
parent82eee6a998251b33ab3984f39b25c27ca72ba8b0 (diff)
downloadpython-setuptools-git-245da5441248eeb2d575034d04cbc241bf545161.tar.gz
Merge branch 'main' into debt/remove-legacy-version
Diffstat (limited to 'setuptools/_distutils')
-rw-r--r--setuptools/_distutils/README11
-rw-r--r--setuptools/_distutils/__init__.py12
-rw-r--r--setuptools/_distutils/_collections.py138
-rw-r--r--setuptools/_distutils/_functools.py20
-rw-r--r--setuptools/_distutils/_log.py4
-rw-r--r--setuptools/_distutils/_macos_compat.py12
-rw-r--r--setuptools/_distutils/_msvccompiler.py305
-rw-r--r--setuptools/_distutils/archive_util.py86
-rw-r--r--setuptools/_distutils/bcppcompiler.py245
-rw-r--r--setuptools/_distutils/ccompiler.py539
-rw-r--r--setuptools/_distutils/cmd.py158
-rw-r--r--setuptools/_distutils/command/__init__.py46
-rw-r--r--setuptools/_distutils/command/_framework_compat.py55
-rw-r--r--setuptools/_distutils/command/bdist.py126
-rw-r--r--setuptools/_distutils/command/bdist_dumb.py129
-rw-r--r--setuptools/_distutils/command/bdist_msi.py749
-rw-r--r--setuptools/_distutils/command/bdist_rpm.py448
-rw-r--r--setuptools/_distutils/command/bdist_wininst.py377
-rw-r--r--setuptools/_distutils/command/build.py100
-rw-r--r--setuptools/_distutils/command/build_clib.py119
-rw-r--r--setuptools/_distutils/command/build_ext.py292
-rw-r--r--setuptools/_distutils/command/build_py.py107
-rw-r--r--setuptools/_distutils/command/build_scripts.py237
-rw-r--r--setuptools/_distutils/command/check.py137
-rw-r--r--setuptools/_distutils/command/clean.py58
-rw-r--r--setuptools/_distutils/command/config.py161
-rw-r--r--setuptools/_distutils/command/install.py383
-rw-r--r--setuptools/_distutils/command/install_data.py37
-rw-r--r--setuptools/_distutils/command/install_egg_info.py32
-rw-r--r--setuptools/_distutils/command/install_headers.py18
-rw-r--r--setuptools/_distutils/command/install_lib.py99
-rw-r--r--setuptools/_distutils/command/install_scripts.py17
-rw-r--r--setuptools/_distutils/command/py37compat.py3
-rw-r--r--setuptools/_distutils/command/register.py149
-rw-r--r--setuptools/_distutils/command/sdist.py213
-rw-r--r--setuptools/_distutils/command/upload.py63
-rw-r--r--setuptools/_distutils/config.py59
-rw-r--r--setuptools/_distutils/core.py114
-rw-r--r--setuptools/_distutils/cygwinccompiler.py314
-rw-r--r--setuptools/_distutils/dep_util.py26
-rw-r--r--setuptools/_distutils/dir_util.py81
-rw-r--r--setuptools/_distutils/dist.py424
-rw-r--r--setuptools/_distutils/errors.py66
-rw-r--r--setuptools/_distutils/extension.py78
-rw-r--r--setuptools/_distutils/fancy_getopt.py117
-rw-r--r--setuptools/_distutils/file_util.py63
-rw-r--r--setuptools/_distutils/filelist.py114
-rw-r--r--setuptools/_distutils/log.py98
-rw-r--r--setuptools/_distutils/msvc9compiler.py346
-rw-r--r--setuptools/_distutils/msvccompiler.py316
-rw-r--r--setuptools/_distutils/py35compat.py19
-rw-r--r--setuptools/_distutils/py38compat.py3
-rw-r--r--setuptools/_distutils/py39compat.py22
-rw-r--r--setuptools/_distutils/spawn.py15
-rw-r--r--setuptools/_distutils/sysconfig.py323
-rw-r--r--setuptools/_distutils/tests/__init__.py38
-rw-r--r--setuptools/_distutils/tests/py35compat.py77
-rw-r--r--setuptools/_distutils/tests/py37compat.py18
-rw-r--r--setuptools/_distutils/tests/py38compat.py28
-rw-r--r--setuptools/_distutils/tests/support.py130
-rw-r--r--setuptools/_distutils/tests/test_archive_util.py265
-rw-r--r--setuptools/_distutils/tests/test_bdist.py49
-rw-r--r--setuptools/_distutils/tests/test_bdist_dumb.py61
-rw-r--r--setuptools/_distutils/tests/test_bdist_msi.py28
-rw-r--r--setuptools/_distutils/tests/test_bdist_rpm.py120
-rw-r--r--setuptools/_distutils/tests/test_bdist_wininst.py40
-rw-r--r--setuptools/_distutils/tests/test_build.py37
-rw-r--r--setuptools/_distutils/tests/test_build_clib.py75
-rw-r--r--setuptools/_distutils/tests/test_build_ext.py343
-rw-r--r--setuptools/_distutils/tests/test_build_py.py100
-rw-r--r--setuptools/_distutils/tests/test_build_scripts.py76
-rw-r--r--setuptools/_distutils/tests/test_ccompiler.py55
-rw-r--r--setuptools/_distutils/tests/test_check.py142
-rw-r--r--setuptools/_distutils/tests/test_clean.py28
-rw-r--r--setuptools/_distutils/tests/test_cmd.py101
-rw-r--r--setuptools/_distutils/tests/test_config.py89
-rw-r--r--setuptools/_distutils/tests/test_config_cmd.py57
-rw-r--r--setuptools/_distutils/tests/test_core.py140
-rw-r--r--setuptools/_distutils/tests/test_cygwinccompiler.py130
-rw-r--r--setuptools/_distutils/tests/test_dep_util.py39
-rw-r--r--setuptools/_distutils/tests/test_dir_util.py106
-rw-r--r--setuptools/_distutils/tests/test_dist.py607
-rw-r--r--setuptools/_distutils/tests/test_extension.py99
-rw-r--r--setuptools/_distutils/tests/test_file_util.py87
-rw-r--r--setuptools/_distutils/tests/test_filelist.py315
-rw-r--r--setuptools/_distutils/tests/test_install.py177
-rw-r--r--setuptools/_distutils/tests/test_install_data.py43
-rw-r--r--setuptools/_distutils/tests/test_install_headers.py22
-rw-r--r--setuptools/_distutils/tests/test_install_lib.py53
-rw-r--r--setuptools/_distutils/tests/test_install_scripts.py63
-rw-r--r--setuptools/_distutils/tests/test_log.py49
-rw-r--r--setuptools/_distutils/tests/test_msvc9compiler.py43
-rw-r--r--setuptools/_distutils/tests/test_msvccompiler.py81
-rw-r--r--setuptools/_distutils/tests/test_register.py169
-rw-r--r--setuptools/_distutils/tests/test_sdist.py293
-rw-r--r--setuptools/_distutils/tests/test_spawn.py176
-rw-r--r--setuptools/_distutils/tests/test_sysconfig.py348
-rw-r--r--setuptools/_distutils/tests/test_text_file.py69
-rw-r--r--setuptools/_distutils/tests/test_unixccompiler.py172
-rw-r--r--setuptools/_distutils/tests/test_upload.py134
-rw-r--r--setuptools/_distutils/tests/test_util.py218
-rw-r--r--setuptools/_distutils/tests/test_version.py123
-rw-r--r--setuptools/_distutils/tests/test_versionpredicate.py13
-rw-r--r--setuptools/_distutils/tests/unix_compat.py10
-rw-r--r--setuptools/_distutils/tests/xxmodule-3.8.c411
-rw-r--r--setuptools/_distutils/tests/xxmodule.c412
-rw-r--r--setuptools/_distutils/text_file.py201
-rw-r--r--setuptools/_distutils/unixccompiler.py386
-rw-r--r--setuptools/_distutils/util.py199
-rw-r--r--setuptools/_distutils/version.py55
-rw-r--r--setuptools/_distutils/versionpredicate.py38
111 files changed, 8028 insertions, 7693 deletions
diff --git a/setuptools/_distutils/README b/setuptools/_distutils/README
deleted file mode 100644
index 23f48850..00000000
--- a/setuptools/_distutils/README
+++ /dev/null
@@ -1,11 +0,0 @@
-This directory contains the Distutils package.
-
-There's a full documentation available at:
-
- http://docs.python.org/distutils/
-
-The Distutils-SIG web page is also a good starting point:
-
- http://www.python.org/sigs/distutils-sig/
-
-$Id$
diff --git a/setuptools/_distutils/__init__.py b/setuptools/_distutils/__init__.py
index 8fd493b4..1a188c35 100644
--- a/setuptools/_distutils/__init__.py
+++ b/setuptools/_distutils/__init__.py
@@ -1,17 +1,7 @@
-"""distutils
-
-The main package for the Python Module Distribution Utilities. Normally
-used from a setup script as
-
- from distutils.core import setup
-
- setup (...)
-"""
-
import sys
import importlib
-__version__ = sys.version[:sys.version.index(' ')]
+__version__, _, _ = sys.version.partition(' ')
try:
diff --git a/setuptools/_distutils/_collections.py b/setuptools/_distutils/_collections.py
index 98fce800..02556614 100644
--- a/setuptools/_distutils/_collections.py
+++ b/setuptools/_distutils/_collections.py
@@ -1,5 +1,7 @@
import collections
+import functools
import itertools
+import operator
# from jaraco.collections 3.5.1
@@ -54,3 +56,139 @@ class DictStack(list, collections.abc.Mapping):
def __len__(self):
return len(list(iter(self)))
+
+
+# from jaraco.collections 3.7
+class RangeMap(dict):
+ """
+ A dictionary-like object that uses the keys as bounds for a range.
+ Inclusion of the value for that range is determined by the
+ key_match_comparator, which defaults to less-than-or-equal.
+ A value is returned for a key if it is the first key that matches in
+ the sorted list of keys.
+
+ One may supply keyword parameters to be passed to the sort function used
+ to sort keys (i.e. key, reverse) as sort_params.
+
+ Let's create a map that maps 1-3 -> 'a', 4-6 -> 'b'
+
+ >>> r = RangeMap({3: 'a', 6: 'b'}) # boy, that was easy
+ >>> r[1], r[2], r[3], r[4], r[5], r[6]
+ ('a', 'a', 'a', 'b', 'b', 'b')
+
+ Even float values should work so long as the comparison operator
+ supports it.
+
+ >>> r[4.5]
+ 'b'
+
+ But you'll notice that the way rangemap is defined, it must be open-ended
+ on one side.
+
+ >>> r[0]
+ 'a'
+ >>> r[-1]
+ 'a'
+
+ One can close the open-end of the RangeMap by using undefined_value
+
+ >>> r = RangeMap({0: RangeMap.undefined_value, 3: 'a', 6: 'b'})
+ >>> r[0]
+ Traceback (most recent call last):
+ ...
+ KeyError: 0
+
+ One can get the first or last elements in the range by using RangeMap.Item
+
+ >>> last_item = RangeMap.Item(-1)
+ >>> r[last_item]
+ 'b'
+
+ .last_item is a shortcut for Item(-1)
+
+ >>> r[RangeMap.last_item]
+ 'b'
+
+ Sometimes it's useful to find the bounds for a RangeMap
+
+ >>> r.bounds()
+ (0, 6)
+
+ RangeMap supports .get(key, default)
+
+ >>> r.get(0, 'not found')
+ 'not found'
+
+ >>> r.get(7, 'not found')
+ 'not found'
+
+ One often wishes to define the ranges by their left-most values,
+ which requires use of sort params and a key_match_comparator.
+
+ >>> r = RangeMap({1: 'a', 4: 'b'},
+ ... sort_params=dict(reverse=True),
+ ... key_match_comparator=operator.ge)
+ >>> r[1], r[2], r[3], r[4], r[5], r[6]
+ ('a', 'a', 'a', 'b', 'b', 'b')
+
+ That wasn't nearly as easy as before, so an alternate constructor
+ is provided:
+
+ >>> r = RangeMap.left({1: 'a', 4: 'b', 7: RangeMap.undefined_value})
+ >>> r[1], r[2], r[3], r[4], r[5], r[6]
+ ('a', 'a', 'a', 'b', 'b', 'b')
+
+ """
+
+ def __init__(self, source, sort_params={}, key_match_comparator=operator.le):
+ dict.__init__(self, source)
+ self.sort_params = sort_params
+ self.match = key_match_comparator
+
+ @classmethod
+ def left(cls, source):
+ return cls(
+ source, sort_params=dict(reverse=True), key_match_comparator=operator.ge
+ )
+
+ def __getitem__(self, item):
+ sorted_keys = sorted(self.keys(), **self.sort_params)
+ if isinstance(item, RangeMap.Item):
+ result = self.__getitem__(sorted_keys[item])
+ else:
+ key = self._find_first_match_(sorted_keys, item)
+ result = dict.__getitem__(self, key)
+ if result is RangeMap.undefined_value:
+ raise KeyError(key)
+ return result
+
+ def get(self, key, default=None):
+ """
+ Return the value for key if key is in the dictionary, else default.
+ If default is not given, it defaults to None, so that this method
+ never raises a KeyError.
+ """
+ try:
+ return self[key]
+ except KeyError:
+ return default
+
+ def _find_first_match_(self, keys, item):
+ is_match = functools.partial(self.match, item)
+ matches = list(filter(is_match, keys))
+ if matches:
+ return matches[0]
+ raise KeyError(item)
+
+ def bounds(self):
+ sorted_keys = sorted(self.keys(), **self.sort_params)
+ return (sorted_keys[RangeMap.first_item], sorted_keys[RangeMap.last_item])
+
+ # some special values for the RangeMap
+ undefined_value = type(str('RangeValueUndefined'), (), {})()
+
+ class Item(int):
+ "RangeMap Item"
+
+ first_item = Item(0)
+ last_item = Item(-1)
diff --git a/setuptools/_distutils/_functools.py b/setuptools/_distutils/_functools.py
new file mode 100644
index 00000000..e7053bac
--- /dev/null
+++ b/setuptools/_distutils/_functools.py
@@ -0,0 +1,20 @@
+import functools
+
+
+# from jaraco.functools 3.5
+def pass_none(func):
+ """
+ Wrap func so it's not called if its first param is None
+
+ >>> print_text = pass_none(print)
+ >>> print_text('text')
+ text
+ >>> print_text(None)
+ """
+
+ @functools.wraps(func)
+ def wrapper(param, *args, **kwargs):
+ if param is not None:
+ return func(param, *args, **kwargs)
+
+ return wrapper
diff --git a/setuptools/_distutils/_log.py b/setuptools/_distutils/_log.py
new file mode 100644
index 00000000..4a2ae0ac
--- /dev/null
+++ b/setuptools/_distutils/_log.py
@@ -0,0 +1,4 @@
+import logging
+
+
+log = logging.getLogger()
diff --git a/setuptools/_distutils/_macos_compat.py b/setuptools/_distutils/_macos_compat.py
new file mode 100644
index 00000000..17769e91
--- /dev/null
+++ b/setuptools/_distutils/_macos_compat.py
@@ -0,0 +1,12 @@
+import sys
+import importlib
+
+
+def bypass_compiler_fixup(cmd, args):
+ return cmd
+
+
+if sys.platform == 'darwin':
+ compiler_fixup = importlib.import_module('_osx_support').compiler_fixup
+else:
+ compiler_fixup = bypass_compiler_fixup
diff --git a/setuptools/_distutils/_msvccompiler.py b/setuptools/_distutils/_msvccompiler.py
index f2f801c5..8b4023c4 100644
--- a/setuptools/_distutils/_msvccompiler.py
+++ b/setuptools/_distutils/_msvccompiler.py
@@ -17,24 +17,31 @@ import os
import subprocess
import contextlib
import warnings
-import unittest.mock
+import unittest.mock as mock
+
with contextlib.suppress(ImportError):
import winreg
-from distutils.errors import DistutilsExecError, DistutilsPlatformError, \
- CompileError, LibError, LinkError
-from distutils.ccompiler import CCompiler, gen_lib_options
-from distutils import log
-from distutils.util import get_platform
+from .errors import (
+ DistutilsExecError,
+ DistutilsPlatformError,
+ CompileError,
+ LibError,
+ LinkError,
+)
+from .ccompiler import CCompiler, gen_lib_options
+from ._log import log
+from .util import get_platform
from itertools import count
+
def _find_vc2015():
try:
key = winreg.OpenKeyEx(
winreg.HKEY_LOCAL_MACHINE,
r"Software\Microsoft\VisualStudio\SxS\VC7",
- access=winreg.KEY_READ | winreg.KEY_WOW64_32KEY
+ access=winreg.KEY_READ | winreg.KEY_WOW64_32KEY,
)
except OSError:
log.debug("Visual C++ is not registered")
@@ -57,6 +64,7 @@ def _find_vc2015():
best_version, best_dir = version, vc_dir
return best_version, best_dir
+
def _find_vc2017():
"""Returns "15, path" based on the result of invoking vswhere.exe
If no install is found, returns "None, None"
@@ -72,14 +80,23 @@ def _find_vc2017():
return None, None
try:
- path = subprocess.check_output([
- os.path.join(root, "Microsoft Visual Studio", "Installer", "vswhere.exe"),
- "-latest",
- "-prerelease",
- "-requires", "Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
- "-property", "installationPath",
- "-products", "*",
- ], encoding="mbcs", errors="strict").strip()
+ path = subprocess.check_output(
+ [
+ os.path.join(
+ root, "Microsoft Visual Studio", "Installer", "vswhere.exe"
+ ),
+ "-latest",
+ "-prerelease",
+ "-requires",
+ "Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
+ "-property",
+ "installationPath",
+ "-products",
+ "*",
+ ],
+ encoding="mbcs",
+ errors="strict",
+ ).strip()
except (subprocess.CalledProcessError, OSError, UnicodeDecodeError):
return None, None
@@ -89,13 +106,15 @@ def _find_vc2017():
return None, None
+
PLAT_SPEC_TO_RUNTIME = {
- 'x86' : 'x86',
- 'x86_amd64' : 'x64',
- 'x86_arm' : 'arm',
- 'x86_arm64' : 'arm64'
+ 'x86': 'x86',
+ 'x86_amd64': 'x64',
+ 'x86_arm': 'arm',
+ 'x86_arm64': 'arm64',
}
+
def _find_vcvarsall(plat_spec):
# bpo-38597: Removed vcruntime return value
_, best_dir = _find_vc2017()
@@ -114,12 +133,10 @@ def _find_vcvarsall(plat_spec):
return vcvarsall, None
+
def _get_vc_env(plat_spec):
if os.getenv("DISTUTILS_USE_SDK"):
- return {
- key.lower(): value
- for key, value in os.environ.items()
- }
+ return {key.lower(): value for key, value in os.environ.items()}
vcvarsall, _ = _find_vcvarsall(plat_spec)
if not vcvarsall:
@@ -127,23 +144,22 @@ def _get_vc_env(plat_spec):
try:
out = subprocess.check_output(
- 'cmd /u /c "{}" {} && set'.format(vcvarsall, plat_spec),
+ f'cmd /u /c "{vcvarsall}" {plat_spec} && set',
stderr=subprocess.STDOUT,
).decode('utf-16le', errors='replace')
except subprocess.CalledProcessError as exc:
log.error(exc.output)
- raise DistutilsPlatformError("Error executing {}"
- .format(exc.cmd))
+ raise DistutilsPlatformError(f"Error executing {exc.cmd}")
env = {
key.lower(): value
- for key, _, value in
- (line.partition('=') for line in out.splitlines())
+ for key, _, value in (line.partition('=') for line in out.splitlines())
if key and value
}
return env
+
def _find_exe(exe, paths=None):
"""Return path to an MSVC executable program.
@@ -161,19 +177,21 @@ def _find_exe(exe, paths=None):
return fn
return exe
+
# A map keyed by get_platform() return values to values accepted by
# 'vcvarsall.bat'. Always cross-compile from x86 to work with the
# lighter-weight MSVC installs that do not include native 64-bit tools.
PLAT_TO_VCVARS = {
- 'win32' : 'x86',
- 'win-amd64' : 'x86_amd64',
- 'win-arm32' : 'x86_arm',
- 'win-arm64' : 'x86_arm64'
+ 'win32': 'x86',
+ 'win-amd64': 'x86_amd64',
+ 'win-arm32': 'x86_arm',
+ 'win-arm64': 'x86_arm64',
}
-class MSVCCompiler(CCompiler) :
+
+class MSVCCompiler(CCompiler):
"""Concrete class that implements an interface to Microsoft Visual C++,
- as defined by the CCompiler abstract class."""
+ as defined by the CCompiler abstract class."""
compiler_type = 'msvc'
@@ -192,8 +210,7 @@ class MSVCCompiler(CCompiler) :
# Needed for the filename generation methods provided by the
# base class, CCompiler.
- src_extensions = (_c_extensions + _cpp_extensions +
- _rc_extensions + _mc_extensions)
+ src_extensions = _c_extensions + _cpp_extensions + _rc_extensions + _mc_extensions
res_extension = '.res'
obj_extension = '.obj'
static_lib_extension = '.lib'
@@ -201,13 +218,24 @@ class MSVCCompiler(CCompiler) :
static_lib_format = shared_lib_format = '%s%s'
exe_extension = '.exe'
-
def __init__(self, verbose=0, dry_run=0, force=0):
super().__init__(verbose, dry_run, force)
# target platform (.plat_name is consistent with 'bdist')
self.plat_name = None
self.initialized = False
+ @classmethod
+ def _configure(cls, vc_env):
+ """
+ Set class-level include/lib dirs.
+ """
+ cls.include_dirs = cls._parse_path(vc_env.get('include', ''))
+ cls.library_dirs = cls._parse_path(vc_env.get('lib', ''))
+
+ @staticmethod
+ def _parse_path(val):
+ return [dir.rstrip(os.sep) for dir in val.split(os.pathsep) if dir]
+
def initialize(self, plat_name=None):
# multi-init means we would need to check platform same each time...
assert not self.initialized, "don't init multiple times"
@@ -215,58 +243,62 @@ class MSVCCompiler(CCompiler) :
plat_name = get_platform()
# sanity check for platforms to prevent obscure errors later.
if plat_name not in PLAT_TO_VCVARS:
- raise DistutilsPlatformError("--plat-name must be one of {}"
- .format(tuple(PLAT_TO_VCVARS)))
+ raise DistutilsPlatformError(
+ f"--plat-name must be one of {tuple(PLAT_TO_VCVARS)}"
+ )
# Get the vcvarsall.bat spec for the requested platform.
plat_spec = PLAT_TO_VCVARS[plat_name]
vc_env = _get_vc_env(plat_spec)
if not vc_env:
- raise DistutilsPlatformError("Unable to find a compatible "
- "Visual Studio installation.")
+ raise DistutilsPlatformError(
+ "Unable to find a compatible " "Visual Studio installation."
+ )
+ self._configure(vc_env)
self._paths = vc_env.get('path', '')
paths = self._paths.split(os.pathsep)
self.cc = _find_exe("cl.exe", paths)
self.linker = _find_exe("link.exe", paths)
self.lib = _find_exe("lib.exe", paths)
- self.rc = _find_exe("rc.exe", paths) # resource compiler
- self.mc = _find_exe("mc.exe", paths) # message compiler
- self.mt = _find_exe("mt.exe", paths) # message compiler
-
- for dir in vc_env.get('include', '').split(os.pathsep):
- if dir:
- self.add_include_dir(dir.rstrip(os.sep))
-
- for dir in vc_env.get('lib', '').split(os.pathsep):
- if dir:
- self.add_library_dir(dir.rstrip(os.sep))
+ self.rc = _find_exe("rc.exe", paths) # resource compiler
+ self.mc = _find_exe("mc.exe", paths) # message compiler
+ self.mt = _find_exe("mt.exe", paths) # message compiler
self.preprocess_options = None
# bpo-38597: Always compile with dynamic linking
# Future releases of Python 3.x will include all past
# versions of vcruntime*.dll for compatibility.
- self.compile_options = [
- '/nologo', '/O2', '/W3', '/GL', '/DNDEBUG', '/MD'
- ]
+ self.compile_options = ['/nologo', '/O2', '/W3', '/GL', '/DNDEBUG', '/MD']
self.compile_options_debug = [
- '/nologo', '/Od', '/MDd', '/Zi', '/W3', '/D_DEBUG'
+ '/nologo',
+ '/Od',
+ '/MDd',
+ '/Zi',
+ '/W3',
+ '/D_DEBUG',
]
- ldflags = [
- '/nologo', '/INCREMENTAL:NO', '/LTCG'
- ]
+ ldflags = ['/nologo', '/INCREMENTAL:NO', '/LTCG']
- ldflags_debug = [
- '/nologo', '/INCREMENTAL:NO', '/LTCG', '/DEBUG:FULL'
- ]
+ ldflags_debug = ['/nologo', '/INCREMENTAL:NO', '/LTCG', '/DEBUG:FULL']
self.ldflags_exe = [*ldflags, '/MANIFEST:EMBED,ID=1']
self.ldflags_exe_debug = [*ldflags_debug, '/MANIFEST:EMBED,ID=1']
- self.ldflags_shared = [*ldflags, '/DLL', '/MANIFEST:EMBED,ID=2', '/MANIFESTUAC:NO']
- self.ldflags_shared_debug = [*ldflags_debug, '/DLL', '/MANIFEST:EMBED,ID=2', '/MANIFESTUAC:NO']
+ self.ldflags_shared = [
+ *ldflags,
+ '/DLL',
+ '/MANIFEST:EMBED,ID=2',
+ '/MANIFESTUAC:NO',
+ ]
+ self.ldflags_shared_debug = [
+ *ldflags_debug,
+ '/DLL',
+ '/MANIFEST:EMBED,ID=2',
+ '/MANIFESTUAC:NO',
+ ]
self.ldflags_static = [*ldflags]
self.ldflags_static_debug = [*ldflags_debug]
@@ -286,47 +318,33 @@ class MSVCCompiler(CCompiler) :
# -- Worker methods ------------------------------------------------
- def object_filenames(self,
- source_filenames,
- strip_dir=0,
- output_dir=''):
- ext_map = {
- **{ext: self.obj_extension for ext in self.src_extensions},
- **{ext: self.res_extension for ext in self._rc_extensions + self._mc_extensions},
+ @property
+ def out_extensions(self):
+ return {
+ **super().out_extensions,
+ **{
+ ext: self.res_extension
+ for ext in self._rc_extensions + self._mc_extensions
+ },
}
- output_dir = output_dir or ''
-
- def make_out_path(p):
- base, ext = os.path.splitext(p)
- if strip_dir:
- base = os.path.basename(base)
- else:
- _, base = os.path.splitdrive(base)
- if base.startswith((os.path.sep, os.path.altsep)):
- base = base[1:]
- try:
- # XXX: This may produce absurdly long paths. We should check
- # the length of the result and trim base until we fit within
- # 260 characters.
- return os.path.join(output_dir, base + ext_map[ext])
- except LookupError:
- # Better to raise an exception instead of silently continuing
- # and later complain about sources and targets having
- # different lengths
- raise CompileError("Don't know how to compile {}".format(p))
-
- return list(map(make_out_path, source_filenames))
-
-
- def compile(self, sources,
- output_dir=None, macros=None, include_dirs=None, debug=0,
- extra_preargs=None, extra_postargs=None, depends=None):
+ def compile( # noqa: C901
+ self,
+ sources,
+ output_dir=None,
+ macros=None,
+ include_dirs=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ depends=None,
+ ):
if not self.initialized:
self.initialize()
- compile_info = self._setup_compile(output_dir, macros, include_dirs,
- sources, depends, extra_postargs)
+ compile_info = self._setup_compile(
+ output_dir, macros, include_dirs, sources, depends, extra_postargs
+ )
macros, objects, extra_postargs, pp_opts, build = compile_info
compile_opts = extra_preargs or []
@@ -336,7 +354,6 @@ class MSVCCompiler(CCompiler) :
else:
compile_opts.extend(self.compile_options)
-
add_cpp_opts = False
for obj in objects:
@@ -381,7 +398,7 @@ class MSVCCompiler(CCompiler) :
try:
# first compile .MC to .RC and .H file
self.spawn([self.mc, '-h', h_dir, '-r', rc_dir, src])
- base, _ = os.path.splitext(os.path.basename (src))
+ base, _ = os.path.splitext(os.path.basename(src))
rc_file = os.path.join(rc_dir, base + '.rc')
# then compile .RC to .RES file
self.spawn([self.rc, "/fo" + obj, rc_file])
@@ -391,8 +408,7 @@ class MSVCCompiler(CCompiler) :
continue
else:
# how to handle this file?
- raise CompileError("Don't know how to compile {} to {}"
- .format(src, obj))
+ raise CompileError(f"Don't know how to compile {src} to {obj}")
args = [self.cc] + compile_opts + pp_opts
if add_cpp_opts:
@@ -408,24 +424,19 @@ class MSVCCompiler(CCompiler) :
return objects
-
- def create_static_lib(self,
- objects,
- output_libname,
- output_dir=None,
- debug=0,
- target_lang=None):
+ def create_static_lib(
+ self, objects, output_libname, output_dir=None, debug=0, target_lang=None
+ ):
if not self.initialized:
self.initialize()
objects, output_dir = self._fix_object_args(objects, output_dir)
- output_filename = self.library_filename(output_libname,
- output_dir=output_dir)
+ output_filename = self.library_filename(output_libname, output_dir=output_dir)
if self._need_link(objects, output_filename):
lib_args = objects + ['/OUT:' + output_filename]
if debug:
- pass # XXX what goes here?
+ pass # XXX what goes here?
try:
log.debug('Executing "%s" %s', self.lib, ' '.join(lib_args))
self.spawn([self.lib] + lib_args)
@@ -434,36 +445,36 @@ class MSVCCompiler(CCompiler) :
else:
log.debug("skipping %s (up-to-date)", output_filename)
-
- def link(self,
- target_desc,
- objects,
- output_filename,
- output_dir=None,
- libraries=None,
- library_dirs=None,
- runtime_library_dirs=None,
- export_symbols=None,
- debug=0,
- extra_preargs=None,
- extra_postargs=None,
- build_temp=None,
- target_lang=None):
+ def link(
+ self,
+ target_desc,
+ objects,
+ output_filename,
+ output_dir=None,
+ libraries=None,
+ library_dirs=None,
+ runtime_library_dirs=None,
+ export_symbols=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ build_temp=None,
+ target_lang=None,
+ ):
if not self.initialized:
self.initialize()
objects, output_dir = self._fix_object_args(objects, output_dir)
- fixed_args = self._fix_lib_args(libraries, library_dirs,
- runtime_library_dirs)
+ fixed_args = self._fix_lib_args(libraries, library_dirs, runtime_library_dirs)
libraries, library_dirs, runtime_library_dirs = fixed_args
if runtime_library_dirs:
- self.warn("I don't know what to do with 'runtime_library_dirs': "
- + str(runtime_library_dirs))
+ self.warn(
+ "I don't know what to do with 'runtime_library_dirs': "
+ + str(runtime_library_dirs)
+ )
- lib_opts = gen_lib_options(self,
- library_dirs, runtime_library_dirs,
- libraries)
+ lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs, libraries)
if output_dir is not None:
output_filename = os.path.join(output_dir, output_filename)
@@ -472,8 +483,9 @@ class MSVCCompiler(CCompiler) :
export_opts = ["/EXPORT:" + sym for sym in (export_symbols or [])]
- ld_args = (ldflags + lib_opts + export_opts +
- objects + ['/OUT:' + output_filename])
+ ld_args = (
+ ldflags + lib_opts + export_opts + objects + ['/OUT:' + output_filename]
+ )
# The MSVC linker generates .lib and .exp files, which cannot be
# suppressed by any linker switches. The .lib files may even be
@@ -483,11 +495,10 @@ class MSVCCompiler(CCompiler) :
build_temp = os.path.dirname(objects[0])
if export_symbols is not None:
(dll_name, dll_ext) = os.path.splitext(
- os.path.basename(output_filename))
- implib_file = os.path.join(
- build_temp,
- self.library_filename(dll_name))
- ld_args.append ('/IMPLIB:' + implib_file)
+ os.path.basename(output_filename)
+ )
+ implib_file = os.path.join(build_temp, self.library_filename(dll_name))
+ ld_args.append('/IMPLIB:' + implib_file)
if extra_preargs:
ld_args[:0] = extra_preargs
@@ -525,9 +536,8 @@ class MSVCCompiler(CCompiler) :
raise
else:
return
- warnings.warn(
- "Fallback spawn triggered. Please update distutils monkeypatch.")
- with unittest.mock.patch.dict('os.environ', env):
+ warnings.warn("Fallback spawn triggered. Please update distutils monkeypatch.")
+ with mock.patch.dict('os.environ', env):
bag.value = super().spawn(cmd)
# -- Miscellaneous methods -----------------------------------------
@@ -539,7 +549,8 @@ class MSVCCompiler(CCompiler) :
def runtime_library_dir_option(self, dir):
raise DistutilsPlatformError(
- "don't know how to set runtime library search path for MSVC")
+ "don't know how to set runtime library search path for MSVC"
+ )
def library_option(self, lib):
return self.library_filename(lib)
diff --git a/setuptools/_distutils/archive_util.py b/setuptools/_distutils/archive_util.py
index 565a3117..7f9e1e00 100644
--- a/setuptools/_distutils/archive_util.py
+++ b/setuptools/_distutils/archive_util.py
@@ -13,10 +13,10 @@ except ImportError:
zipfile = None
-from distutils.errors import DistutilsExecError
-from distutils.spawn import spawn
-from distutils.dir_util import mkpath
-from distutils import log
+from .errors import DistutilsExecError
+from .spawn import spawn
+from .dir_util import mkpath
+from ._log import log
try:
from pwd import getpwnam
@@ -28,6 +28,7 @@ try:
except ImportError:
getgrnam = None
+
def _get_gid(name):
"""Returns a gid, given a group name."""
if getgrnam is None or name is None:
@@ -40,6 +41,7 @@ def _get_gid(name):
return result[2]
return None
+
def _get_uid(name):
"""Returns an uid, given a user name."""
if getpwnam is None or name is None:
@@ -52,8 +54,10 @@ def _get_uid(name):
return result[2]
return None
-def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
- owner=None, group=None):
+
+def make_tarball(
+ base_name, base_dir, compress="gzip", verbose=0, dry_run=0, owner=None, group=None
+):
"""Create a (possibly compressed) tar file from all the files under
'base_dir'.
@@ -69,16 +73,21 @@ def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
Returns the output filename.
"""
- tar_compression = {'gzip': 'gz', 'bzip2': 'bz2', 'xz': 'xz', None: '',
- 'compress': ''}
- compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'xz': '.xz',
- 'compress': '.Z'}
+ tar_compression = {
+ 'gzip': 'gz',
+ 'bzip2': 'bz2',
+ 'xz': 'xz',
+ None: '',
+ 'compress': '',
+ }
+ compress_ext = {'gzip': '.gz', 'bzip2': '.bz2', 'xz': '.xz', 'compress': '.Z'}
# flags for compression program, each element of list will be an argument
if compress is not None and compress not in compress_ext.keys():
raise ValueError(
- "bad value for 'compress': must be None, 'gzip', 'bzip2', "
- "'xz' or 'compress'")
+ "bad value for 'compress': must be None, 'gzip', 'bzip2', "
+ "'xz' or 'compress'"
+ )
archive_name = base_name + '.tar'
if compress != 'compress':
@@ -112,7 +121,7 @@ def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
# compression using `compress`
if compress == 'compress':
- warn("'compress' will be deprecated.", PendingDeprecationWarning)
+ warn("'compress' is deprecated.", DeprecationWarning)
# the option varies depending on the platform
compressed_name = archive_name + compress_ext[compress]
if sys.platform == 'win32':
@@ -124,7 +133,8 @@ def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
return archive_name
-def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
+
+def make_zipfile(base_name, base_dir, verbose=0, dry_run=0): # noqa: C901
"""Create a zip file from all the files under 'base_dir'.
The output zip file will be named 'base_name' + ".zip". Uses either the
@@ -145,26 +155,29 @@ def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
zipoptions = "-rq"
try:
- spawn(["zip", zipoptions, zip_filename, base_dir],
- dry_run=dry_run)
+ spawn(["zip", zipoptions, zip_filename, base_dir], dry_run=dry_run)
except DistutilsExecError:
# XXX really should distinguish between "couldn't find
# external 'zip' command" and "zip failed".
- raise DistutilsExecError(("unable to create zip file '%s': "
- "could neither import the 'zipfile' module nor "
- "find a standalone zip utility") % zip_filename)
+ raise DistutilsExecError(
+ (
+ "unable to create zip file '%s': "
+ "could neither import the 'zipfile' module nor "
+ "find a standalone zip utility"
+ )
+ % zip_filename
+ )
else:
- log.info("creating '%s' and adding '%s' to it",
- zip_filename, base_dir)
+ log.info("creating '%s' and adding '%s' to it", zip_filename, base_dir)
if not dry_run:
try:
- zip = zipfile.ZipFile(zip_filename, "w",
- compression=zipfile.ZIP_DEFLATED)
+ zip = zipfile.ZipFile(
+ zip_filename, "w", compression=zipfile.ZIP_DEFLATED
+ )
except RuntimeError:
- zip = zipfile.ZipFile(zip_filename, "w",
- compression=zipfile.ZIP_STORED)
+ zip = zipfile.ZipFile(zip_filename, "w", compression=zipfile.ZIP_STORED)
with zip:
if base_dir != os.curdir:
@@ -184,14 +197,16 @@ def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
return zip_filename
+
ARCHIVE_FORMATS = {
'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
'xztar': (make_tarball, [('compress', 'xz')], "xz'ed tar-file"),
- 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"),
- 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"),
- 'zip': (make_zipfile, [],"ZIP file")
- }
+ 'ztar': (make_tarball, [('compress', 'compress')], "compressed tar file"),
+ 'tar': (make_tarball, [('compress', None)], "uncompressed tar file"),
+ 'zip': (make_zipfile, [], "ZIP file"),
+}
+
def check_archive_formats(formats):
"""Returns the first format from the 'format' list that is unknown.
@@ -203,8 +218,17 @@ def check_archive_formats(formats):
return format
return None
-def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
- dry_run=0, owner=None, group=None):
+
+def make_archive(
+ base_name,
+ format,
+ root_dir=None,
+ base_dir=None,
+ verbose=0,
+ dry_run=0,
+ owner=None,
+ group=None,
+):
"""Create an archive file (eg. zip or tar).
'base_name' is the name of the file to create, minus any format-specific
diff --git a/setuptools/_distutils/bcppcompiler.py b/setuptools/_distutils/bcppcompiler.py
index 2eb6d2e9..5d6b8653 100644
--- a/setuptools/_distutils/bcppcompiler.py
+++ b/setuptools/_distutils/bcppcompiler.py
@@ -13,16 +13,30 @@ for the Borland C++ compiler.
import os
-from distutils.errors import \
- DistutilsExecError, \
- CompileError, LibError, LinkError, UnknownFileError
-from distutils.ccompiler import \
- CCompiler, gen_preprocess_options
-from distutils.file_util import write_file
-from distutils.dep_util import newer
-from distutils import log
-
-class BCPPCompiler(CCompiler) :
+import warnings
+
+from .errors import (
+ DistutilsExecError,
+ CompileError,
+ LibError,
+ LinkError,
+ UnknownFileError,
+)
+from .ccompiler import CCompiler, gen_preprocess_options
+from .file_util import write_file
+from .dep_util import newer
+from ._log import log
+
+
+warnings.warn(
+ "bcppcompiler is deprecated and slated to be removed "
+ "in the future. Please discontinue use or file an issue "
+ "with pypa/distutils describing your use case.",
+ DeprecationWarning,
+)
+
+
+class BCPPCompiler(CCompiler):
"""Concrete class that implements an interface to the Borland C/C++
compiler, as defined by the CCompiler abstract class.
"""
@@ -49,11 +63,7 @@ class BCPPCompiler(CCompiler) :
static_lib_format = shared_lib_format = '%s%s'
exe_extension = '.exe'
-
- def __init__ (self,
- verbose=0,
- dry_run=0,
- force=0):
+ def __init__(self, verbose=0, dry_run=0, force=0):
super().__init__(verbose, dry_run, force)
@@ -73,24 +83,31 @@ class BCPPCompiler(CCompiler) :
self.ldflags_shared_debug = ['/Tpd', '/Gn', '/q', '/x']
self.ldflags_static = []
self.ldflags_exe = ['/Gn', '/q', '/x']
- self.ldflags_exe_debug = ['/Gn', '/q', '/x','/r']
-
+ self.ldflags_exe_debug = ['/Gn', '/q', '/x', '/r']
# -- Worker methods ------------------------------------------------
- def compile(self, sources,
- output_dir=None, macros=None, include_dirs=None, debug=0,
- extra_preargs=None, extra_postargs=None, depends=None):
-
- macros, objects, extra_postargs, pp_opts, build = \
- self._setup_compile(output_dir, macros, include_dirs, sources,
- depends, extra_postargs)
+ def compile( # noqa: C901
+ self,
+ sources,
+ output_dir=None,
+ macros=None,
+ include_dirs=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ depends=None,
+ ):
+
+ macros, objects, extra_postargs, pp_opts, build = self._setup_compile(
+ output_dir, macros, include_dirs, sources, depends, extra_postargs
+ )
compile_opts = extra_preargs or []
- compile_opts.append ('-c')
+ compile_opts.append('-c')
if debug:
- compile_opts.extend (self.compile_options_debug)
+ compile_opts.extend(self.compile_options_debug)
else:
- compile_opts.extend (self.compile_options)
+ compile_opts.extend(self.compile_options)
for obj in objects:
try:
@@ -106,14 +123,14 @@ class BCPPCompiler(CCompiler) :
if ext == '.res':
# This is already a binary file -- skip it.
- continue # the 'for' loop
+ continue # the 'for' loop
if ext == '.rc':
# This needs to be compiled to a .res file -- do it now.
try:
- self.spawn (["brcc32", "-fo", obj, src])
+ self.spawn(["brcc32", "-fo", obj, src])
except DistutilsExecError as msg:
raise CompileError(msg)
- continue # the 'for' loop
+ continue # the 'for' loop
# The next two are both for the real compiler.
if ext in self._c_extensions:
@@ -132,9 +149,14 @@ class BCPPCompiler(CCompiler) :
# Note that the source file names must appear at the end of
# the command line.
try:
- self.spawn ([self.cc] + compile_opts + pp_opts +
- [input_opt, output_opt] +
- extra_postargs + [src])
+ self.spawn(
+ [self.cc]
+ + compile_opts
+ + pp_opts
+ + [input_opt, output_opt]
+ + extra_postargs
+ + [src]
+ )
except DistutilsExecError as msg:
raise CompileError(msg)
@@ -142,24 +164,19 @@ class BCPPCompiler(CCompiler) :
# compile ()
+ def create_static_lib(
+ self, objects, output_libname, output_dir=None, debug=0, target_lang=None
+ ):
- def create_static_lib (self,
- objects,
- output_libname,
- output_dir=None,
- debug=0,
- target_lang=None):
+ (objects, output_dir) = self._fix_object_args(objects, output_dir)
+ output_filename = self.library_filename(output_libname, output_dir=output_dir)
- (objects, output_dir) = self._fix_object_args (objects, output_dir)
- output_filename = \
- self.library_filename (output_libname, output_dir=output_dir)
-
- if self._need_link (objects, output_filename):
+ if self._need_link(objects, output_filename):
lib_args = [output_filename, '/u'] + objects
if debug:
- pass # XXX what goes here?
+ pass # XXX what goes here?
try:
- self.spawn ([self.lib] + lib_args)
+ self.spawn([self.lib] + lib_args)
except DistutilsExecError as msg:
raise LibError(msg)
else:
@@ -167,37 +184,41 @@ class BCPPCompiler(CCompiler) :
# create_static_lib ()
-
- def link (self,
- target_desc,
- objects,
- output_filename,
- output_dir=None,
- libraries=None,
- library_dirs=None,
- runtime_library_dirs=None,
- export_symbols=None,
- debug=0,
- extra_preargs=None,
- extra_postargs=None,
- build_temp=None,
- target_lang=None):
+ def link( # noqa: C901
+ self,
+ target_desc,
+ objects,
+ output_filename,
+ output_dir=None,
+ libraries=None,
+ library_dirs=None,
+ runtime_library_dirs=None,
+ export_symbols=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ build_temp=None,
+ target_lang=None,
+ ):
# XXX this ignores 'build_temp'! should follow the lead of
# msvccompiler.py
- (objects, output_dir) = self._fix_object_args (objects, output_dir)
- (libraries, library_dirs, runtime_library_dirs) = \
- self._fix_lib_args (libraries, library_dirs, runtime_library_dirs)
+ (objects, output_dir) = self._fix_object_args(objects, output_dir)
+ (libraries, library_dirs, runtime_library_dirs) = self._fix_lib_args(
+ libraries, library_dirs, runtime_library_dirs
+ )
if runtime_library_dirs:
- log.warn("I don't know what to do with 'runtime_library_dirs': %s",
- str(runtime_library_dirs))
+ log.warning(
+ "I don't know what to do with 'runtime_library_dirs': %s",
+ str(runtime_library_dirs),
+ )
if output_dir is not None:
- output_filename = os.path.join (output_dir, output_filename)
+ output_filename = os.path.join(output_dir, output_filename)
- if self._need_link (objects, output_filename):
+ if self._need_link(objects, output_filename):
# Figure out linker args based on type of target.
if target_desc == CCompiler.EXECUTABLE:
@@ -213,20 +234,18 @@ class BCPPCompiler(CCompiler) :
else:
ld_args = self.ldflags_shared[:]
-
# Create a temporary exports file for use by the linker
if export_symbols is None:
def_file = ''
else:
- head, tail = os.path.split (output_filename)
- modname, ext = os.path.splitext (tail)
- temp_dir = os.path.dirname(objects[0]) # preserve tree structure
- def_file = os.path.join (temp_dir, '%s.def' % modname)
+ head, tail = os.path.split(output_filename)
+ modname, ext = os.path.splitext(tail)
+ temp_dir = os.path.dirname(objects[0]) # preserve tree structure
+ def_file = os.path.join(temp_dir, '%s.def' % modname)
contents = ['EXPORTS']
- for sym in (export_symbols or []):
- contents.append(' %s=_%s' % (sym, sym))
- self.execute(write_file, (def_file, contents),
- "writing %s" % def_file)
+ for sym in export_symbols or []:
+ contents.append(' {}=_{}'.format(sym, sym))
+ self.execute(write_file, (def_file, contents), "writing %s" % def_file)
# Borland C++ has problems with '/' in paths
objects2 = map(os.path.normpath, objects)
@@ -241,10 +260,9 @@ class BCPPCompiler(CCompiler) :
else:
objects.append(file)
-
- for l in library_dirs:
- ld_args.append("/L%s" % os.path.normpath(l))
- ld_args.append("/L.") # we sometimes use relative paths
+ for ell in library_dirs:
+ ld_args.append("/L%s" % os.path.normpath(ell))
+ ld_args.append("/L.") # we sometimes use relative paths
# list of object files
ld_args.extend(objects)
@@ -260,7 +278,7 @@ class BCPPCompiler(CCompiler) :
# them. Arghghh!. Apparently it works fine as coded...
# name of dll/exe file
- ld_args.extend([',',output_filename])
+ ld_args.extend([',', output_filename])
# no map file and start libraries
ld_args.append(',,')
@@ -276,24 +294,23 @@ class BCPPCompiler(CCompiler) :
ld_args.append(libfile)
# some default libraries
- ld_args.append ('import32')
- ld_args.append ('cw32mt')
+ ld_args.append('import32')
+ ld_args.append('cw32mt')
# def file for export symbols
- ld_args.extend([',',def_file])
+ ld_args.extend([',', def_file])
# add resource files
ld_args.append(',')
ld_args.extend(resources)
-
if extra_preargs:
ld_args[:0] = extra_preargs
if extra_postargs:
ld_args.extend(extra_postargs)
- self.mkpath (os.path.dirname (output_filename))
+ self.mkpath(os.path.dirname(output_filename))
try:
- self.spawn ([self.linker] + ld_args)
+ self.spawn([self.linker] + ld_args)
except DistutilsExecError as msg:
raise LinkError(msg)
@@ -304,8 +321,7 @@ class BCPPCompiler(CCompiler) :
# -- Miscellaneous methods -----------------------------------------
-
- def find_library_file (self, dirs, lib, debug=0):
+ def find_library_file(self, dirs, lib, debug=0):
# List of effective library names to try, in order of preference:
# xxx_bcpp.lib is better than xxx.lib
# and xxx_d.lib is better than xxx.lib if debug is set
@@ -316,7 +332,7 @@ class BCPPCompiler(CCompiler) :
# compiler they care about, since (almost?) every Windows compiler
# seems to have a different format for static libraries.
if debug:
- dlib = (lib + "_d")
+ dlib = lib + "_d"
try_names = (dlib + "_bcpp", lib + "_bcpp", dlib, lib)
else:
try_names = (lib + "_bcpp", lib)
@@ -331,43 +347,42 @@ class BCPPCompiler(CCompiler) :
return None
# overwrite the one from CCompiler to support rc and res-files
- def object_filenames (self,
- source_filenames,
- strip_dir=0,
- output_dir=''):
- if output_dir is None: output_dir = ''
+ def object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
+ if output_dir is None:
+ output_dir = ''
obj_names = []
for src_name in source_filenames:
# use normcase to make sure '.rc' is really '.rc' and not '.RC'
- (base, ext) = os.path.splitext (os.path.normcase(src_name))
- if ext not in (self.src_extensions + ['.rc','.res']):
- raise UnknownFileError("unknown file type '%s' (from '%s')" % \
- (ext, src_name))
+ (base, ext) = os.path.splitext(os.path.normcase(src_name))
+ if ext not in (self.src_extensions + ['.rc', '.res']):
+ raise UnknownFileError(
+ "unknown file type '{}' (from '{}')".format(ext, src_name)
+ )
if strip_dir:
- base = os.path.basename (base)
+ base = os.path.basename(base)
if ext == '.res':
# these can go unchanged
- obj_names.append (os.path.join (output_dir, base + ext))
+ obj_names.append(os.path.join(output_dir, base + ext))
elif ext == '.rc':
# these need to be compiled to .res-files
- obj_names.append (os.path.join (output_dir, base + '.res'))
+ obj_names.append(os.path.join(output_dir, base + '.res'))
else:
- obj_names.append (os.path.join (output_dir,
- base + self.obj_extension))
+ obj_names.append(os.path.join(output_dir, base + self.obj_extension))
return obj_names
# object_filenames ()
- def preprocess (self,
- source,
- output_file=None,
- macros=None,
- include_dirs=None,
- extra_preargs=None,
- extra_postargs=None):
-
- (_, macros, include_dirs) = \
- self._fix_compile_args(None, macros, include_dirs)
+ def preprocess(
+ self,
+ source,
+ output_file=None,
+ macros=None,
+ include_dirs=None,
+ extra_preargs=None,
+ extra_postargs=None,
+ ):
+
+ (_, macros, include_dirs) = self._fix_compile_args(None, macros, include_dirs)
pp_opts = gen_preprocess_options(macros, include_dirs)
pp_args = ['cpp32.exe'] + pp_opts
if output_file is not None:
diff --git a/setuptools/_distutils/ccompiler.py b/setuptools/_distutils/ccompiler.py
index 777fc661..64635311 100644
--- a/setuptools/_distutils/ccompiler.py
+++ b/setuptools/_distutils/ccompiler.py
@@ -3,14 +3,24 @@
Contains CCompiler, an abstract base class that defines the interface
for the Distutils compiler abstraction model."""
-import sys, os, re
-from distutils.errors import *
-from distutils.spawn import spawn
-from distutils.file_util import move_file
-from distutils.dir_util import mkpath
-from distutils.dep_util import newer_group
-from distutils.util import split_quoted, execute
-from distutils import log
+import sys
+import os
+import re
+
+from .errors import (
+ CompileError,
+ LinkError,
+ UnknownFileError,
+ DistutilsPlatformError,
+ DistutilsModuleError,
+)
+from .spawn import spawn
+from .file_util import move_file
+from .dir_util import mkpath
+from .dep_util import newer_group
+from .util import split_quoted, execute
+from ._log import log
+
class CCompiler:
"""Abstract base class to define the interface that must be implemented
@@ -56,17 +66,16 @@ class CCompiler:
# think this is useless without the ability to null out the
# library search path anyways.
-
# Subclasses that rely on the standard filename generation methods
# implemented below should override these; see the comment near
# those methods ('object_filenames()' et. al.) for details:
- src_extensions = None # list of strings
- obj_extension = None # string
+ src_extensions = None # list of strings
+ obj_extension = None # string
static_lib_extension = None
- shared_lib_extension = None # string
- static_lib_format = None # format string
- shared_lib_format = None # prob. same as static_lib_format
- exe_extension = None # string
+ shared_lib_extension = None # string
+ static_lib_format = None # format string
+ shared_lib_format = None # prob. same as static_lib_format
+ exe_extension = None # string
# Default language settings. language_map is used to detect a source
# file or Extension target language, checking source filenames.
@@ -74,14 +83,25 @@ class CCompiler:
# what language to use when mixing source types. For example, if some
# extension has two files with ".c" extension, and one with ".cpp", it
# is still linked as c++.
- language_map = {".c" : "c",
- ".cc" : "c++",
- ".cpp" : "c++",
- ".cxx" : "c++",
- ".m" : "objc",
- }
+ language_map = {
+ ".c": "c",
+ ".cc": "c++",
+ ".cpp": "c++",
+ ".cxx": "c++",
+ ".m": "objc",
+ }
language_order = ["c++", "objc", "c"]
+ include_dirs = []
+ """
+ include dirs specific to this compiler class
+ """
+
+ library_dirs = []
+ """
+ library dirs specific to this compiler class
+ """
+
def __init__(self, verbose=0, dry_run=0, force=0):
self.dry_run = dry_run
self.force = force
@@ -146,8 +166,10 @@ class CCompiler:
for key in kwargs:
if key not in self.executables:
- raise ValueError("unknown executable '%s' for class %s" %
- (key, self.__class__.__name__))
+ raise ValueError(
+ "unknown executable '%s' for class %s"
+ % (key, self.__class__.__name__)
+ )
self.set_executable(key, kwargs[key])
def set_executable(self, key, value):
@@ -170,14 +192,19 @@ class CCompiler:
nothing if all definitions are OK, raise TypeError otherwise.
"""
for defn in definitions:
- if not (isinstance(defn, tuple) and
- (len(defn) in (1, 2) and
- (isinstance (defn[1], str) or defn[1] is None)) and
- isinstance (defn[0], str)):
- raise TypeError(("invalid macro definition '%s': " % defn) + \
- "must be tuple (string,), (string, string), or " + \
- "(string, None)")
-
+ if not (
+ isinstance(defn, tuple)
+ and (
+ len(defn) in (1, 2)
+ and (isinstance(defn[1], str) or defn[1] is None)
+ )
+ and isinstance(defn[0], str)
+ ):
+ raise TypeError(
+ ("invalid macro definition '%s': " % defn)
+ + "must be tuple (string,), (string, string), or "
+ + "(string, None)"
+ )
# -- Bookkeeping methods -------------------------------------------
@@ -190,7 +217,7 @@ class CCompiler:
"""
# Delete from the list of macro definitions/undefinitions if
# already there (so that this one will take precedence).
- i = self._find_macro (name)
+ i = self._find_macro(name)
if i is not None:
del self.macros[i]
@@ -207,7 +234,7 @@ class CCompiler:
"""
# Delete from the list of macro definitions/undefinitions if
# already there (so that this one will take precedence).
- i = self._find_macro (name)
+ i = self._find_macro(name)
if i is not None:
del self.macros[i]
@@ -301,41 +328,20 @@ class CCompiler:
"""
self.objects = objects[:]
-
# -- Private utility methods --------------------------------------
# (here for the convenience of subclasses)
# Helper method to prep compiler in subclass compile() methods
- def _setup_compile(self, outdir, macros, incdirs, sources, depends,
- extra):
+ def _setup_compile(self, outdir, macros, incdirs, sources, depends, extra):
"""Process arguments and decide which source files to compile."""
- if outdir is None:
- outdir = self.output_dir
- elif not isinstance(outdir, str):
- raise TypeError("'output_dir' must be a string or None")
-
- if macros is None:
- macros = self.macros
- elif isinstance(macros, list):
- macros = macros + (self.macros or [])
- else:
- raise TypeError("'macros' (if supplied) must be a list of tuples")
-
- if incdirs is None:
- incdirs = self.include_dirs
- elif isinstance(incdirs, (list, tuple)):
- incdirs = list(incdirs) + (self.include_dirs or [])
- else:
- raise TypeError(
- "'include_dirs' (if supplied) must be a list of strings")
+ outdir, macros, incdirs = self._fix_compile_args(outdir, macros, incdirs)
if extra is None:
extra = []
# Get the list of expected output (object) files
- objects = self.object_filenames(sources, strip_dir=0,
- output_dir=outdir)
+ objects = self.object_filenames(sources, strip_dir=0, output_dir=outdir)
assert len(objects) == len(sources)
pp_opts = gen_preprocess_options(macros, incdirs)
@@ -386,8 +392,10 @@ class CCompiler:
elif isinstance(include_dirs, (list, tuple)):
include_dirs = list(include_dirs) + (self.include_dirs or [])
else:
- raise TypeError(
- "'include_dirs' (if supplied) must be a list of strings")
+ raise TypeError("'include_dirs' (if supplied) must be a list of strings")
+
+ # add include dirs for class
+ include_dirs += self.__class__.include_dirs
return output_dir, macros, include_dirs
@@ -434,27 +442,30 @@ class CCompiler:
if libraries is None:
libraries = self.libraries
elif isinstance(libraries, (list, tuple)):
- libraries = list (libraries) + (self.libraries or [])
+ libraries = list(libraries) + (self.libraries or [])
else:
- raise TypeError(
- "'libraries' (if supplied) must be a list of strings")
+ raise TypeError("'libraries' (if supplied) must be a list of strings")
if library_dirs is None:
library_dirs = self.library_dirs
elif isinstance(library_dirs, (list, tuple)):
- library_dirs = list (library_dirs) + (self.library_dirs or [])
+ library_dirs = list(library_dirs) + (self.library_dirs or [])
else:
- raise TypeError(
- "'library_dirs' (if supplied) must be a list of strings")
+ raise TypeError("'library_dirs' (if supplied) must be a list of strings")
+
+ # add library dirs for class
+ library_dirs += self.__class__.library_dirs
if runtime_library_dirs is None:
runtime_library_dirs = self.runtime_library_dirs
elif isinstance(runtime_library_dirs, (list, tuple)):
- runtime_library_dirs = (list(runtime_library_dirs) +
- (self.runtime_library_dirs or []))
+ runtime_library_dirs = list(runtime_library_dirs) + (
+ self.runtime_library_dirs or []
+ )
else:
- raise TypeError("'runtime_library_dirs' (if supplied) "
- "must be a list of strings")
+ raise TypeError(
+ "'runtime_library_dirs' (if supplied) " "must be a list of strings"
+ )
return (libraries, library_dirs, runtime_library_dirs)
@@ -466,9 +477,9 @@ class CCompiler:
return True
else:
if self.dry_run:
- newer = newer_group (objects, output_file, missing='newer')
+ newer = newer_group(objects, output_file, missing='newer')
else:
- newer = newer_group (objects, output_file)
+ newer = newer_group(objects, output_file)
return newer
def detect_language(self, sources):
@@ -491,12 +502,18 @@ class CCompiler:
pass
return lang
-
# -- Worker methods ------------------------------------------------
# (must be implemented by subclasses)
- def preprocess(self, source, output_file=None, macros=None,
- include_dirs=None, extra_preargs=None, extra_postargs=None):
+ def preprocess(
+ self,
+ source,
+ output_file=None,
+ macros=None,
+ include_dirs=None,
+ extra_preargs=None,
+ extra_postargs=None,
+ ):
"""Preprocess a single C/C++ source file, named in 'source'.
Output will be written to file named 'output_file', or stdout if
'output_file' not supplied. 'macros' is a list of macro
@@ -508,9 +525,17 @@ class CCompiler:
"""
pass
- def compile(self, sources, output_dir=None, macros=None,
- include_dirs=None, debug=0, extra_preargs=None,
- extra_postargs=None, depends=None):
+ def compile(
+ self,
+ sources,
+ output_dir=None,
+ macros=None,
+ include_dirs=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ depends=None,
+ ):
"""Compile one or more source files.
'sources' must be a list of filenames, most likely C/C++
@@ -561,9 +586,9 @@ class CCompiler:
"""
# A concrete compiler class can either override this method
# entirely or implement _compile().
- macros, objects, extra_postargs, pp_opts, build = \
- self._setup_compile(output_dir, macros, include_dirs, sources,
- depends, extra_postargs)
+ macros, objects, extra_postargs, pp_opts, build = self._setup_compile(
+ output_dir, macros, include_dirs, sources, depends, extra_postargs
+ )
cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
for obj in objects:
@@ -582,8 +607,9 @@ class CCompiler:
# should implement _compile().
pass
- def create_static_lib(self, objects, output_libname, output_dir=None,
- debug=0, target_lang=None):
+ def create_static_lib(
+ self, objects, output_libname, output_dir=None, debug=0, target_lang=None
+ ):
"""Link a bunch of stuff together to create a static library file.
The "bunch of stuff" consists of the list of object files supplied
as 'objects', the extra object files supplied to
@@ -608,26 +634,27 @@ class CCompiler:
"""
pass
-
# values for target_desc parameter in link()
SHARED_OBJECT = "shared_object"
SHARED_LIBRARY = "shared_library"
EXECUTABLE = "executable"
- def link(self,
- target_desc,
- objects,
- output_filename,
- output_dir=None,
- libraries=None,
- library_dirs=None,
- runtime_library_dirs=None,
- export_symbols=None,
- debug=0,
- extra_preargs=None,
- extra_postargs=None,
- build_temp=None,
- target_lang=None):
+ def link(
+ self,
+ target_desc,
+ objects,
+ output_filename,
+ output_dir=None,
+ libraries=None,
+ library_dirs=None,
+ runtime_library_dirs=None,
+ export_symbols=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ build_temp=None,
+ target_lang=None,
+ ):
"""Link a bunch of stuff together to create an executable or
shared library file.
@@ -673,66 +700,98 @@ class CCompiler:
"""
raise NotImplementedError
-
# Old 'link_*()' methods, rewritten to use the new 'link()' method.
- def link_shared_lib(self,
- objects,
- output_libname,
- output_dir=None,
- libraries=None,
- library_dirs=None,
- runtime_library_dirs=None,
- export_symbols=None,
- debug=0,
- extra_preargs=None,
- extra_postargs=None,
- build_temp=None,
- target_lang=None):
- self.link(CCompiler.SHARED_LIBRARY, objects,
- self.library_filename(output_libname, lib_type='shared'),
- output_dir,
- libraries, library_dirs, runtime_library_dirs,
- export_symbols, debug,
- extra_preargs, extra_postargs, build_temp, target_lang)
-
-
- def link_shared_object(self,
- objects,
- output_filename,
- output_dir=None,
- libraries=None,
- library_dirs=None,
- runtime_library_dirs=None,
- export_symbols=None,
- debug=0,
- extra_preargs=None,
- extra_postargs=None,
- build_temp=None,
- target_lang=None):
- self.link(CCompiler.SHARED_OBJECT, objects,
- output_filename, output_dir,
- libraries, library_dirs, runtime_library_dirs,
- export_symbols, debug,
- extra_preargs, extra_postargs, build_temp, target_lang)
-
-
- def link_executable(self,
- objects,
- output_progname,
- output_dir=None,
- libraries=None,
- library_dirs=None,
- runtime_library_dirs=None,
- debug=0,
- extra_preargs=None,
- extra_postargs=None,
- target_lang=None):
- self.link(CCompiler.EXECUTABLE, objects,
- self.executable_filename(output_progname), output_dir,
- libraries, library_dirs, runtime_library_dirs, None,
- debug, extra_preargs, extra_postargs, None, target_lang)
-
+ def link_shared_lib(
+ self,
+ objects,
+ output_libname,
+ output_dir=None,
+ libraries=None,
+ library_dirs=None,
+ runtime_library_dirs=None,
+ export_symbols=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ build_temp=None,
+ target_lang=None,
+ ):
+ self.link(
+ CCompiler.SHARED_LIBRARY,
+ objects,
+ self.library_filename(output_libname, lib_type='shared'),
+ output_dir,
+ libraries,
+ library_dirs,
+ runtime_library_dirs,
+ export_symbols,
+ debug,
+ extra_preargs,
+ extra_postargs,
+ build_temp,
+ target_lang,
+ )
+
+ def link_shared_object(
+ self,
+ objects,
+ output_filename,
+ output_dir=None,
+ libraries=None,
+ library_dirs=None,
+ runtime_library_dirs=None,
+ export_symbols=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ build_temp=None,
+ target_lang=None,
+ ):
+ self.link(
+ CCompiler.SHARED_OBJECT,
+ objects,
+ output_filename,
+ output_dir,
+ libraries,
+ library_dirs,
+ runtime_library_dirs,
+ export_symbols,
+ debug,
+ extra_preargs,
+ extra_postargs,
+ build_temp,
+ target_lang,
+ )
+
+ def link_executable(
+ self,
+ objects,
+ output_progname,
+ output_dir=None,
+ libraries=None,
+ library_dirs=None,
+ runtime_library_dirs=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ target_lang=None,
+ ):
+ self.link(
+ CCompiler.EXECUTABLE,
+ objects,
+ self.executable_filename(output_progname),
+ output_dir,
+ libraries,
+ library_dirs,
+ runtime_library_dirs,
+ None,
+ debug,
+ extra_preargs,
+ extra_postargs,
+ None,
+ target_lang,
+ )
# -- Miscellaneous methods -----------------------------------------
# These are all used by the 'gen_lib_options() function; there is
@@ -757,8 +816,14 @@ class CCompiler:
"""
raise NotImplementedError
- def has_function(self, funcname, includes=None, include_dirs=None,
- libraries=None, library_dirs=None):
+ def has_function( # noqa: C901
+ self,
+ funcname,
+ includes=None,
+ include_dirs=None,
+ libraries=None,
+ library_dirs=None,
+ ):
"""Return a boolean indicating whether funcname is supported on
the current platform. The optional arguments can be used to
augment the compilation environment.
@@ -767,6 +832,7 @@ class CCompiler:
# import math which might not be available at that point - maybe
# the necessary logic should just be inlined?
import tempfile
+
if includes is None:
includes = []
if include_dirs is None:
@@ -780,12 +846,15 @@ class CCompiler:
try:
for incl in includes:
f.write("""#include "%s"\n""" % incl)
- f.write("""\
+ f.write(
+ """\
int main (int argc, char **argv) {
%s();
return 0;
}
-""" % funcname)
+"""
+ % funcname
+ )
finally:
f.close()
try:
@@ -796,9 +865,9 @@ int main (int argc, char **argv) {
os.remove(fname)
try:
- self.link_executable(objects, "a.out",
- libraries=libraries,
- library_dirs=library_dirs)
+ self.link_executable(
+ objects, "a.out", libraries=libraries, library_dirs=library_dirs
+ )
except (LinkError, TypeError):
return False
else:
@@ -808,7 +877,7 @@ int main (int argc, char **argv) {
os.remove(fn)
return True
- def find_library_file (self, dirs, lib, debug=0):
+ def find_library_file(self, dirs, lib, debug=0):
"""Search the specified list of directories for a static or shared
library file 'lib' and return the full path to that file. If
'debug' true, look for a debugging version (if that makes sense on
@@ -854,19 +923,39 @@ int main (int argc, char **argv) {
def object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
if output_dir is None:
output_dir = ''
- obj_names = []
- for src_name in source_filenames:
- base, ext = os.path.splitext(src_name)
- base = os.path.splitdrive(base)[1] # Chop off the drive
- base = base[os.path.isabs(base):] # If abs, chop off leading /
- if ext not in self.src_extensions:
- raise UnknownFileError(
- "unknown file type '%s' (from '%s')" % (ext, src_name))
- if strip_dir:
- base = os.path.basename(base)
- obj_names.append(os.path.join(output_dir,
- base + self.obj_extension))
- return obj_names
+ return list(
+ self._make_out_path(output_dir, strip_dir, src_name)
+ for src_name in source_filenames
+ )
+
+ @property
+ def out_extensions(self):
+ return dict.fromkeys(self.src_extensions, self.obj_extension)
+
+ def _make_out_path(self, output_dir, strip_dir, src_name):
+ base, ext = os.path.splitext(src_name)
+ base = self._make_relative(base)
+ try:
+ new_ext = self.out_extensions[ext]
+ except LookupError:
+ raise UnknownFileError(
+ "unknown file type '{}' (from '{}')".format(ext, src_name)
+ )
+ if strip_dir:
+ base = os.path.basename(base)
+ return os.path.join(output_dir, base + new_ext)
+
+ @staticmethod
+ def _make_relative(base):
+ """
+ In order to ensure that a filename always honors the
+ indicated output_dir, make sure it's relative.
+ Ref python/cpython#37775.
+ """
+ # Chop off the drive
+ no_drive = os.path.splitdrive(base)[1]
+ # If abs, chop off leading /
+ return no_drive[os.path.isabs(no_drive) :]
def shared_object_filename(self, basename, strip_dir=0, output_dir=''):
assert output_dir is not None
@@ -880,12 +969,13 @@ int main (int argc, char **argv) {
basename = os.path.basename(basename)
return os.path.join(output_dir, basename + (self.exe_extension or ''))
- def library_filename(self, libname, lib_type='static', # or 'shared'
- strip_dir=0, output_dir=''):
+ def library_filename(
+ self, libname, lib_type='static', strip_dir=0, output_dir='' # or 'shared'
+ ):
assert output_dir is not None
- if lib_type not in ("static", "shared", "dylib", "xcode_stub"):
- raise ValueError(
- "'lib_type' must be \"static\", \"shared\", \"dylib\", or \"xcode_stub\"")
+ expected = '"static", "shared", "dylib", "xcode_stub"'
+ if lib_type not in eval(expected):
+ raise ValueError(f"'lib_type' must be {expected}")
fmt = getattr(self, lib_type + "_lib_format")
ext = getattr(self, lib_type + "_lib_extension")
@@ -896,7 +986,6 @@ int main (int argc, char **argv) {
return os.path.join(output_dir, dir, filename)
-
# -- Utility methods -----------------------------------------------
def announce(self, msg, level=1):
@@ -904,6 +993,7 @@ int main (int argc, char **argv) {
def debug_print(self, msg):
from distutils.debug import DEBUG
+
if DEBUG:
print(msg)
@@ -919,7 +1009,7 @@ int main (int argc, char **argv) {
def move_file(self, src, dst):
return move_file(src, dst, dry_run=self.dry_run)
- def mkpath (self, name, mode=0o777):
+ def mkpath(self, name, mode=0o777):
mkpath(name, mode, dry_run=self.dry_run)
@@ -928,54 +1018,59 @@ int main (int argc, char **argv) {
# patterns. Order is important; platform mappings are preferred over
# OS names.
_default_compilers = (
-
# Platform string mappings
-
# on a cygwin built python we can use gcc like an ordinary UNIXish
# compiler
('cygwin.*', 'unix'),
-
# OS name mappings
('posix', 'unix'),
('nt', 'msvc'),
+)
- )
def get_default_compiler(osname=None, platform=None):
"""Determine the default compiler to use for the given platform.
- osname should be one of the standard Python OS names (i.e. the
- ones returned by os.name) and platform the common value
- returned by sys.platform for the platform in question.
+ osname should be one of the standard Python OS names (i.e. the
+ ones returned by os.name) and platform the common value
+ returned by sys.platform for the platform in question.
- The default values are os.name and sys.platform in case the
- parameters are not given.
+ The default values are os.name and sys.platform in case the
+ parameters are not given.
"""
if osname is None:
osname = os.name
if platform is None:
platform = sys.platform
for pattern, compiler in _default_compilers:
- if re.match(pattern, platform) is not None or \
- re.match(pattern, osname) is not None:
+ if (
+ re.match(pattern, platform) is not None
+ or re.match(pattern, osname) is not None
+ ):
return compiler
# Default to Unix compiler
return 'unix'
+
# Map compiler types to (module_name, class_name) pairs -- ie. where to
# find the code that implements an interface to this compiler. (The module
# is assumed to be in the 'distutils' package.)
-compiler_class = { 'unix': ('unixccompiler', 'UnixCCompiler',
- "standard UNIX-style compiler"),
- 'msvc': ('_msvccompiler', 'MSVCCompiler',
- "Microsoft Visual C++"),
- 'cygwin': ('cygwinccompiler', 'CygwinCCompiler',
- "Cygwin port of GNU C Compiler for Win32"),
- 'mingw32': ('cygwinccompiler', 'Mingw32CCompiler',
- "Mingw32 port of GNU C Compiler for Win32"),
- 'bcpp': ('bcppcompiler', 'BCPPCompiler',
- "Borland C++ Compiler"),
- }
+compiler_class = {
+ 'unix': ('unixccompiler', 'UnixCCompiler', "standard UNIX-style compiler"),
+ 'msvc': ('_msvccompiler', 'MSVCCompiler', "Microsoft Visual C++"),
+ 'cygwin': (
+ 'cygwinccompiler',
+ 'CygwinCCompiler',
+ "Cygwin port of GNU C Compiler for Win32",
+ ),
+ 'mingw32': (
+ 'cygwinccompiler',
+ 'Mingw32CCompiler',
+ "Mingw32 port of GNU C Compiler for Win32",
+ ),
+ 'bcpp': ('bcppcompiler', 'BCPPCompiler', "Borland C++ Compiler"),
+}
+
def show_compilers():
"""Print list of available compilers (used by the "--help-compiler"
@@ -985,10 +1080,10 @@ def show_compilers():
# "--compiler", which just happens to be the case for the three
# commands that use it.
from distutils.fancy_getopt import FancyGetopt
+
compilers = []
for compiler in compiler_class.keys():
- compilers.append(("compiler="+compiler, None,
- compiler_class[compiler][2]))
+ compilers.append(("compiler=" + compiler, None, compiler_class[compiler][2]))
compilers.sort()
pretty_printer = FancyGetopt(compilers)
pretty_printer.print_help("List of available compilers:")
@@ -1021,17 +1116,18 @@ def new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0):
try:
module_name = "distutils." + module_name
- __import__ (module_name)
+ __import__(module_name)
module = sys.modules[module_name]
klass = vars(module)[class_name]
except ImportError:
raise DistutilsModuleError(
- "can't compile C/C++ code: unable to load module '%s'" % \
- module_name)
+ "can't compile C/C++ code: unable to load module '%s'" % module_name
+ )
except KeyError:
raise DistutilsModuleError(
- "can't compile C/C++ code: unable to find class '%s' "
- "in module '%s'" % (class_name, module_name))
+ "can't compile C/C++ code: unable to find class '%s' "
+ "in module '%s'" % (class_name, module_name)
+ )
# XXX The None is necessary to preserve backwards compatibility
# with classes that expect verbose to be the first positional
@@ -1064,14 +1160,14 @@ def gen_preprocess_options(macros, include_dirs):
for macro in macros:
if not (isinstance(macro, tuple) and 1 <= len(macro) <= 2):
raise TypeError(
- "bad macro definition '%s': "
- "each element of 'macros' list must be a 1- or 2-tuple"
- % macro)
+ "bad macro definition '%s': "
+ "each element of 'macros' list must be a 1- or 2-tuple" % macro
+ )
- if len(macro) == 1: # undefine this macro
+ if len(macro) == 1: # undefine this macro
pp_opts.append("-U%s" % macro[0])
elif len(macro) == 2:
- if macro[1] is None: # define with no explicit value
+ if macro[1] is None: # define with no explicit value
pp_opts.append("-D%s" % macro[0])
else:
# XXX *don't* need to be clever about quoting the
@@ -1084,7 +1180,7 @@ def gen_preprocess_options(macros, include_dirs):
return pp_opts
-def gen_lib_options (compiler, library_dirs, runtime_library_dirs, libraries):
+def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
"""Generate linker options for searching library directories and
linking with specific libraries. 'libraries' and 'library_dirs' are,
respectively, lists of library names (not filenames!) and search
@@ -1116,8 +1212,9 @@ def gen_lib_options (compiler, library_dirs, runtime_library_dirs, libraries):
if lib_file:
lib_opts.append(lib_file)
else:
- compiler.warn("no library file corresponding to "
- "'%s' found (skipping)" % lib)
+ compiler.warn(
+ "no library file corresponding to " "'%s' found (skipping)" % lib
+ )
else:
- lib_opts.append(compiler.library_option (lib))
+ lib_opts.append(compiler.library_option(lib))
return lib_opts
diff --git a/setuptools/_distutils/cmd.py b/setuptools/_distutils/cmd.py
index dba3191e..918db853 100644
--- a/setuptools/_distutils/cmd.py
+++ b/setuptools/_distutils/cmd.py
@@ -4,10 +4,15 @@ Provides the Command class, the base class for the command classes
in the distutils.command package.
"""
-import sys, os, re
-from distutils.errors import DistutilsOptionError
-from distutils import util, dir_util, file_util, archive_util, dep_util
-from distutils import log
+import sys
+import os
+import re
+import logging
+
+from .errors import DistutilsOptionError
+from . import util, dir_util, file_util, archive_util, dep_util
+from ._log import log
+
class Command:
"""Abstract base class for defining command classes, the "worker bees"
@@ -41,7 +46,6 @@ class Command:
# defined. The canonical example is the "install" command.
sub_commands = []
-
# -- Creation/initialization methods -------------------------------
def __init__(self, dist):
@@ -130,8 +134,9 @@ class Command:
This method must be implemented by all command classes.
"""
- raise RuntimeError("abstract method -- subclass %s must override"
- % self.__class__)
+ raise RuntimeError(
+ "abstract method -- subclass %s must override" % self.__class__
+ )
def finalize_options(self):
"""Set final values for all the options that this command supports.
@@ -144,23 +149,23 @@ class Command:
This method must be implemented by all command classes.
"""
- raise RuntimeError("abstract method -- subclass %s must override"
- % self.__class__)
-
+ raise RuntimeError(
+ "abstract method -- subclass %s must override" % self.__class__
+ )
def dump_options(self, header=None, indent=""):
from distutils.fancy_getopt import longopt_xlate
+
if header is None:
header = "command options for '%s':" % self.get_command_name()
- self.announce(indent + header, level=log.INFO)
+ self.announce(indent + header, level=logging.INFO)
indent = indent + " "
for (option, _, _) in self.user_options:
option = option.translate(longopt_xlate)
if option[-1] == "=":
option = option[:-1]
value = getattr(self, option)
- self.announce(indent + "%s = %s" % (option, value),
- level=log.INFO)
+ self.announce(indent + "{} = {}".format(option, value), level=logging.INFO)
def run(self):
"""A command's raison d'etre: carry out the action it exists to
@@ -172,13 +177,11 @@ class Command:
This method must be implemented by all command classes.
"""
- raise RuntimeError("abstract method -- subclass %s must override"
- % self.__class__)
+ raise RuntimeError(
+ "abstract method -- subclass %s must override" % self.__class__
+ )
- def announce(self, msg, level=1):
- """If the current verbosity level is of greater than or equal to
- 'level' print 'msg' to stdout.
- """
+ def announce(self, msg, level=logging.DEBUG):
log.log(level, msg)
def debug_print(self, msg):
@@ -186,11 +189,11 @@ class Command:
DISTUTILS_DEBUG environment variable) flag is true.
"""
from distutils.debug import DEBUG
+
if DEBUG:
print(msg)
sys.stdout.flush()
-
# -- Option validation methods -------------------------------------
# (these are very handy in writing the 'finalize_options()' method)
#
@@ -210,8 +213,9 @@ class Command:
setattr(self, option, default)
return default
elif not isinstance(val, str):
- raise DistutilsOptionError("'%s' must be a %s (got `%s`)"
- % (option, what, val))
+ raise DistutilsOptionError(
+ "'{}' must be a {} (got `{}`)".format(option, what, val)
+ )
return val
def ensure_string(self, option, default=None):
@@ -238,27 +242,29 @@ class Command:
ok = False
if not ok:
raise DistutilsOptionError(
- "'%s' must be a list of strings (got %r)"
- % (option, val))
+ "'{}' must be a list of strings (got {!r})".format(option, val)
+ )
- def _ensure_tested_string(self, option, tester, what, error_fmt,
- default=None):
+ def _ensure_tested_string(self, option, tester, what, error_fmt, default=None):
val = self._ensure_stringlike(option, what, default)
if val is not None and not tester(val):
- raise DistutilsOptionError(("error in '%s' option: " + error_fmt)
- % (option, val))
+ raise DistutilsOptionError(
+ ("error in '%s' option: " + error_fmt) % (option, val)
+ )
def ensure_filename(self, option):
"""Ensure that 'option' is the name of an existing file."""
- self._ensure_tested_string(option, os.path.isfile,
- "filename",
- "'%s' does not exist or is not a file")
+ self._ensure_tested_string(
+ option, os.path.isfile, "filename", "'%s' does not exist or is not a file"
+ )
def ensure_dirname(self, option):
- self._ensure_tested_string(option, os.path.isdir,
- "directory name",
- "'%s' does not exist or is not a directory")
-
+ self._ensure_tested_string(
+ option,
+ os.path.isdir,
+ "directory name",
+ "'%s' does not exist or is not a directory",
+ )
# -- Convenience methods for commands ------------------------------
@@ -302,8 +308,7 @@ class Command:
# XXX rename to 'get_reinitialized_command()'? (should do the
# same in dist.py, if so)
def reinitialize_command(self, command, reinit_subcommands=0):
- return self.distribution.reinitialize_command(command,
- reinit_subcommands)
+ return self.distribution.reinitialize_command(command, reinit_subcommands)
def run_command(self, command):
"""Run some other command: uses the 'run_command()' method of
@@ -325,11 +330,10 @@ class Command:
commands.append(cmd_name)
return commands
-
# -- External world manipulation -----------------------------------
def warn(self, msg):
- log.warn("warning: %s: %s\n", self.get_command_name(), msg)
+ log.warning("warning: %s: %s\n", self.get_command_name(), msg)
def execute(self, func, args, msg=None, level=1):
util.execute(func, args, msg, dry_run=self.dry_run)
@@ -337,41 +341,70 @@ class Command:
def mkpath(self, name, mode=0o777):
dir_util.mkpath(name, mode, dry_run=self.dry_run)
- def copy_file(self, infile, outfile, preserve_mode=1, preserve_times=1,
- link=None, level=1):
+ def copy_file(
+ self, infile, outfile, preserve_mode=1, preserve_times=1, link=None, level=1
+ ):
"""Copy a file respecting verbose, dry-run and force flags. (The
former two default to whatever is in the Distribution object, and
the latter defaults to false for commands that don't define it.)"""
- return file_util.copy_file(infile, outfile, preserve_mode,
- preserve_times, not self.force, link,
- dry_run=self.dry_run)
-
- def copy_tree(self, infile, outfile, preserve_mode=1, preserve_times=1,
- preserve_symlinks=0, level=1):
+ return file_util.copy_file(
+ infile,
+ outfile,
+ preserve_mode,
+ preserve_times,
+ not self.force,
+ link,
+ dry_run=self.dry_run,
+ )
+
+ def copy_tree(
+ self,
+ infile,
+ outfile,
+ preserve_mode=1,
+ preserve_times=1,
+ preserve_symlinks=0,
+ level=1,
+ ):
"""Copy an entire directory tree respecting verbose, dry-run,
and force flags.
"""
- return dir_util.copy_tree(infile, outfile, preserve_mode,
- preserve_times, preserve_symlinks,
- not self.force, dry_run=self.dry_run)
-
- def move_file (self, src, dst, level=1):
+ return dir_util.copy_tree(
+ infile,
+ outfile,
+ preserve_mode,
+ preserve_times,
+ preserve_symlinks,
+ not self.force,
+ dry_run=self.dry_run,
+ )
+
+ def move_file(self, src, dst, level=1):
"""Move a file respecting dry-run flag."""
return file_util.move_file(src, dst, dry_run=self.dry_run)
def spawn(self, cmd, search_path=1, level=1):
"""Spawn an external command respecting dry-run flag."""
from distutils.spawn import spawn
- spawn(cmd, search_path, dry_run=self.dry_run)
- def make_archive(self, base_name, format, root_dir=None, base_dir=None,
- owner=None, group=None):
- return archive_util.make_archive(base_name, format, root_dir, base_dir,
- dry_run=self.dry_run,
- owner=owner, group=group)
+ spawn(cmd, search_path, dry_run=self.dry_run)
- def make_file(self, infiles, outfile, func, args,
- exec_msg=None, skip_msg=None, level=1):
+ def make_archive(
+ self, base_name, format, root_dir=None, base_dir=None, owner=None, group=None
+ ):
+ return archive_util.make_archive(
+ base_name,
+ format,
+ root_dir,
+ base_dir,
+ dry_run=self.dry_run,
+ owner=owner,
+ group=group,
+ )
+
+ def make_file(
+ self, infiles, outfile, func, args, exec_msg=None, skip_msg=None, level=1
+ ):
"""Special case of 'execute()' for operations that process one or
more input files and generate one output file. Works just like
'execute()', except the operation is skipped and a different
@@ -387,11 +420,10 @@ class Command:
if isinstance(infiles, str):
infiles = (infiles,)
elif not isinstance(infiles, (list, tuple)):
- raise TypeError(
- "'infiles' must be a string, or a list or tuple of strings")
+ raise TypeError("'infiles' must be a string, or a list or tuple of strings")
if exec_msg is None:
- exec_msg = "generating %s from %s" % (outfile, ', '.join(infiles))
+ exec_msg = "generating {} from {}".format(outfile, ', '.join(infiles))
# If 'outfile' must be regenerated (either because it doesn't
# exist, is out-of-date, or the 'force' flag is true) then
diff --git a/setuptools/_distutils/command/__init__.py b/setuptools/_distutils/command/__init__.py
index 481eea9f..028dcfa0 100644
--- a/setuptools/_distutils/command/__init__.py
+++ b/setuptools/_distutils/command/__init__.py
@@ -3,29 +3,23 @@
Package containing implementation of all the standard Distutils
commands."""
-__all__ = ['build',
- 'build_py',
- 'build_ext',
- 'build_clib',
- 'build_scripts',
- 'clean',
- 'install',
- 'install_lib',
- 'install_headers',
- 'install_scripts',
- 'install_data',
- 'sdist',
- 'register',
- 'bdist',
- 'bdist_dumb',
- 'bdist_rpm',
- 'bdist_wininst',
- 'check',
- 'upload',
- # These two are reserved for future use:
- #'bdist_sdux',
- #'bdist_pkgtool',
- # Note:
- # bdist_packager is not included because it only provides
- # an abstract base class
- ]
+__all__ = [ # noqa: F822
+ 'build',
+ 'build_py',
+ 'build_ext',
+ 'build_clib',
+ 'build_scripts',
+ 'clean',
+ 'install',
+ 'install_lib',
+ 'install_headers',
+ 'install_scripts',
+ 'install_data',
+ 'sdist',
+ 'register',
+ 'bdist',
+ 'bdist_dumb',
+ 'bdist_rpm',
+ 'check',
+ 'upload',
+]
diff --git a/setuptools/_distutils/command/_framework_compat.py b/setuptools/_distutils/command/_framework_compat.py
new file mode 100644
index 00000000..cffa27cb
--- /dev/null
+++ b/setuptools/_distutils/command/_framework_compat.py
@@ -0,0 +1,55 @@
+"""
+Backward compatibility for homebrew builds on macOS.
+"""
+
+
+import sys
+import os
+import functools
+import subprocess
+import sysconfig
+
+
+@functools.lru_cache()
+def enabled():
+ """
+ Only enabled for Python 3.9 framework homebrew builds
+ except ensurepip and venv.
+ """
+ PY39 = (3, 9) < sys.version_info < (3, 10)
+ framework = sys.platform == 'darwin' and sys._framework
+ homebrew = "Cellar" in sysconfig.get_config_var('projectbase')
+ venv = sys.prefix != sys.base_prefix
+ ensurepip = os.environ.get("ENSUREPIP_OPTIONS")
+ return PY39 and framework and homebrew and not venv and not ensurepip
+
+
+schemes = dict(
+ osx_framework_library=dict(
+ stdlib='{installed_base}/{platlibdir}/python{py_version_short}',
+ platstdlib='{platbase}/{platlibdir}/python{py_version_short}',
+ purelib='{homebrew_prefix}/lib/python{py_version_short}/site-packages',
+ platlib='{homebrew_prefix}/{platlibdir}/python{py_version_short}/site-packages',
+ include='{installed_base}/include/python{py_version_short}{abiflags}',
+ platinclude='{installed_platbase}/include/python{py_version_short}{abiflags}',
+ scripts='{homebrew_prefix}/bin',
+ data='{homebrew_prefix}',
+ )
+)
+
+
+@functools.lru_cache()
+def vars():
+ if not enabled():
+ return {}
+ homebrew_prefix = subprocess.check_output(['brew', '--prefix'], text=True).strip()
+ return locals()
+
+
+def scheme(name):
+ """
+ Override the selected scheme for posix_prefix.
+ """
+ if not enabled() or not name.endswith('_prefix'):
+ return name
+ return 'osx_framework_library'
diff --git a/setuptools/_distutils/command/bdist.py b/setuptools/_distutils/command/bdist.py
index 014871d2..bf0baab0 100644
--- a/setuptools/_distutils/command/bdist.py
+++ b/setuptools/_distutils/command/bdist.py
@@ -4,79 +4,93 @@ Implements the Distutils 'bdist' command (create a built [binary]
distribution)."""
import os
-from distutils.core import Command
-from distutils.errors import *
-from distutils.util import get_platform
+import warnings
+
+from ..core import Command
+from ..errors import DistutilsPlatformError, DistutilsOptionError
+from ..util import get_platform
def show_formats():
- """Print list of available formats (arguments to "--format" option).
- """
- from distutils.fancy_getopt import FancyGetopt
+ """Print list of available formats (arguments to "--format" option)."""
+ from ..fancy_getopt import FancyGetopt
+
formats = []
for format in bdist.format_commands:
- formats.append(("formats=" + format, None,
- bdist.format_command[format][1]))
+ formats.append(("formats=" + format, None, bdist.format_commands[format][1]))
pretty_printer = FancyGetopt(formats)
pretty_printer.print_help("List of available distribution formats:")
+class ListCompat(dict):
+ # adapter to allow for Setuptools compatibility in format_commands
+ def append(self, item):
+ warnings.warn(
+ """format_commands is now a dict. append is deprecated.""",
+ DeprecationWarning,
+ stacklevel=2,
+ )
+
+
class bdist(Command):
description = "create a built (binary) distribution"
- user_options = [('bdist-base=', 'b',
- "temporary directory for creating built distributions"),
- ('plat-name=', 'p',
- "platform name to embed in generated filenames "
- "(default: %s)" % get_platform()),
- ('formats=', None,
- "formats for distribution (comma-separated list)"),
- ('dist-dir=', 'd',
- "directory to put final built distributions in "
- "[default: dist]"),
- ('skip-build', None,
- "skip rebuilding everything (for testing/debugging)"),
- ('owner=', 'u',
- "Owner name used when creating a tar file"
- " [default: current user]"),
- ('group=', 'g',
- "Group name used when creating a tar file"
- " [default: current group]"),
- ]
+ user_options = [
+ ('bdist-base=', 'b', "temporary directory for creating built distributions"),
+ (
+ 'plat-name=',
+ 'p',
+ "platform name to embed in generated filenames "
+ "(default: %s)" % get_platform(),
+ ),
+ ('formats=', None, "formats for distribution (comma-separated list)"),
+ (
+ 'dist-dir=',
+ 'd',
+ "directory to put final built distributions in " "[default: dist]",
+ ),
+ ('skip-build', None, "skip rebuilding everything (for testing/debugging)"),
+ (
+ 'owner=',
+ 'u',
+ "Owner name used when creating a tar file" " [default: current user]",
+ ),
+ (
+ 'group=',
+ 'g',
+ "Group name used when creating a tar file" " [default: current group]",
+ ),
+ ]
boolean_options = ['skip-build']
help_options = [
- ('help-formats', None,
- "lists available distribution formats", show_formats),
- ]
+ ('help-formats', None, "lists available distribution formats", show_formats),
+ ]
# The following commands do not take a format option from bdist
no_format_option = ('bdist_rpm',)
# This won't do in reality: will need to distinguish RPM-ish Linux,
# Debian-ish Linux, Solaris, FreeBSD, ..., Windows, Mac OS.
- default_format = {'posix': 'gztar',
- 'nt': 'zip'}
-
- # Establish the preferred order (for the --help-formats option).
- format_commands = ['rpm', 'gztar', 'bztar', 'xztar', 'ztar', 'tar',
- 'wininst', 'zip', 'msi']
-
- # And the real information.
- format_command = {'rpm': ('bdist_rpm', "RPM distribution"),
- 'gztar': ('bdist_dumb', "gzip'ed tar file"),
- 'bztar': ('bdist_dumb', "bzip2'ed tar file"),
- 'xztar': ('bdist_dumb', "xz'ed tar file"),
- 'ztar': ('bdist_dumb', "compressed tar file"),
- 'tar': ('bdist_dumb', "tar file"),
- 'wininst': ('bdist_wininst',
- "Windows executable installer"),
- 'zip': ('bdist_dumb', "ZIP file"),
- 'msi': ('bdist_msi', "Microsoft Installer")
- }
-
+ default_format = {'posix': 'gztar', 'nt': 'zip'}
+
+ # Define commands in preferred order for the --help-formats option
+ format_commands = ListCompat(
+ {
+ 'rpm': ('bdist_rpm', "RPM distribution"),
+ 'gztar': ('bdist_dumb', "gzip'ed tar file"),
+ 'bztar': ('bdist_dumb', "bzip2'ed tar file"),
+ 'xztar': ('bdist_dumb', "xz'ed tar file"),
+ 'ztar': ('bdist_dumb', "compressed tar file"),
+ 'tar': ('bdist_dumb', "tar file"),
+ 'zip': ('bdist_dumb', "ZIP file"),
+ }
+ )
+
+ # for compatibility until consumers only reference format_commands
+ format_command = format_commands
def initialize_options(self):
self.bdist_base = None
@@ -100,8 +114,7 @@ class bdist(Command):
# "build/bdist.<plat>/dumb", "build/bdist.<plat>/rpm", etc.)
if self.bdist_base is None:
build_base = self.get_finalized_command('build').build_base
- self.bdist_base = os.path.join(build_base,
- 'bdist.' + self.plat_name)
+ self.bdist_base = os.path.join(build_base, 'bdist.' + self.plat_name)
self.ensure_string_list('formats')
if self.formats is None:
@@ -109,8 +122,9 @@ class bdist(Command):
self.formats = [self.default_format[os.name]]
except KeyError:
raise DistutilsPlatformError(
- "don't know how to create built distributions "
- "on platform %s" % os.name)
+ "don't know how to create built distributions "
+ "on platform %s" % os.name
+ )
if self.dist_dir is None:
self.dist_dir = "dist"
@@ -120,7 +134,7 @@ class bdist(Command):
commands = []
for format in self.formats:
try:
- commands.append(self.format_command[format][0])
+ commands.append(self.format_commands[format][0])
except KeyError:
raise DistutilsOptionError("invalid format '%s'" % format)
@@ -138,6 +152,6 @@ class bdist(Command):
# If we're going to need to run this command again, tell it to
# keep its temporary files around so subsequent runs go faster.
- if cmd_name in commands[i+1:]:
+ if cmd_name in commands[i + 1 :]:
sub_cmd.keep_temp = 1
self.run_command(cmd_name)
diff --git a/setuptools/_distutils/command/bdist_dumb.py b/setuptools/_distutils/command/bdist_dumb.py
index f0d6b5b8..071da77e 100644
--- a/setuptools/_distutils/command/bdist_dumb.py
+++ b/setuptools/_distutils/command/bdist_dumb.py
@@ -5,47 +5,59 @@ distribution -- i.e., just an archive to be unpacked under $prefix or
$exec_prefix)."""
import os
-from distutils.core import Command
-from distutils.util import get_platform
-from distutils.dir_util import remove_tree, ensure_relative
-from distutils.errors import *
-from distutils.sysconfig import get_python_version
-from distutils import log
+from ..core import Command
+from ..util import get_platform
+from ..dir_util import remove_tree, ensure_relative
+from ..errors import DistutilsPlatformError
+from ..sysconfig import get_python_version
+from distutils._log import log
+
class bdist_dumb(Command):
description = "create a \"dumb\" built distribution"
- user_options = [('bdist-dir=', 'd',
- "temporary directory for creating the distribution"),
- ('plat-name=', 'p',
- "platform name to embed in generated filenames "
- "(default: %s)" % get_platform()),
- ('format=', 'f',
- "archive format to create (tar, gztar, bztar, xztar, "
- "ztar, zip)"),
- ('keep-temp', 'k',
- "keep the pseudo-installation tree around after " +
- "creating the distribution archive"),
- ('dist-dir=', 'd',
- "directory to put final built distributions in"),
- ('skip-build', None,
- "skip rebuilding everything (for testing/debugging)"),
- ('relative', None,
- "build the archive using relative paths "
- "(default: false)"),
- ('owner=', 'u',
- "Owner name used when creating a tar file"
- " [default: current user]"),
- ('group=', 'g',
- "Group name used when creating a tar file"
- " [default: current group]"),
- ]
+ user_options = [
+ ('bdist-dir=', 'd', "temporary directory for creating the distribution"),
+ (
+ 'plat-name=',
+ 'p',
+ "platform name to embed in generated filenames "
+ "(default: %s)" % get_platform(),
+ ),
+ (
+ 'format=',
+ 'f',
+ "archive format to create (tar, gztar, bztar, xztar, " "ztar, zip)",
+ ),
+ (
+ 'keep-temp',
+ 'k',
+ "keep the pseudo-installation tree around after "
+ + "creating the distribution archive",
+ ),
+ ('dist-dir=', 'd', "directory to put final built distributions in"),
+ ('skip-build', None, "skip rebuilding everything (for testing/debugging)"),
+ (
+ 'relative',
+ None,
+ "build the archive using relative paths " "(default: false)",
+ ),
+ (
+ 'owner=',
+ 'u',
+ "Owner name used when creating a tar file" " [default: current user]",
+ ),
+ (
+ 'group=',
+ 'g',
+ "Group name used when creating a tar file" " [default: current group]",
+ ),
+ ]
boolean_options = ['keep-temp', 'skip-build', 'relative']
- default_format = { 'posix': 'gztar',
- 'nt': 'zip' }
+ default_format = {'posix': 'gztar', 'nt': 'zip'}
def initialize_options(self):
self.bdist_dir = None
@@ -68,13 +80,16 @@ class bdist_dumb(Command):
self.format = self.default_format[os.name]
except KeyError:
raise DistutilsPlatformError(
- "don't know how to create dumb built distributions "
- "on platform %s" % os.name)
+ "don't know how to create dumb built distributions "
+ "on platform %s" % os.name
+ )
- self.set_undefined_options('bdist',
- ('dist_dir', 'dist_dir'),
- ('plat_name', 'plat_name'),
- ('skip_build', 'skip_build'))
+ self.set_undefined_options(
+ 'bdist',
+ ('dist_dir', 'dist_dir'),
+ ('plat_name', 'plat_name'),
+ ('skip_build', 'skip_build'),
+ )
def run(self):
if not self.skip_build:
@@ -90,34 +105,40 @@ class bdist_dumb(Command):
# And make an archive relative to the root of the
# pseudo-installation tree.
- archive_basename = "%s.%s" % (self.distribution.get_fullname(),
- self.plat_name)
+ archive_basename = "{}.{}".format(
+ self.distribution.get_fullname(), self.plat_name
+ )
pseudoinstall_root = os.path.join(self.dist_dir, archive_basename)
if not self.relative:
archive_root = self.bdist_dir
else:
- if (self.distribution.has_ext_modules() and
- (install.install_base != install.install_platbase)):
+ if self.distribution.has_ext_modules() and (
+ install.install_base != install.install_platbase
+ ):
raise DistutilsPlatformError(
- "can't make a dumb built distribution where "
- "base and platbase are different (%s, %s)"
- % (repr(install.install_base),
- repr(install.install_platbase)))
+ "can't make a dumb built distribution where "
+ "base and platbase are different (%s, %s)"
+ % (repr(install.install_base), repr(install.install_platbase))
+ )
else:
- archive_root = os.path.join(self.bdist_dir,
- ensure_relative(install.install_base))
+ archive_root = os.path.join(
+ self.bdist_dir, ensure_relative(install.install_base)
+ )
# Make the archive
- filename = self.make_archive(pseudoinstall_root,
- self.format, root_dir=archive_root,
- owner=self.owner, group=self.group)
+ filename = self.make_archive(
+ pseudoinstall_root,
+ self.format,
+ root_dir=archive_root,
+ owner=self.owner,
+ group=self.group,
+ )
if self.distribution.has_ext_modules():
pyversion = get_python_version()
else:
pyversion = 'any'
- self.distribution.dist_files.append(('bdist_dumb', pyversion,
- filename))
+ self.distribution.dist_files.append(('bdist_dumb', pyversion, filename))
if not self.keep_temp:
remove_tree(self.bdist_dir, dry_run=self.dry_run)
diff --git a/setuptools/_distutils/command/bdist_msi.py b/setuptools/_distutils/command/bdist_msi.py
deleted file mode 100644
index 15259532..00000000
--- a/setuptools/_distutils/command/bdist_msi.py
+++ /dev/null
@@ -1,749 +0,0 @@
-# Copyright (C) 2005, 2006 Martin von Löwis
-# Licensed to PSF under a Contributor Agreement.
-# The bdist_wininst command proper
-# based on bdist_wininst
-"""
-Implements the bdist_msi command.
-"""
-
-import os
-import sys
-import warnings
-from distutils.core import Command
-from distutils.dir_util import remove_tree
-from distutils.sysconfig import get_python_version
-from distutils.version import StrictVersion
-from distutils.errors import DistutilsOptionError
-from distutils.util import get_platform
-from distutils import log
-import msilib
-from msilib import schema, sequence, text
-from msilib import Directory, Feature, Dialog, add_data
-
-class PyDialog(Dialog):
- """Dialog class with a fixed layout: controls at the top, then a ruler,
- then a list of buttons: back, next, cancel. Optionally a bitmap at the
- left."""
- def __init__(self, *args, **kw):
- """Dialog(database, name, x, y, w, h, attributes, title, first,
- default, cancel, bitmap=true)"""
- super().__init__(*args)
- ruler = self.h - 36
- bmwidth = 152*ruler/328
- #if kw.get("bitmap", True):
- # self.bitmap("Bitmap", 0, 0, bmwidth, ruler, "PythonWin")
- self.line("BottomLine", 0, ruler, self.w, 0)
-
- def title(self, title):
- "Set the title text of the dialog at the top."
- # name, x, y, w, h, flags=Visible|Enabled|Transparent|NoPrefix,
- # text, in VerdanaBold10
- self.text("Title", 15, 10, 320, 60, 0x30003,
- r"{\VerdanaBold10}%s" % title)
-
- def back(self, title, next, name = "Back", active = 1):
- """Add a back button with a given title, the tab-next button,
- its name in the Control table, possibly initially disabled.
-
- Return the button, so that events can be associated"""
- if active:
- flags = 3 # Visible|Enabled
- else:
- flags = 1 # Visible
- return self.pushbutton(name, 180, self.h-27 , 56, 17, flags, title, next)
-
- def cancel(self, title, next, name = "Cancel", active = 1):
- """Add a cancel button with a given title, the tab-next button,
- its name in the Control table, possibly initially disabled.
-
- Return the button, so that events can be associated"""
- if active:
- flags = 3 # Visible|Enabled
- else:
- flags = 1 # Visible
- return self.pushbutton(name, 304, self.h-27, 56, 17, flags, title, next)
-
- def next(self, title, next, name = "Next", active = 1):
- """Add a Next button with a given title, the tab-next button,
- its name in the Control table, possibly initially disabled.
-
- Return the button, so that events can be associated"""
- if active:
- flags = 3 # Visible|Enabled
- else:
- flags = 1 # Visible
- return self.pushbutton(name, 236, self.h-27, 56, 17, flags, title, next)
-
- def xbutton(self, name, title, next, xpos):
- """Add a button with a given title, the tab-next button,
- its name in the Control table, giving its x position; the
- y-position is aligned with the other buttons.
-
- Return the button, so that events can be associated"""
- return self.pushbutton(name, int(self.w*xpos - 28), self.h-27, 56, 17, 3, title, next)
-
-class bdist_msi(Command):
-
- description = "create a Microsoft Installer (.msi) binary distribution"
-
- user_options = [('bdist-dir=', None,
- "temporary directory for creating the distribution"),
- ('plat-name=', 'p',
- "platform name to embed in generated filenames "
- "(default: %s)" % get_platform()),
- ('keep-temp', 'k',
- "keep the pseudo-installation tree around after " +
- "creating the distribution archive"),
- ('target-version=', None,
- "require a specific python version" +
- " on the target system"),
- ('no-target-compile', 'c',
- "do not compile .py to .pyc on the target system"),
- ('no-target-optimize', 'o',
- "do not compile .py to .pyo (optimized) "
- "on the target system"),
- ('dist-dir=', 'd',
- "directory to put final built distributions in"),
- ('skip-build', None,
- "skip rebuilding everything (for testing/debugging)"),
- ('install-script=', None,
- "basename of installation script to be run after "
- "installation or before deinstallation"),
- ('pre-install-script=', None,
- "Fully qualified filename of a script to be run before "
- "any files are installed. This script need not be in the "
- "distribution"),
- ]
-
- boolean_options = ['keep-temp', 'no-target-compile', 'no-target-optimize',
- 'skip-build']
-
- all_versions = ['2.0', '2.1', '2.2', '2.3', '2.4',
- '2.5', '2.6', '2.7', '2.8', '2.9',
- '3.0', '3.1', '3.2', '3.3', '3.4',
- '3.5', '3.6', '3.7', '3.8', '3.9']
- other_version = 'X'
-
- def __init__(self, *args, **kw):
- super().__init__(*args, **kw)
- warnings.warn("bdist_msi command is deprecated since Python 3.9, "
- "use bdist_wheel (wheel packages) instead",
- DeprecationWarning, 2)
-
- def initialize_options(self):
- self.bdist_dir = None
- self.plat_name = None
- self.keep_temp = 0
- self.no_target_compile = 0
- self.no_target_optimize = 0
- self.target_version = None
- self.dist_dir = None
- self.skip_build = None
- self.install_script = None
- self.pre_install_script = None
- self.versions = None
-
- def finalize_options(self):
- self.set_undefined_options('bdist', ('skip_build', 'skip_build'))
-
- if self.bdist_dir is None:
- bdist_base = self.get_finalized_command('bdist').bdist_base
- self.bdist_dir = os.path.join(bdist_base, 'msi')
-
- short_version = get_python_version()
- if (not self.target_version) and self.distribution.has_ext_modules():
- self.target_version = short_version
-
- if self.target_version:
- self.versions = [self.target_version]
- if not self.skip_build and self.distribution.has_ext_modules()\
- and self.target_version != short_version:
- raise DistutilsOptionError(
- "target version can only be %s, or the '--skip-build'"
- " option must be specified" % (short_version,))
- else:
- self.versions = list(self.all_versions)
-
- self.set_undefined_options('bdist',
- ('dist_dir', 'dist_dir'),
- ('plat_name', 'plat_name'),
- )
-
- if self.pre_install_script:
- raise DistutilsOptionError(
- "the pre-install-script feature is not yet implemented")
-
- if self.install_script:
- for script in self.distribution.scripts:
- if self.install_script == os.path.basename(script):
- break
- else:
- raise DistutilsOptionError(
- "install_script '%s' not found in scripts"
- % self.install_script)
- self.install_script_key = None
-
- def run(self):
- if not self.skip_build:
- self.run_command('build')
-
- install = self.reinitialize_command('install', reinit_subcommands=1)
- install.prefix = self.bdist_dir
- install.skip_build = self.skip_build
- install.warn_dir = 0
-
- install_lib = self.reinitialize_command('install_lib')
- # we do not want to include pyc or pyo files
- install_lib.compile = 0
- install_lib.optimize = 0
-
- if self.distribution.has_ext_modules():
- # If we are building an installer for a Python version other
- # than the one we are currently running, then we need to ensure
- # our build_lib reflects the other Python version rather than ours.
- # Note that for target_version!=sys.version, we must have skipped the
- # build step, so there is no issue with enforcing the build of this
- # version.
- target_version = self.target_version
- if not target_version:
- assert self.skip_build, "Should have already checked this"
- target_version = '%d.%d' % sys.version_info[:2]
- plat_specifier = ".%s-%s" % (self.plat_name, target_version)
- build = self.get_finalized_command('build')
- build.build_lib = os.path.join(build.build_base,
- 'lib' + plat_specifier)
-
- log.info("installing to %s", self.bdist_dir)
- install.ensure_finalized()
-
- # avoid warning of 'install_lib' about installing
- # into a directory not in sys.path
- sys.path.insert(0, os.path.join(self.bdist_dir, 'PURELIB'))
-
- install.run()
-
- del sys.path[0]
-
- self.mkpath(self.dist_dir)
- fullname = self.distribution.get_fullname()
- installer_name = self.get_installer_filename(fullname)
- installer_name = os.path.abspath(installer_name)
- if os.path.exists(installer_name): os.unlink(installer_name)
-
- metadata = self.distribution.metadata
- author = metadata.author
- if not author:
- author = metadata.maintainer
- if not author:
- author = "UNKNOWN"
- version = metadata.get_version()
- # ProductVersion must be strictly numeric
- # XXX need to deal with prerelease versions
- sversion = "%d.%d.%d" % StrictVersion(version).version
- # Prefix ProductName with Python x.y, so that
- # it sorts together with the other Python packages
- # in Add-Remove-Programs (APR)
- fullname = self.distribution.get_fullname()
- if self.target_version:
- product_name = "Python %s %s" % (self.target_version, fullname)
- else:
- product_name = "Python %s" % (fullname)
- self.db = msilib.init_database(installer_name, schema,
- product_name, msilib.gen_uuid(),
- sversion, author)
- msilib.add_tables(self.db, sequence)
- props = [('DistVersion', version)]
- email = metadata.author_email or metadata.maintainer_email
- if email:
- props.append(("ARPCONTACT", email))
- if metadata.url:
- props.append(("ARPURLINFOABOUT", metadata.url))
- if props:
- add_data(self.db, 'Property', props)
-
- self.add_find_python()
- self.add_files()
- self.add_scripts()
- self.add_ui()
- self.db.Commit()
-
- if hasattr(self.distribution, 'dist_files'):
- tup = 'bdist_msi', self.target_version or 'any', fullname
- self.distribution.dist_files.append(tup)
-
- if not self.keep_temp:
- remove_tree(self.bdist_dir, dry_run=self.dry_run)
-
- def add_files(self):
- db = self.db
- cab = msilib.CAB("distfiles")
- rootdir = os.path.abspath(self.bdist_dir)
-
- root = Directory(db, cab, None, rootdir, "TARGETDIR", "SourceDir")
- f = Feature(db, "Python", "Python", "Everything",
- 0, 1, directory="TARGETDIR")
-
- items = [(f, root, '')]
- for version in self.versions + [self.other_version]:
- target = "TARGETDIR" + version
- name = default = "Python" + version
- desc = "Everything"
- if version is self.other_version:
- title = "Python from another location"
- level = 2
- else:
- title = "Python %s from registry" % version
- level = 1
- f = Feature(db, name, title, desc, 1, level, directory=target)
- dir = Directory(db, cab, root, rootdir, target, default)
- items.append((f, dir, version))
- db.Commit()
-
- seen = {}
- for feature, dir, version in items:
- todo = [dir]
- while todo:
- dir = todo.pop()
- for file in os.listdir(dir.absolute):
- afile = os.path.join(dir.absolute, file)
- if os.path.isdir(afile):
- short = "%s|%s" % (dir.make_short(file), file)
- default = file + version
- newdir = Directory(db, cab, dir, file, default, short)
- todo.append(newdir)
- else:
- if not dir.component:
- dir.start_component(dir.logical, feature, 0)
- if afile not in seen:
- key = seen[afile] = dir.add_file(file)
- if file==self.install_script:
- if self.install_script_key:
- raise DistutilsOptionError(
- "Multiple files with name %s" % file)
- self.install_script_key = '[#%s]' % key
- else:
- key = seen[afile]
- add_data(self.db, "DuplicateFile",
- [(key + version, dir.component, key, None, dir.logical)])
- db.Commit()
- cab.commit(db)
-
- def add_find_python(self):
- """Adds code to the installer to compute the location of Python.
-
- Properties PYTHON.MACHINE.X.Y and PYTHON.USER.X.Y will be set from the
- registry for each version of Python.
-
- Properties TARGETDIRX.Y will be set from PYTHON.USER.X.Y if defined,
- else from PYTHON.MACHINE.X.Y.
-
- Properties PYTHONX.Y will be set to TARGETDIRX.Y\\python.exe"""
-
- start = 402
- for ver in self.versions:
- install_path = r"SOFTWARE\Python\PythonCore\%s\InstallPath" % ver
- machine_reg = "python.machine." + ver
- user_reg = "python.user." + ver
- machine_prop = "PYTHON.MACHINE." + ver
- user_prop = "PYTHON.USER." + ver
- machine_action = "PythonFromMachine" + ver
- user_action = "PythonFromUser" + ver
- exe_action = "PythonExe" + ver
- target_dir_prop = "TARGETDIR" + ver
- exe_prop = "PYTHON" + ver
- if msilib.Win64:
- # type: msidbLocatorTypeRawValue + msidbLocatorType64bit
- Type = 2+16
- else:
- Type = 2
- add_data(self.db, "RegLocator",
- [(machine_reg, 2, install_path, None, Type),
- (user_reg, 1, install_path, None, Type)])
- add_data(self.db, "AppSearch",
- [(machine_prop, machine_reg),
- (user_prop, user_reg)])
- add_data(self.db, "CustomAction",
- [(machine_action, 51+256, target_dir_prop, "[" + machine_prop + "]"),
- (user_action, 51+256, target_dir_prop, "[" + user_prop + "]"),
- (exe_action, 51+256, exe_prop, "[" + target_dir_prop + "]\\python.exe"),
- ])
- add_data(self.db, "InstallExecuteSequence",
- [(machine_action, machine_prop, start),
- (user_action, user_prop, start + 1),
- (exe_action, None, start + 2),
- ])
- add_data(self.db, "InstallUISequence",
- [(machine_action, machine_prop, start),
- (user_action, user_prop, start + 1),
- (exe_action, None, start + 2),
- ])
- add_data(self.db, "Condition",
- [("Python" + ver, 0, "NOT TARGETDIR" + ver)])
- start += 4
- assert start < 500
-
- def add_scripts(self):
- if self.install_script:
- start = 6800
- for ver in self.versions + [self.other_version]:
- install_action = "install_script." + ver
- exe_prop = "PYTHON" + ver
- add_data(self.db, "CustomAction",
- [(install_action, 50, exe_prop, self.install_script_key)])
- add_data(self.db, "InstallExecuteSequence",
- [(install_action, "&Python%s=3" % ver, start)])
- start += 1
- # XXX pre-install scripts are currently refused in finalize_options()
- # but if this feature is completed, it will also need to add
- # entries for each version as the above code does
- if self.pre_install_script:
- scriptfn = os.path.join(self.bdist_dir, "preinstall.bat")
- with open(scriptfn, "w") as f:
- # The batch file will be executed with [PYTHON], so that %1
- # is the path to the Python interpreter; %0 will be the path
- # of the batch file.
- # rem ="""
- # %1 %0
- # exit
- # """
- # <actual script>
- f.write('rem ="""\n%1 %0\nexit\n"""\n')
- with open(self.pre_install_script) as fin:
- f.write(fin.read())
- add_data(self.db, "Binary",
- [("PreInstall", msilib.Binary(scriptfn))
- ])
- add_data(self.db, "CustomAction",
- [("PreInstall", 2, "PreInstall", None)
- ])
- add_data(self.db, "InstallExecuteSequence",
- [("PreInstall", "NOT Installed", 450)])
-
-
- def add_ui(self):
- db = self.db
- x = y = 50
- w = 370
- h = 300
- title = "[ProductName] Setup"
-
- # see "Dialog Style Bits"
- modal = 3 # visible | modal
- modeless = 1 # visible
- track_disk_space = 32
-
- # UI customization properties
- add_data(db, "Property",
- # See "DefaultUIFont Property"
- [("DefaultUIFont", "DlgFont8"),
- # See "ErrorDialog Style Bit"
- ("ErrorDialog", "ErrorDlg"),
- ("Progress1", "Install"), # modified in maintenance type dlg
- ("Progress2", "installs"),
- ("MaintenanceForm_Action", "Repair"),
- # possible values: ALL, JUSTME
- ("WhichUsers", "ALL")
- ])
-
- # Fonts, see "TextStyle Table"
- add_data(db, "TextStyle",
- [("DlgFont8", "Tahoma", 9, None, 0),
- ("DlgFontBold8", "Tahoma", 8, None, 1), #bold
- ("VerdanaBold10", "Verdana", 10, None, 1),
- ("VerdanaRed9", "Verdana", 9, 255, 0),
- ])
-
- # UI Sequences, see "InstallUISequence Table", "Using a Sequence Table"
- # Numbers indicate sequence; see sequence.py for how these action integrate
- add_data(db, "InstallUISequence",
- [("PrepareDlg", "Not Privileged or Windows9x or Installed", 140),
- ("WhichUsersDlg", "Privileged and not Windows9x and not Installed", 141),
- # In the user interface, assume all-users installation if privileged.
- ("SelectFeaturesDlg", "Not Installed", 1230),
- # XXX no support for resume installations yet
- #("ResumeDlg", "Installed AND (RESUME OR Preselected)", 1240),
- ("MaintenanceTypeDlg", "Installed AND NOT RESUME AND NOT Preselected", 1250),
- ("ProgressDlg", None, 1280)])
-
- add_data(db, 'ActionText', text.ActionText)
- add_data(db, 'UIText', text.UIText)
- #####################################################################
- # Standard dialogs: FatalError, UserExit, ExitDialog
- fatal=PyDialog(db, "FatalError", x, y, w, h, modal, title,
- "Finish", "Finish", "Finish")
- fatal.title("[ProductName] Installer ended prematurely")
- fatal.back("< Back", "Finish", active = 0)
- fatal.cancel("Cancel", "Back", active = 0)
- fatal.text("Description1", 15, 70, 320, 80, 0x30003,
- "[ProductName] setup ended prematurely because of an error. Your system has not been modified. To install this program at a later time, please run the installation again.")
- fatal.text("Description2", 15, 155, 320, 20, 0x30003,
- "Click the Finish button to exit the Installer.")
- c=fatal.next("Finish", "Cancel", name="Finish")
- c.event("EndDialog", "Exit")
-
- user_exit=PyDialog(db, "UserExit", x, y, w, h, modal, title,
- "Finish", "Finish", "Finish")
- user_exit.title("[ProductName] Installer was interrupted")
- user_exit.back("< Back", "Finish", active = 0)
- user_exit.cancel("Cancel", "Back", active = 0)
- user_exit.text("Description1", 15, 70, 320, 80, 0x30003,
- "[ProductName] setup was interrupted. Your system has not been modified. "
- "To install this program at a later time, please run the installation again.")
- user_exit.text("Description2", 15, 155, 320, 20, 0x30003,
- "Click the Finish button to exit the Installer.")
- c = user_exit.next("Finish", "Cancel", name="Finish")
- c.event("EndDialog", "Exit")
-
- exit_dialog = PyDialog(db, "ExitDialog", x, y, w, h, modal, title,
- "Finish", "Finish", "Finish")
- exit_dialog.title("Completing the [ProductName] Installer")
- exit_dialog.back("< Back", "Finish", active = 0)
- exit_dialog.cancel("Cancel", "Back", active = 0)
- exit_dialog.text("Description", 15, 235, 320, 20, 0x30003,
- "Click the Finish button to exit the Installer.")
- c = exit_dialog.next("Finish", "Cancel", name="Finish")
- c.event("EndDialog", "Return")
-
- #####################################################################
- # Required dialog: FilesInUse, ErrorDlg
- inuse = PyDialog(db, "FilesInUse",
- x, y, w, h,
- 19, # KeepModeless|Modal|Visible
- title,
- "Retry", "Retry", "Retry", bitmap=False)
- inuse.text("Title", 15, 6, 200, 15, 0x30003,
- r"{\DlgFontBold8}Files in Use")
- inuse.text("Description", 20, 23, 280, 20, 0x30003,
- "Some files that need to be updated are currently in use.")
- inuse.text("Text", 20, 55, 330, 50, 3,
- "The following applications are using files that need to be updated by this setup. Close these applications and then click Retry to continue the installation or Cancel to exit it.")
- inuse.control("List", "ListBox", 20, 107, 330, 130, 7, "FileInUseProcess",
- None, None, None)
- c=inuse.back("Exit", "Ignore", name="Exit")
- c.event("EndDialog", "Exit")
- c=inuse.next("Ignore", "Retry", name="Ignore")
- c.event("EndDialog", "Ignore")
- c=inuse.cancel("Retry", "Exit", name="Retry")
- c.event("EndDialog","Retry")
-
- # See "Error Dialog". See "ICE20" for the required names of the controls.
- error = Dialog(db, "ErrorDlg",
- 50, 10, 330, 101,
- 65543, # Error|Minimize|Modal|Visible
- title,
- "ErrorText", None, None)
- error.text("ErrorText", 50,9,280,48,3, "")
- #error.control("ErrorIcon", "Icon", 15, 9, 24, 24, 5242881, None, "py.ico", None, None)
- error.pushbutton("N",120,72,81,21,3,"No",None).event("EndDialog","ErrorNo")
- error.pushbutton("Y",240,72,81,21,3,"Yes",None).event("EndDialog","ErrorYes")
- error.pushbutton("A",0,72,81,21,3,"Abort",None).event("EndDialog","ErrorAbort")
- error.pushbutton("C",42,72,81,21,3,"Cancel",None).event("EndDialog","ErrorCancel")
- error.pushbutton("I",81,72,81,21,3,"Ignore",None).event("EndDialog","ErrorIgnore")
- error.pushbutton("O",159,72,81,21,3,"Ok",None).event("EndDialog","ErrorOk")
- error.pushbutton("R",198,72,81,21,3,"Retry",None).event("EndDialog","ErrorRetry")
-
- #####################################################################
- # Global "Query Cancel" dialog
- cancel = Dialog(db, "CancelDlg", 50, 10, 260, 85, 3, title,
- "No", "No", "No")
- cancel.text("Text", 48, 15, 194, 30, 3,
- "Are you sure you want to cancel [ProductName] installation?")
- #cancel.control("Icon", "Icon", 15, 15, 24, 24, 5242881, None,
- # "py.ico", None, None)
- c=cancel.pushbutton("Yes", 72, 57, 56, 17, 3, "Yes", "No")
- c.event("EndDialog", "Exit")
-
- c=cancel.pushbutton("No", 132, 57, 56, 17, 3, "No", "Yes")
- c.event("EndDialog", "Return")
-
- #####################################################################
- # Global "Wait for costing" dialog
- costing = Dialog(db, "WaitForCostingDlg", 50, 10, 260, 85, modal, title,
- "Return", "Return", "Return")
- costing.text("Text", 48, 15, 194, 30, 3,
- "Please wait while the installer finishes determining your disk space requirements.")
- c = costing.pushbutton("Return", 102, 57, 56, 17, 3, "Return", None)
- c.event("EndDialog", "Exit")
-
- #####################################################################
- # Preparation dialog: no user input except cancellation
- prep = PyDialog(db, "PrepareDlg", x, y, w, h, modeless, title,
- "Cancel", "Cancel", "Cancel")
- prep.text("Description", 15, 70, 320, 40, 0x30003,
- "Please wait while the Installer prepares to guide you through the installation.")
- prep.title("Welcome to the [ProductName] Installer")
- c=prep.text("ActionText", 15, 110, 320, 20, 0x30003, "Pondering...")
- c.mapping("ActionText", "Text")
- c=prep.text("ActionData", 15, 135, 320, 30, 0x30003, None)
- c.mapping("ActionData", "Text")
- prep.back("Back", None, active=0)
- prep.next("Next", None, active=0)
- c=prep.cancel("Cancel", None)
- c.event("SpawnDialog", "CancelDlg")
-
- #####################################################################
- # Feature (Python directory) selection
- seldlg = PyDialog(db, "SelectFeaturesDlg", x, y, w, h, modal, title,
- "Next", "Next", "Cancel")
- seldlg.title("Select Python Installations")
-
- seldlg.text("Hint", 15, 30, 300, 20, 3,
- "Select the Python locations where %s should be installed."
- % self.distribution.get_fullname())
-
- seldlg.back("< Back", None, active=0)
- c = seldlg.next("Next >", "Cancel")
- order = 1
- c.event("[TARGETDIR]", "[SourceDir]", ordering=order)
- for version in self.versions + [self.other_version]:
- order += 1
- c.event("[TARGETDIR]", "[TARGETDIR%s]" % version,
- "FEATURE_SELECTED AND &Python%s=3" % version,
- ordering=order)
- c.event("SpawnWaitDialog", "WaitForCostingDlg", ordering=order + 1)
- c.event("EndDialog", "Return", ordering=order + 2)
- c = seldlg.cancel("Cancel", "Features")
- c.event("SpawnDialog", "CancelDlg")
-
- c = seldlg.control("Features", "SelectionTree", 15, 60, 300, 120, 3,
- "FEATURE", None, "PathEdit", None)
- c.event("[FEATURE_SELECTED]", "1")
- ver = self.other_version
- install_other_cond = "FEATURE_SELECTED AND &Python%s=3" % ver
- dont_install_other_cond = "FEATURE_SELECTED AND &Python%s<>3" % ver
-
- c = seldlg.text("Other", 15, 200, 300, 15, 3,
- "Provide an alternate Python location")
- c.condition("Enable", install_other_cond)
- c.condition("Show", install_other_cond)
- c.condition("Disable", dont_install_other_cond)
- c.condition("Hide", dont_install_other_cond)
-
- c = seldlg.control("PathEdit", "PathEdit", 15, 215, 300, 16, 1,
- "TARGETDIR" + ver, None, "Next", None)
- c.condition("Enable", install_other_cond)
- c.condition("Show", install_other_cond)
- c.condition("Disable", dont_install_other_cond)
- c.condition("Hide", dont_install_other_cond)
-
- #####################################################################
- # Disk cost
- cost = PyDialog(db, "DiskCostDlg", x, y, w, h, modal, title,
- "OK", "OK", "OK", bitmap=False)
- cost.text("Title", 15, 6, 200, 15, 0x30003,
- r"{\DlgFontBold8}Disk Space Requirements")
- cost.text("Description", 20, 20, 280, 20, 0x30003,
- "The disk space required for the installation of the selected features.")
- cost.text("Text", 20, 53, 330, 60, 3,
- "The highlighted volumes (if any) do not have enough disk space "
- "available for the currently selected features. You can either "
- "remove some files from the highlighted volumes, or choose to "
- "install less features onto local drive(s), or select different "
- "destination drive(s).")
- cost.control("VolumeList", "VolumeCostList", 20, 100, 330, 150, 393223,
- None, "{120}{70}{70}{70}{70}", None, None)
- cost.xbutton("OK", "Ok", None, 0.5).event("EndDialog", "Return")
-
- #####################################################################
- # WhichUsers Dialog. Only available on NT, and for privileged users.
- # This must be run before FindRelatedProducts, because that will
- # take into account whether the previous installation was per-user
- # or per-machine. We currently don't support going back to this
- # dialog after "Next" was selected; to support this, we would need to
- # find how to reset the ALLUSERS property, and how to re-run
- # FindRelatedProducts.
- # On Windows9x, the ALLUSERS property is ignored on the command line
- # and in the Property table, but installer fails according to the documentation
- # if a dialog attempts to set ALLUSERS.
- whichusers = PyDialog(db, "WhichUsersDlg", x, y, w, h, modal, title,
- "AdminInstall", "Next", "Cancel")
- whichusers.title("Select whether to install [ProductName] for all users of this computer.")
- # A radio group with two options: allusers, justme
- g = whichusers.radiogroup("AdminInstall", 15, 60, 260, 50, 3,
- "WhichUsers", "", "Next")
- g.add("ALL", 0, 5, 150, 20, "Install for all users")
- g.add("JUSTME", 0, 25, 150, 20, "Install just for me")
-
- whichusers.back("Back", None, active=0)
-
- c = whichusers.next("Next >", "Cancel")
- c.event("[ALLUSERS]", "1", 'WhichUsers="ALL"', 1)
- c.event("EndDialog", "Return", ordering = 2)
-
- c = whichusers.cancel("Cancel", "AdminInstall")
- c.event("SpawnDialog", "CancelDlg")
-
- #####################################################################
- # Installation Progress dialog (modeless)
- progress = PyDialog(db, "ProgressDlg", x, y, w, h, modeless, title,
- "Cancel", "Cancel", "Cancel", bitmap=False)
- progress.text("Title", 20, 15, 200, 15, 0x30003,
- r"{\DlgFontBold8}[Progress1] [ProductName]")
- progress.text("Text", 35, 65, 300, 30, 3,
- "Please wait while the Installer [Progress2] [ProductName]. "
- "This may take several minutes.")
- progress.text("StatusLabel", 35, 100, 35, 20, 3, "Status:")
-
- c=progress.text("ActionText", 70, 100, w-70, 20, 3, "Pondering...")
- c.mapping("ActionText", "Text")
-
- #c=progress.text("ActionData", 35, 140, 300, 20, 3, None)
- #c.mapping("ActionData", "Text")
-
- c=progress.control("ProgressBar", "ProgressBar", 35, 120, 300, 10, 65537,
- None, "Progress done", None, None)
- c.mapping("SetProgress", "Progress")
-
- progress.back("< Back", "Next", active=False)
- progress.next("Next >", "Cancel", active=False)
- progress.cancel("Cancel", "Back").event("SpawnDialog", "CancelDlg")
-
- ###################################################################
- # Maintenance type: repair/uninstall
- maint = PyDialog(db, "MaintenanceTypeDlg", x, y, w, h, modal, title,
- "Next", "Next", "Cancel")
- maint.title("Welcome to the [ProductName] Setup Wizard")
- maint.text("BodyText", 15, 63, 330, 42, 3,
- "Select whether you want to repair or remove [ProductName].")
- g=maint.radiogroup("RepairRadioGroup", 15, 108, 330, 60, 3,
- "MaintenanceForm_Action", "", "Next")
- #g.add("Change", 0, 0, 200, 17, "&Change [ProductName]")
- g.add("Repair", 0, 18, 200, 17, "&Repair [ProductName]")
- g.add("Remove", 0, 36, 200, 17, "Re&move [ProductName]")
-
- maint.back("< Back", None, active=False)
- c=maint.next("Finish", "Cancel")
- # Change installation: Change progress dialog to "Change", then ask
- # for feature selection
- #c.event("[Progress1]", "Change", 'MaintenanceForm_Action="Change"', 1)
- #c.event("[Progress2]", "changes", 'MaintenanceForm_Action="Change"', 2)
-
- # Reinstall: Change progress dialog to "Repair", then invoke reinstall
- # Also set list of reinstalled features to "ALL"
- c.event("[REINSTALL]", "ALL", 'MaintenanceForm_Action="Repair"', 5)
- c.event("[Progress1]", "Repairing", 'MaintenanceForm_Action="Repair"', 6)
- c.event("[Progress2]", "repairs", 'MaintenanceForm_Action="Repair"', 7)
- c.event("Reinstall", "ALL", 'MaintenanceForm_Action="Repair"', 8)
-
- # Uninstall: Change progress to "Remove", then invoke uninstall
- # Also set list of removed features to "ALL"
- c.event("[REMOVE]", "ALL", 'MaintenanceForm_Action="Remove"', 11)
- c.event("[Progress1]", "Removing", 'MaintenanceForm_Action="Remove"', 12)
- c.event("[Progress2]", "removes", 'MaintenanceForm_Action="Remove"', 13)
- c.event("Remove", "ALL", 'MaintenanceForm_Action="Remove"', 14)
-
- # Close dialog when maintenance action scheduled
- c.event("EndDialog", "Return", 'MaintenanceForm_Action<>"Change"', 20)
- #c.event("NewDialog", "SelectFeaturesDlg", 'MaintenanceForm_Action="Change"', 21)
-
- maint.cancel("Cancel", "RepairRadioGroup").event("SpawnDialog", "CancelDlg")
-
- def get_installer_filename(self, fullname):
- # Factored out to allow overriding in subclasses
- if self.target_version:
- base_name = "%s.%s-py%s.msi" % (fullname, self.plat_name,
- self.target_version)
- else:
- base_name = "%s.%s.msi" % (fullname, self.plat_name)
- installer_name = os.path.join(self.dist_dir, base_name)
- return installer_name
diff --git a/setuptools/_distutils/command/bdist_rpm.py b/setuptools/_distutils/command/bdist_rpm.py
index 550cbfa1..340527b0 100644
--- a/setuptools/_distutils/command/bdist_rpm.py
+++ b/setuptools/_distutils/command/bdist_rpm.py
@@ -3,134 +3,153 @@
Implements the Distutils 'bdist_rpm' command (create RPM source and binary
distributions)."""
-import subprocess, sys, os
-from distutils.core import Command
-from distutils.debug import DEBUG
-from distutils.file_util import write_file
-from distutils.errors import *
-from distutils.sysconfig import get_python_version
-from distutils import log
+import subprocess
+import sys
+import os
+
+from ..core import Command
+from ..debug import DEBUG
+from ..file_util import write_file
+from ..errors import (
+ DistutilsOptionError,
+ DistutilsPlatformError,
+ DistutilsFileError,
+ DistutilsExecError,
+)
+from ..sysconfig import get_python_version
+from distutils._log import log
+
class bdist_rpm(Command):
description = "create an RPM distribution"
user_options = [
- ('bdist-base=', None,
- "base directory for creating built distributions"),
- ('rpm-base=', None,
- "base directory for creating RPMs (defaults to \"rpm\" under "
- "--bdist-base; must be specified for RPM 2)"),
- ('dist-dir=', 'd',
- "directory to put final RPM files in "
- "(and .spec files if --spec-only)"),
- ('python=', None,
- "path to Python interpreter to hard-code in the .spec file "
- "(default: \"python\")"),
- ('fix-python', None,
- "hard-code the exact path to the current Python interpreter in "
- "the .spec file"),
- ('spec-only', None,
- "only regenerate spec file"),
- ('source-only', None,
- "only generate source RPM"),
- ('binary-only', None,
- "only generate binary RPM"),
- ('use-bzip2', None,
- "use bzip2 instead of gzip to create source distribution"),
-
+ ('bdist-base=', None, "base directory for creating built distributions"),
+ (
+ 'rpm-base=',
+ None,
+ "base directory for creating RPMs (defaults to \"rpm\" under "
+ "--bdist-base; must be specified for RPM 2)",
+ ),
+ (
+ 'dist-dir=',
+ 'd',
+ "directory to put final RPM files in " "(and .spec files if --spec-only)",
+ ),
+ (
+ 'python=',
+ None,
+ "path to Python interpreter to hard-code in the .spec file "
+ "(default: \"python\")",
+ ),
+ (
+ 'fix-python',
+ None,
+ "hard-code the exact path to the current Python interpreter in "
+ "the .spec file",
+ ),
+ ('spec-only', None, "only regenerate spec file"),
+ ('source-only', None, "only generate source RPM"),
+ ('binary-only', None, "only generate binary RPM"),
+ ('use-bzip2', None, "use bzip2 instead of gzip to create source distribution"),
# More meta-data: too RPM-specific to put in the setup script,
# but needs to go in the .spec file -- so we make these options
# to "bdist_rpm". The idea is that packagers would put this
# info in setup.cfg, although they are of course free to
# supply it on the command line.
- ('distribution-name=', None,
- "name of the (Linux) distribution to which this "
- "RPM applies (*not* the name of the module distribution!)"),
- ('group=', None,
- "package classification [default: \"Development/Libraries\"]"),
- ('release=', None,
- "RPM release number"),
- ('serial=', None,
- "RPM serial number"),
- ('vendor=', None,
- "RPM \"vendor\" (eg. \"Joe Blow <joe@example.com>\") "
- "[default: maintainer or author from setup script]"),
- ('packager=', None,
- "RPM packager (eg. \"Jane Doe <jane@example.net>\") "
- "[default: vendor]"),
- ('doc-files=', None,
- "list of documentation files (space or comma-separated)"),
- ('changelog=', None,
- "RPM changelog"),
- ('icon=', None,
- "name of icon file"),
- ('provides=', None,
- "capabilities provided by this package"),
- ('requires=', None,
- "capabilities required by this package"),
- ('conflicts=', None,
- "capabilities which conflict with this package"),
- ('build-requires=', None,
- "capabilities required to build this package"),
- ('obsoletes=', None,
- "capabilities made obsolete by this package"),
- ('no-autoreq', None,
- "do not automatically calculate dependencies"),
-
+ (
+ 'distribution-name=',
+ None,
+ "name of the (Linux) distribution to which this "
+ "RPM applies (*not* the name of the module distribution!)",
+ ),
+ ('group=', None, "package classification [default: \"Development/Libraries\"]"),
+ ('release=', None, "RPM release number"),
+ ('serial=', None, "RPM serial number"),
+ (
+ 'vendor=',
+ None,
+ "RPM \"vendor\" (eg. \"Joe Blow <joe@example.com>\") "
+ "[default: maintainer or author from setup script]",
+ ),
+ (
+ 'packager=',
+ None,
+ "RPM packager (eg. \"Jane Doe <jane@example.net>\") " "[default: vendor]",
+ ),
+ ('doc-files=', None, "list of documentation files (space or comma-separated)"),
+ ('changelog=', None, "RPM changelog"),
+ ('icon=', None, "name of icon file"),
+ ('provides=', None, "capabilities provided by this package"),
+ ('requires=', None, "capabilities required by this package"),
+ ('conflicts=', None, "capabilities which conflict with this package"),
+ ('build-requires=', None, "capabilities required to build this package"),
+ ('obsoletes=', None, "capabilities made obsolete by this package"),
+ ('no-autoreq', None, "do not automatically calculate dependencies"),
# Actions to take when building RPM
- ('keep-temp', 'k',
- "don't clean up RPM build directory"),
- ('no-keep-temp', None,
- "clean up RPM build directory [default]"),
- ('use-rpm-opt-flags', None,
- "compile with RPM_OPT_FLAGS when building from source RPM"),
- ('no-rpm-opt-flags', None,
- "do not pass any RPM CFLAGS to compiler"),
- ('rpm3-mode', None,
- "RPM 3 compatibility mode (default)"),
- ('rpm2-mode', None,
- "RPM 2 compatibility mode"),
-
+ ('keep-temp', 'k', "don't clean up RPM build directory"),
+ ('no-keep-temp', None, "clean up RPM build directory [default]"),
+ (
+ 'use-rpm-opt-flags',
+ None,
+ "compile with RPM_OPT_FLAGS when building from source RPM",
+ ),
+ ('no-rpm-opt-flags', None, "do not pass any RPM CFLAGS to compiler"),
+ ('rpm3-mode', None, "RPM 3 compatibility mode (default)"),
+ ('rpm2-mode', None, "RPM 2 compatibility mode"),
# Add the hooks necessary for specifying custom scripts
- ('prep-script=', None,
- "Specify a script for the PREP phase of RPM building"),
- ('build-script=', None,
- "Specify a script for the BUILD phase of RPM building"),
-
- ('pre-install=', None,
- "Specify a script for the pre-INSTALL phase of RPM building"),
- ('install-script=', None,
- "Specify a script for the INSTALL phase of RPM building"),
- ('post-install=', None,
- "Specify a script for the post-INSTALL phase of RPM building"),
-
- ('pre-uninstall=', None,
- "Specify a script for the pre-UNINSTALL phase of RPM building"),
- ('post-uninstall=', None,
- "Specify a script for the post-UNINSTALL phase of RPM building"),
-
- ('clean-script=', None,
- "Specify a script for the CLEAN phase of RPM building"),
-
- ('verify-script=', None,
- "Specify a script for the VERIFY phase of the RPM build"),
-
+ ('prep-script=', None, "Specify a script for the PREP phase of RPM building"),
+ ('build-script=', None, "Specify a script for the BUILD phase of RPM building"),
+ (
+ 'pre-install=',
+ None,
+ "Specify a script for the pre-INSTALL phase of RPM building",
+ ),
+ (
+ 'install-script=',
+ None,
+ "Specify a script for the INSTALL phase of RPM building",
+ ),
+ (
+ 'post-install=',
+ None,
+ "Specify a script for the post-INSTALL phase of RPM building",
+ ),
+ (
+ 'pre-uninstall=',
+ None,
+ "Specify a script for the pre-UNINSTALL phase of RPM building",
+ ),
+ (
+ 'post-uninstall=',
+ None,
+ "Specify a script for the post-UNINSTALL phase of RPM building",
+ ),
+ ('clean-script=', None, "Specify a script for the CLEAN phase of RPM building"),
+ (
+ 'verify-script=',
+ None,
+ "Specify a script for the VERIFY phase of the RPM build",
+ ),
# Allow a packager to explicitly force an architecture
- ('force-arch=', None,
- "Force an architecture onto the RPM build process"),
-
- ('quiet', 'q',
- "Run the INSTALL phase of RPM building in quiet mode"),
- ]
-
- boolean_options = ['keep-temp', 'use-rpm-opt-flags', 'rpm3-mode',
- 'no-autoreq', 'quiet']
-
- negative_opt = {'no-keep-temp': 'keep-temp',
- 'no-rpm-opt-flags': 'use-rpm-opt-flags',
- 'rpm2-mode': 'rpm3-mode'}
-
+ ('force-arch=', None, "Force an architecture onto the RPM build process"),
+ ('quiet', 'q', "Run the INSTALL phase of RPM building in quiet mode"),
+ ]
+
+ boolean_options = [
+ 'keep-temp',
+ 'use-rpm-opt-flags',
+ 'rpm3-mode',
+ 'no-autoreq',
+ 'quiet',
+ ]
+
+ negative_opt = {
+ 'no-keep-temp': 'keep-temp',
+ 'no-rpm-opt-flags': 'use-rpm-opt-flags',
+ 'rpm2-mode': 'rpm3-mode',
+ }
def initialize_options(self):
self.bdist_base = None
@@ -181,8 +200,7 @@ class bdist_rpm(Command):
self.set_undefined_options('bdist', ('bdist_base', 'bdist_base'))
if self.rpm_base is None:
if not self.rpm3_mode:
- raise DistutilsOptionError(
- "you must specify --rpm-base in RPM 2 mode")
+ raise DistutilsOptionError("you must specify --rpm-base in RPM 2 mode")
self.rpm_base = os.path.join(self.bdist_base, "rpm")
if self.python is None:
@@ -192,14 +210,17 @@ class bdist_rpm(Command):
self.python = "python3"
elif self.fix_python:
raise DistutilsOptionError(
- "--python and --fix-python are mutually exclusive options")
+ "--python and --fix-python are mutually exclusive options"
+ )
if os.name != 'posix':
- raise DistutilsPlatformError("don't know how to create RPM "
- "distributions on platform %s" % os.name)
+ raise DistutilsPlatformError(
+ "don't know how to create RPM " "distributions on platform %s" % os.name
+ )
if self.binary_only and self.source_only:
raise DistutilsOptionError(
- "cannot supply both '--source-only' and '--binary-only'")
+ "cannot supply both '--source-only' and '--binary-only'"
+ )
# don't pass CFLAGS to pure python distributions
if not self.distribution.has_ext_modules():
@@ -210,9 +231,11 @@ class bdist_rpm(Command):
def finalize_package_data(self):
self.ensure_string('group', "Development/Libraries")
- self.ensure_string('vendor',
- "%s <%s>" % (self.distribution.get_contact(),
- self.distribution.get_contact_email()))
+ self.ensure_string(
+ 'vendor',
+ "%s <%s>"
+ % (self.distribution.get_contact(), self.distribution.get_contact_email()),
+ )
self.ensure_string('packager')
self.ensure_string_list('doc_files')
if isinstance(self.doc_files, list):
@@ -221,12 +244,12 @@ class bdist_rpm(Command):
self.doc_files.append(readme)
self.ensure_string('release', "1")
- self.ensure_string('serial') # should it be an int?
+ self.ensure_string('serial') # should it be an int?
self.ensure_string('distribution_name')
self.ensure_string('changelog')
- # Format changelog correctly
+ # Format changelog correctly
self.changelog = self._format_changelog(self.changelog)
self.ensure_filename('icon')
@@ -253,7 +276,7 @@ class bdist_rpm(Command):
self.ensure_string('force_arch')
- def run(self):
+ def run(self): # noqa: C901
if DEBUG:
print("before _get_package_data():")
print("vendor =", self.vendor)
@@ -274,14 +297,12 @@ class bdist_rpm(Command):
# Spec file goes into 'dist_dir' if '--spec-only specified',
# build/rpm.<plat> otherwise.
- spec_path = os.path.join(spec_dir,
- "%s.spec" % self.distribution.get_name())
- self.execute(write_file,
- (spec_path,
- self._make_spec_file()),
- "writing '%s'" % spec_path)
-
- if self.spec_only: # stop if requested
+ spec_path = os.path.join(spec_dir, "%s.spec" % self.distribution.get_name())
+ self.execute(
+ write_file, (spec_path, self._make_spec_file()), "writing '%s'" % spec_path
+ )
+
+ if self.spec_only: # stop if requested
return
# Make a source distribution and copy to SOURCES directory with
@@ -303,14 +324,13 @@ class bdist_rpm(Command):
if os.path.exists(self.icon):
self.copy_file(self.icon, source_dir)
else:
- raise DistutilsFileError(
- "icon file '%s' does not exist" % self.icon)
+ raise DistutilsFileError("icon file '%s' does not exist" % self.icon)
# build package
log.info("building RPMs")
rpm_cmd = ['rpmbuild']
- if self.source_only: # what kind of RPMs?
+ if self.source_only: # what kind of RPMs?
rpm_cmd.append('-bs')
elif self.binary_only:
rpm_cmd.append('-bb')
@@ -318,8 +338,7 @@ class bdist_rpm(Command):
rpm_cmd.append('-ba')
rpm_cmd.extend(['--define', '__python %s' % self.python])
if self.rpm3_mode:
- rpm_cmd.extend(['--define',
- '_topdir %s' % os.path.abspath(self.rpm_base)])
+ rpm_cmd.extend(['--define', '_topdir %s' % os.path.abspath(self.rpm_base)])
if not self.keep_temp:
rpm_cmd.append('--clean')
@@ -334,8 +353,11 @@ class bdist_rpm(Command):
nvr_string = "%{name}-%{version}-%{release}"
src_rpm = nvr_string + ".src.rpm"
non_src_rpm = "%{arch}/" + nvr_string + ".%{arch}.rpm"
- q_cmd = r"rpm -q --qf '%s %s\n' --specfile '%s'" % (
- src_rpm, non_src_rpm, spec_path)
+ q_cmd = r"rpm -q --qf '{} {}\n' --specfile '{}'".format(
+ src_rpm,
+ non_src_rpm,
+ spec_path,
+ )
out = os.popen(q_cmd)
try:
@@ -345,12 +367,12 @@ class bdist_rpm(Command):
line = out.readline()
if not line:
break
- l = line.strip().split()
- assert(len(l) == 2)
- binary_rpms.append(l[1])
+ ell = line.strip().split()
+ assert len(ell) == 2
+ binary_rpms.append(ell[1])
# The source rpm is named after the first entry in the spec file
if source_rpm is None:
- source_rpm = l[0]
+ source_rpm = ell[0]
status = out.close()
if status:
@@ -369,38 +391,37 @@ class bdist_rpm(Command):
if not self.binary_only:
srpm = os.path.join(rpm_dir['SRPMS'], source_rpm)
- assert(os.path.exists(srpm))
+ assert os.path.exists(srpm)
self.move_file(srpm, self.dist_dir)
filename = os.path.join(self.dist_dir, source_rpm)
- self.distribution.dist_files.append(
- ('bdist_rpm', pyversion, filename))
+ self.distribution.dist_files.append(('bdist_rpm', pyversion, filename))
if not self.source_only:
for rpm in binary_rpms:
rpm = os.path.join(rpm_dir['RPMS'], rpm)
if os.path.exists(rpm):
self.move_file(rpm, self.dist_dir)
- filename = os.path.join(self.dist_dir,
- os.path.basename(rpm))
+ filename = os.path.join(self.dist_dir, os.path.basename(rpm))
self.distribution.dist_files.append(
- ('bdist_rpm', pyversion, filename))
+ ('bdist_rpm', pyversion, filename)
+ )
def _dist_path(self, path):
return os.path.join(self.dist_dir, os.path.basename(path))
- def _make_spec_file(self):
+ def _make_spec_file(self): # noqa: C901
"""Generate the text of an RPM spec file and return it as a
list of strings (one per line).
"""
# definitions and headers
spec_file = [
'%define name ' + self.distribution.get_name(),
- '%define version ' + self.distribution.get_version().replace('-','_'),
+ '%define version ' + self.distribution.get_version().replace('-', '_'),
'%define unmangled_version ' + self.distribution.get_version(),
- '%define release ' + self.release.replace('-','_'),
+ '%define release ' + self.release.replace('-', '_'),
'',
- 'Summary: ' + self.distribution.get_description(),
- ]
+ 'Summary: ' + (self.distribution.get_description() or "UNKNOWN"),
+ ]
# Workaround for #14443 which affects some RPM based systems such as
# RHEL6 (and probably derivatives)
@@ -408,8 +429,9 @@ class bdist_rpm(Command):
# Generate a potential replacement value for __os_install_post (whilst
# normalizing the whitespace to simplify the test for whether the
# invocation of brp-python-bytecompile passes in __python):
- vendor_hook = '\n'.join([' %s \\' % line.strip()
- for line in vendor_hook.splitlines()])
+ vendor_hook = '\n'.join(
+ [' %s \\' % line.strip() for line in vendor_hook.splitlines()]
+ )
problem = "brp-python-bytecompile \\\n"
fixed = "brp-python-bytecompile %{__python} \\\n"
fixed_hook = vendor_hook.replace(problem, fixed)
@@ -420,14 +442,17 @@ class bdist_rpm(Command):
# put locale summaries into spec file
# XXX not supported for now (hard to put a dictionary
# in a config file -- arg!)
- #for locale in self.summaries.keys():
+ # for locale in self.summaries.keys():
# spec_file.append('Summary(%s): %s' % (locale,
# self.summaries[locale]))
- spec_file.extend([
- 'Name: %{name}',
- 'Version: %{version}',
- 'Release: %{release}',])
+ spec_file.extend(
+ [
+ 'Name: %{name}',
+ 'Version: %{version}',
+ 'Release: %{release}',
+ ]
+ )
# XXX yuck! this filename is available from the "sdist" command,
# but only after it has run: and we create the spec file before
@@ -437,42 +462,44 @@ class bdist_rpm(Command):
else:
spec_file.append('Source0: %{name}-%{unmangled_version}.tar.gz')
- spec_file.extend([
- 'License: ' + self.distribution.get_license(),
- 'Group: ' + self.group,
- 'BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot',
- 'Prefix: %{_prefix}', ])
+ spec_file.extend(
+ [
+ 'License: ' + (self.distribution.get_license() or "UNKNOWN"),
+ 'Group: ' + self.group,
+ 'BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot',
+ 'Prefix: %{_prefix}',
+ ]
+ )
if not self.force_arch:
# noarch if no extension modules
if not self.distribution.has_ext_modules():
spec_file.append('BuildArch: noarch')
else:
- spec_file.append( 'BuildArch: %s' % self.force_arch )
-
- for field in ('Vendor',
- 'Packager',
- 'Provides',
- 'Requires',
- 'Conflicts',
- 'Obsoletes',
- ):
+ spec_file.append('BuildArch: %s' % self.force_arch)
+
+ for field in (
+ 'Vendor',
+ 'Packager',
+ 'Provides',
+ 'Requires',
+ 'Conflicts',
+ 'Obsoletes',
+ ):
val = getattr(self, field.lower())
if isinstance(val, list):
- spec_file.append('%s: %s' % (field, ' '.join(val)))
+ spec_file.append('{}: {}'.format(field, ' '.join(val)))
elif val is not None:
- spec_file.append('%s: %s' % (field, val))
-
+ spec_file.append('{}: {}'.format(field, val))
- if self.distribution.get_url() != 'UNKNOWN':
+ if self.distribution.get_url():
spec_file.append('Url: ' + self.distribution.get_url())
if self.distribution_name:
spec_file.append('Distribution: ' + self.distribution_name)
if self.build_requires:
- spec_file.append('BuildRequires: ' +
- ' '.join(self.build_requires))
+ spec_file.append('BuildRequires: ' + ' '.join(self.build_requires))
if self.icon:
spec_file.append('Icon: ' + os.path.basename(self.icon))
@@ -480,16 +507,18 @@ class bdist_rpm(Command):
if self.no_autoreq:
spec_file.append('AutoReq: 0')
- spec_file.extend([
- '',
- '%description',
- self.distribution.get_long_description()
- ])
+ spec_file.extend(
+ [
+ '',
+ '%description',
+ self.distribution.get_long_description() or "",
+ ]
+ )
# put locale descriptions into spec file
# XXX again, suppressed because config file syntax doesn't
# easily support this ;-(
- #for locale in self.descriptions.keys():
+ # for locale in self.descriptions.keys():
# spec_file.extend([
# '',
# '%description -l ' + locale,
@@ -498,7 +527,7 @@ class bdist_rpm(Command):
# rpm scripts
# figure out default build script
- def_setup_call = "%s %s" % (self.python,os.path.basename(sys.argv[0]))
+ def_setup_call = "{} {}".format(self.python, os.path.basename(sys.argv[0]))
def_build = "%s build" % def_setup_call
if self.use_rpm_opt_flags:
def_build = 'env CFLAGS="$RPM_OPT_FLAGS" ' + def_build
@@ -509,8 +538,9 @@ class bdist_rpm(Command):
# that we open and interpolate into the spec file, but the defaults
# are just text that we drop in as-is. Hmmm.
- install_cmd = ('%s install -O1 --root=$RPM_BUILD_ROOT '
- '--record=INSTALLED_FILES') % def_setup_call
+ install_cmd = (
+ '%s install -O1 --root=$RPM_BUILD_ROOT ' '--record=INSTALLED_FILES'
+ ) % def_setup_call
script_options = [
('prep', 'prep_script', "%setup -n %{name}-%{unmangled_version}"),
@@ -529,37 +559,43 @@ class bdist_rpm(Command):
# use 'default' as contents of script
val = getattr(self, attr)
if val or default:
- spec_file.extend([
- '',
- '%' + rpm_opt,])
+ spec_file.extend(
+ [
+ '',
+ '%' + rpm_opt,
+ ]
+ )
if val:
with open(val) as f:
spec_file.extend(f.read().split('\n'))
else:
spec_file.append(default)
-
# files section
- spec_file.extend([
- '',
- '%files -f INSTALLED_FILES',
- '%defattr(-,root,root)',
- ])
+ spec_file.extend(
+ [
+ '',
+ '%files -f INSTALLED_FILES',
+ '%defattr(-,root,root)',
+ ]
+ )
if self.doc_files:
spec_file.append('%doc ' + ' '.join(self.doc_files))
if self.changelog:
- spec_file.extend([
- '',
- '%changelog',])
+ spec_file.extend(
+ [
+ '',
+ '%changelog',
+ ]
+ )
spec_file.extend(self.changelog)
return spec_file
def _format_changelog(self, changelog):
- """Format the changelog correctly and convert it to a list of strings
- """
+ """Format the changelog correctly and convert it to a list of strings"""
if not changelog:
return changelog
new_changelog = []
diff --git a/setuptools/_distutils/command/bdist_wininst.py b/setuptools/_distutils/command/bdist_wininst.py
deleted file mode 100644
index 0e9ddaa2..00000000
--- a/setuptools/_distutils/command/bdist_wininst.py
+++ /dev/null
@@ -1,377 +0,0 @@
-"""distutils.command.bdist_wininst
-
-Implements the Distutils 'bdist_wininst' command: create a windows installer
-exe-program."""
-
-import os
-import sys
-import warnings
-from distutils.core import Command
-from distutils.util import get_platform
-from distutils.dir_util import remove_tree
-from distutils.errors import *
-from distutils.sysconfig import get_python_version
-from distutils import log
-
-class bdist_wininst(Command):
-
- description = "create an executable installer for MS Windows"
-
- user_options = [('bdist-dir=', None,
- "temporary directory for creating the distribution"),
- ('plat-name=', 'p',
- "platform name to embed in generated filenames "
- "(default: %s)" % get_platform()),
- ('keep-temp', 'k',
- "keep the pseudo-installation tree around after " +
- "creating the distribution archive"),
- ('target-version=', None,
- "require a specific python version" +
- " on the target system"),
- ('no-target-compile', 'c',
- "do not compile .py to .pyc on the target system"),
- ('no-target-optimize', 'o',
- "do not compile .py to .pyo (optimized) "
- "on the target system"),
- ('dist-dir=', 'd',
- "directory to put final built distributions in"),
- ('bitmap=', 'b',
- "bitmap to use for the installer instead of python-powered logo"),
- ('title=', 't',
- "title to display on the installer background instead of default"),
- ('skip-build', None,
- "skip rebuilding everything (for testing/debugging)"),
- ('install-script=', None,
- "basename of installation script to be run after "
- "installation or before deinstallation"),
- ('pre-install-script=', None,
- "Fully qualified filename of a script to be run before "
- "any files are installed. This script need not be in the "
- "distribution"),
- ('user-access-control=', None,
- "specify Vista's UAC handling - 'none'/default=no "
- "handling, 'auto'=use UAC if target Python installed for "
- "all users, 'force'=always use UAC"),
- ]
-
- boolean_options = ['keep-temp', 'no-target-compile', 'no-target-optimize',
- 'skip-build']
-
- # bpo-10945: bdist_wininst requires mbcs encoding only available on Windows
- _unsupported = (sys.platform != "win32")
-
- def __init__(self, *args, **kw):
- super().__init__(*args, **kw)
- warnings.warn("bdist_wininst command is deprecated since Python 3.8, "
- "use bdist_wheel (wheel packages) instead",
- DeprecationWarning, 2)
-
- def initialize_options(self):
- self.bdist_dir = None
- self.plat_name = None
- self.keep_temp = 0
- self.no_target_compile = 0
- self.no_target_optimize = 0
- self.target_version = None
- self.dist_dir = None
- self.bitmap = None
- self.title = None
- self.skip_build = None
- self.install_script = None
- self.pre_install_script = None
- self.user_access_control = None
-
-
- def finalize_options(self):
- self.set_undefined_options('bdist', ('skip_build', 'skip_build'))
-
- if self.bdist_dir is None:
- if self.skip_build and self.plat_name:
- # If build is skipped and plat_name is overridden, bdist will
- # not see the correct 'plat_name' - so set that up manually.
- bdist = self.distribution.get_command_obj('bdist')
- bdist.plat_name = self.plat_name
- # next the command will be initialized using that name
- bdist_base = self.get_finalized_command('bdist').bdist_base
- self.bdist_dir = os.path.join(bdist_base, 'wininst')
-
- if not self.target_version:
- self.target_version = ""
-
- if not self.skip_build and self.distribution.has_ext_modules():
- short_version = get_python_version()
- if self.target_version and self.target_version != short_version:
- raise DistutilsOptionError(
- "target version can only be %s, or the '--skip-build'" \
- " option must be specified" % (short_version,))
- self.target_version = short_version
-
- self.set_undefined_options('bdist',
- ('dist_dir', 'dist_dir'),
- ('plat_name', 'plat_name'),
- )
-
- if self.install_script:
- for script in self.distribution.scripts:
- if self.install_script == os.path.basename(script):
- break
- else:
- raise DistutilsOptionError(
- "install_script '%s' not found in scripts"
- % self.install_script)
-
- def run(self):
- if (sys.platform != "win32" and
- (self.distribution.has_ext_modules() or
- self.distribution.has_c_libraries())):
- raise DistutilsPlatformError \
- ("distribution contains extensions and/or C libraries; "
- "must be compiled on a Windows 32 platform")
-
- if not self.skip_build:
- self.run_command('build')
-
- install = self.reinitialize_command('install', reinit_subcommands=1)
- install.root = self.bdist_dir
- install.skip_build = self.skip_build
- install.warn_dir = 0
- install.plat_name = self.plat_name
-
- install_lib = self.reinitialize_command('install_lib')
- # we do not want to include pyc or pyo files
- install_lib.compile = 0
- install_lib.optimize = 0
-
- if self.distribution.has_ext_modules():
- # If we are building an installer for a Python version other
- # than the one we are currently running, then we need to ensure
- # our build_lib reflects the other Python version rather than ours.
- # Note that for target_version!=sys.version, we must have skipped the
- # build step, so there is no issue with enforcing the build of this
- # version.
- target_version = self.target_version
- if not target_version:
- assert self.skip_build, "Should have already checked this"
- target_version = '%d.%d' % sys.version_info[:2]
- plat_specifier = ".%s-%s" % (self.plat_name, target_version)
- build = self.get_finalized_command('build')
- build.build_lib = os.path.join(build.build_base,
- 'lib' + plat_specifier)
-
- # Use a custom scheme for the zip-file, because we have to decide
- # at installation time which scheme to use.
- for key in ('purelib', 'platlib', 'headers', 'scripts', 'data'):
- value = key.upper()
- if key == 'headers':
- value = value + '/Include/$dist_name'
- setattr(install,
- 'install_' + key,
- value)
-
- log.info("installing to %s", self.bdist_dir)
- install.ensure_finalized()
-
- # avoid warning of 'install_lib' about installing
- # into a directory not in sys.path
- sys.path.insert(0, os.path.join(self.bdist_dir, 'PURELIB'))
-
- install.run()
-
- del sys.path[0]
-
- # And make an archive relative to the root of the
- # pseudo-installation tree.
- from tempfile import mktemp
- archive_basename = mktemp()
- fullname = self.distribution.get_fullname()
- arcname = self.make_archive(archive_basename, "zip",
- root_dir=self.bdist_dir)
- # create an exe containing the zip-file
- self.create_exe(arcname, fullname, self.bitmap)
- if self.distribution.has_ext_modules():
- pyversion = get_python_version()
- else:
- pyversion = 'any'
- self.distribution.dist_files.append(('bdist_wininst', pyversion,
- self.get_installer_filename(fullname)))
- # remove the zip-file again
- log.debug("removing temporary file '%s'", arcname)
- os.remove(arcname)
-
- if not self.keep_temp:
- remove_tree(self.bdist_dir, dry_run=self.dry_run)
-
- def get_inidata(self):
- # Return data describing the installation.
- lines = []
- metadata = self.distribution.metadata
-
- # Write the [metadata] section.
- lines.append("[metadata]")
-
- # 'info' will be displayed in the installer's dialog box,
- # describing the items to be installed.
- info = (metadata.long_description or '') + '\n'
-
- # Escape newline characters
- def escape(s):
- return s.replace("\n", "\\n")
-
- for name in ["author", "author_email", "description", "maintainer",
- "maintainer_email", "name", "url", "version"]:
- data = getattr(metadata, name, "")
- if data:
- info = info + ("\n %s: %s" % \
- (name.capitalize(), escape(data)))
- lines.append("%s=%s" % (name, escape(data)))
-
- # The [setup] section contains entries controlling
- # the installer runtime.
- lines.append("\n[Setup]")
- if self.install_script:
- lines.append("install_script=%s" % self.install_script)
- lines.append("info=%s" % escape(info))
- lines.append("target_compile=%d" % (not self.no_target_compile))
- lines.append("target_optimize=%d" % (not self.no_target_optimize))
- if self.target_version:
- lines.append("target_version=%s" % self.target_version)
- if self.user_access_control:
- lines.append("user_access_control=%s" % self.user_access_control)
-
- title = self.title or self.distribution.get_fullname()
- lines.append("title=%s" % escape(title))
- import time
- import distutils
- build_info = "Built %s with distutils-%s" % \
- (time.ctime(time.time()), distutils.__version__)
- lines.append("build_info=%s" % build_info)
- return "\n".join(lines)
-
- def create_exe(self, arcname, fullname, bitmap=None):
- import struct
-
- self.mkpath(self.dist_dir)
-
- cfgdata = self.get_inidata()
-
- installer_name = self.get_installer_filename(fullname)
- self.announce("creating %s" % installer_name)
-
- if bitmap:
- with open(bitmap, "rb") as f:
- bitmapdata = f.read()
- bitmaplen = len(bitmapdata)
- else:
- bitmaplen = 0
-
- with open(installer_name, "wb") as file:
- file.write(self.get_exe_bytes())
- if bitmap:
- file.write(bitmapdata)
-
- # Convert cfgdata from unicode to ascii, mbcs encoded
- if isinstance(cfgdata, str):
- cfgdata = cfgdata.encode("mbcs")
-
- # Append the pre-install script
- cfgdata = cfgdata + b"\0"
- if self.pre_install_script:
- # We need to normalize newlines, so we open in text mode and
- # convert back to bytes. "latin-1" simply avoids any possible
- # failures.
- with open(self.pre_install_script, "r",
- encoding="latin-1") as script:
- script_data = script.read().encode("latin-1")
- cfgdata = cfgdata + script_data + b"\n\0"
- else:
- # empty pre-install script
- cfgdata = cfgdata + b"\0"
- file.write(cfgdata)
-
- # The 'magic number' 0x1234567B is used to make sure that the
- # binary layout of 'cfgdata' is what the wininst.exe binary
- # expects. If the layout changes, increment that number, make
- # the corresponding changes to the wininst.exe sources, and
- # recompile them.
- header = struct.pack("<iii",
- 0x1234567B, # tag
- len(cfgdata), # length
- bitmaplen, # number of bytes in bitmap
- )
- file.write(header)
- with open(arcname, "rb") as f:
- file.write(f.read())
-
- def get_installer_filename(self, fullname):
- # Factored out to allow overriding in subclasses
- if self.target_version:
- # if we create an installer for a specific python version,
- # it's better to include this in the name
- installer_name = os.path.join(self.dist_dir,
- "%s.%s-py%s.exe" %
- (fullname, self.plat_name, self.target_version))
- else:
- installer_name = os.path.join(self.dist_dir,
- "%s.%s.exe" % (fullname, self.plat_name))
- return installer_name
-
- def get_exe_bytes(self):
- # If a target-version other than the current version has been
- # specified, then using the MSVC version from *this* build is no good.
- # Without actually finding and executing the target version and parsing
- # its sys.version, we just hard-code our knowledge of old versions.
- # NOTE: Possible alternative is to allow "--target-version" to
- # specify a Python executable rather than a simple version string.
- # We can then execute this program to obtain any info we need, such
- # as the real sys.version string for the build.
- cur_version = get_python_version()
-
- # If the target version is *later* than us, then we assume they
- # use what we use
- # string compares seem wrong, but are what sysconfig.py itself uses
- if self.target_version and self.target_version < cur_version:
- if self.target_version < "2.4":
- bv = '6.0'
- elif self.target_version == "2.4":
- bv = '7.1'
- elif self.target_version == "2.5":
- bv = '8.0'
- elif self.target_version <= "3.2":
- bv = '9.0'
- elif self.target_version <= "3.4":
- bv = '10.0'
- else:
- bv = '14.0'
- else:
- # for current version - use authoritative check.
- try:
- from msvcrt import CRT_ASSEMBLY_VERSION
- except ImportError:
- # cross-building, so assume the latest version
- bv = '14.0'
- else:
- # as far as we know, CRT is binary compatible based on
- # the first field, so assume 'x.0' until proven otherwise
- major = CRT_ASSEMBLY_VERSION.partition('.')[0]
- bv = major + '.0'
-
-
- # wininst-x.y.exe is in the same directory as this file
- directory = os.path.dirname(__file__)
- # we must use a wininst-x.y.exe built with the same C compiler
- # used for python. XXX What about mingw, borland, and so on?
-
- # if plat_name starts with "win" but is not "win32"
- # we want to strip "win" and leave the rest (e.g. -amd64)
- # for all other cases, we don't want any suffix
- if self.plat_name != 'win32' and self.plat_name[:3] == 'win':
- sfix = self.plat_name[3:]
- else:
- sfix = ''
-
- filename = os.path.join(directory, "wininst-%s%s.exe" % (bv, sfix))
- f = open(filename, "rb")
- try:
- return f.read()
- finally:
- f.close()
diff --git a/setuptools/_distutils/command/build.py b/setuptools/_distutils/command/build.py
index 4355a632..c3ab410f 100644
--- a/setuptools/_distutils/command/build.py
+++ b/setuptools/_distutils/command/build.py
@@ -2,14 +2,16 @@
Implements the Distutils 'build' command."""
-import sys, os
-from distutils.core import Command
-from distutils.errors import DistutilsOptionError
-from distutils.util import get_platform
+import sys
+import os
+from ..core import Command
+from ..errors import DistutilsOptionError
+from ..util import get_platform
def show_compilers():
- from distutils.ccompiler import show_compilers
+ from ..ccompiler import show_compilers
+
show_compilers()
@@ -18,40 +20,35 @@ class build(Command):
description = "build everything needed to install"
user_options = [
- ('build-base=', 'b',
- "base directory for build library"),
- ('build-purelib=', None,
- "build directory for platform-neutral distributions"),
- ('build-platlib=', None,
- "build directory for platform-specific distributions"),
- ('build-lib=', None,
- "build directory for all distribution (defaults to either " +
- "build-purelib or build-platlib"),
- ('build-scripts=', None,
- "build directory for scripts"),
- ('build-temp=', 't',
- "temporary build directory"),
- ('plat-name=', 'p',
- "platform name to build for, if supported "
- "(default: %s)" % get_platform()),
- ('compiler=', 'c',
- "specify the compiler type"),
- ('parallel=', 'j',
- "number of parallel build jobs"),
- ('debug', 'g',
- "compile extensions and libraries with debugging information"),
- ('force', 'f',
- "forcibly build everything (ignore file timestamps)"),
- ('executable=', 'e',
- "specify final destination interpreter path (build.py)"),
- ]
+ ('build-base=', 'b', "base directory for build library"),
+ ('build-purelib=', None, "build directory for platform-neutral distributions"),
+ ('build-platlib=', None, "build directory for platform-specific distributions"),
+ (
+ 'build-lib=',
+ None,
+ "build directory for all distribution (defaults to either "
+ + "build-purelib or build-platlib",
+ ),
+ ('build-scripts=', None, "build directory for scripts"),
+ ('build-temp=', 't', "temporary build directory"),
+ (
+ 'plat-name=',
+ 'p',
+ "platform name to build for, if supported "
+ "(default: %s)" % get_platform(),
+ ),
+ ('compiler=', 'c', "specify the compiler type"),
+ ('parallel=', 'j', "number of parallel build jobs"),
+ ('debug', 'g', "compile extensions and libraries with debugging information"),
+ ('force', 'f', "forcibly build everything (ignore file timestamps)"),
+ ('executable=', 'e', "specify final destination interpreter path (build.py)"),
+ ]
boolean_options = ['debug', 'force']
help_options = [
- ('help-compiler', None,
- "list available compilers", show_compilers),
- ]
+ ('help-compiler', None, "list available compilers", show_compilers),
+ ]
def initialize_options(self):
self.build_base = 'build'
@@ -69,7 +66,7 @@ class build(Command):
self.executable = None
self.parallel = None
- def finalize_options(self):
+ def finalize_options(self): # noqa: C901
if self.plat_name is None:
self.plat_name = get_platform()
else:
@@ -78,10 +75,11 @@ class build(Command):
# other platforms.
if os.name != 'nt':
raise DistutilsOptionError(
- "--plat-name only supported on Windows (try "
- "using './configure --help' on your platform)")
+ "--plat-name only supported on Windows (try "
+ "using './configure --help' on your platform)"
+ )
- plat_specifier = ".%s-%d.%d" % (self.plat_name, *sys.version_info[:2])
+ plat_specifier = ".{}-{}".format(self.plat_name, sys.implementation.cache_tag)
# Make it so Python 2.x and Python 2.x with --with-pydebug don't
# share the same build directories. Doing so confuses the build
@@ -95,8 +93,7 @@ class build(Command):
if self.build_purelib is None:
self.build_purelib = os.path.join(self.build_base, 'lib')
if self.build_platlib is None:
- self.build_platlib = os.path.join(self.build_base,
- 'lib' + plat_specifier)
+ self.build_platlib = os.path.join(self.build_base, 'lib' + plat_specifier)
# 'build_lib' is the actual directory that we will use for this
# particular module distribution -- if user didn't supply it, pick
@@ -110,11 +107,11 @@ class build(Command):
# 'build_temp' -- temporary directory for compiler turds,
# "build/temp.<plat>"
if self.build_temp is None:
- self.build_temp = os.path.join(self.build_base,
- 'temp' + plat_specifier)
+ self.build_temp = os.path.join(self.build_base, 'temp' + plat_specifier)
if self.build_scripts is None:
- self.build_scripts = os.path.join(self.build_base,
- 'scripts-%d.%d' % sys.version_info[:2])
+ self.build_scripts = os.path.join(
+ self.build_base, 'scripts-%d.%d' % sys.version_info[:2]
+ )
if self.executable is None and sys.executable:
self.executable = os.path.normpath(sys.executable)
@@ -134,7 +131,6 @@ class build(Command):
for cmd_name in self.get_sub_commands():
self.run_command(cmd_name)
-
# -- Predicates for the sub-command list ---------------------------
def has_pure_modules(self):
@@ -149,9 +145,9 @@ class build(Command):
def has_scripts(self):
return self.distribution.has_scripts()
-
- sub_commands = [('build_py', has_pure_modules),
- ('build_clib', has_c_libraries),
- ('build_ext', has_ext_modules),
- ('build_scripts', has_scripts),
- ]
+ sub_commands = [
+ ('build_py', has_pure_modules),
+ ('build_clib', has_c_libraries),
+ ('build_ext', has_ext_modules),
+ ('build_scripts', has_scripts),
+ ]
diff --git a/setuptools/_distutils/command/build_clib.py b/setuptools/_distutils/command/build_clib.py
index 3e20ef23..f90c5664 100644
--- a/setuptools/_distutils/command/build_clib.py
+++ b/setuptools/_distutils/command/build_clib.py
@@ -15,13 +15,15 @@ module."""
# cut 'n paste. Sigh.
import os
-from distutils.core import Command
-from distutils.errors import *
-from distutils.sysconfig import customize_compiler
-from distutils import log
+from ..core import Command
+from ..errors import DistutilsSetupError
+from ..sysconfig import customize_compiler
+from distutils._log import log
+
def show_compilers():
- from distutils.ccompiler import show_compilers
+ from ..ccompiler import show_compilers
+
show_compilers()
@@ -30,24 +32,18 @@ class build_clib(Command):
description = "build C/C++ libraries used by Python extensions"
user_options = [
- ('build-clib=', 'b',
- "directory to build C/C++ libraries to"),
- ('build-temp=', 't',
- "directory to put temporary build by-products"),
- ('debug', 'g',
- "compile with debugging information"),
- ('force', 'f',
- "forcibly build everything (ignore file timestamps)"),
- ('compiler=', 'c',
- "specify the compiler type"),
- ]
+ ('build-clib=', 'b', "directory to build C/C++ libraries to"),
+ ('build-temp=', 't', "directory to put temporary build by-products"),
+ ('debug', 'g', "compile with debugging information"),
+ ('force', 'f', "forcibly build everything (ignore file timestamps)"),
+ ('compiler=', 'c', "specify the compiler type"),
+ ]
boolean_options = ['debug', 'force']
help_options = [
- ('help-compiler', None,
- "list available compilers", show_compilers),
- ]
+ ('help-compiler', None, "list available compilers", show_compilers),
+ ]
def initialize_options(self):
self.build_clib = None
@@ -64,19 +60,20 @@ class build_clib(Command):
self.force = 0
self.compiler = None
-
def finalize_options(self):
# This might be confusing: both build-clib and build-temp default
# to build-temp as defined by the "build" command. This is because
# I think that C libraries are really just temporary build
# by-products, at least from the point of view of building Python
# extensions -- but I want to keep my options open.
- self.set_undefined_options('build',
- ('build_temp', 'build_clib'),
- ('build_temp', 'build_temp'),
- ('compiler', 'compiler'),
- ('debug', 'debug'),
- ('force', 'force'))
+ self.set_undefined_options(
+ 'build',
+ ('build_temp', 'build_clib'),
+ ('build_temp', 'build_temp'),
+ ('compiler', 'compiler'),
+ ('debug', 'debug'),
+ ('force', 'force'),
+ )
self.libraries = self.distribution.libraries
if self.libraries:
@@ -90,23 +87,23 @@ class build_clib(Command):
# XXX same as for build_ext -- what about 'self.define' and
# 'self.undef' ?
-
def run(self):
if not self.libraries:
return
# Yech -- this is cut 'n pasted from build_ext.py!
- from distutils.ccompiler import new_compiler
- self.compiler = new_compiler(compiler=self.compiler,
- dry_run=self.dry_run,
- force=self.force)
+ from ..ccompiler import new_compiler
+
+ self.compiler = new_compiler(
+ compiler=self.compiler, dry_run=self.dry_run, force=self.force
+ )
customize_compiler(self.compiler)
if self.include_dirs is not None:
self.compiler.set_include_dirs(self.include_dirs)
if self.define is not None:
# 'define' option is a list of (name,value) tuples
- for (name,value) in self.define:
+ for (name, value) in self.define:
self.compiler.define_macro(name, value)
if self.undef is not None:
for macro in self.undef:
@@ -114,7 +111,6 @@ class build_clib(Command):
self.build_libraries(self.libraries)
-
def check_library_list(self, libraries):
"""Ensure that the list of libraries is valid.
@@ -126,30 +122,31 @@ class build_clib(Command):
just returns otherwise.
"""
if not isinstance(libraries, list):
- raise DistutilsSetupError(
- "'libraries' option must be a list of tuples")
+ raise DistutilsSetupError("'libraries' option must be a list of tuples")
for lib in libraries:
if not isinstance(lib, tuple) and len(lib) != 2:
- raise DistutilsSetupError(
- "each element of 'libraries' must a 2-tuple")
+ raise DistutilsSetupError("each element of 'libraries' must a 2-tuple")
name, build_info = lib
if not isinstance(name, str):
raise DistutilsSetupError(
- "first element of each tuple in 'libraries' "
- "must be a string (the library name)")
+ "first element of each tuple in 'libraries' "
+ "must be a string (the library name)"
+ )
if '/' in name or (os.sep != '/' and os.sep in name):
- raise DistutilsSetupError("bad library name '%s': "
- "may not contain directory separators" % lib[0])
+ raise DistutilsSetupError(
+ "bad library name '%s': "
+ "may not contain directory separators" % lib[0]
+ )
if not isinstance(build_info, dict):
raise DistutilsSetupError(
- "second element of each tuple in 'libraries' "
- "must be a dictionary (build info)")
-
+ "second element of each tuple in 'libraries' "
+ "must be a dictionary (build info)"
+ )
def get_library_names(self):
# Assume the library list is valid -- 'check_library_list()' is
@@ -162,7 +159,6 @@ class build_clib(Command):
lib_names.append(lib_name)
return lib_names
-
def get_source_files(self):
self.check_library_list(self.libraries)
filenames = []
@@ -170,22 +166,23 @@ class build_clib(Command):
sources = build_info.get('sources')
if sources is None or not isinstance(sources, (list, tuple)):
raise DistutilsSetupError(
- "in 'libraries' option (library '%s'), "
- "'sources' must be present and must be "
- "a list of source filenames" % lib_name)
+ "in 'libraries' option (library '%s'), "
+ "'sources' must be present and must be "
+ "a list of source filenames" % lib_name
+ )
filenames.extend(sources)
return filenames
-
def build_libraries(self, libraries):
for (lib_name, build_info) in libraries:
sources = build_info.get('sources')
if sources is None or not isinstance(sources, (list, tuple)):
raise DistutilsSetupError(
- "in 'libraries' option (library '%s'), "
- "'sources' must be present and must be "
- "a list of source filenames" % lib_name)
+ "in 'libraries' option (library '%s'), "
+ "'sources' must be present and must be "
+ "a list of source filenames" % lib_name
+ )
sources = list(sources)
log.info("building '%s' library", lib_name)
@@ -195,15 +192,17 @@ class build_clib(Command):
# files in a temporary build directory.)
macros = build_info.get('macros')
include_dirs = build_info.get('include_dirs')
- objects = self.compiler.compile(sources,
- output_dir=self.build_temp,
- macros=macros,
- include_dirs=include_dirs,
- debug=self.debug)
+ objects = self.compiler.compile(
+ sources,
+ output_dir=self.build_temp,
+ macros=macros,
+ include_dirs=include_dirs,
+ debug=self.debug,
+ )
# Now "link" the object files together into a static library.
# (On Unix at least, this isn't really linking -- it just
# builds an archive. Whatever.)
- self.compiler.create_static_lib(objects, lib_name,
- output_dir=self.build_clib,
- debug=self.debug)
+ self.compiler.create_static_lib(
+ objects, lib_name, output_dir=self.build_clib, debug=self.debug
+ )
diff --git a/setuptools/_distutils/command/build_ext.py b/setuptools/_distutils/command/build_ext.py
index 181671bf..f4c0eccd 100644
--- a/setuptools/_distutils/command/build_ext.py
+++ b/setuptools/_distutils/command/build_ext.py
@@ -8,26 +8,33 @@ import contextlib
import os
import re
import sys
-from distutils.core import Command
-from distutils.errors import *
-from distutils.sysconfig import customize_compiler, get_python_version
-from distutils.sysconfig import get_config_h_filename
-from distutils.dep_util import newer_group
-from distutils.extension import Extension
-from distutils.util import get_platform
-from distutils import log
+from ..core import Command
+from ..errors import (
+ DistutilsOptionError,
+ DistutilsSetupError,
+ CCompilerError,
+ DistutilsError,
+ CompileError,
+ DistutilsPlatformError,
+)
+from ..sysconfig import customize_compiler, get_python_version
+from ..sysconfig import get_config_h_filename
+from ..dep_util import newer_group
+from ..extension import Extension
+from ..util import get_platform
+from distutils._log import log
from . import py37compat
from site import USER_BASE
# An extension name is just a dot-separated list of Python NAMEs (ie.
# the same as a fully-qualified module name).
-extension_name_re = re.compile \
- (r'^[a-zA-Z_][a-zA-Z_0-9]*(\.[a-zA-Z_][a-zA-Z_0-9]*)*$')
+extension_name_re = re.compile(r'^[a-zA-Z_][a-zA-Z_0-9]*(\.[a-zA-Z_][a-zA-Z_0-9]*)*$')
-def show_compilers ():
- from distutils.ccompiler import show_compilers
+def show_compilers():
+ from ..ccompiler import show_compilers
+
show_compilers()
@@ -55,54 +62,50 @@ class build_ext(Command):
sep_by = " (separated by '%s')" % os.pathsep
user_options = [
- ('build-lib=', 'b',
- "directory for compiled extension modules"),
- ('build-temp=', 't',
- "directory for temporary files (build by-products)"),
- ('plat-name=', 'p',
- "platform name to cross-compile for, if supported "
- "(default: %s)" % get_platform()),
- ('inplace', 'i',
- "ignore build-lib and put compiled extensions into the source " +
- "directory alongside your pure Python modules"),
- ('include-dirs=', 'I',
- "list of directories to search for header files" + sep_by),
- ('define=', 'D',
- "C preprocessor macros to define"),
- ('undef=', 'U',
- "C preprocessor macros to undefine"),
- ('libraries=', 'l',
- "external C libraries to link with"),
- ('library-dirs=', 'L',
- "directories to search for external C libraries" + sep_by),
- ('rpath=', 'R',
- "directories to search for shared C libraries at runtime"),
- ('link-objects=', 'O',
- "extra explicit link objects to include in the link"),
- ('debug', 'g',
- "compile/link with debugging information"),
- ('force', 'f',
- "forcibly build everything (ignore file timestamps)"),
- ('compiler=', 'c',
- "specify the compiler type"),
- ('parallel=', 'j',
- "number of parallel build jobs"),
- ('swig-cpp', None,
- "make SWIG create C++ files (default is C)"),
- ('swig-opts=', None,
- "list of SWIG command line options"),
- ('swig=', None,
- "path to the SWIG executable"),
- ('user', None,
- "add user include, library and rpath")
- ]
+ ('build-lib=', 'b', "directory for compiled extension modules"),
+ ('build-temp=', 't', "directory for temporary files (build by-products)"),
+ (
+ 'plat-name=',
+ 'p',
+ "platform name to cross-compile for, if supported "
+ "(default: %s)" % get_platform(),
+ ),
+ (
+ 'inplace',
+ 'i',
+ "ignore build-lib and put compiled extensions into the source "
+ + "directory alongside your pure Python modules",
+ ),
+ (
+ 'include-dirs=',
+ 'I',
+ "list of directories to search for header files" + sep_by,
+ ),
+ ('define=', 'D', "C preprocessor macros to define"),
+ ('undef=', 'U', "C preprocessor macros to undefine"),
+ ('libraries=', 'l', "external C libraries to link with"),
+ (
+ 'library-dirs=',
+ 'L',
+ "directories to search for external C libraries" + sep_by,
+ ),
+ ('rpath=', 'R', "directories to search for shared C libraries at runtime"),
+ ('link-objects=', 'O', "extra explicit link objects to include in the link"),
+ ('debug', 'g', "compile/link with debugging information"),
+ ('force', 'f', "forcibly build everything (ignore file timestamps)"),
+ ('compiler=', 'c', "specify the compiler type"),
+ ('parallel=', 'j', "number of parallel build jobs"),
+ ('swig-cpp', None, "make SWIG create C++ files (default is C)"),
+ ('swig-opts=', None, "list of SWIG command line options"),
+ ('swig=', None, "path to the SWIG executable"),
+ ('user', None, "add user include, library and rpath"),
+ ]
boolean_options = ['inplace', 'debug', 'force', 'swig-cpp', 'user']
help_options = [
- ('help-compiler', None,
- "list available compilers", show_compilers),
- ]
+ ('help-compiler', None, "list available compilers", show_compilers),
+ ]
def initialize_options(self):
self.extensions = None
@@ -128,18 +131,19 @@ class build_ext(Command):
self.user = None
self.parallel = None
- def finalize_options(self):
+ def finalize_options(self): # noqa: C901
from distutils import sysconfig
- self.set_undefined_options('build',
- ('build_lib', 'build_lib'),
- ('build_temp', 'build_temp'),
- ('compiler', 'compiler'),
- ('debug', 'debug'),
- ('force', 'force'),
- ('parallel', 'parallel'),
- ('plat_name', 'plat_name'),
- )
+ self.set_undefined_options(
+ 'build',
+ ('build_lib', 'build_lib'),
+ ('build_temp', 'build_temp'),
+ ('compiler', 'compiler'),
+ ('debug', 'debug'),
+ ('force', 'force'),
+ ('parallel', 'parallel'),
+ ('plat_name', 'plat_name'),
+ )
if self.package is None:
self.package = self.distribution.ext_package
@@ -164,8 +168,7 @@ class build_ext(Command):
# any local include dirs take precedence.
self.include_dirs.extend(py_include.split(os.path.pathsep))
if plat_py_include != py_include:
- self.include_dirs.extend(
- plat_py_include.split(os.path.pathsep))
+ self.include_dirs.extend(plat_py_include.split(os.path.pathsep))
self.ensure_string_list('libraries')
self.ensure_string_list('link_objects')
@@ -220,9 +223,11 @@ class build_ext(Command):
if sys.platform[:6] == 'cygwin':
if not sysconfig.python_build:
# building third party extensions
- self.library_dirs.append(os.path.join(sys.prefix, "lib",
- "python" + get_python_version(),
- "config"))
+ self.library_dirs.append(
+ os.path.join(
+ sys.prefix, "lib", "python" + get_python_version(), "config"
+ )
+ )
else:
# building python standard extensions
self.library_dirs.append('.')
@@ -230,7 +235,7 @@ class build_ext(Command):
# For building extensions with a shared Python library,
# Python's library directory must be appended to library_dirs
# See Issues: #1600860, #4366
- if (sysconfig.get_config_var('Py_ENABLE_SHARED')):
+ if sysconfig.get_config_var('Py_ENABLE_SHARED'):
if not sysconfig.python_build:
# building third party extensions
self.library_dirs.append(sysconfig.get_config_var('LIBDIR'))
@@ -274,8 +279,8 @@ class build_ext(Command):
except ValueError:
raise DistutilsOptionError("parallel should be an integer")
- def run(self):
- from distutils.ccompiler import new_compiler
+ def run(self): # noqa: C901
+ from ..ccompiler import new_compiler
# 'self.extensions', as supplied by setup.py, is a list of
# Extension instances. See the documentation for Extension (in
@@ -302,10 +307,12 @@ class build_ext(Command):
# Setup the CCompiler object that we'll use to do all the
# compiling and linking
- self.compiler = new_compiler(compiler=self.compiler,
- verbose=self.verbose,
- dry_run=self.dry_run,
- force=self.force)
+ self.compiler = new_compiler(
+ compiler=self.compiler,
+ verbose=self.verbose,
+ dry_run=self.dry_run,
+ force=self.force,
+ )
customize_compiler(self.compiler)
# If we are cross-compiling, init the compiler now (if we are not
# cross-compiling, init would not hurt, but people may rely on
@@ -338,7 +345,7 @@ class build_ext(Command):
# Now actually compile and link everything.
self.build_extensions()
- def check_extensions_list(self, extensions):
+ def check_extensions_list(self, extensions): # noqa: C901
"""Ensure that the list of extensions (presumably provided as a
command option 'extensions') is valid, i.e. it is a list of
Extension objects. We also support the old-style list of 2-tuples,
@@ -350,34 +357,40 @@ class build_ext(Command):
"""
if not isinstance(extensions, list):
raise DistutilsSetupError(
- "'ext_modules' option must be a list of Extension instances")
+ "'ext_modules' option must be a list of Extension instances"
+ )
for i, ext in enumerate(extensions):
if isinstance(ext, Extension):
- continue # OK! (assume type-checking done
- # by Extension constructor)
+ continue # OK! (assume type-checking done
+ # by Extension constructor)
if not isinstance(ext, tuple) or len(ext) != 2:
raise DistutilsSetupError(
- "each element of 'ext_modules' option must be an "
- "Extension instance or 2-tuple")
+ "each element of 'ext_modules' option must be an "
+ "Extension instance or 2-tuple"
+ )
ext_name, build_info = ext
- log.warn("old-style (ext_name, build_info) tuple found in "
- "ext_modules for extension '%s' "
- "-- please convert to Extension instance", ext_name)
+ log.warning(
+ "old-style (ext_name, build_info) tuple found in "
+ "ext_modules for extension '%s' "
+ "-- please convert to Extension instance",
+ ext_name,
+ )
- if not (isinstance(ext_name, str) and
- extension_name_re.match(ext_name)):
+ if not (isinstance(ext_name, str) and extension_name_re.match(ext_name)):
raise DistutilsSetupError(
- "first element of each tuple in 'ext_modules' "
- "must be the extension name (a string)")
+ "first element of each tuple in 'ext_modules' "
+ "must be the extension name (a string)"
+ )
if not isinstance(build_info, dict):
raise DistutilsSetupError(
- "second element of each tuple in 'ext_modules' "
- "must be a dictionary (build info)")
+ "second element of each tuple in 'ext_modules' "
+ "must be a dictionary (build info)"
+ )
# OK, the (ext_name, build_info) dict is type-safe: convert it
# to an Extension instance.
@@ -385,9 +398,14 @@ class build_ext(Command):
# Easy stuff: one-to-one mapping from dict elements to
# instance attributes.
- for key in ('include_dirs', 'library_dirs', 'libraries',
- 'extra_objects', 'extra_compile_args',
- 'extra_link_args'):
+ for key in (
+ 'include_dirs',
+ 'library_dirs',
+ 'libraries',
+ 'extra_objects',
+ 'extra_compile_args',
+ 'extra_link_args',
+ ):
val = build_info.get(key)
if val is not None:
setattr(ext, key, val)
@@ -395,8 +413,9 @@ class build_ext(Command):
# Medium-easy stuff: same syntax/semantics, different names.
ext.runtime_library_dirs = build_info.get('rpath')
if 'def_file' in build_info:
- log.warn("'def_file' element of build info dict "
- "no longer supported")
+ log.warning(
+ "'def_file' element of build info dict " "no longer supported"
+ )
# Non-trivial stuff: 'macros' split into 'define_macros'
# and 'undef_macros'.
@@ -407,8 +426,9 @@ class build_ext(Command):
for macro in macros:
if not (isinstance(macro, tuple) and len(macro) in (1, 2)):
raise DistutilsSetupError(
- "'macros' element of build info dict "
- "must be 1- or 2-tuple")
+ "'macros' element of build info dict "
+ "must be 1- or 2-tuple"
+ )
if len(macro) == 1:
ext.undef_macros.append(macro[0])
elif len(macro) == 2:
@@ -461,8 +481,9 @@ class build_ext(Command):
return
with ThreadPoolExecutor(max_workers=workers) as executor:
- futures = [executor.submit(self.build_extension, ext)
- for ext in self.extensions]
+ futures = [
+ executor.submit(self.build_extension, ext) for ext in self.extensions
+ ]
for ext, fut in zip(self.extensions, futures):
with self._filter_build_errors(ext):
fut.result()
@@ -479,16 +500,16 @@ class build_ext(Command):
except (CCompilerError, DistutilsError, CompileError) as e:
if not ext.optional:
raise
- self.warn('building extension "%s" failed: %s' %
- (ext.name, e))
+ self.warn('building extension "{}" failed: {}'.format(ext.name, e))
def build_extension(self, ext):
sources = ext.sources
if sources is None or not isinstance(sources, (list, tuple)):
raise DistutilsSetupError(
- "in 'ext_modules' option (extension '%s'), "
- "'sources' must be present and must be "
- "a list of source filenames" % ext.name)
+ "in 'ext_modules' option (extension '%s'), "
+ "'sources' must be present and must be "
+ "a list of source filenames" % ext.name
+ )
# sort to make the resulting .so file build reproducible
sources = sorted(sources)
@@ -525,13 +546,15 @@ class build_ext(Command):
for undef in ext.undef_macros:
macros.append((undef,))
- objects = self.compiler.compile(sources,
- output_dir=self.build_temp,
- macros=macros,
- include_dirs=ext.include_dirs,
- debug=self.debug,
- extra_postargs=extra_args,
- depends=ext.depends)
+ objects = self.compiler.compile(
+ sources,
+ output_dir=self.build_temp,
+ macros=macros,
+ include_dirs=ext.include_dirs,
+ debug=self.debug,
+ extra_postargs=extra_args,
+ depends=ext.depends,
+ )
# XXX outdated variable, kept here in case third-part code
# needs it.
@@ -548,7 +571,8 @@ class build_ext(Command):
language = ext.language or self.compiler.detect_language(sources)
self.compiler.link_shared_object(
- objects, ext_path,
+ objects,
+ ext_path,
libraries=self.get_libraries(ext),
library_dirs=ext.library_dirs,
runtime_library_dirs=ext.runtime_library_dirs,
@@ -556,7 +580,8 @@ class build_ext(Command):
export_symbols=self.get_export_symbols(ext),
debug=self.debug,
build_temp=self.build_temp,
- target_lang=language)
+ target_lang=language,
+ )
def swig_sources(self, sources, extension):
"""Walk the list of source files in 'sources', looking for SWIG
@@ -574,17 +599,20 @@ class build_ext(Command):
# the temp dir.
if self.swig_cpp:
- log.warn("--swig-cpp is deprecated - use --swig-opts=-c++")
+ log.warning("--swig-cpp is deprecated - use --swig-opts=-c++")
- if self.swig_cpp or ('-c++' in self.swig_opts) or \
- ('-c++' in extension.swig_opts):
+ if (
+ self.swig_cpp
+ or ('-c++' in self.swig_opts)
+ or ('-c++' in extension.swig_opts)
+ ):
target_ext = '.cpp'
else:
target_ext = '.c'
for source in sources:
(base, ext) = os.path.splitext(source)
- if ext == ".i": # SWIG interface file
+ if ext == ".i": # SWIG interface file
new_sources.append(base + '_wrap' + target_ext)
swig_sources.append(source)
swig_targets[source] = new_sources[-1]
@@ -631,8 +659,9 @@ class build_ext(Command):
return "swig.exe"
else:
raise DistutilsPlatformError(
- "I don't know how to find (much less run) SWIG "
- "on platform '%s'" % os.name)
+ "I don't know how to find (much less run) SWIG "
+ "on platform '%s'" % os.name
+ )
# -- Name generators -----------------------------------------------
# (extension names, filenames, whatever)
@@ -650,7 +679,7 @@ class build_ext(Command):
# no further work needed
# returning :
# build_dir/package/path/filename
- filename = os.path.join(*modpath[:-1]+[filename])
+ filename = os.path.join(*modpath[:-1] + [filename])
return os.path.join(self.build_lib, filename)
# the inplace option requires to find the package directory
@@ -677,7 +706,8 @@ class build_ext(Command):
of the file from which it will be loaded (eg. "foo/bar.so", or
"foo\bar.pyd").
"""
- from distutils.sysconfig import get_config_var
+ from ..sysconfig import get_config_var
+
ext_path = ext_name.split('.')
ext_suffix = get_config_var('EXT_SUFFIX')
return os.path.join(*ext_path) + ext_suffix
@@ -703,7 +733,7 @@ class build_ext(Command):
ext.export_symbols.append(initfunc_name)
return ext.export_symbols
- def get_libraries(self, ext):
+ def get_libraries(self, ext): # noqa: C901
"""Return the list of libraries to link against when building a
shared extension. On most platforms, this is just 'ext.libraries';
on Windows, we add the Python library (eg. python20.dll).
@@ -714,13 +744,16 @@ class build_ext(Command):
# to need it mentioned explicitly, though, so that's what we do.
# Append '_d' to the python import library on debug builds.
if sys.platform == "win32":
- from distutils._msvccompiler import MSVCCompiler
+ from .._msvccompiler import MSVCCompiler
+
if not isinstance(self.compiler, MSVCCompiler):
template = "python%d%d"
if self.debug:
template = template + '_d'
- pythonlib = (template %
- (sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff))
+ pythonlib = template % (
+ sys.hexversion >> 24,
+ (sys.hexversion >> 16) & 0xFF,
+ )
# don't extend ext.libraries, it may be shared with other
# extensions, it is a reference to the original list
return ext.libraries + [pythonlib]
@@ -733,7 +766,8 @@ class build_ext(Command):
# On Cygwin (and if required, other POSIX-like platforms based on
# Windows like MinGW) it is simply necessary that all symbols in
# shared libraries are resolved at link time.
- from distutils.sysconfig import get_config_var
+ from ..sysconfig import get_config_var
+
link_libpython = False
if get_config_var('Py_ENABLE_SHARED'):
# A native build on an Android device or on Cygwin
diff --git a/setuptools/_distutils/command/build_py.py b/setuptools/_distutils/command/build_py.py
index 7ef9bcef..9f783244 100644
--- a/setuptools/_distutils/command/build_py.py
+++ b/setuptools/_distutils/command/build_py.py
@@ -7,12 +7,13 @@ import importlib.util
import sys
import glob
-from distutils.core import Command
-from distutils.errors import *
-from distutils.util import convert_path
-from distutils import log
+from ..core import Command
+from ..errors import DistutilsOptionError, DistutilsFileError
+from ..util import convert_path
+from distutils._log import log
-class build_py (Command):
+
+class build_py(Command):
description = "\"build\" pure Python modules (copy to build directory)"
@@ -20,14 +21,17 @@ class build_py (Command):
('build-lib=', 'd', "directory to \"build\" (copy) to"),
('compile', 'c', "compile .py to .pyc"),
('no-compile', None, "don't compile .py files [default]"),
- ('optimize=', 'O',
- "also compile with optimization: -O1 for \"python -O\", "
- "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"),
+ (
+ 'optimize=',
+ 'O',
+ "also compile with optimization: -O1 for \"python -O\", "
+ "-O2 for \"python -OO\", and -O0 to disable [default: -O0]",
+ ),
('force', 'f', "forcibly build everything (ignore file timestamps)"),
- ]
+ ]
boolean_options = ['compile', 'force']
- negative_opt = {'no-compile' : 'compile'}
+ negative_opt = {'no-compile': 'compile'}
def initialize_options(self):
self.build_lib = None
@@ -40,9 +44,9 @@ class build_py (Command):
self.force = None
def finalize_options(self):
- self.set_undefined_options('build',
- ('build_lib', 'build_lib'),
- ('force', 'force'))
+ self.set_undefined_options(
+ 'build', ('build_lib', 'build_lib'), ('force', 'force')
+ )
# Get the distribution options that are aliases for build_py
# options -- list of packages and list of modules.
@@ -109,42 +113,42 @@ class build_py (Command):
# Length of path to strip from found files
plen = 0
if src_dir:
- plen = len(src_dir)+1
+ plen = len(src_dir) + 1
# Strip directory from globbed filenames
- filenames = [
- file[plen:] for file in self.find_data_files(package, src_dir)
- ]
+ filenames = [file[plen:] for file in self.find_data_files(package, src_dir)]
data.append((package, src_dir, build_dir, filenames))
return data
def find_data_files(self, package, src_dir):
"""Return filenames for package's data files in 'src_dir'"""
- globs = (self.package_data.get('', [])
- + self.package_data.get(package, []))
+ globs = self.package_data.get('', []) + self.package_data.get(package, [])
files = []
for pattern in globs:
# Each pattern has to be converted to a platform-specific path
- filelist = glob.glob(os.path.join(glob.escape(src_dir), convert_path(pattern)))
+ filelist = glob.glob(
+ os.path.join(glob.escape(src_dir), convert_path(pattern))
+ )
# Files that match more than one pattern are only added once
- files.extend([fn for fn in filelist if fn not in files
- and os.path.isfile(fn)])
+ files.extend(
+ [fn for fn in filelist if fn not in files and os.path.isfile(fn)]
+ )
return files
def build_package_data(self):
"""Copy data files into build directory"""
- lastdir = None
for package, src_dir, build_dir, filenames in self.data_files:
for filename in filenames:
target = os.path.join(build_dir, filename)
self.mkpath(os.path.dirname(target))
- self.copy_file(os.path.join(src_dir, filename), target,
- preserve_mode=False)
+ self.copy_file(
+ os.path.join(src_dir, filename), target, preserve_mode=False
+ )
def get_package_dir(self, package):
"""Return the directory, relative to the top of the source
- distribution, where package 'package' should be found
- (at least according to the 'package_dir' option, if any)."""
+ distribution, where package 'package' should be found
+ (at least according to the 'package_dir' option, if any)."""
path = package.split('.')
if not self.package_dir:
@@ -188,20 +192,19 @@ class build_py (Command):
if package_dir != "":
if not os.path.exists(package_dir):
raise DistutilsFileError(
- "package directory '%s' does not exist" % package_dir)
+ "package directory '%s' does not exist" % package_dir
+ )
if not os.path.isdir(package_dir):
raise DistutilsFileError(
- "supposed package directory '%s' exists, "
- "but is not a directory" % package_dir)
+ "supposed package directory '%s' exists, "
+ "but is not a directory" % package_dir
+ )
- # Require __init__.py for all but the "root package"
+ # Directories without __init__.py are namespace packages (PEP 420).
if package:
init_py = os.path.join(package_dir, "__init__.py")
if os.path.isfile(init_py):
return init_py
- else:
- log.warn(("package init file '%s' not found " +
- "(or not a regular file)"), init_py)
# Either not in a package at all (__init__.py not expected), or
# __init__.py doesn't exist -- so don't return the filename.
@@ -209,7 +212,7 @@ class build_py (Command):
def check_module(self, module, module_file):
if not os.path.isfile(module_file):
- log.warn("file %s (for module %s) not found", module_file, module)
+ log.warning("file %s (for module %s) not found", module_file, module)
return False
else:
return True
@@ -313,17 +316,21 @@ class build_py (Command):
outputs.append(filename)
if include_bytecode:
if self.compile:
- outputs.append(importlib.util.cache_from_source(
- filename, optimization=''))
+ outputs.append(
+ importlib.util.cache_from_source(filename, optimization='')
+ )
if self.optimize > 0:
- outputs.append(importlib.util.cache_from_source(
- filename, optimization=self.optimize))
+ outputs.append(
+ importlib.util.cache_from_source(
+ filename, optimization=self.optimize
+ )
+ )
outputs += [
os.path.join(build_dir, filename)
for package, src_dir, build_dir, filenames in self.data_files
for filename in filenames
- ]
+ ]
return outputs
@@ -332,7 +339,8 @@ class build_py (Command):
package = package.split('.')
elif not isinstance(package, (list, tuple)):
raise TypeError(
- "'package' must be a string (dot-separated), list, or tuple")
+ "'package' must be a string (dot-separated), list, or tuple"
+ )
# Now put the module source file into the "build" area -- this is
# easy, we just copy it somewhere under self.build_lib (the build
@@ -376,7 +384,8 @@ class build_py (Command):
self.warn('byte-compiling is disabled, skipping.')
return
- from distutils.util import byte_compile
+ from ..util import byte_compile
+
prefix = self.build_lib
if prefix[-1] != os.sep:
prefix = prefix + os.sep
@@ -385,8 +394,14 @@ class build_py (Command):
# method of the "install_lib" command, except for the determination
# of the 'prefix' string. Hmmm.
if self.compile:
- byte_compile(files, optimize=0,
- force=self.force, prefix=prefix, dry_run=self.dry_run)
+ byte_compile(
+ files, optimize=0, force=self.force, prefix=prefix, dry_run=self.dry_run
+ )
if self.optimize > 0:
- byte_compile(files, optimize=self.optimize,
- force=self.force, prefix=prefix, dry_run=self.dry_run)
+ byte_compile(
+ files,
+ optimize=self.optimize,
+ force=self.force,
+ prefix=prefix,
+ dry_run=self.dry_run,
+ )
diff --git a/setuptools/_distutils/command/build_scripts.py b/setuptools/_distutils/command/build_scripts.py
index e3312cf0..87174f6b 100644
--- a/setuptools/_distutils/command/build_scripts.py
+++ b/setuptools/_distutils/command/build_scripts.py
@@ -2,17 +2,24 @@
Implements the Distutils 'build_scripts' command."""
-import os, re
+import os
+import re
from stat import ST_MODE
from distutils import sysconfig
-from distutils.core import Command
-from distutils.dep_util import newer
-from distutils.util import convert_path
-from distutils import log
+from ..core import Command
+from ..dep_util import newer
+from ..util import convert_path
+from distutils._log import log
import tokenize
-# check if Python is called on the first line with this expression
-first_line_re = re.compile(b'^#!.*python[0-9.]*([ \t].*)?$')
+shebang_pattern = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
+"""
+Pattern matching a Python interpreter indicated in first line of a script.
+"""
+
+# for Setuptools compatibility
+first_line_re = shebang_pattern
+
class build_scripts(Command):
@@ -22,23 +29,23 @@ class build_scripts(Command):
('build-dir=', 'd', "directory to \"build\" (copy) to"),
('force', 'f', "forcibly build everything (ignore file timestamps"),
('executable=', 'e', "specify final destination interpreter path"),
- ]
+ ]
boolean_options = ['force']
-
def initialize_options(self):
self.build_dir = None
self.scripts = None
self.force = None
self.executable = None
- self.outfiles = None
def finalize_options(self):
- self.set_undefined_options('build',
- ('build_scripts', 'build_dir'),
- ('force', 'force'),
- ('executable', 'executable'))
+ self.set_undefined_options(
+ 'build',
+ ('build_scripts', 'build_dir'),
+ ('force', 'force'),
+ ('executable', 'executable'),
+ )
self.scripts = self.distribution.scripts
def get_source_files(self):
@@ -49,104 +56,118 @@ class build_scripts(Command):
return
self.copy_scripts()
-
def copy_scripts(self):
- r"""Copy each script listed in 'self.scripts'; if it's marked as a
- Python script in the Unix way (first line matches 'first_line_re',
- ie. starts with "\#!" and contains "python"), then adjust the first
- line to refer to the current Python interpreter as we copy.
+ """
+ Copy each script listed in ``self.scripts``.
+
+ If a script is marked as a Python script (first line matches
+ 'shebang_pattern', i.e. starts with ``#!`` and contains
+ "python"), then adjust in the copy the first line to refer to
+ the current Python interpreter.
"""
self.mkpath(self.build_dir)
outfiles = []
updated_files = []
for script in self.scripts:
- adjust = False
- script = convert_path(script)
- outfile = os.path.join(self.build_dir, os.path.basename(script))
- outfiles.append(outfile)
-
- if not self.force and not newer(script, outfile):
- log.debug("not copying %s (up-to-date)", script)
- continue
-
- # Always open the file, but ignore failures in dry-run mode --
- # that way, we'll get accurate feedback if we can read the
- # script.
- try:
- f = open(script, "rb")
- except OSError:
- if not self.dry_run:
- raise
- f = None
- else:
- encoding, lines = tokenize.detect_encoding(f.readline)
- f.seek(0)
- first_line = f.readline()
- if not first_line:
- self.warn("%s is an empty file (skipping)" % script)
- continue
-
- match = first_line_re.match(first_line)
- if match:
- adjust = True
- post_interp = match.group(1) or b''
-
- if adjust:
- log.info("copying and adjusting %s -> %s", script,
- self.build_dir)
- updated_files.append(outfile)
- if not self.dry_run:
- if not sysconfig.python_build:
- executable = self.executable
- else:
- executable = os.path.join(
- sysconfig.get_config_var("BINDIR"),
- "python%s%s" % (sysconfig.get_config_var("VERSION"),
- sysconfig.get_config_var("EXE")))
- executable = os.fsencode(executable)
- shebang = b"#!" + executable + post_interp + b"\n"
- # Python parser starts to read a script using UTF-8 until
- # it gets a #coding:xxx cookie. The shebang has to be the
- # first line of a file, the #coding:xxx cookie cannot be
- # written before. So the shebang has to be decodable from
- # UTF-8.
- try:
- shebang.decode('utf-8')
- except UnicodeDecodeError:
- raise ValueError(
- "The shebang ({!r}) is not decodable "
- "from utf-8".format(shebang))
- # If the script is encoded to a custom encoding (use a
- # #coding:xxx cookie), the shebang has to be decodable from
- # the script encoding too.
- try:
- shebang.decode(encoding)
- except UnicodeDecodeError:
- raise ValueError(
- "The shebang ({!r}) is not decodable "
- "from the script encoding ({})"
- .format(shebang, encoding))
- with open(outfile, "wb") as outf:
- outf.write(shebang)
- outf.writelines(f.readlines())
- if f:
- f.close()
- else:
- if f:
- f.close()
- updated_files.append(outfile)
- self.copy_file(script, outfile)
-
- if os.name == 'posix':
- for file in outfiles:
- if self.dry_run:
- log.info("changing mode of %s", file)
- else:
- oldmode = os.stat(file)[ST_MODE] & 0o7777
- newmode = (oldmode | 0o555) & 0o7777
- if newmode != oldmode:
- log.info("changing mode of %s from %o to %o",
- file, oldmode, newmode)
- os.chmod(file, newmode)
- # XXX should we modify self.outfiles?
+ self._copy_script(script, outfiles, updated_files)
+
+ self._change_modes(outfiles)
+
return outfiles, updated_files
+
+ def _copy_script(self, script, outfiles, updated_files): # noqa: C901
+ shebang_match = None
+ script = convert_path(script)
+ outfile = os.path.join(self.build_dir, os.path.basename(script))
+ outfiles.append(outfile)
+
+ if not self.force and not newer(script, outfile):
+ log.debug("not copying %s (up-to-date)", script)
+ return
+
+ # Always open the file, but ignore failures in dry-run mode
+ # in order to attempt to copy directly.
+ try:
+ f = tokenize.open(script)
+ except OSError:
+ if not self.dry_run:
+ raise
+ f = None
+ else:
+ first_line = f.readline()
+ if not first_line:
+ self.warn("%s is an empty file (skipping)" % script)
+ return
+
+ shebang_match = shebang_pattern.match(first_line)
+
+ updated_files.append(outfile)
+ if shebang_match:
+ log.info("copying and adjusting %s -> %s", script, self.build_dir)
+ if not self.dry_run:
+ if not sysconfig.python_build:
+ executable = self.executable
+ else:
+ executable = os.path.join(
+ sysconfig.get_config_var("BINDIR"),
+ "python%s%s"
+ % (
+ sysconfig.get_config_var("VERSION"),
+ sysconfig.get_config_var("EXE"),
+ ),
+ )
+ post_interp = shebang_match.group(1) or ''
+ shebang = "#!" + executable + post_interp + "\n"
+ self._validate_shebang(shebang, f.encoding)
+ with open(outfile, "w", encoding=f.encoding) as outf:
+ outf.write(shebang)
+ outf.writelines(f.readlines())
+ if f:
+ f.close()
+ else:
+ if f:
+ f.close()
+ self.copy_file(script, outfile)
+
+ def _change_modes(self, outfiles):
+ if os.name != 'posix':
+ return
+
+ for file in outfiles:
+ self._change_mode(file)
+
+ def _change_mode(self, file):
+ if self.dry_run:
+ log.info("changing mode of %s", file)
+ return
+
+ oldmode = os.stat(file)[ST_MODE] & 0o7777
+ newmode = (oldmode | 0o555) & 0o7777
+ if newmode != oldmode:
+ log.info("changing mode of %s from %o to %o", file, oldmode, newmode)
+ os.chmod(file, newmode)
+
+ @staticmethod
+ def _validate_shebang(shebang, encoding):
+ # Python parser starts to read a script using UTF-8 until
+ # it gets a #coding:xxx cookie. The shebang has to be the
+ # first line of a file, the #coding:xxx cookie cannot be
+ # written before. So the shebang has to be encodable to
+ # UTF-8.
+ try:
+ shebang.encode('utf-8')
+ except UnicodeEncodeError:
+ raise ValueError(
+ "The shebang ({!r}) is not encodable " "to utf-8".format(shebang)
+ )
+
+ # If the script is encoded to a custom encoding (use a
+ # #coding:xxx cookie), the shebang has to be encodable to
+ # the script encoding too.
+ try:
+ shebang.encode(encoding)
+ except UnicodeEncodeError:
+ raise ValueError(
+ "The shebang ({!r}) is not encodable "
+ "to the script encoding ({})".format(shebang, encoding)
+ )
diff --git a/setuptools/_distutils/command/check.py b/setuptools/_distutils/command/check.py
index 525540b6..575e49fb 100644
--- a/setuptools/_distutils/command/check.py
+++ b/setuptools/_distutils/command/check.py
@@ -2,46 +2,56 @@
Implements the Distutils 'check' command.
"""
-from distutils.core import Command
-from distutils.errors import DistutilsSetupError
-
-try:
- # docutils is installed
- from docutils.utils import Reporter
- from docutils.parsers.rst import Parser
- from docutils import frontend
- from docutils import nodes
-
- class SilentReporter(Reporter):
-
- def __init__(self, source, report_level, halt_level, stream=None,
- debug=0, encoding='ascii', error_handler='replace'):
+import contextlib
+
+from ..core import Command
+from ..errors import DistutilsSetupError
+
+with contextlib.suppress(ImportError):
+ import docutils.utils
+ import docutils.parsers.rst
+ import docutils.frontend
+ import docutils.nodes
+
+ class SilentReporter(docutils.utils.Reporter):
+ def __init__(
+ self,
+ source,
+ report_level,
+ halt_level,
+ stream=None,
+ debug=0,
+ encoding='ascii',
+ error_handler='replace',
+ ):
self.messages = []
- super().__init__(source, report_level, halt_level, stream,
- debug, encoding, error_handler)
+ super().__init__(
+ source, report_level, halt_level, stream, debug, encoding, error_handler
+ )
def system_message(self, level, message, *children, **kwargs):
self.messages.append((level, message, children, kwargs))
- return nodes.system_message(message, level=level,
- type=self.levels[level],
- *children, **kwargs)
+ return docutils.nodes.system_message(
+ message, level=level, type=self.levels[level], *children, **kwargs
+ )
- HAS_DOCUTILS = True
-except Exception:
- # Catch all exceptions because exceptions besides ImportError probably
- # indicate that docutils is not ported to Py3k.
- HAS_DOCUTILS = False
class check(Command):
- """This command checks the meta-data of the package.
- """
- description = ("perform some checks on the package")
- user_options = [('metadata', 'm', 'Verify meta-data'),
- ('restructuredtext', 'r',
- ('Checks if long string meta-data syntax '
- 'are reStructuredText-compliant')),
- ('strict', 's',
- 'Will exit with an error if a check fails')]
+ """This command checks the meta-data of the package."""
+
+ description = "perform some checks on the package"
+ user_options = [
+ ('metadata', 'm', 'Verify meta-data'),
+ (
+ 'restructuredtext',
+ 'r',
+ (
+ 'Checks if long string meta-data syntax '
+ 'are reStructuredText-compliant'
+ ),
+ ),
+ ('strict', 's', 'Will exit with an error if a check fails'),
+ ]
boolean_options = ['metadata', 'restructuredtext', 'strict']
@@ -66,8 +76,11 @@ class check(Command):
if self.metadata:
self.check_metadata()
if self.restructuredtext:
- if HAS_DOCUTILS:
- self.check_restructuredtext()
+ if 'docutils' in globals():
+ try:
+ self.check_restructuredtext()
+ except TypeError as exc:
+ raise DistutilsSetupError(str(exc))
elif self.strict:
raise DistutilsSetupError('The docutils package is needed.')
@@ -80,34 +93,19 @@ class check(Command):
"""Ensures that all required elements of meta-data are supplied.
Required fields:
- name, version, URL
-
- Recommended fields:
- (author and author_email) or (maintainer and maintainer_email))
+ name, version
Warns if any are missing.
"""
metadata = self.distribution.metadata
missing = []
- for attr in ('name', 'version', 'url'):
- if not (hasattr(metadata, attr) and getattr(metadata, attr)):
+ for attr in 'name', 'version':
+ if not getattr(metadata, attr, None):
missing.append(attr)
if missing:
- self.warn("missing required meta-data: %s" % ', '.join(missing))
- if metadata.author:
- if not metadata.author_email:
- self.warn("missing meta-data: if 'author' supplied, " +
- "'author_email' should be supplied too")
- elif metadata.maintainer:
- if not metadata.maintainer_email:
- self.warn("missing meta-data: if 'maintainer' supplied, " +
- "'maintainer_email' should be supplied too")
- else:
- self.warn("missing meta-data: either (author and author_email) " +
- "or (maintainer and maintainer_email) " +
- "should be supplied")
+ self.warn("missing required meta-data: %s" % ', '.join(missing))
def check_restructuredtext(self):
"""Checks if the long string fields are reST-compliant."""
@@ -117,32 +115,37 @@ class check(Command):
if line is None:
warning = warning[1]
else:
- warning = '%s (line %s)' % (warning[1], line)
+ warning = '{} (line {})'.format(warning[1], line)
self.warn(warning)
def _check_rst_data(self, data):
"""Returns warnings when the provided data doesn't compile."""
# the include and csv_table directives need this to be a path
source_path = self.distribution.script_name or 'setup.py'
- parser = Parser()
- settings = frontend.OptionParser(components=(Parser,)).get_default_values()
+ parser = docutils.parsers.rst.Parser()
+ settings = docutils.frontend.OptionParser(
+ components=(docutils.parsers.rst.Parser,)
+ ).get_default_values()
settings.tab_width = 4
settings.pep_references = None
settings.rfc_references = None
- reporter = SilentReporter(source_path,
- settings.report_level,
- settings.halt_level,
- stream=settings.warning_stream,
- debug=settings.debug,
- encoding=settings.error_encoding,
- error_handler=settings.error_encoding_error_handler)
-
- document = nodes.document(settings, reporter, source=source_path)
+ reporter = SilentReporter(
+ source_path,
+ settings.report_level,
+ settings.halt_level,
+ stream=settings.warning_stream,
+ debug=settings.debug,
+ encoding=settings.error_encoding,
+ error_handler=settings.error_encoding_error_handler,
+ )
+
+ document = docutils.nodes.document(settings, reporter, source=source_path)
document.note_source(source_path, -1)
try:
parser.parse(data, document)
except AttributeError as e:
reporter.messages.append(
- (-1, 'Could not finish the parsing: %s.' % e, '', {}))
+ (-1, 'Could not finish the parsing: %s.' % e, '', {})
+ )
return reporter.messages
diff --git a/setuptools/_distutils/command/clean.py b/setuptools/_distutils/command/clean.py
index 0cb27016..d6eb3eba 100644
--- a/setuptools/_distutils/command/clean.py
+++ b/setuptools/_distutils/command/clean.py
@@ -5,26 +5,29 @@ Implements the Distutils 'clean' command."""
# contributed by Bastian Kleineidam <calvin@cs.uni-sb.de>, added 2000-03-18
import os
-from distutils.core import Command
-from distutils.dir_util import remove_tree
-from distutils import log
+from ..core import Command
+from ..dir_util import remove_tree
+from distutils._log import log
+
class clean(Command):
description = "clean up temporary files from 'build' command"
user_options = [
- ('build-base=', 'b',
- "base build directory (default: 'build.build-base')"),
- ('build-lib=', None,
- "build directory for all modules (default: 'build.build-lib')"),
- ('build-temp=', 't',
- "temporary build directory (default: 'build.build-temp')"),
- ('build-scripts=', None,
- "build directory for scripts (default: 'build.build-scripts')"),
- ('bdist-base=', None,
- "temporary directory for built distributions"),
- ('all', 'a',
- "remove all build output, not just temporary by-products")
+ ('build-base=', 'b', "base build directory (default: 'build.build-base')"),
+ (
+ 'build-lib=',
+ None,
+ "build directory for all modules (default: 'build.build-lib')",
+ ),
+ ('build-temp=', 't', "temporary build directory (default: 'build.build-temp')"),
+ (
+ 'build-scripts=',
+ None,
+ "build directory for scripts (default: 'build.build-scripts')",
+ ),
+ ('bdist-base=', None, "temporary directory for built distributions"),
+ ('all', 'a', "remove all build output, not just temporary by-products"),
]
boolean_options = ['all']
@@ -38,13 +41,14 @@ class clean(Command):
self.all = None
def finalize_options(self):
- self.set_undefined_options('build',
- ('build_base', 'build_base'),
- ('build_lib', 'build_lib'),
- ('build_scripts', 'build_scripts'),
- ('build_temp', 'build_temp'))
- self.set_undefined_options('bdist',
- ('bdist_base', 'bdist_base'))
+ self.set_undefined_options(
+ 'build',
+ ('build_base', 'build_base'),
+ ('build_lib', 'build_lib'),
+ ('build_scripts', 'build_scripts'),
+ ('build_temp', 'build_temp'),
+ )
+ self.set_undefined_options('bdist', ('bdist_base', 'bdist_base'))
def run(self):
# remove the build/temp.<plat> directory (unless it's already
@@ -52,19 +56,15 @@ class clean(Command):
if os.path.exists(self.build_temp):
remove_tree(self.build_temp, dry_run=self.dry_run)
else:
- log.debug("'%s' does not exist -- can't clean it",
- self.build_temp)
+ log.debug("'%s' does not exist -- can't clean it", self.build_temp)
if self.all:
# remove build directories
- for directory in (self.build_lib,
- self.bdist_base,
- self.build_scripts):
+ for directory in (self.build_lib, self.bdist_base, self.build_scripts):
if os.path.exists(directory):
remove_tree(directory, dry_run=self.dry_run)
else:
- log.warn("'%s' does not exist -- can't clean it",
- directory)
+ log.warning("'%s' does not exist -- can't clean it", directory)
# just for the heck of it, try to remove the base build directory:
# we might have emptied it right now, but if not we don't care
diff --git a/setuptools/_distutils/command/config.py b/setuptools/_distutils/command/config.py
index aeda408e..8bf0e489 100644
--- a/setuptools/_distutils/command/config.py
+++ b/setuptools/_distutils/command/config.py
@@ -9,41 +9,36 @@ configure-like tasks: "try to compile this C code", or "figure out where
this header file lives".
"""
-import os, re
+import os
+import re
-from distutils.core import Command
-from distutils.errors import DistutilsExecError
-from distutils.sysconfig import customize_compiler
-from distutils import log
+from ..core import Command
+from ..errors import DistutilsExecError
+from ..sysconfig import customize_compiler
+from distutils._log import log
LANG_EXT = {"c": ".c", "c++": ".cxx"}
+
class config(Command):
description = "prepare to build"
user_options = [
- ('compiler=', None,
- "specify the compiler type"),
- ('cc=', None,
- "specify the compiler executable"),
- ('include-dirs=', 'I',
- "list of directories to search for header files"),
- ('define=', 'D',
- "C preprocessor macros to define"),
- ('undef=', 'U',
- "C preprocessor macros to undefine"),
- ('libraries=', 'l',
- "external C libraries to link with"),
- ('library-dirs=', 'L',
- "directories to search for external C libraries"),
-
- ('noisy', None,
- "show every action (compile, link, run, ...) taken"),
- ('dump-source', None,
- "dump generated source files before attempting to compile them"),
- ]
-
+ ('compiler=', None, "specify the compiler type"),
+ ('cc=', None, "specify the compiler executable"),
+ ('include-dirs=', 'I', "list of directories to search for header files"),
+ ('define=', 'D', "C preprocessor macros to define"),
+ ('undef=', 'U', "C preprocessor macros to undefine"),
+ ('libraries=', 'l', "external C libraries to link with"),
+ ('library-dirs=', 'L', "directories to search for external C libraries"),
+ ('noisy', None, "show every action (compile, link, run, ...) taken"),
+ (
+ 'dump-source',
+ None,
+ "dump generated source files before attempting to compile them",
+ ),
+ ]
# The three standard command methods: since the "config" command
# does nothing by default, these are empty.
@@ -92,10 +87,12 @@ class config(Command):
"""
# We do this late, and only on-demand, because this is an expensive
# import.
- from distutils.ccompiler import CCompiler, new_compiler
+ from ..ccompiler import CCompiler, new_compiler
+
if not isinstance(self.compiler, CCompiler):
- self.compiler = new_compiler(compiler=self.compiler,
- dry_run=self.dry_run, force=1)
+ self.compiler = new_compiler(
+ compiler=self.compiler, dry_run=self.dry_run, force=1
+ )
customize_compiler(self.compiler)
if self.include_dirs:
self.compiler.set_include_dirs(self.include_dirs)
@@ -132,14 +129,16 @@ class config(Command):
self.compiler.compile([src], include_dirs=include_dirs)
return (src, obj)
- def _link(self, body, headers, include_dirs, libraries, library_dirs,
- lang):
+ def _link(self, body, headers, include_dirs, libraries, library_dirs, lang):
(src, obj) = self._compile(body, headers, include_dirs, lang)
prog = os.path.splitext(os.path.basename(src))[0]
- self.compiler.link_executable([obj], prog,
- libraries=libraries,
- library_dirs=library_dirs,
- target_lang=lang)
+ self.compiler.link_executable(
+ [obj],
+ prog,
+ libraries=libraries,
+ library_dirs=library_dirs,
+ target_lang=lang,
+ )
if self.compiler.exe_extension is not None:
prog = prog + self.compiler.exe_extension
@@ -158,7 +157,6 @@ class config(Command):
except OSError:
pass
-
# XXX these ignore the dry-run flag: what to do, what to do? even if
# you want a dry-run build, you still need some sort of configuration
# info. My inclination is to make it up to the real config command to
@@ -176,7 +174,8 @@ class config(Command):
preprocessor succeeded, false if there were any errors.
('body' probably isn't of much use, but what the heck.)
"""
- from distutils.ccompiler import CompileError
+ from ..ccompiler import CompileError
+
self._check_compiler()
ok = True
try:
@@ -187,8 +186,7 @@ class config(Command):
self._clean()
return ok
- def search_cpp(self, pattern, body=None, headers=None, include_dirs=None,
- lang="c"):
+ def search_cpp(self, pattern, body=None, headers=None, include_dirs=None, lang="c"):
"""Construct a source file (just like 'try_cpp()'), run it through
the preprocessor, and return true if any line of the output matches
'pattern'. 'pattern' should either be a compiled regex object or a
@@ -219,7 +217,8 @@ class config(Command):
"""Try to compile a source file built from 'body' and 'headers'.
Return true on success, false otherwise.
"""
- from distutils.ccompiler import CompileError
+ from ..ccompiler import CompileError
+
self._check_compiler()
try:
self._compile(body, headers, include_dirs, lang)
@@ -231,17 +230,24 @@ class config(Command):
self._clean()
return ok
- def try_link(self, body, headers=None, include_dirs=None, libraries=None,
- library_dirs=None, lang="c"):
+ def try_link(
+ self,
+ body,
+ headers=None,
+ include_dirs=None,
+ libraries=None,
+ library_dirs=None,
+ lang="c",
+ ):
"""Try to compile and link a source file, built from 'body' and
'headers', to executable form. Return true on success, false
otherwise.
"""
- from distutils.ccompiler import CompileError, LinkError
+ from ..ccompiler import CompileError, LinkError
+
self._check_compiler()
try:
- self._link(body, headers, include_dirs,
- libraries, library_dirs, lang)
+ self._link(body, headers, include_dirs, libraries, library_dirs, lang)
ok = True
except (CompileError, LinkError):
ok = False
@@ -250,17 +256,26 @@ class config(Command):
self._clean()
return ok
- def try_run(self, body, headers=None, include_dirs=None, libraries=None,
- library_dirs=None, lang="c"):
+ def try_run(
+ self,
+ body,
+ headers=None,
+ include_dirs=None,
+ libraries=None,
+ library_dirs=None,
+ lang="c",
+ ):
"""Try to compile, link to an executable, and run a program
built from 'body' and 'headers'. Return true on success, false
otherwise.
"""
- from distutils.ccompiler import CompileError, LinkError
+ from ..ccompiler import CompileError, LinkError
+
self._check_compiler()
try:
- src, obj, exe = self._link(body, headers, include_dirs,
- libraries, library_dirs, lang)
+ src, obj, exe = self._link(
+ body, headers, include_dirs, libraries, library_dirs, lang
+ )
self.spawn([exe])
ok = True
except (CompileError, LinkError, DistutilsExecError):
@@ -270,13 +285,20 @@ class config(Command):
self._clean()
return ok
-
# -- High-level methods --------------------------------------------
# (these are the ones that are actually likely to be useful
# when implementing a real-world config command!)
- def check_func(self, func, headers=None, include_dirs=None,
- libraries=None, library_dirs=None, decl=0, call=0):
+ def check_func(
+ self,
+ func,
+ headers=None,
+ include_dirs=None,
+ libraries=None,
+ library_dirs=None,
+ decl=0,
+ call=0,
+ ):
"""Determine if function 'func' is available by constructing a
source file that refers to 'func', and compiles and links it.
If everything succeeds, returns true; otherwise returns false.
@@ -302,11 +324,16 @@ class config(Command):
body.append("}")
body = "\n".join(body) + "\n"
- return self.try_link(body, headers, include_dirs,
- libraries, library_dirs)
+ return self.try_link(body, headers, include_dirs, libraries, library_dirs)
- def check_lib(self, library, library_dirs=None, headers=None,
- include_dirs=None, other_libraries=[]):
+ def check_lib(
+ self,
+ library,
+ library_dirs=None,
+ headers=None,
+ include_dirs=None,
+ other_libraries=[],
+ ):
"""Determine if 'library' is available to be linked against,
without actually checking that any particular symbols are provided
by it. 'headers' will be used in constructing the source file to
@@ -316,17 +343,23 @@ class config(Command):
has symbols that depend on other libraries.
"""
self._check_compiler()
- return self.try_link("int main (void) { }", headers, include_dirs,
- [library] + other_libraries, library_dirs)
-
- def check_header(self, header, include_dirs=None, library_dirs=None,
- lang="c"):
+ return self.try_link(
+ "int main (void) { }",
+ headers,
+ include_dirs,
+ [library] + other_libraries,
+ library_dirs,
+ )
+
+ def check_header(self, header, include_dirs=None, library_dirs=None, lang="c"):
"""Determine if the system header file named by 'header_file'
exists and can be found by the preprocessor; return true if so,
false otherwise.
"""
- return self.try_cpp(body="/* No body */", headers=[header],
- include_dirs=include_dirs)
+ return self.try_cpp(
+ body="/* No body */", headers=[header], include_dirs=include_dirs
+ )
+
def dump_file(filename, head=None):
"""Dumps a file content into log.info.
diff --git a/setuptools/_distutils/command/install.py b/setuptools/_distutils/command/install.py
index 41c17d8a..08d2f881 100644
--- a/setuptools/_distutils/command/install.py
+++ b/setuptools/_distutils/command/install.py
@@ -8,19 +8,20 @@ import contextlib
import sysconfig
import itertools
-from distutils import log
-from distutils.core import Command
-from distutils.debug import DEBUG
-from distutils.sysconfig import get_config_vars
-from distutils.errors import DistutilsPlatformError
-from distutils.file_util import write_file
-from distutils.util import convert_path, subst_vars, change_root
-from distutils.util import get_platform
-from distutils.errors import DistutilsOptionError
+from distutils._log import log
+from ..core import Command
+from ..debug import DEBUG
+from ..sysconfig import get_config_vars
+from ..file_util import write_file
+from ..util import convert_path, subst_vars, change_root
+from ..util import get_platform
+from ..errors import DistutilsOptionError, DistutilsPlatformError
+from . import _framework_compat as fw
from .. import _collections
from site import USER_BASE
from site import USER_SITE
+
HAS_USER_SITE = True
WINDOWS_SCHEME = {
@@ -28,59 +29,66 @@ WINDOWS_SCHEME = {
'platlib': '{base}/Lib/site-packages',
'headers': '{base}/Include/{dist_name}',
'scripts': '{base}/Scripts',
- 'data' : '{base}',
+ 'data': '{base}',
}
INSTALL_SCHEMES = {
'posix_prefix': {
'purelib': '{base}/lib/{implementation_lower}{py_version_short}/site-packages',
- 'platlib': '{platbase}/{platlibdir}/{implementation_lower}{py_version_short}/site-packages',
- 'headers': '{base}/include/{implementation_lower}{py_version_short}{abiflags}/{dist_name}',
+ 'platlib': '{platbase}/{platlibdir}/{implementation_lower}'
+ '{py_version_short}/site-packages',
+ 'headers': '{base}/include/{implementation_lower}'
+ '{py_version_short}{abiflags}/{dist_name}',
'scripts': '{base}/bin',
- 'data' : '{base}',
- },
+ 'data': '{base}',
+ },
'posix_home': {
'purelib': '{base}/lib/{implementation_lower}',
'platlib': '{base}/{platlibdir}/{implementation_lower}',
'headers': '{base}/include/{implementation_lower}/{dist_name}',
'scripts': '{base}/bin',
- 'data' : '{base}',
- },
+ 'data': '{base}',
+ },
'nt': WINDOWS_SCHEME,
'pypy': {
'purelib': '{base}/site-packages',
'platlib': '{base}/site-packages',
'headers': '{base}/include/{dist_name}',
'scripts': '{base}/bin',
- 'data' : '{base}',
- },
+ 'data': '{base}',
+ },
'pypy_nt': {
'purelib': '{base}/site-packages',
'platlib': '{base}/site-packages',
'headers': '{base}/include/{dist_name}',
'scripts': '{base}/Scripts',
- 'data' : '{base}',
- },
- }
+ 'data': '{base}',
+ },
+}
# user site schemes
if HAS_USER_SITE:
INSTALL_SCHEMES['nt_user'] = {
'purelib': '{usersite}',
'platlib': '{usersite}',
- 'headers': '{userbase}/{implementation}{py_version_nodot_plat}/Include/{dist_name}',
+ 'headers': '{userbase}/{implementation}{py_version_nodot_plat}'
+ '/Include/{dist_name}',
'scripts': '{userbase}/{implementation}{py_version_nodot_plat}/Scripts',
- 'data' : '{userbase}',
- }
+ 'data': '{userbase}',
+ }
INSTALL_SCHEMES['posix_user'] = {
'purelib': '{usersite}',
'platlib': '{usersite}',
- 'headers':
- '{userbase}/include/{implementation_lower}{py_version_short}{abiflags}/{dist_name}',
+ 'headers': '{userbase}/include/{implementation_lower}'
+ '{py_version_short}{abiflags}/{dist_name}',
'scripts': '{userbase}/bin',
- 'data' : '{userbase}',
- }
+ 'data': '{userbase}',
+ }
+
+
+INSTALL_SCHEMES.update(fw.schemes)
+
# The keys to an installation scheme; if any new types of files are to be
# installed, be sure to add an entry to every installation scheme above,
@@ -128,11 +136,7 @@ def _remove_set(ob, attrs):
"""
Include only attrs that are None in ob.
"""
- return {
- key: value
- for key, value in attrs.items()
- if getattr(ob, key) is None
- }
+ return {key: value for key, value in attrs.items() if getattr(ob, key) is None}
def _resolve_scheme(name):
@@ -140,7 +144,7 @@ def _resolve_scheme(name):
try:
resolved = sysconfig.get_preferred_scheme(key)
except Exception:
- resolved = _pypy_hack(name)
+ resolved = fw.scheme(_pypy_hack(name))
return resolved
@@ -164,10 +168,7 @@ def _inject_headers(name, scheme):
def _scheme_attrs(scheme):
"""Resolve install directories by applying the install schemes."""
- return {
- f'install_{key}': scheme[key]
- for key in SCHEME_KEYS
- }
+ return {f'install_{key}': scheme[key] for key in SCHEME_KEYS}
def _pypy_hack(name):
@@ -184,72 +185,73 @@ class install(Command):
user_options = [
# Select installation scheme and set base director(y|ies)
- ('prefix=', None,
- "installation prefix"),
- ('exec-prefix=', None,
- "(Unix only) prefix for platform-specific files"),
- ('home=', None,
- "(Unix only) home directory to install under"),
-
+ ('prefix=', None, "installation prefix"),
+ ('exec-prefix=', None, "(Unix only) prefix for platform-specific files"),
+ ('home=', None, "(Unix only) home directory to install under"),
# Or, just set the base director(y|ies)
- ('install-base=', None,
- "base installation directory (instead of --prefix or --home)"),
- ('install-platbase=', None,
- "base installation directory for platform-specific files " +
- "(instead of --exec-prefix or --home)"),
- ('root=', None,
- "install everything relative to this alternate root directory"),
-
+ (
+ 'install-base=',
+ None,
+ "base installation directory (instead of --prefix or --home)",
+ ),
+ (
+ 'install-platbase=',
+ None,
+ "base installation directory for platform-specific files "
+ + "(instead of --exec-prefix or --home)",
+ ),
+ ('root=', None, "install everything relative to this alternate root directory"),
# Or, explicitly set the installation scheme
- ('install-purelib=', None,
- "installation directory for pure Python module distributions"),
- ('install-platlib=', None,
- "installation directory for non-pure module distributions"),
- ('install-lib=', None,
- "installation directory for all module distributions " +
- "(overrides --install-purelib and --install-platlib)"),
-
- ('install-headers=', None,
- "installation directory for C/C++ headers"),
- ('install-scripts=', None,
- "installation directory for Python scripts"),
- ('install-data=', None,
- "installation directory for data files"),
-
+ (
+ 'install-purelib=',
+ None,
+ "installation directory for pure Python module distributions",
+ ),
+ (
+ 'install-platlib=',
+ None,
+ "installation directory for non-pure module distributions",
+ ),
+ (
+ 'install-lib=',
+ None,
+ "installation directory for all module distributions "
+ + "(overrides --install-purelib and --install-platlib)",
+ ),
+ ('install-headers=', None, "installation directory for C/C++ headers"),
+ ('install-scripts=', None, "installation directory for Python scripts"),
+ ('install-data=', None, "installation directory for data files"),
# Byte-compilation options -- see install_lib.py for details, as
# these are duplicated from there (but only install_lib does
# anything with them).
('compile', 'c', "compile .py to .pyc [default]"),
('no-compile', None, "don't compile .py files"),
- ('optimize=', 'O',
- "also compile with optimization: -O1 for \"python -O\", "
- "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"),
-
+ (
+ 'optimize=',
+ 'O',
+ "also compile with optimization: -O1 for \"python -O\", "
+ "-O2 for \"python -OO\", and -O0 to disable [default: -O0]",
+ ),
# Miscellaneous control options
- ('force', 'f',
- "force installation (overwrite any existing files)"),
- ('skip-build', None,
- "skip rebuilding everything (for testing/debugging)"),
-
+ ('force', 'f', "force installation (overwrite any existing files)"),
+ ('skip-build', None, "skip rebuilding everything (for testing/debugging)"),
# Where to install documentation (eventually!)
- #('doc-format=', None, "format of documentation to generate"),
- #('install-man=', None, "directory for Unix man pages"),
- #('install-html=', None, "directory for HTML documentation"),
- #('install-info=', None, "directory for GNU info files"),
-
- ('record=', None,
- "filename in which to record list of installed files"),
- ]
+ # ('doc-format=', None, "format of documentation to generate"),
+ # ('install-man=', None, "directory for Unix man pages"),
+ # ('install-html=', None, "directory for HTML documentation"),
+ # ('install-info=', None, "directory for GNU info files"),
+ ('record=', None, "filename in which to record list of installed files"),
+ ]
boolean_options = ['compile', 'force', 'skip-build']
if HAS_USER_SITE:
- user_options.append(('user', None,
- "install in user site-package '%s'" % USER_SITE))
+ user_options.append(
+ ('user', None, "install in user site-package '%s'" % USER_SITE)
+ )
boolean_options.append('user')
- negative_opt = {'no-compile' : 'compile'}
-
+ negative_opt = {'no-compile': 'compile'}
def initialize_options(self):
"""Initializes options."""
@@ -271,10 +273,10 @@ class install(Command):
# supplied by the user, they are filled in using the installation
# scheme implied by prefix/exec-prefix/home and the contents of
# that installation scheme.
- self.install_purelib = None # for pure module distributions
- self.install_platlib = None # non-pure (dists w/ extensions)
- self.install_headers = None # for C/C++ headers
- self.install_lib = None # set to either purelib or platlib
+ self.install_purelib = None # for pure module distributions
+ self.install_platlib = None # non-pure (dists w/ extensions)
+ self.install_headers = None # for C/C++ headers
+ self.install_lib = None # set to either purelib or platlib
self.install_scripts = None
self.install_data = None
self.install_userbase = USER_BASE
@@ -316,20 +318,19 @@ class install(Command):
# Not defined yet because we don't know anything about
# documentation yet.
- #self.install_man = None
- #self.install_html = None
- #self.install_info = None
+ # self.install_man = None
+ # self.install_html = None
+ # self.install_info = None
self.record = None
-
# -- Option finalizing methods -------------------------------------
# (This is rather more involved than for most commands,
# because this is where the policy for installing third-
# party Python modules on various platforms given a wide
# array of user input is decided. Yes, it's quite complex!)
- def finalize_options(self):
+ def finalize_options(self): # noqa: C901
"""Finalizes options."""
# This method (and its helpers, like 'finalize_unix()',
# 'finalize_other()', and 'select_scheme()') is where the default
@@ -345,20 +346,30 @@ class install(Command):
# Check for errors/inconsistencies in the options; first, stuff
# that's wrong on any platform.
- if ((self.prefix or self.exec_prefix or self.home) and
- (self.install_base or self.install_platbase)):
+ if (self.prefix or self.exec_prefix or self.home) and (
+ self.install_base or self.install_platbase
+ ):
raise DistutilsOptionError(
- "must supply either prefix/exec-prefix/home or " +
- "install-base/install-platbase -- not both")
+ "must supply either prefix/exec-prefix/home or "
+ + "install-base/install-platbase -- not both"
+ )
if self.home and (self.prefix or self.exec_prefix):
raise DistutilsOptionError(
- "must supply either home or prefix/exec-prefix -- not both")
+ "must supply either home or prefix/exec-prefix -- not both"
+ )
- if self.user and (self.prefix or self.exec_prefix or self.home or
- self.install_base or self.install_platbase):
- raise DistutilsOptionError("can't combine user with prefix, "
- "exec_prefix/home, or install_(plat)base")
+ if self.user and (
+ self.prefix
+ or self.exec_prefix
+ or self.home
+ or self.install_base
+ or self.install_platbase
+ ):
+ raise DistutilsOptionError(
+ "can't combine user with prefix, "
+ "exec_prefix/home, or install_(plat)base"
+ )
# Next, stuff that's wrong (or dubious) only on certain platforms.
if os.name != "posix":
@@ -423,7 +434,8 @@ class install(Command):
local_vars['usersite'] = self.install_usersite
self.config_vars = _collections.DictStack(
- [compat_vars, sysconfig.get_config_vars(), local_vars])
+ [fw.vars(), compat_vars, sysconfig.get_config_vars(), local_vars]
+ )
self.expand_basedirs()
@@ -436,6 +448,7 @@ class install(Command):
if DEBUG:
from pprint import pprint
+
print("config vars:")
pprint(dict(self.config_vars))
@@ -454,17 +467,23 @@ class install(Command):
# module distribution is pure or not. Of course, if the user
# already specified install_lib, use their selection.
if self.install_lib is None:
- if self.distribution.has_ext_modules(): # has extensions: non-pure
+ if self.distribution.has_ext_modules(): # has extensions: non-pure
self.install_lib = self.install_platlib
else:
self.install_lib = self.install_purelib
-
# Convert directories from Unix /-separated syntax to the local
# convention.
- self.convert_paths('lib', 'purelib', 'platlib',
- 'scripts', 'data', 'headers',
- 'userbase', 'usersite')
+ self.convert_paths(
+ 'lib',
+ 'purelib',
+ 'platlib',
+ 'scripts',
+ 'data',
+ 'headers',
+ 'userbase',
+ 'usersite',
+ )
# Deprecated
# Well, we're not actually fully completely finalized yet: we still
@@ -472,21 +491,22 @@ class install(Command):
# non-packagized module distributions (hello, Numerical Python!) to
# get their own directories.
self.handle_extra_path()
- self.install_libbase = self.install_lib # needed for .pth file
+ self.install_libbase = self.install_lib # needed for .pth file
self.install_lib = os.path.join(self.install_lib, self.extra_dirs)
# If a new root directory was supplied, make all the installation
# dirs relative to it.
if self.root is not None:
- self.change_roots('libbase', 'lib', 'purelib', 'platlib',
- 'scripts', 'data', 'headers')
+ self.change_roots(
+ 'libbase', 'lib', 'purelib', 'platlib', 'scripts', 'data', 'headers'
+ )
self.dump_dirs("after prepending root")
# Find out the build directories, ie. where to install from.
- self.set_undefined_options('build',
- ('build_base', 'build_base'),
- ('build_lib', 'build_lib'))
+ self.set_undefined_options(
+ 'build', ('build_base', 'build_base'), ('build_lib', 'build_lib')
+ )
# Punt on doc directories for now -- after all, we're punting on
# documentation completely!
@@ -495,7 +515,8 @@ class install(Command):
"""Dumps the list of user options."""
if not DEBUG:
return
- from distutils.fancy_getopt import longopt_xlate
+ from ..fancy_getopt import longopt_xlate
+
log.debug(msg + ":")
for opt in self.user_options:
opt_name = opt[0]
@@ -515,24 +536,24 @@ class install(Command):
if self.install_base is not None or self.install_platbase is not None:
incomplete_scheme = (
(
- self.install_lib is None and
- self.install_purelib is None and
- self.install_platlib is None
- ) or
- self.install_headers is None or
- self.install_scripts is None or
- self.install_data is None
+ self.install_lib is None
+ and self.install_purelib is None
+ and self.install_platlib is None
+ )
+ or self.install_headers is None
+ or self.install_scripts is None
+ or self.install_data is None
)
if incomplete_scheme:
raise DistutilsOptionError(
- "install-base or install-platbase supplied, but "
- "installation scheme is incomplete")
+ "install-base or install-platbase supplied, but "
+ "installation scheme is incomplete"
+ )
return
if self.user:
if self.install_userbase is None:
- raise DistutilsPlatformError(
- "User base directory is not specified")
+ raise DistutilsPlatformError("User base directory is not specified")
self.install_base = self.install_platbase = self.install_userbase
self.select_scheme("posix_user")
elif self.home is not None:
@@ -542,15 +563,14 @@ class install(Command):
if self.prefix is None:
if self.exec_prefix is not None:
raise DistutilsOptionError(
- "must not supply exec-prefix without prefix")
+ "must not supply exec-prefix without prefix"
+ )
# Allow Fedora to add components to the prefix
_prefix_addition = getattr(sysconfig, '_prefix_addition', "")
- self.prefix = (
- os.path.normpath(sys.prefix) + _prefix_addition)
- self.exec_prefix = (
- os.path.normpath(sys.exec_prefix) + _prefix_addition)
+ self.prefix = os.path.normpath(sys.prefix) + _prefix_addition
+ self.exec_prefix = os.path.normpath(sys.exec_prefix) + _prefix_addition
else:
if self.exec_prefix is None:
@@ -564,8 +584,7 @@ class install(Command):
"""Finalizes options for non-posix platforms"""
if self.user:
if self.install_userbase is None:
- raise DistutilsPlatformError(
- "User base directory is not specified")
+ raise DistutilsPlatformError("User base directory is not specified")
self.install_base = self.install_platbase = self.install_userbase
self.select_scheme(os.name + "_user")
elif self.home is not None:
@@ -580,7 +599,8 @@ class install(Command):
self.select_scheme(os.name)
except KeyError:
raise DistutilsPlatformError(
- "I don't know how to install stuff on '%s'" % os.name)
+ "I don't know how to install stuff on '%s'" % os.name
+ )
def select_scheme(self, name):
_select_scheme(self, name)
@@ -601,9 +621,16 @@ class install(Command):
def expand_dirs(self):
"""Calls `os.path.expanduser` on install dirs."""
- self._expand_attrs(['install_purelib', 'install_platlib',
- 'install_lib', 'install_headers',
- 'install_scripts', 'install_data',])
+ self._expand_attrs(
+ [
+ 'install_purelib',
+ 'install_platlib',
+ 'install_lib',
+ 'install_headers',
+ 'install_scripts',
+ 'install_data',
+ ]
+ )
def convert_paths(self, *names):
"""Call `convert_path` over `names`."""
@@ -617,7 +644,7 @@ class install(Command):
self.extra_path = self.distribution.extra_path
if self.extra_path is not None:
- log.warn(
+ log.warning(
"Distribution option extra_path is deprecated. "
"See issue27919 for details."
)
@@ -630,8 +657,9 @@ class install(Command):
path_file, extra_dirs = self.extra_path
else:
raise DistutilsOptionError(
- "'extra_path' option must be a list, tuple, or "
- "comma-separated string with 1 or 2 elements")
+ "'extra_path' option must be a list, tuple, or "
+ "comma-separated string with 1 or 2 elements"
+ )
# convert to local form in case Unix notation used (as it
# should be in setup scripts)
@@ -674,8 +702,7 @@ class install(Command):
# internally, and not to sys.path, so we don't check the platform
# matches what we are running.
if self.warn_dir and build_plat != get_platform():
- raise DistutilsPlatformError("Can't install when "
- "cross-compiling")
+ raise DistutilsPlatformError("Can't install when " "cross-compiling")
# Run all sub-commands (at least those that need to be run)
for cmd_name in self.get_sub_commands():
@@ -687,38 +714,43 @@ class install(Command):
# write list of installed files, if requested.
if self.record:
outputs = self.get_outputs()
- if self.root: # strip any package prefix
+ if self.root: # strip any package prefix
root_len = len(self.root)
for counter in range(len(outputs)):
outputs[counter] = outputs[counter][root_len:]
- self.execute(write_file,
- (self.record, outputs),
- "writing list of installed files to '%s'" %
- self.record)
+ self.execute(
+ write_file,
+ (self.record, outputs),
+ "writing list of installed files to '%s'" % self.record,
+ )
sys_path = map(os.path.normpath, sys.path)
sys_path = map(os.path.normcase, sys_path)
install_lib = os.path.normcase(os.path.normpath(self.install_lib))
- if (self.warn_dir and
- not (self.path_file and self.install_path_file) and
- install_lib not in sys_path):
- log.debug(("modules installed to '%s', which is not in "
- "Python's module search path (sys.path) -- "
- "you'll have to change the search path yourself"),
- self.install_lib)
+ if (
+ self.warn_dir
+ and not (self.path_file and self.install_path_file)
+ and install_lib not in sys_path
+ ):
+ log.debug(
+ (
+ "modules installed to '%s', which is not in "
+ "Python's module search path (sys.path) -- "
+ "you'll have to change the search path yourself"
+ ),
+ self.install_lib,
+ )
def create_path_file(self):
"""Creates the .pth file"""
- filename = os.path.join(self.install_libbase,
- self.path_file + ".pth")
+ filename = os.path.join(self.install_libbase, self.path_file + ".pth")
if self.install_path_file:
- self.execute(write_file,
- (filename, [self.extra_dirs]),
- "creating %s" % filename)
+ self.execute(
+ write_file, (filename, [self.extra_dirs]), "creating %s" % filename
+ )
else:
self.warn("path file '%s' not created" % filename)
-
# -- Reporting methods ---------------------------------------------
def get_outputs(self):
@@ -733,8 +765,7 @@ class install(Command):
outputs.append(filename)
if self.path_file and self.install_path_file:
- outputs.append(os.path.join(self.install_libbase,
- self.path_file + ".pth"))
+ outputs.append(os.path.join(self.install_libbase, self.path_file + ".pth"))
return outputs
@@ -753,8 +784,9 @@ class install(Command):
def has_lib(self):
"""Returns true if the current distribution has any Python
modules to install."""
- return (self.distribution.has_pure_modules() or
- self.distribution.has_ext_modules())
+ return (
+ self.distribution.has_pure_modules() or self.distribution.has_ext_modules()
+ )
def has_headers(self):
"""Returns true if the current distribution has any headers to
@@ -773,9 +805,10 @@ class install(Command):
# 'sub_commands': a list of commands this command might have to run to
# get its work done. See cmd.py for more info.
- sub_commands = [('install_lib', has_lib),
- ('install_headers', has_headers),
- ('install_scripts', has_scripts),
- ('install_data', has_data),
- ('install_egg_info', lambda self:True),
- ]
+ sub_commands = [
+ ('install_lib', has_lib),
+ ('install_headers', has_headers),
+ ('install_scripts', has_scripts),
+ ('install_data', has_data),
+ ('install_egg_info', lambda self: True),
+ ]
diff --git a/setuptools/_distutils/command/install_data.py b/setuptools/_distutils/command/install_data.py
index 947cd76a..d92ed87a 100644
--- a/setuptools/_distutils/command/install_data.py
+++ b/setuptools/_distutils/command/install_data.py
@@ -6,21 +6,24 @@ platform-independent data files."""
# contributed by Bastian Kleineidam
import os
-from distutils.core import Command
-from distutils.util import change_root, convert_path
+from ..core import Command
+from ..util import change_root, convert_path
+
class install_data(Command):
description = "install data files"
user_options = [
- ('install-dir=', 'd',
- "base directory for installing data files "
- "(default: installation base dir)"),
- ('root=', None,
- "install everything relative to this alternate root directory"),
+ (
+ 'install-dir=',
+ 'd',
+ "base directory for installing data files "
+ "(default: installation base dir)",
+ ),
+ ('root=', None, "install everything relative to this alternate root directory"),
('force', 'f', "force installation (overwrite existing files)"),
- ]
+ ]
boolean_options = ['force']
@@ -33,11 +36,12 @@ class install_data(Command):
self.warn_dir = 1
def finalize_options(self):
- self.set_undefined_options('install',
- ('install_data', 'install_dir'),
- ('root', 'root'),
- ('force', 'force'),
- )
+ self.set_undefined_options(
+ 'install',
+ ('install_data', 'install_dir'),
+ ('root', 'root'),
+ ('force', 'force'),
+ )
def run(self):
self.mkpath(self.install_dir)
@@ -46,9 +50,10 @@ class install_data(Command):
# it's a simple file, so copy it
f = convert_path(f)
if self.warn_dir:
- self.warn("setup script did not provide a directory for "
- "'%s' -- installing right in '%s'" %
- (f, self.install_dir))
+ self.warn(
+ "setup script did not provide a directory for "
+ "'%s' -- installing right in '%s'" % (f, self.install_dir)
+ )
(out, _) = self.copy_file(f, self.install_dir)
self.outfiles.append(out)
else:
diff --git a/setuptools/_distutils/command/install_egg_info.py b/setuptools/_distutils/command/install_egg_info.py
index adc0323f..f3e8f344 100644
--- a/setuptools/_distutils/command/install_egg_info.py
+++ b/setuptools/_distutils/command/install_egg_info.py
@@ -1,12 +1,18 @@
-"""distutils.command.install_egg_info
+"""
+distutils.command.install_egg_info
Implements the Distutils 'install_egg_info' command, for installing
-a package's PKG-INFO metadata."""
+a package's PKG-INFO metadata.
+"""
+import os
+import sys
+import re
+
+from ..cmd import Command
+from .. import dir_util
+from .._log import log
-from distutils.cmd import Command
-from distutils import log, dir_util
-import os, sys, re
class install_egg_info(Command):
"""Install an .egg-info file for the package"""
@@ -28,11 +34,11 @@ class install_egg_info(Command):
return "%s-%s-py%d.%d.egg-info" % (
to_filename(safe_name(self.distribution.get_name())),
to_filename(safe_version(self.distribution.get_version())),
- *sys.version_info[:2]
+ *sys.version_info[:2],
)
def finalize_options(self):
- self.set_undefined_options('install_lib',('install_dir','install_dir'))
+ self.set_undefined_options('install_lib', ('install_dir', 'install_dir'))
self.target = os.path.join(self.install_dir, self.basename)
self.outputs = [self.target]
@@ -41,10 +47,11 @@ class install_egg_info(Command):
if os.path.isdir(target) and not os.path.islink(target):
dir_util.remove_tree(target, dry_run=self.dry_run)
elif os.path.exists(target):
- self.execute(os.unlink,(self.target,),"Removing "+target)
+ self.execute(os.unlink, (self.target,), "Removing " + target)
elif not os.path.isdir(self.install_dir):
- self.execute(os.makedirs, (self.install_dir,),
- "Creating "+self.install_dir)
+ self.execute(
+ os.makedirs, (self.install_dir,), "Creating " + self.install_dir
+ )
log.info("Writing %s", target)
if not self.dry_run:
with open(target, 'w', encoding='UTF-8') as f:
@@ -58,6 +65,7 @@ class install_egg_info(Command):
# can be replaced by importing them from pkg_resources once it is included
# in the stdlib.
+
def safe_name(name):
"""Convert an arbitrary string to a standard distribution name
@@ -72,7 +80,7 @@ def safe_version(version):
Spaces become dots, and all other non-alphanumeric characters become
dashes, with runs of multiple dashes condensed to a single dash.
"""
- version = version.replace(' ','.')
+ version = version.replace(' ', '.')
return re.sub('[^A-Za-z0-9.]+', '-', version)
@@ -81,4 +89,4 @@ def to_filename(name):
Any '-' characters are currently replaced with '_'.
"""
- return name.replace('-','_')
+ return name.replace('-', '_')
diff --git a/setuptools/_distutils/command/install_headers.py b/setuptools/_distutils/command/install_headers.py
index 9bb0b18d..1cdee823 100644
--- a/setuptools/_distutils/command/install_headers.py
+++ b/setuptools/_distutils/command/install_headers.py
@@ -3,7 +3,7 @@
Implements the Distutils 'install_headers' command, to install C/C++ header
files to the Python include directory."""
-from distutils.core import Command
+from ..core import Command
# XXX force is never used
@@ -11,11 +11,10 @@ class install_headers(Command):
description = "install C/C++ header files"
- user_options = [('install-dir=', 'd',
- "directory to install header files to"),
- ('force', 'f',
- "force installation (overwrite existing files)"),
- ]
+ user_options = [
+ ('install-dir=', 'd', "directory to install header files to"),
+ ('force', 'f', "force installation (overwrite existing files)"),
+ ]
boolean_options = ['force']
@@ -25,10 +24,9 @@ class install_headers(Command):
self.outfiles = []
def finalize_options(self):
- self.set_undefined_options('install',
- ('install_headers', 'install_dir'),
- ('force', 'force'))
-
+ self.set_undefined_options(
+ 'install', ('install_headers', 'install_dir'), ('force', 'force')
+ )
def run(self):
headers = self.distribution.headers
diff --git a/setuptools/_distutils/command/install_lib.py b/setuptools/_distutils/command/install_lib.py
index 6154cf09..840d3403 100644
--- a/setuptools/_distutils/command/install_lib.py
+++ b/setuptools/_distutils/command/install_lib.py
@@ -7,13 +7,14 @@ import os
import importlib.util
import sys
-from distutils.core import Command
-from distutils.errors import DistutilsOptionError
+from ..core import Command
+from ..errors import DistutilsOptionError
# Extension for Python source files.
PYTHON_SOURCE_EXTENSION = ".py"
+
class install_lib(Command):
description = "install all Python modules (extensions and pure Python)"
@@ -35,18 +36,21 @@ class install_lib(Command):
user_options = [
('install-dir=', 'd', "directory to install to"),
- ('build-dir=','b', "build directory (where to install from)"),
+ ('build-dir=', 'b', "build directory (where to install from)"),
('force', 'f', "force installation (overwrite existing files)"),
('compile', 'c', "compile .py to .pyc [default]"),
('no-compile', None, "don't compile .py files"),
- ('optimize=', 'O',
- "also compile with optimization: -O1 for \"python -O\", "
- "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"),
+ (
+ 'optimize=',
+ 'O',
+ "also compile with optimization: -O1 for \"python -O\", "
+ "-O2 for \"python -OO\", and -O0 to disable [default: -O0]",
+ ),
('skip-build', None, "skip the build steps"),
- ]
+ ]
boolean_options = ['force', 'compile', 'skip-build']
- negative_opt = {'no-compile' : 'compile'}
+ negative_opt = {'no-compile': 'compile'}
def initialize_options(self):
# let the 'install' command dictate our installation directory
@@ -61,14 +65,15 @@ class install_lib(Command):
# Get all the information we need to install pure Python modules
# from the umbrella 'install' command -- build (source) directory,
# install (target) directory, and whether to compile .py files.
- self.set_undefined_options('install',
- ('build_lib', 'build_dir'),
- ('install_lib', 'install_dir'),
- ('force', 'force'),
- ('compile', 'compile'),
- ('optimize', 'optimize'),
- ('skip_build', 'skip_build'),
- )
+ self.set_undefined_options(
+ 'install',
+ ('build_lib', 'build_dir'),
+ ('install_lib', 'install_dir'),
+ ('force', 'force'),
+ ('compile', 'compile'),
+ ('optimize', 'optimize'),
+ ('skip_build', 'skip_build'),
+ )
if self.compile is None:
self.compile = True
@@ -110,8 +115,9 @@ class install_lib(Command):
if os.path.isdir(self.build_dir):
outfiles = self.copy_tree(self.build_dir, self.install_dir)
else:
- self.warn("'%s' does not exist -- no Python modules to install" %
- self.build_dir)
+ self.warn(
+ "'%s' does not exist -- no Python modules to install" % self.build_dir
+ )
return
return outfiles
@@ -120,7 +126,7 @@ class install_lib(Command):
self.warn('byte-compiling is disabled, skipping.')
return
- from distutils.util import byte_compile
+ from ..util import byte_compile
# Get the "--root" directory supplied to the "install" command,
# and use it as a prefix to strip off the purported filename
@@ -129,14 +135,22 @@ class install_lib(Command):
install_root = self.get_finalized_command('install').root
if self.compile:
- byte_compile(files, optimize=0,
- force=self.force, prefix=install_root,
- dry_run=self.dry_run)
+ byte_compile(
+ files,
+ optimize=0,
+ force=self.force,
+ prefix=install_root,
+ dry_run=self.dry_run,
+ )
if self.optimize > 0:
- byte_compile(files, optimize=self.optimize,
- force=self.force, prefix=install_root,
- verbose=self.verbose, dry_run=self.dry_run)
-
+ byte_compile(
+ files,
+ optimize=self.optimize,
+ force=self.force,
+ prefix=install_root,
+ verbose=self.verbose,
+ dry_run=self.dry_run,
+ )
# -- Utility methods -----------------------------------------------
@@ -165,15 +179,18 @@ class install_lib(Command):
if ext != PYTHON_SOURCE_EXTENSION:
continue
if self.compile:
- bytecode_files.append(importlib.util.cache_from_source(
- py_file, optimization=''))
+ bytecode_files.append(
+ importlib.util.cache_from_source(py_file, optimization='')
+ )
if self.optimize > 0:
- bytecode_files.append(importlib.util.cache_from_source(
- py_file, optimization=self.optimize))
+ bytecode_files.append(
+ importlib.util.cache_from_source(
+ py_file, optimization=self.optimize
+ )
+ )
return bytecode_files
-
# -- External interface --------------------------------------------
# (called by outsiders)
@@ -182,19 +199,23 @@ class install_lib(Command):
were actually run. Not affected by the "dry-run" flag or whether
modules have actually been built yet.
"""
- pure_outputs = \
- self._mutate_outputs(self.distribution.has_pure_modules(),
- 'build_py', 'build_lib',
- self.install_dir)
+ pure_outputs = self._mutate_outputs(
+ self.distribution.has_pure_modules(),
+ 'build_py',
+ 'build_lib',
+ self.install_dir,
+ )
if self.compile:
bytecode_outputs = self._bytecode_filenames(pure_outputs)
else:
bytecode_outputs = []
- ext_outputs = \
- self._mutate_outputs(self.distribution.has_ext_modules(),
- 'build_ext', 'build_lib',
- self.install_dir)
+ ext_outputs = self._mutate_outputs(
+ self.distribution.has_ext_modules(),
+ 'build_ext',
+ 'build_lib',
+ self.install_dir,
+ )
return pure_outputs + bytecode_outputs + ext_outputs
diff --git a/setuptools/_distutils/command/install_scripts.py b/setuptools/_distutils/command/install_scripts.py
index 31a1130e..ec6ec5ac 100644
--- a/setuptools/_distutils/command/install_scripts.py
+++ b/setuptools/_distutils/command/install_scripts.py
@@ -6,8 +6,8 @@ Python scripts."""
# contributed by Bastian Kleineidam
import os
-from distutils.core import Command
-from distutils import log
+from ..core import Command
+from distutils._log import log
from stat import ST_MODE
@@ -17,7 +17,7 @@ class install_scripts(Command):
user_options = [
('install-dir=', 'd', "directory to install scripts to"),
- ('build-dir=','b', "build directory (where to install from)"),
+ ('build-dir=', 'b', "build directory (where to install from)"),
('force', 'f', "force installation (overwrite existing files)"),
('skip-build', None, "skip the build steps"),
]
@@ -32,11 +32,12 @@ class install_scripts(Command):
def finalize_options(self):
self.set_undefined_options('build', ('build_scripts', 'build_dir'))
- self.set_undefined_options('install',
- ('install_scripts', 'install_dir'),
- ('force', 'force'),
- ('skip_build', 'skip_build'),
- )
+ self.set_undefined_options(
+ 'install',
+ ('install_scripts', 'install_dir'),
+ ('force', 'force'),
+ ('skip_build', 'skip_build'),
+ )
def run(self):
if not self.skip_build:
diff --git a/setuptools/_distutils/command/py37compat.py b/setuptools/_distutils/command/py37compat.py
index 754715a5..aa0c0a7f 100644
--- a/setuptools/_distutils/command/py37compat.py
+++ b/setuptools/_distutils/command/py37compat.py
@@ -7,12 +7,13 @@ def _pythonlib_compat():
library. See pypa/distutils#9.
"""
from distutils import sysconfig
+
if not sysconfig.get_config_var('Py_ENABLED_SHARED'):
return
yield 'python{}.{}{}'.format(
sys.hexversion >> 24,
- (sys.hexversion >> 16) & 0xff,
+ (sys.hexversion >> 16) & 0xFF,
sysconfig.get_config_var('ABIFLAGS'),
)
diff --git a/setuptools/_distutils/command/register.py b/setuptools/_distutils/command/register.py
index 0fac94e9..55c1045e 100644
--- a/setuptools/_distutils/command/register.py
+++ b/setuptools/_distutils/command/register.py
@@ -7,24 +7,31 @@ Implements the Distutils 'register' command (register with the repository).
import getpass
import io
-import urllib.parse, urllib.request
+import logging
+import urllib.parse
+import urllib.request
from warnings import warn
-from distutils.core import PyPIRCCommand
-from distutils.errors import *
-from distutils import log
+from ..core import PyPIRCCommand
+from distutils._log import log
+
class register(PyPIRCCommand):
- description = ("register the distribution with the Python package index")
+ description = "register the distribution with the Python package index"
user_options = PyPIRCCommand.user_options + [
- ('list-classifiers', None,
- 'list the valid Trove classifiers'),
- ('strict', None ,
- 'Will stop the registering if the meta-data are not fully compliant')
- ]
+ ('list-classifiers', None, 'list the valid Trove classifiers'),
+ (
+ 'strict',
+ None,
+ 'Will stop the registering if the meta-data are not fully compliant',
+ ),
+ ]
boolean_options = PyPIRCCommand.boolean_options + [
- 'verify', 'list-classifiers', 'strict']
+ 'verify',
+ 'list-classifiers',
+ 'strict',
+ ]
sub_commands = [('check', lambda self: True)]
@@ -36,8 +43,10 @@ class register(PyPIRCCommand):
def finalize_options(self):
PyPIRCCommand.finalize_options(self)
# setting options for the `check` subcommand
- check_options = {'strict': ('register', self.strict),
- 'restructuredtext': ('register', 1)}
+ check_options = {
+ 'strict': ('register', self.strict),
+ 'restructuredtext': ('register', 1),
+ }
self.distribution.command_options['check'] = check_options
def run(self):
@@ -57,8 +66,11 @@ class register(PyPIRCCommand):
def check_metadata(self):
"""Deprecated API."""
- warn("distutils.command.register.check_metadata is deprecated, \
- use the check command instead", PendingDeprecationWarning)
+ warn(
+ "distutils.command.register.check_metadata is deprecated; "
+ "use the check command instead",
+ DeprecationWarning,
+ )
check = self.distribution.get_command_obj('check')
check.ensure_finalized()
check.strict = self.strict
@@ -66,8 +78,7 @@ class register(PyPIRCCommand):
check.run()
def _set_config(self):
- ''' Reads the configuration file and set attributes.
- '''
+ '''Reads the configuration file and set attributes.'''
config = self._read_pypirc()
if config != {}:
self.username = config['username']
@@ -83,45 +94,43 @@ class register(PyPIRCCommand):
self.has_config = False
def classifiers(self):
- ''' Fetch the list of classifiers from the server.
- '''
- url = self.repository+'?:action=list_classifiers'
+ '''Fetch the list of classifiers from the server.'''
+ url = self.repository + '?:action=list_classifiers'
response = urllib.request.urlopen(url)
log.info(self._read_pypi_response(response))
def verify_metadata(self):
- ''' Send the metadata to the package index server to be checked.
- '''
+ '''Send the metadata to the package index server to be checked.'''
# send the info to the server and report the result
(code, result) = self.post_to_server(self.build_post_data('verify'))
log.info('Server response (%s): %s', code, result)
- def send_metadata(self):
- ''' Send the metadata to the package index server.
+ def send_metadata(self): # noqa: C901
+ '''Send the metadata to the package index server.
- Well, do the following:
- 1. figure who the user is, and then
- 2. send the data as a Basic auth'ed POST.
+ Well, do the following:
+ 1. figure who the user is, and then
+ 2. send the data as a Basic auth'ed POST.
- First we try to read the username/password from $HOME/.pypirc,
- which is a ConfigParser-formatted file with a section
- [distutils] containing username and password entries (both
- in clear text). Eg:
+ First we try to read the username/password from $HOME/.pypirc,
+ which is a ConfigParser-formatted file with a section
+ [distutils] containing username and password entries (both
+ in clear text). Eg:
- [distutils]
- index-servers =
- pypi
+ [distutils]
+ index-servers =
+ pypi
- [pypi]
- username: fred
- password: sekrit
+ [pypi]
+ username: fred
+ password: sekrit
- Otherwise, to figure who the user is, we offer the user three
- choices:
+ Otherwise, to figure who the user is, we offer the user three
+ choices:
- 1. use existing login,
- 2. register as a new user, or
- 3. set the password to a random string and email the user.
+ 1. use existing login,
+ 2. register as a new user, or
+ 3. set the password to a random string and email the user.
'''
# see if we can short-cut and get the username/password from the
@@ -137,13 +146,16 @@ class register(PyPIRCCommand):
# get the user's login info
choices = '1 2 3 4'.split()
while choice not in choices:
- self.announce('''\
+ self.announce(
+ '''\
We need to know who you are, so please choose either:
1. use your existing login,
2. register as a new user,
3. have the server generate a new password for you (and email it to you), or
4. quit
-Your selection [default 1]: ''', log.INFO)
+Your selection [default 1]: ''',
+ logging.INFO,
+ )
choice = input()
if not choice:
choice = '1'
@@ -162,10 +174,8 @@ Your selection [default 1]: ''', log.INFO)
host = urllib.parse.urlparse(self.repository)[1]
auth.add_password(self.realm, host, username, password)
# send the info to the server and report the result
- code, result = self.post_to_server(self.build_post_data('submit'),
- auth)
- self.announce('Server response (%s): %s' % (code, result),
- log.INFO)
+ code, result = self.post_to_server(self.build_post_data('submit'), auth)
+ self.announce('Server response ({}): {}'.format(code, result), logging.INFO)
# possibly save the login
if code == 200:
@@ -174,10 +184,17 @@ Your selection [default 1]: ''', log.INFO)
# so the upload command can reuse it
self.distribution.password = password
else:
- self.announce(('I can store your PyPI login so future '
- 'submissions will be faster.'), log.INFO)
- self.announce('(the login will be stored in %s)' % \
- self._get_rc_file(), log.INFO)
+ self.announce(
+ (
+ 'I can store your PyPI login so future '
+ 'submissions will be faster.'
+ ),
+ logging.INFO,
+ )
+ self.announce(
+ '(the login will be stored in %s)' % self._get_rc_file(),
+ logging.INFO,
+ )
choice = 'X'
while choice.lower() not in 'yn':
choice = input('Save your login (y/N)?')
@@ -208,8 +225,7 @@ Your selection [default 1]: ''', log.INFO)
log.info('Server response (%s): %s', code, result)
else:
log.info('You will receive an email shortly.')
- log.info(('Follow the instructions in it to '
- 'complete registration.'))
+ log.info('Follow the instructions in it to ' 'complete registration.')
elif choice == '3':
data = {':action': 'password_reset'}
data['email'] = ''
@@ -224,7 +240,7 @@ Your selection [default 1]: ''', log.INFO)
meta = self.distribution.metadata
data = {
':action': action,
- 'metadata_version' : '1.0',
+ 'metadata_version': '1.0',
'name': meta.get_name(),
'version': meta.get_version(),
'summary': meta.get_description(),
@@ -246,13 +262,13 @@ Your selection [default 1]: ''', log.INFO)
data['metadata_version'] = '1.1'
return data
- def post_to_server(self, data, auth=None):
- ''' Post a query to the server, and return a string response.
- '''
+ def post_to_server(self, data, auth=None): # noqa: C901
+ '''Post a query to the server, and return a string response.'''
if 'name' in data:
- self.announce('Registering %s to %s' % (data['name'],
- self.repository),
- log.INFO)
+ self.announce(
+ 'Registering {} to {}'.format(data['name'], self.repository),
+ logging.INFO,
+ )
# Build up the MIME payload for the urllib2 POST data
boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
sep_boundary = '\n--' + boundary
@@ -260,12 +276,12 @@ Your selection [default 1]: ''', log.INFO)
body = io.StringIO()
for key, value in data.items():
# handle multiple entries for the same name
- if type(value) not in (type([]), type( () )):
+ if type(value) not in (type([]), type(())):
value = [value]
for value in value:
value = str(value)
body.write(sep_boundary)
- body.write('\nContent-Disposition: form-data; name="%s"'%key)
+ body.write('\nContent-Disposition: form-data; name="%s"' % key)
body.write("\n\n")
body.write(value)
if value and value[-1] == '\r':
@@ -276,8 +292,9 @@ Your selection [default 1]: ''', log.INFO)
# build the Request
headers = {
- 'Content-type': 'multipart/form-data; boundary=%s; charset=utf-8'%boundary,
- 'Content-length': str(len(body))
+ 'Content-type': 'multipart/form-data; boundary=%s; charset=utf-8'
+ % boundary,
+ 'Content-length': str(len(body)),
}
req = urllib.request.Request(self.repository, body, headers)
@@ -300,5 +317,5 @@ Your selection [default 1]: ''', log.INFO)
result = 200, 'OK'
if self.show_response:
msg = '\n'.join(('-' * 75, data, '-' * 75))
- self.announce(msg, log.INFO)
+ self.announce(msg, logging.INFO)
return result
diff --git a/setuptools/_distutils/command/sdist.py b/setuptools/_distutils/command/sdist.py
index b4996fcb..5cfd4c14 100644
--- a/setuptools/_distutils/command/sdist.py
+++ b/setuptools/_distutils/command/sdist.py
@@ -7,30 +7,29 @@ import sys
from glob import glob
from warnings import warn
-from distutils.core import Command
+from ..core import Command
from distutils import dir_util
from distutils import file_util
from distutils import archive_util
-from distutils.text_file import TextFile
-from distutils.filelist import FileList
-from distutils import log
-from distutils.util import convert_path
-from distutils.errors import DistutilsTemplateError, DistutilsOptionError
+from ..text_file import TextFile
+from ..filelist import FileList
+from distutils._log import log
+from ..util import convert_path
+from ..errors import DistutilsOptionError, DistutilsTemplateError
def show_formats():
"""Print all possible values for the 'formats' option (used by
the "--help-formats" command-line option).
"""
- from distutils.fancy_getopt import FancyGetopt
- from distutils.archive_util import ARCHIVE_FORMATS
+ from ..fancy_getopt import FancyGetopt
+ from ..archive_util import ARCHIVE_FORMATS
+
formats = []
for format in ARCHIVE_FORMATS.keys():
- formats.append(("formats=" + format, None,
- ARCHIVE_FORMATS[format][2]))
+ formats.append(("formats=" + format, None, ARCHIVE_FORMATS[format][2]))
formats.sort()
- FancyGetopt(formats).print_help(
- "List of available source distribution formats:")
+ FancyGetopt(formats).print_help("List of available source distribution formats:")
class sdist(Command):
@@ -44,55 +43,77 @@ class sdist(Command):
return self.metadata_check
user_options = [
- ('template=', 't',
- "name of manifest template file [default: MANIFEST.in]"),
- ('manifest=', 'm',
- "name of manifest file [default: MANIFEST]"),
- ('use-defaults', None,
- "include the default file set in the manifest "
- "[default; disable with --no-defaults]"),
- ('no-defaults', None,
- "don't include the default file set"),
- ('prune', None,
- "specifically exclude files/directories that should not be "
- "distributed (build tree, RCS/CVS dirs, etc.) "
- "[default; disable with --no-prune]"),
- ('no-prune', None,
- "don't automatically exclude anything"),
- ('manifest-only', 'o',
- "just regenerate the manifest and then stop "
- "(implies --force-manifest)"),
- ('force-manifest', 'f',
- "forcibly regenerate the manifest and carry on as usual. "
- "Deprecated: now the manifest is always regenerated."),
- ('formats=', None,
- "formats for source distribution (comma-separated list)"),
- ('keep-temp', 'k',
- "keep the distribution tree around after creating " +
- "archive file(s)"),
- ('dist-dir=', 'd',
- "directory to put the source distribution archive(s) in "
- "[default: dist]"),
- ('metadata-check', None,
- "Ensure that all required elements of meta-data "
- "are supplied. Warn if any missing. [default]"),
- ('owner=', 'u',
- "Owner name used when creating a tar file [default: current user]"),
- ('group=', 'g',
- "Group name used when creating a tar file [default: current group]"),
- ]
-
- boolean_options = ['use-defaults', 'prune',
- 'manifest-only', 'force-manifest',
- 'keep-temp', 'metadata-check']
+ ('template=', 't', "name of manifest template file [default: MANIFEST.in]"),
+ ('manifest=', 'm', "name of manifest file [default: MANIFEST]"),
+ (
+ 'use-defaults',
+ None,
+ "include the default file set in the manifest "
+ "[default; disable with --no-defaults]",
+ ),
+ ('no-defaults', None, "don't include the default file set"),
+ (
+ 'prune',
+ None,
+ "specifically exclude files/directories that should not be "
+ "distributed (build tree, RCS/CVS dirs, etc.) "
+ "[default; disable with --no-prune]",
+ ),
+ ('no-prune', None, "don't automatically exclude anything"),
+ (
+ 'manifest-only',
+ 'o',
+ "just regenerate the manifest and then stop " "(implies --force-manifest)",
+ ),
+ (
+ 'force-manifest',
+ 'f',
+ "forcibly regenerate the manifest and carry on as usual. "
+ "Deprecated: now the manifest is always regenerated.",
+ ),
+ ('formats=', None, "formats for source distribution (comma-separated list)"),
+ (
+ 'keep-temp',
+ 'k',
+ "keep the distribution tree around after creating " + "archive file(s)",
+ ),
+ (
+ 'dist-dir=',
+ 'd',
+ "directory to put the source distribution archive(s) in " "[default: dist]",
+ ),
+ (
+ 'metadata-check',
+ None,
+ "Ensure that all required elements of meta-data "
+ "are supplied. Warn if any missing. [default]",
+ ),
+ (
+ 'owner=',
+ 'u',
+ "Owner name used when creating a tar file [default: current user]",
+ ),
+ (
+ 'group=',
+ 'g',
+ "Group name used when creating a tar file [default: current group]",
+ ),
+ ]
+
+ boolean_options = [
+ 'use-defaults',
+ 'prune',
+ 'manifest-only',
+ 'force-manifest',
+ 'keep-temp',
+ 'metadata-check',
+ ]
help_options = [
- ('help-formats', None,
- "list available distribution formats", show_formats),
- ]
+ ('help-formats', None, "list available distribution formats", show_formats),
+ ]
- negative_opt = {'no-defaults': 'use-defaults',
- 'no-prune': 'prune' }
+ negative_opt = {'no-defaults': 'use-defaults', 'no-prune': 'prune'}
sub_commands = [('check', checking_metadata)]
@@ -131,8 +152,7 @@ class sdist(Command):
bad_format = archive_util.check_archive_formats(self.formats)
if bad_format:
- raise DistutilsOptionError(
- "unknown archive format '%s'" % bad_format)
+ raise DistutilsOptionError("unknown archive format '%s'" % bad_format)
if self.dist_dir is None:
self.dist_dir = "dist"
@@ -161,8 +181,11 @@ class sdist(Command):
def check_metadata(self):
"""Deprecated API."""
- warn("distutils.command.sdist.check_metadata is deprecated, \
- use the check command instead", PendingDeprecationWarning)
+ warn(
+ "distutils.command.sdist.check_metadata is deprecated, \
+ use the check command instead",
+ PendingDeprecationWarning,
+ )
check = self.distribution.get_command_obj('check')
check.ensure_finalized()
check.run()
@@ -189,9 +212,10 @@ class sdist(Command):
return
if not template_exists:
- self.warn(("manifest template '%s' does not exist " +
- "(using default file list)") %
- self.template)
+ self.warn(
+ ("manifest template '%s' does not exist " + "(using default file list)")
+ % self.template
+ )
self.filelist.findall()
if self.use_defaults:
@@ -259,8 +283,9 @@ class sdist(Command):
break
if not got_it:
- self.warn("standard file not found: should have one of " +
- ', '.join(alts))
+ self.warn(
+ "standard file not found: should have one of " + ', '.join(alts)
+ )
else:
if self._cs_path_exists(fn):
self.filelist.append(fn)
@@ -328,14 +353,20 @@ class sdist(Command):
'self.filelist', which updates itself accordingly.
"""
log.info("reading manifest template '%s'", self.template)
- template = TextFile(self.template, strip_comments=1, skip_blanks=1,
- join_lines=1, lstrip_ws=1, rstrip_ws=1,
- collapse_join=1)
+ template = TextFile(
+ self.template,
+ strip_comments=1,
+ skip_blanks=1,
+ join_lines=1,
+ lstrip_ws=1,
+ rstrip_ws=1,
+ collapse_join=1,
+ )
try:
while True:
line = template.readline()
- if line is None: # end of file
+ if line is None: # end of file
break
try:
@@ -344,9 +375,10 @@ class sdist(Command):
# malformed lines, or a ValueError from the lower-level
# convert_path function
except (DistutilsTemplateError, ValueError) as msg:
- self.warn("%s, line %d: %s" % (template.filename,
- template.current_line,
- msg))
+ self.warn(
+ "%s, line %d: %s"
+ % (template.filename, template.current_line, msg)
+ )
finally:
template.close()
@@ -369,9 +401,8 @@ class sdist(Command):
else:
seps = '/'
- vcs_dirs = ['RCS', 'CVS', r'\.svn', r'\.hg', r'\.git', r'\.bzr',
- '_darcs']
- vcs_ptrn = r'(^|%s)(%s)(%s).*' % (seps, '|'.join(vcs_dirs), seps)
+ vcs_dirs = ['RCS', 'CVS', r'\.svn', r'\.hg', r'\.git', r'\.bzr', '_darcs']
+ vcs_ptrn = r'(^|{})({})({}).*'.format(seps, '|'.join(vcs_dirs), seps)
self.filelist.exclude_pattern(vcs_ptrn, is_regex=1)
def write_manifest(self):
@@ -380,14 +411,19 @@ class sdist(Command):
named by 'self.manifest'.
"""
if self._manifest_is_not_generated():
- log.info("not writing to manually maintained "
- "manifest file '%s'" % self.manifest)
+ log.info(
+ "not writing to manually maintained "
+ "manifest file '%s'" % self.manifest
+ )
return
content = self.filelist.files[:]
content.insert(0, '# file GENERATED by distutils, do NOT edit')
- self.execute(file_util.write_file, (self.manifest, content),
- "writing manifest file '%s'" % self.manifest)
+ self.execute(
+ file_util.write_file,
+ (self.manifest, content),
+ "writing manifest file '%s'" % self.manifest,
+ )
def _manifest_is_not_generated(self):
# check for special comment used in 3.1.3 and higher
@@ -437,20 +473,20 @@ class sdist(Command):
# out-of-date, because by default we blow away 'base_dir' when
# we're done making the distribution archives.)
- if hasattr(os, 'link'): # can make hard links on this system
+ if hasattr(os, 'link'): # can make hard links on this system
link = 'hard'
msg = "making hard links in %s..." % base_dir
- else: # nope, have to copy
+ else: # nope, have to copy
link = None
msg = "copying files to %s..." % base_dir
if not files:
- log.warn("no files to distribute -- empty manifest?")
+ log.warning("no files to distribute -- empty manifest?")
else:
log.info(msg)
for file in files:
if not os.path.isfile(file):
- log.warn("'%s' not a regular file -- skipping", file)
+ log.warning("'%s' not a regular file -- skipping", file)
else:
dest = os.path.join(base_dir, file)
self.copy_file(file, dest, link=link)
@@ -471,14 +507,15 @@ class sdist(Command):
base_name = os.path.join(self.dist_dir, base_dir)
self.make_release_tree(base_dir, self.filelist.files)
- archive_files = [] # remember names of files we create
+ archive_files = [] # remember names of files we create
# tar archive must be created last to avoid overwrite and remove
if 'tar' in self.formats:
self.formats.append(self.formats.pop(self.formats.index('tar')))
for fmt in self.formats:
- file = self.make_archive(base_name, fmt, base_dir=base_dir,
- owner=self.owner, group=self.group)
+ file = self.make_archive(
+ base_name, fmt, base_dir=base_dir, owner=self.owner, group=self.group
+ )
archive_files.append(file)
self.distribution.dist_files.append(('sdist', '', file))
diff --git a/setuptools/_distutils/command/upload.py b/setuptools/_distutils/command/upload.py
index 95e9fda1..16e15d8b 100644
--- a/setuptools/_distutils/command/upload.py
+++ b/setuptools/_distutils/command/upload.py
@@ -8,13 +8,13 @@ index).
import os
import io
import hashlib
+import logging
from base64 import standard_b64encode
from urllib.request import urlopen, Request, HTTPError
from urllib.parse import urlparse
-from distutils.errors import DistutilsError, DistutilsOptionError
-from distutils.core import PyPIRCCommand
-from distutils.spawn import spawn
-from distutils import log
+from ..errors import DistutilsError, DistutilsOptionError
+from ..core import PyPIRCCommand
+from ..spawn import spawn
# PyPI Warehouse supports MD5, SHA256, and Blake2 (blake2-256)
@@ -31,10 +31,9 @@ class upload(PyPIRCCommand):
description = "upload binary package to PyPI"
user_options = PyPIRCCommand.user_options + [
- ('sign', 's',
- 'sign files to upload using gpg'),
+ ('sign', 's', 'sign files to upload using gpg'),
('identity=', 'i', 'GPG identity used to sign files'),
- ]
+ ]
boolean_options = PyPIRCCommand.boolean_options + ['sign']
@@ -49,9 +48,7 @@ class upload(PyPIRCCommand):
def finalize_options(self):
PyPIRCCommand.finalize_options(self)
if self.identity and not self.sign:
- raise DistutilsOptionError(
- "Must use --sign for --identity to have meaning"
- )
+ raise DistutilsOptionError("Must use --sign for --identity to have meaning")
config = self._read_pypirc()
if config != {}:
self.username = config['username']
@@ -66,16 +63,17 @@ class upload(PyPIRCCommand):
def run(self):
if not self.distribution.dist_files:
- msg = ("Must create and upload files in one command "
- "(e.g. setup.py sdist upload)")
+ msg = (
+ "Must create and upload files in one command "
+ "(e.g. setup.py sdist upload)"
+ )
raise DistutilsOptionError(msg)
for command, pyversion, filename in self.distribution.dist_files:
self.upload_file(command, pyversion, filename)
- def upload_file(self, command, pyversion, filename):
+ def upload_file(self, command, pyversion, filename): # noqa: C901
# Makes sure the repository URL is compliant
- schema, netloc, url, params, query, fragments = \
- urlparse(self.repository)
+ schema, netloc, url, params, query, fragments = urlparse(self.repository)
if params or query or fragments:
raise AssertionError("Incompatible url %s" % self.repository)
@@ -87,12 +85,11 @@ class upload(PyPIRCCommand):
gpg_args = ["gpg", "--detach-sign", "-a", filename]
if self.identity:
gpg_args[2:2] = ["--local-user", self.identity]
- spawn(gpg_args,
- dry_run=self.dry_run)
+ spawn(gpg_args, dry_run=self.dry_run)
# Fill in the data - send all the meta-data in case we need to
# register a new release
- f = open(filename,'rb')
+ f = open(filename, 'rb')
try:
content = f.read()
finally:
@@ -103,16 +100,13 @@ class upload(PyPIRCCommand):
# action
':action': 'file_upload',
'protocol_version': '1',
-
# identify release
'name': meta.get_name(),
'version': meta.get_version(),
-
# file content
- 'content': (os.path.basename(filename),content),
+ 'content': (os.path.basename(filename), content),
'filetype': command,
'pyversion': pyversion,
-
# additional meta-data
'metadata_version': '1.0',
'summary': meta.get_description(),
@@ -129,7 +123,7 @@ class upload(PyPIRCCommand):
'provides': meta.get_provides(),
'requires': meta.get_requires(),
'obsoletes': meta.get_obsoletes(),
- }
+ }
data['comment'] = ''
@@ -145,8 +139,7 @@ class upload(PyPIRCCommand):
if self.sign:
with open(filename + ".asc", "rb") as f:
- data['gpg_signature'] = (os.path.basename(filename) + ".asc",
- f.read())
+ data['gpg_signature'] = (os.path.basename(filename) + ".asc", f.read())
# set up the authentication
user_pass = (self.username + ":" + self.password).encode('ascii')
@@ -177,8 +170,8 @@ class upload(PyPIRCCommand):
body.write(end_boundary)
body = body.getvalue()
- msg = "Submitting %s to %s" % (filename, self.repository)
- self.announce(msg, log.INFO)
+ msg = "Submitting {} to {}".format(filename, self.repository)
+ self.announce(msg, logging.INFO)
# build the Request
headers = {
@@ -187,8 +180,7 @@ class upload(PyPIRCCommand):
'Authorization': auth,
}
- request = Request(self.repository, data=body,
- headers=headers)
+ request = Request(self.repository, data=body, headers=headers)
# send the data
try:
result = urlopen(request)
@@ -198,17 +190,18 @@ class upload(PyPIRCCommand):
status = e.code
reason = e.msg
except OSError as e:
- self.announce(str(e), log.ERROR)
+ self.announce(str(e), logging.ERROR)
raise
if status == 200:
- self.announce('Server response (%s): %s' % (status, reason),
- log.INFO)
+ self.announce(
+ 'Server response ({}): {}'.format(status, reason), logging.INFO
+ )
if self.show_response:
text = self._read_pypi_response(result)
msg = '\n'.join(('-' * 75, text, '-' * 75))
- self.announce(msg, log.INFO)
+ self.announce(msg, logging.INFO)
else:
- msg = 'Upload failed (%s): %s' % (status, reason)
- self.announce(msg, log.ERROR)
+ msg = 'Upload failed ({}): {}'.format(status, reason)
+ self.announce(msg, logging.ERROR)
raise DistutilsError(msg)
diff --git a/setuptools/_distutils/config.py b/setuptools/_distutils/config.py
index 2171abd6..9a4044ad 100644
--- a/setuptools/_distutils/config.py
+++ b/setuptools/_distutils/config.py
@@ -6,7 +6,7 @@ that uses .pypirc in the distutils.command package.
import os
from configparser import RawConfigParser
-from distutils.cmd import Command
+from .cmd import Command
DEFAULT_PYPIRC = """\
[distutils]
@@ -18,20 +18,19 @@ username:%s
password:%s
"""
+
class PyPIRCCommand(Command):
- """Base command that knows how to handle the .pypirc file
- """
+ """Base command that knows how to handle the .pypirc file"""
+
DEFAULT_REPOSITORY = 'https://upload.pypi.org/legacy/'
DEFAULT_REALM = 'pypi'
repository = None
realm = None
user_options = [
- ('repository=', 'r',
- "url of repository [default: %s]" % \
- DEFAULT_REPOSITORY),
- ('show-response', None,
- 'display full response text from server')]
+ ('repository=', 'r', "url of repository [default: %s]" % DEFAULT_REPOSITORY),
+ ('show-response', None, 'display full response text from server'),
+ ]
boolean_options = ['show-response']
@@ -45,7 +44,7 @@ class PyPIRCCommand(Command):
with os.fdopen(os.open(rc, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
f.write(DEFAULT_PYPIRC % (username, password))
- def _read_pypirc(self):
+ def _read_pypirc(self): # noqa: C901
"""Reads the .pypirc file."""
rc = self._get_rc_file()
if os.path.exists(rc):
@@ -58,9 +57,11 @@ class PyPIRCCommand(Command):
if 'distutils' in sections:
# let's get the list of servers
index_servers = config.get('distutils', 'index-servers')
- _servers = [server.strip() for server in
- index_servers.split('\n')
- if server.strip() != '']
+ _servers = [
+ server.strip()
+ for server in index_servers.split('\n')
+ if server.strip() != ''
+ ]
if _servers == []:
# nothing set, let's try to get the default pypi
if 'pypi' in sections:
@@ -74,10 +75,11 @@ class PyPIRCCommand(Command):
current['username'] = config.get(server, 'username')
# optional params
- for key, default in (('repository',
- self.DEFAULT_REPOSITORY),
- ('realm', self.DEFAULT_REALM),
- ('password', None)):
+ for key, default in (
+ ('repository', self.DEFAULT_REPOSITORY),
+ ('realm', self.DEFAULT_REALM),
+ ('password', None),
+ ):
if config.has_option(server, key):
current[key] = config.get(server, key)
else:
@@ -86,13 +88,17 @@ class PyPIRCCommand(Command):
# work around people having "repository" for the "pypi"
# section of their config set to the HTTP (rather than
# HTTPS) URL
- if (server == 'pypi' and
- repository in (self.DEFAULT_REPOSITORY, 'pypi')):
+ if server == 'pypi' and repository in (
+ self.DEFAULT_REPOSITORY,
+ 'pypi',
+ ):
current['repository'] = self.DEFAULT_REPOSITORY
return current
- if (current['server'] == repository or
- current['repository'] == repository):
+ if (
+ current['server'] == repository
+ or current['repository'] == repository
+ ):
return current
elif 'server-login' in sections:
# old format
@@ -101,17 +107,20 @@ class PyPIRCCommand(Command):
repository = config.get(server, 'repository')
else:
repository = self.DEFAULT_REPOSITORY
- return {'username': config.get(server, 'username'),
- 'password': config.get(server, 'password'),
- 'repository': repository,
- 'server': server,
- 'realm': self.DEFAULT_REALM}
+ return {
+ 'username': config.get(server, 'username'),
+ 'password': config.get(server, 'password'),
+ 'repository': repository,
+ 'server': server,
+ 'realm': self.DEFAULT_REALM,
+ }
return {}
def _read_pypi_response(self, response):
"""Read and decode a PyPI HTTP response."""
import cgi
+
content_type = response.getheader('content-type', 'text/plain')
encoding = cgi.parse_header(content_type)[1].get('charset', 'ascii')
return response.read().decode(encoding)
diff --git a/setuptools/_distutils/core.py b/setuptools/_distutils/core.py
index f43888ea..34cafbce 100644
--- a/setuptools/_distutils/core.py
+++ b/setuptools/_distutils/core.py
@@ -10,14 +10,22 @@ import os
import sys
import tokenize
-from distutils.debug import DEBUG
-from distutils.errors import *
+from .debug import DEBUG
+from .errors import (
+ DistutilsSetupError,
+ DistutilsError,
+ CCompilerError,
+ DistutilsArgError,
+)
# Mainly import these so setup scripts can "from distutils.core import" them.
-from distutils.dist import Distribution
-from distutils.cmd import Command
-from distutils.config import PyPIRCCommand
-from distutils.extension import Extension
+from .dist import Distribution
+from .cmd import Command
+from .config import PyPIRCCommand
+from .extension import Extension
+
+
+__all__ = ['Distribution', 'Command', 'PyPIRCCommand', 'Extension', 'setup']
# This is a barebones help message generated displayed when the user
# runs the setup script with no arguments at all. More useful help
@@ -30,9 +38,10 @@ usage: %(script)s [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: %(script)s cmd --help
"""
-def gen_usage (script_name):
+
+def gen_usage(script_name):
script = os.path.basename(script_name)
- return USAGE % vars()
+ return USAGE % locals()
# Some mild magic to control the behaviour of 'setup()' from 'run_setup()'.
@@ -40,22 +49,51 @@ _setup_stop_after = None
_setup_distribution = None
# Legal keyword arguments for the setup() function
-setup_keywords = ('distclass', 'script_name', 'script_args', 'options',
- 'name', 'version', 'author', 'author_email',
- 'maintainer', 'maintainer_email', 'url', 'license',
- 'description', 'long_description', 'keywords',
- 'platforms', 'classifiers', 'download_url',
- 'requires', 'provides', 'obsoletes',
- )
+setup_keywords = (
+ 'distclass',
+ 'script_name',
+ 'script_args',
+ 'options',
+ 'name',
+ 'version',
+ 'author',
+ 'author_email',
+ 'maintainer',
+ 'maintainer_email',
+ 'url',
+ 'license',
+ 'description',
+ 'long_description',
+ 'keywords',
+ 'platforms',
+ 'classifiers',
+ 'download_url',
+ 'requires',
+ 'provides',
+ 'obsoletes',
+)
# Legal keyword arguments for the Extension constructor
-extension_keywords = ('name', 'sources', 'include_dirs',
- 'define_macros', 'undef_macros',
- 'library_dirs', 'libraries', 'runtime_library_dirs',
- 'extra_objects', 'extra_compile_args', 'extra_link_args',
- 'swig_opts', 'export_symbols', 'depends', 'language')
-
-def setup (**attrs):
+extension_keywords = (
+ 'name',
+ 'sources',
+ 'include_dirs',
+ 'define_macros',
+ 'undef_macros',
+ 'library_dirs',
+ 'libraries',
+ 'runtime_library_dirs',
+ 'extra_objects',
+ 'extra_compile_args',
+ 'extra_link_args',
+ 'swig_opts',
+ 'export_symbols',
+ 'depends',
+ 'language',
+)
+
+
+def setup(**attrs): # noqa: C901
"""The gateway to the Distutils: do everything your setup script needs
to do, in a highly flexible and user-driven way. Briefly: create a
Distribution instance; find and parse config files; parse the command
@@ -100,7 +138,7 @@ def setup (**attrs):
if 'script_name' not in attrs:
attrs['script_name'] = os.path.basename(sys.argv[0])
- if 'script_args' not in attrs:
+ if 'script_args' not in attrs:
attrs['script_args'] = sys.argv[1:]
# Create the Distribution instance, using the remaining arguments
@@ -111,8 +149,7 @@ def setup (**attrs):
if 'name' not in attrs:
raise SystemExit("error in setup command: %s" % msg)
else:
- raise SystemExit("error in %s setup command: %s" % \
- (attrs['name'], msg))
+ raise SystemExit("error in {} setup command: {}".format(attrs['name'], msg))
if _setup_stop_after == "init":
return dist
@@ -149,10 +186,11 @@ def setup (**attrs):
return dist
+
# setup ()
-def run_commands (dist):
+def run_commands(dist):
"""Given a Distribution object run all the commands,
raising ``SystemExit`` errors in the case of failure.
@@ -165,13 +203,12 @@ def run_commands (dist):
raise SystemExit("interrupted")
except OSError as exc:
if DEBUG:
- sys.stderr.write("error: %s\n" % (exc,))
+ sys.stderr.write("error: {}\n".format(exc))
raise
else:
- raise SystemExit("error: %s" % (exc,))
+ raise SystemExit("error: {}".format(exc))
- except (DistutilsError,
- CCompilerError) as msg:
+ except (DistutilsError, CCompilerError) as msg:
if DEBUG:
raise
else:
@@ -180,7 +217,7 @@ def run_commands (dist):
return dist
-def run_setup (script_name, script_args=None, stop_after="run"):
+def run_setup(script_name, script_args=None, stop_after="run"):
"""Run a setup script in a somewhat controlled environment, and
return the Distribution instance that drives things. This is useful
if you need to find out the distribution meta-data (passed as
@@ -212,7 +249,7 @@ def run_setup (script_name, script_args=None, stop_after="run"):
used to drive the Distutils.
"""
if stop_after not in ('init', 'config', 'commandline', 'run'):
- raise ValueError("invalid value for 'stop_after': %r" % (stop_after,))
+ raise ValueError("invalid value for 'stop_after': {!r}".format(stop_after))
global _setup_stop_after, _setup_distribution
_setup_stop_after = stop_after
@@ -237,13 +274,18 @@ def run_setup (script_name, script_args=None, stop_after="run"):
pass
if _setup_distribution is None:
- raise RuntimeError(("'distutils.core.setup()' was never called -- "
- "perhaps '%s' is not a Distutils setup script?") % \
- script_name)
+ raise RuntimeError(
+ (
+ "'distutils.core.setup()' was never called -- "
+ "perhaps '%s' is not a Distutils setup script?"
+ )
+ % script_name
+ )
# I wonder if the setup script's namespace -- g and l -- would be of
# any interest to callers?
- #print "_setup_distribution:", _setup_distribution
+ # print "_setup_distribution:", _setup_distribution
return _setup_distribution
+
# run_setup ()
diff --git a/setuptools/_distutils/cygwinccompiler.py b/setuptools/_distutils/cygwinccompiler.py
index c5c86d8f..f15b8eee 100644
--- a/setuptools/_distutils/cygwinccompiler.py
+++ b/setuptools/_distutils/cygwinccompiler.py
@@ -6,104 +6,81 @@ the Mingw32CCompiler class which handles the mingw32 port of GCC (same as
cygwin in no-cygwin mode).
"""
-# problems:
-#
-# * if you use a msvc compiled python version (1.5.2)
-# 1. you have to insert a __GNUC__ section in its config.h
-# 2. you have to generate an import library for its dll
-# - create a def-file for python??.dll
-# - create an import library using
-# dlltool --dllname python15.dll --def python15.def \
-# --output-lib libpython15.a
-#
-# see also http://starship.python.net/crew/kernr/mingw32/Notes.html
-#
-# * We put export_symbols in a def-file, and don't use
-# --export-all-symbols because it doesn't worked reliable in some
-# tested configurations. And because other windows compilers also
-# need their symbols specified this no serious problem.
-#
-# tested configurations:
-#
-# * cygwin gcc 2.91.57/ld 2.9.4/dllwrap 0.2.4 works
-# (after patching python's config.h and for C++ some other include files)
-# see also http://starship.python.net/crew/kernr/mingw32/Notes.html
-# * mingw32 gcc 2.95.2/ld 2.9.4/dllwrap 0.2.4 works
-# (ld doesn't support -shared, so we use dllwrap)
-# * cygwin gcc 2.95.2/ld 2.10.90/dllwrap 2.10.90 works now
-# - its dllwrap doesn't work, there is a bug in binutils 2.10.90
-# see also http://sources.redhat.com/ml/cygwin/2000-06/msg01274.html
-# - using gcc -mdll instead dllwrap doesn't work without -static because
-# it tries to link against dlls instead their import libraries. (If
-# it finds the dll first.)
-# By specifying -static we force ld to link against the import libraries,
-# this is windows standard and there are normally not the necessary symbols
-# in the dlls.
-# *** only the version of June 2000 shows these problems
-# * cygwin gcc 3.2/ld 2.13.90 works
-# (ld supports -shared)
-# * mingw gcc 3.2/ld 2.13 works
-# (ld supports -shared)
-# * llvm-mingw with Clang 11 works
-# (lld supports -shared)
-
import os
+import re
import sys
import copy
import shlex
import warnings
from subprocess import check_output
-from distutils.unixccompiler import UnixCCompiler
-from distutils.file_util import write_file
-from distutils.errors import (DistutilsExecError, CCompilerError,
- CompileError, UnknownFileError)
-from distutils.version import LooseVersion, suppress_known_deprecation
+from .unixccompiler import UnixCCompiler
+from .file_util import write_file
+from .errors import (
+ DistutilsExecError,
+ DistutilsPlatformError,
+ CCompilerError,
+ CompileError,
+)
+from .version import LooseVersion, suppress_known_deprecation
+from ._collections import RangeMap
+
+
+_msvcr_lookup = RangeMap.left(
+ {
+ # MSVC 7.0
+ 1300: ['msvcr70'],
+ # MSVC 7.1
+ 1310: ['msvcr71'],
+ # VS2005 / MSVC 8.0
+ 1400: ['msvcr80'],
+ # VS2008 / MSVC 9.0
+ 1500: ['msvcr90'],
+ # VS2010 / MSVC 10.0
+ 1600: ['msvcr100'],
+ # VS2012 / MSVC 11.0
+ 1700: ['msvcr110'],
+ # VS2013 / MSVC 12.0
+ 1800: ['msvcr120'],
+ # VS2015 / MSVC 14.0
+ 1900: ['ucrt', 'vcruntime140'],
+ 2000: RangeMap.undefined_value,
+ },
+)
+
def get_msvcr():
"""Include the appropriate MSVC runtime library if Python was built
with MSVC 7.0 or later.
"""
- msc_pos = sys.version.find('MSC v.')
- if msc_pos != -1:
- msc_ver = sys.version[msc_pos+6:msc_pos+10]
- if msc_ver == '1300':
- # MSVC 7.0
- return ['msvcr70']
- elif msc_ver == '1310':
- # MSVC 7.1
- return ['msvcr71']
- elif msc_ver == '1400':
- # VS2005 / MSVC 8.0
- return ['msvcr80']
- elif msc_ver == '1500':
- # VS2008 / MSVC 9.0
- return ['msvcr90']
- elif msc_ver == '1600':
- # VS2010 / MSVC 10.0
- return ['msvcr100']
- elif msc_ver == '1700':
- # VS2012 / MSVC 11.0
- return ['msvcr110']
- elif msc_ver == '1800':
- # VS2013 / MSVC 12.0
- return ['msvcr120']
- elif 1900 <= int(msc_ver) < 2000:
- # VS2015 / MSVC 14.0
- return ['ucrt', 'vcruntime140']
- else:
- raise ValueError("Unknown MS Compiler version %s " % msc_ver)
+ match = re.search(r'MSC v\.(\d{4})', sys.version)
+ try:
+ msc_ver = int(match.group(1))
+ except AttributeError:
+ return
+ try:
+ return _msvcr_lookup[msc_ver]
+ except KeyError:
+ raise ValueError("Unknown MS Compiler version %s " % msc_ver)
+
+
+_runtime_library_dirs_msg = (
+ "Unable to set runtime library search path on Windows, "
+ "usually indicated by `runtime_library_dirs` parameter to Extension"
+)
class CygwinCCompiler(UnixCCompiler):
- """ Handles the Cygwin port of the GNU C compiler to Windows.
- """
+ """Handles the Cygwin port of the GNU C compiler to Windows."""
+
compiler_type = 'cygwin'
obj_extension = ".o"
static_lib_extension = ".a"
- shared_lib_extension = ".dll"
+ shared_lib_extension = ".dll.a"
+ dylib_lib_extension = ".dll"
static_lib_format = "lib%s%s"
- shared_lib_format = "%s%s"
+ shared_lib_format = "lib%s%s"
+ dylib_lib_format = "cyg%s%s"
exe_extension = ".exe"
def __init__(self, verbose=0, dry_run=0, force=0):
@@ -111,14 +88,15 @@ class CygwinCCompiler(UnixCCompiler):
super().__init__(verbose, dry_run, force)
status, details = check_config_h()
- self.debug_print("Python's GCC status: %s (details: %s)" %
- (status, details))
+ self.debug_print(
+ "Python's GCC status: {} (details: {})".format(status, details)
+ )
if status is not CONFIG_H_OK:
self.warn(
"Python's pyconfig.h doesn't seem to support your compiler. "
"Reason: %s. "
- "Compiling may fail because of undefined preprocessor macros."
- % details)
+ "Compiling may fail because of undefined preprocessor macros." % details
+ )
self.cc = os.environ.get('CC', 'gcc')
self.cxx = os.environ.get('CXX', 'g++')
@@ -126,12 +104,13 @@ class CygwinCCompiler(UnixCCompiler):
self.linker_dll = self.cc
shared_option = "-shared"
- self.set_executables(compiler='%s -mcygwin -O -Wall' % self.cc,
- compiler_so='%s -mcygwin -mdll -O -Wall' % self.cc,
- compiler_cxx='%s -mcygwin -O -Wall' % self.cxx,
- linker_exe='%s -mcygwin' % self.cc,
- linker_so=('%s -mcygwin %s' %
- (self.linker_dll, shared_option)))
+ self.set_executables(
+ compiler='%s -mcygwin -O -Wall' % self.cc,
+ compiler_so='%s -mcygwin -mdll -O -Wall' % self.cc,
+ compiler_cxx='%s -mcygwin -O -Wall' % self.cxx,
+ linker_exe='%s -mcygwin' % self.cc,
+ linker_so=('{} -mcygwin {}'.format(self.linker_dll, shared_option)),
+ )
# Include the appropriate MSVC runtime library if Python was built
# with MSVC 7.0 or later.
@@ -160,30 +139,47 @@ class CygwinCCompiler(UnixCCompiler):
self.spawn(["windres", "-i", src, "-o", obj])
except DistutilsExecError as msg:
raise CompileError(msg)
- else: # for other files use the C-compiler
+ else: # for other files use the C-compiler
try:
- self.spawn(self.compiler_so + cc_args + [src, '-o', obj] +
- extra_postargs)
+ self.spawn(
+ self.compiler_so + cc_args + [src, '-o', obj] + extra_postargs
+ )
except DistutilsExecError as msg:
raise CompileError(msg)
- def link(self, target_desc, objects, output_filename, output_dir=None,
- libraries=None, library_dirs=None, runtime_library_dirs=None,
- export_symbols=None, debug=0, extra_preargs=None,
- extra_postargs=None, build_temp=None, target_lang=None):
+ def link(
+ self,
+ target_desc,
+ objects,
+ output_filename,
+ output_dir=None,
+ libraries=None,
+ library_dirs=None,
+ runtime_library_dirs=None,
+ export_symbols=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ build_temp=None,
+ target_lang=None,
+ ):
"""Link the objects."""
# use separate copies, so we can modify the lists
extra_preargs = copy.copy(extra_preargs or [])
libraries = copy.copy(libraries or [])
objects = copy.copy(objects or [])
+ if runtime_library_dirs:
+ self.warn(_runtime_library_dirs_msg)
+
# Additional libraries
libraries.extend(self.dll_libraries)
# handle export symbols by creating a def-file
# with executables this only works with gcc/ld as linker
- if ((export_symbols is not None) and
- (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
+ if (export_symbols is not None) and (
+ target_desc != self.EXECUTABLE or self.linker_dll == "gcc"
+ ):
# (The linker doesn't do anything if output is up-to-date.
# So it would probably better to check if we really need this,
# but for this we had to insert some unchanged parts of
@@ -195,29 +191,24 @@ class CygwinCCompiler(UnixCCompiler):
temp_dir = os.path.dirname(objects[0])
# name of dll to give the helper files the same base name
(dll_name, dll_extension) = os.path.splitext(
- os.path.basename(output_filename))
+ os.path.basename(output_filename)
+ )
# generate the filenames for these files
def_file = os.path.join(temp_dir, dll_name + ".def")
- lib_file = os.path.join(temp_dir, 'lib' + dll_name + ".a")
# Generate .def file
- contents = [
- "LIBRARY %s" % os.path.basename(output_filename),
- "EXPORTS"]
+ contents = ["LIBRARY %s" % os.path.basename(output_filename), "EXPORTS"]
for sym in export_symbols:
contents.append(sym)
- self.execute(write_file, (def_file, contents),
- "writing %s" % def_file)
+ self.execute(write_file, (def_file, contents), "writing %s" % def_file)
- # next add options for def-file and to creating import libraries
+ # next add options for def-file
- # doesn't work: bfd_close build\...\libfoo.a: Invalid operation
- #extra_preargs.extend(["-Wl,--out-implib,%s" % lib_file])
# for gcc/ld the def-file is specified as any object files
objects.append(def_file)
- #end: if ((export_symbols is not None) and
+ # end: if ((export_symbols is not None) and
# (target_desc != self.EXECUTABLE or self.linker_dll == "gcc")):
# who wants symbols and a many times larger output file
@@ -229,70 +220,74 @@ class CygwinCCompiler(UnixCCompiler):
if not debug:
extra_preargs.append("-s")
- UnixCCompiler.link(self, target_desc, objects, output_filename,
- output_dir, libraries, library_dirs,
- runtime_library_dirs,
- None, # export_symbols, we do this in our def-file
- debug, extra_preargs, extra_postargs, build_temp,
- target_lang)
+ UnixCCompiler.link(
+ self,
+ target_desc,
+ objects,
+ output_filename,
+ output_dir,
+ libraries,
+ library_dirs,
+ runtime_library_dirs,
+ None, # export_symbols, we do this in our def-file
+ debug,
+ extra_preargs,
+ extra_postargs,
+ build_temp,
+ target_lang,
+ )
+
+ def runtime_library_dir_option(self, dir):
+ # cygwin doesn't support rpath. While in theory we could error
+ # out like MSVC does, code might expect it to work like on Unix, so
+ # just warn and hope for the best.
+ self.warn(_runtime_library_dirs_msg)
+ return []
# -- Miscellaneous methods -----------------------------------------
- def object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
- """Adds supports for rc and res files."""
- if output_dir is None:
- output_dir = ''
- obj_names = []
- for src_name in source_filenames:
- # use normcase to make sure '.rc' is really '.rc' and not '.RC'
- base, ext = os.path.splitext(os.path.normcase(src_name))
- if ext not in (self.src_extensions + ['.rc','.res']):
- raise UnknownFileError("unknown file type '%s' (from '%s')" % \
- (ext, src_name))
- if strip_dir:
- base = os.path.basename (base)
- if ext in ('.res', '.rc'):
- # these need to be compiled to object files
- obj_names.append (os.path.join(output_dir,
- base + ext + self.obj_extension))
- else:
- obj_names.append (os.path.join(output_dir,
- base + self.obj_extension))
- return obj_names
+ def _make_out_path(self, output_dir, strip_dir, src_name):
+ # use normcase to make sure '.rc' is really '.rc' and not '.RC'
+ norm_src_name = os.path.normcase(src_name)
+ return super()._make_out_path(output_dir, strip_dir, norm_src_name)
+
+ @property
+ def out_extensions(self):
+ """
+ Add support for rc and res files.
+ """
+ return {
+ **super().out_extensions,
+ **{ext: ext + self.obj_extension for ext in ('.res', '.rc')},
+ }
+
# the same as cygwin plus some additional parameters
class Mingw32CCompiler(CygwinCCompiler):
- """ Handles the Mingw32 port of the GNU C compiler to Windows.
- """
+ """Handles the Mingw32 port of the GNU C compiler to Windows."""
+
compiler_type = 'mingw32'
def __init__(self, verbose=0, dry_run=0, force=0):
- super().__init__ (verbose, dry_run, force)
+ super().__init__(verbose, dry_run, force)
shared_option = "-shared"
if is_cygwincc(self.cc):
- raise CCompilerError(
- 'Cygwin gcc cannot be used with --compiler=mingw32')
-
- self.set_executables(compiler='%s -O -Wall' % self.cc,
- compiler_so='%s -mdll -O -Wall' % self.cc,
- compiler_cxx='%s -O -Wall' % self.cxx,
- linker_exe='%s' % self.cc,
- linker_so='%s %s'
- % (self.linker_dll, shared_option))
+ raise CCompilerError('Cygwin gcc cannot be used with --compiler=mingw32')
+
+ self.set_executables(
+ compiler='%s -O -Wall' % self.cc,
+ compiler_so='%s -mdll -O -Wall' % self.cc,
+ compiler_cxx='%s -O -Wall' % self.cxx,
+ linker_exe='%s' % self.cc,
+ linker_so='{} {}'.format(self.linker_dll, shared_option),
+ )
- # Maybe we should also append -mthreads, but then the finished
- # dlls need another dll (mingwm10.dll see Mingw32 docs)
- # (-mthreads: Support thread-safe exception handling on `Mingw32')
+ def runtime_library_dir_option(self, dir):
+ raise DistutilsPlatformError(_runtime_library_dirs_msg)
- # no additional libraries needed
- self.dll_libraries=[]
-
- # Include the appropriate MSVC runtime library if Python was built
- # with MSVC 7.0 or later.
- self.dll_libraries = get_msvcr()
# Because these compilers aren't configured in Python's pyconfig.h file by
# default, we should at least warn the user if he is using an unmodified
@@ -302,6 +297,7 @@ CONFIG_H_OK = "ok"
CONFIG_H_NOTOK = "not ok"
CONFIG_H_UNCERTAIN = "uncertain"
+
def check_config_h():
"""Check if the current Python installation appears amenable to building
extensions with GCC.
@@ -346,8 +342,8 @@ def check_config_h():
finally:
config_h.close()
except OSError as exc:
- return (CONFIG_H_UNCERTAIN,
- "couldn't read '%s': %s" % (fn, exc.strerror))
+ return (CONFIG_H_UNCERTAIN, "couldn't read '{}': {}".format(fn, exc.strerror))
+
def is_cygwincc(cc):
'''Try to determine if the compiler that would be used is from cygwin.'''
diff --git a/setuptools/_distutils/dep_util.py b/setuptools/_distutils/dep_util.py
index d74f5e4e..48da8641 100644
--- a/setuptools/_distutils/dep_util.py
+++ b/setuptools/_distutils/dep_util.py
@@ -5,31 +5,32 @@ and groups of files; also, function based entirely on such
timestamp dependency analysis."""
import os
-from distutils.errors import DistutilsFileError
+from .errors import DistutilsFileError
-def newer (source, target):
+def newer(source, target):
"""Return true if 'source' exists and is more recently modified than
'target', or if 'source' exists and 'target' doesn't. Return false if
both exist and 'target' is the same age or younger than 'source'.
Raise DistutilsFileError if 'source' does not exist.
"""
if not os.path.exists(source):
- raise DistutilsFileError("file '%s' does not exist" %
- os.path.abspath(source))
+ raise DistutilsFileError("file '%s' does not exist" % os.path.abspath(source))
if not os.path.exists(target):
return 1
from stat import ST_MTIME
+
mtime1 = os.stat(source)[ST_MTIME]
mtime2 = os.stat(target)[ST_MTIME]
return mtime1 > mtime2
+
# newer ()
-def newer_pairwise (sources, targets):
+def newer_pairwise(sources, targets):
"""Walk two filename lists in parallel, testing if each source is newer
than its corresponding target. Return a pair of lists (sources,
targets) where source is newer than target, according to the semantics
@@ -48,10 +49,11 @@ def newer_pairwise (sources, targets):
return (n_sources, n_targets)
+
# newer_pairwise ()
-def newer_group (sources, target, missing='error'):
+def newer_group(sources, target, missing='error'):
"""Return true if 'target' is out-of-date with respect to any file
listed in 'sources'. In other words, if 'target' exists and is newer
than every file in 'sources', return false; otherwise return true.
@@ -73,15 +75,16 @@ def newer_group (sources, target, missing='error'):
# we can immediately return true. If we fall through to the end
# of the loop, then 'target' is up-to-date and we return false.
from stat import ST_MTIME
+
target_mtime = os.stat(target)[ST_MTIME]
for source in sources:
if not os.path.exists(source):
- if missing == 'error': # blow up when we stat() the file
+ if missing == 'error': # blow up when we stat() the file
pass
- elif missing == 'ignore': # missing source dropped from
- continue # target's dependency list
- elif missing == 'newer': # missing source means target is
- return 1 # out-of-date
+ elif missing == 'ignore': # missing source dropped from
+ continue # target's dependency list
+ elif missing == 'newer': # missing source means target is
+ return 1 # out-of-date
source_mtime = os.stat(source)[ST_MTIME]
if source_mtime > target_mtime:
@@ -89,4 +92,5 @@ def newer_group (sources, target, missing='error'):
else:
return 0
+
# newer_group ()
diff --git a/setuptools/_distutils/dir_util.py b/setuptools/_distutils/dir_util.py
index d5cd8e3e..80f77649 100644
--- a/setuptools/_distutils/dir_util.py
+++ b/setuptools/_distutils/dir_util.py
@@ -4,17 +4,15 @@ Utility functions for manipulating directories and directory trees."""
import os
import errno
-from distutils.errors import DistutilsFileError, DistutilsInternalError
-from distutils import log
+from .errors import DistutilsInternalError, DistutilsFileError
+from ._log import log
# cache for by mkpath() -- in addition to cheapening redundant calls,
# eliminates redundant "creating /foo/bar/baz" messages in dry-run mode
_path_created = {}
-# I don't use os.makedirs because a) it's new to Python 1.5.2, and
-# b) it blows up if the directory already exists (I want to silently
-# succeed in that case).
-def mkpath(name, mode=0o777, verbose=1, dry_run=0):
+
+def mkpath(name, mode=0o777, verbose=1, dry_run=0): # noqa: C901
"""Create a directory and any missing ancestor directories.
If the directory already exists (or if 'name' is the empty string, which
@@ -23,6 +21,12 @@ def mkpath(name, mode=0o777, verbose=1, dry_run=0):
(eg. some sub-path exists, but is a file rather than a directory).
If 'verbose' is true, print a one-line summary of each mkdir to stdout.
Return the list of directories actually created.
+
+ os.makedirs is not used because:
+
+ a) It's new to Python 1.5.2, and
+ b) it blows up if the directory already exists (in which case it should
+ silently succeed).
"""
global _path_created
@@ -30,7 +34,8 @@ def mkpath(name, mode=0o777, verbose=1, dry_run=0):
# Detect a common bug -- name is None
if not isinstance(name, str):
raise DistutilsInternalError(
- "mkpath: 'name' must be a string (got %r)" % (name,))
+ "mkpath: 'name' must be a string (got {!r})".format(name)
+ )
# XXX what's the better way to handle verbosity? print as we create
# each directory in the path (the current behaviour), or only announce
@@ -45,17 +50,17 @@ def mkpath(name, mode=0o777, verbose=1, dry_run=0):
return created_dirs
(head, tail) = os.path.split(name)
- tails = [tail] # stack of lone dirs to create
+ tails = [tail] # stack of lone dirs to create
while head and tail and not os.path.isdir(head):
(head, tail) = os.path.split(head)
- tails.insert(0, tail) # push next higher dir onto stack
+ tails.insert(0, tail) # push next higher dir onto stack
# now 'head' contains the deepest directory that already exists
# (that is, the child of 'head' in 'name' is the highest directory
# that does *not* exist)
for d in tails:
- #print "head = %s, d = %s: " % (head, d),
+ # print "head = %s, d = %s: " % (head, d),
head = os.path.join(head, d)
abs_head = os.path.abspath(head)
@@ -71,12 +76,14 @@ def mkpath(name, mode=0o777, verbose=1, dry_run=0):
except OSError as exc:
if not (exc.errno == errno.EEXIST and os.path.isdir(head)):
raise DistutilsFileError(
- "could not create '%s': %s" % (head, exc.args[-1]))
+ "could not create '{}': {}".format(head, exc.args[-1])
+ )
created_dirs.append(head)
_path_created[abs_head] = 1
return created_dirs
+
def create_tree(base_dir, files, mode=0o777, verbose=1, dry_run=0):
"""Create all the empty directories under 'base_dir' needed to put 'files'
there.
@@ -96,8 +103,17 @@ def create_tree(base_dir, files, mode=0o777, verbose=1, dry_run=0):
for dir in sorted(need_dir):
mkpath(dir, mode, verbose=verbose, dry_run=dry_run)
-def copy_tree(src, dst, preserve_mode=1, preserve_times=1,
- preserve_symlinks=0, update=0, verbose=1, dry_run=0):
+
+def copy_tree( # noqa: C901
+ src,
+ dst,
+ preserve_mode=1,
+ preserve_times=1,
+ preserve_symlinks=0,
+ update=0,
+ verbose=1,
+ dry_run=0,
+):
"""Copy an entire directory tree 'src' to a new location 'dst'.
Both 'src' and 'dst' must be directory names. If 'src' is not a
@@ -120,8 +136,7 @@ def copy_tree(src, dst, preserve_mode=1, preserve_times=1,
from distutils.file_util import copy_file
if not dry_run and not os.path.isdir(src):
- raise DistutilsFileError(
- "cannot copy tree '%s': not a directory" % src)
+ raise DistutilsFileError("cannot copy tree '%s': not a directory" % src)
try:
names = os.listdir(src)
except OSError as e:
@@ -129,7 +144,8 @@ def copy_tree(src, dst, preserve_mode=1, preserve_times=1,
names = []
else:
raise DistutilsFileError(
- "error listing files in '%s': %s" % (src, e.strerror))
+ "error listing files in '{}': {}".format(src, e.strerror)
+ )
if not dry_run:
mkpath(dst, verbose=verbose)
@@ -154,27 +170,43 @@ def copy_tree(src, dst, preserve_mode=1, preserve_times=1,
elif os.path.isdir(src_name):
outputs.extend(
- copy_tree(src_name, dst_name, preserve_mode,
- preserve_times, preserve_symlinks, update,
- verbose=verbose, dry_run=dry_run))
+ copy_tree(
+ src_name,
+ dst_name,
+ preserve_mode,
+ preserve_times,
+ preserve_symlinks,
+ update,
+ verbose=verbose,
+ dry_run=dry_run,
+ )
+ )
else:
- copy_file(src_name, dst_name, preserve_mode,
- preserve_times, update, verbose=verbose,
- dry_run=dry_run)
+ copy_file(
+ src_name,
+ dst_name,
+ preserve_mode,
+ preserve_times,
+ update,
+ verbose=verbose,
+ dry_run=dry_run,
+ )
outputs.append(dst_name)
return outputs
+
def _build_cmdtuple(path, cmdtuples):
"""Helper for remove_tree()."""
for f in os.listdir(path):
- real_f = os.path.join(path,f)
+ real_f = os.path.join(path, f)
if os.path.isdir(real_f) and not os.path.islink(real_f):
_build_cmdtuple(real_f, cmdtuples)
else:
cmdtuples.append((os.remove, real_f))
cmdtuples.append((os.rmdir, path))
+
def remove_tree(directory, verbose=1, dry_run=0):
"""Recursively remove an entire directory tree.
@@ -197,7 +229,8 @@ def remove_tree(directory, verbose=1, dry_run=0):
if abspath in _path_created:
del _path_created[abspath]
except OSError as exc:
- log.warn("error removing %s: %s", directory, exc)
+ log.warning("error removing %s: %s", directory, exc)
+
def ensure_relative(path):
"""Take the full path 'path', and make it a relative path.
diff --git a/setuptools/_distutils/dist.py b/setuptools/_distutils/dist.py
index 37db4d6c..d7458a05 100644
--- a/setuptools/_distutils/dist.py
+++ b/setuptools/_distutils/dist.py
@@ -7,6 +7,9 @@ being built/installed/distributed.
import sys
import os
import re
+import pathlib
+import contextlib
+import logging
from email import message_from_file
try:
@@ -14,11 +17,16 @@ try:
except ImportError:
warnings = None
-from distutils.errors import *
-from distutils.fancy_getopt import FancyGetopt, translate_longopt
-from distutils.util import check_environ, strtobool, rfc822_escape
-from distutils import log
-from distutils.debug import DEBUG
+from .errors import (
+ DistutilsOptionError,
+ DistutilsModuleError,
+ DistutilsArgError,
+ DistutilsClassError,
+)
+from .fancy_getopt import FancyGetopt, translate_longopt
+from .util import check_environ, strtobool, rfc822_escape
+from ._log import log
+from .debug import DEBUG
# Regex to define acceptable Distutils command names. This is not *quite*
# the same as a Python NAME -- I don't allow leading underscores. The fact
@@ -37,7 +45,7 @@ def _ensure_list(value, fieldname):
typename = type(value).__name__
msg = "Warning: '{fieldname}' should be a list, got type '{typename}'"
msg = msg.format(**locals())
- log.log(log.WARN, msg)
+ log.warning(msg)
value = list(value)
return value
@@ -69,8 +77,7 @@ class Distribution:
('quiet', 'q', "run quietly (turns verbosity off)"),
('dry-run', 'n', "don't actually do anything"),
('help', 'h', "show detailed help message"),
- ('no-user-cfg', None,
- 'ignore pydistutils.cfg in your home directory'),
+ ('no-user-cfg', None, 'ignore pydistutils.cfg in your home directory'),
]
# 'common_usage' is a short (2-3 line) string describing the common
@@ -84,49 +91,32 @@ Common commands: (see '--help-commands' for more)
# options that are not propagated to the commands
display_options = [
- ('help-commands', None,
- "list all available commands"),
- ('name', None,
- "print package name"),
- ('version', 'V',
- "print package version"),
- ('fullname', None,
- "print <package name>-<version>"),
- ('author', None,
- "print the author's name"),
- ('author-email', None,
- "print the author's email address"),
- ('maintainer', None,
- "print the maintainer's name"),
- ('maintainer-email', None,
- "print the maintainer's email address"),
- ('contact', None,
- "print the maintainer's name if known, else the author's"),
- ('contact-email', None,
- "print the maintainer's email address if known, else the author's"),
- ('url', None,
- "print the URL for this package"),
- ('license', None,
- "print the license of the package"),
- ('licence', None,
- "alias for --license"),
- ('description', None,
- "print the package description"),
- ('long-description', None,
- "print the long package description"),
- ('platforms', None,
- "print the list of platforms"),
- ('classifiers', None,
- "print the list of classifiers"),
- ('keywords', None,
- "print the list of keywords"),
- ('provides', None,
- "print the list of packages/modules provided"),
- ('requires', None,
- "print the list of packages/modules required"),
- ('obsoletes', None,
- "print the list of packages/modules made obsolete")
- ]
+ ('help-commands', None, "list all available commands"),
+ ('name', None, "print package name"),
+ ('version', 'V', "print package version"),
+ ('fullname', None, "print <package name>-<version>"),
+ ('author', None, "print the author's name"),
+ ('author-email', None, "print the author's email address"),
+ ('maintainer', None, "print the maintainer's name"),
+ ('maintainer-email', None, "print the maintainer's email address"),
+ ('contact', None, "print the maintainer's name if known, else the author's"),
+ (
+ 'contact-email',
+ None,
+ "print the maintainer's email address if known, else the author's",
+ ),
+ ('url', None, "print the URL for this package"),
+ ('license', None, "print the license of the package"),
+ ('licence', None, "alias for --license"),
+ ('description', None, "print the package description"),
+ ('long-description', None, "print the long package description"),
+ ('platforms', None, "print the list of platforms"),
+ ('classifiers', None, "print the list of classifiers"),
+ ('keywords', None, "print the list of keywords"),
+ ('provides', None, "print the list of packages/modules provided"),
+ ('requires', None, "print the list of packages/modules required"),
+ ('obsoletes', None, "print the list of packages/modules made obsolete"),
+ ]
display_option_names = [translate_longopt(x[0]) for x in display_options]
# negative options are options that exclude other options
@@ -134,7 +124,7 @@ Common commands: (see '--help-commands' for more)
# -- Creation/initialization methods -------------------------------
- def __init__(self, attrs=None):
+ def __init__(self, attrs=None): # noqa: C901
"""Construct a new Distribution instance: initialize all the
attributes of a Distribution, and then use 'attrs' (a dictionary
mapping attribute names to values) to assign some of those
@@ -306,7 +296,7 @@ Common commands: (see '--help-commands' for more)
def dump_option_dicts(self, header=None, commands=None, indent=""):
from pprint import pformat
- if commands is None: # dump all command option dicts
+ if commands is None: # dump all command option dicts
commands = sorted(self.command_options.keys())
if header is not None:
@@ -320,11 +310,9 @@ Common commands: (see '--help-commands' for more)
for cmd_name in commands:
opt_dict = self.command_options.get(cmd_name)
if opt_dict is None:
- self.announce(indent +
- "no option dict for '%s' command" % cmd_name)
+ self.announce(indent + "no option dict for '%s' command" % cmd_name)
else:
- self.announce(indent +
- "option dict for '%s' command:" % cmd_name)
+ self.announce(indent + "option dict for '%s' command:" % cmd_name)
out = pformat(opt_dict)
for line in out.split('\n'):
self.announce(indent + " " + line)
@@ -337,58 +325,61 @@ Common commands: (see '--help-commands' for more)
should be parsed. The filenames returned are guaranteed to exist
(modulo nasty race conditions).
- There are three possible config files: distutils.cfg in the
- Distutils installation directory (ie. where the top-level
- Distutils __inst__.py file lives), a file in the user's home
- directory named .pydistutils.cfg on Unix and pydistutils.cfg
- on Windows/Mac; and setup.cfg in the current directory.
-
- The file in the user's home directory can be disabled with the
- --no-user-cfg option.
+ There are multiple possible config files:
+ - distutils.cfg in the Distutils installation directory (i.e.
+ where the top-level Distutils __inst__.py file lives)
+ - a file in the user's home directory named .pydistutils.cfg
+ on Unix and pydistutils.cfg on Windows/Mac; may be disabled
+ with the ``--no-user-cfg`` option
+ - setup.cfg in the current directory
+ - a file named by an environment variable
"""
- files = []
check_environ()
+ files = [str(path) for path in self._gen_paths() if os.path.isfile(path)]
- # Where to look for the system-wide Distutils config file
- sys_dir = os.path.dirname(sys.modules['distutils'].__file__)
+ if DEBUG:
+ self.announce("using config files: %s" % ', '.join(files))
- # Look for the system config file
- sys_file = os.path.join(sys_dir, "distutils.cfg")
- if os.path.isfile(sys_file):
- files.append(sys_file)
+ return files
- # What to call the per-user config file
- if os.name == 'posix':
- user_filename = ".pydistutils.cfg"
- else:
- user_filename = "pydistutils.cfg"
+ def _gen_paths(self):
+ # The system-wide Distutils config file
+ sys_dir = pathlib.Path(sys.modules['distutils'].__file__).parent
+ yield sys_dir / "distutils.cfg"
- # And look for the user config file
+ # The per-user config file
+ prefix = '.' * (os.name == 'posix')
+ filename = prefix + 'pydistutils.cfg'
if self.want_user_cfg:
- user_file = os.path.join(os.path.expanduser('~'), user_filename)
- if os.path.isfile(user_file):
- files.append(user_file)
+ yield pathlib.Path('~').expanduser() / filename
# All platforms support local setup.cfg
- local_file = "setup.cfg"
- if os.path.isfile(local_file):
- files.append(local_file)
+ yield pathlib.Path('setup.cfg')
- if DEBUG:
- self.announce("using config files: %s" % ', '.join(files))
-
- return files
+ # Additional config indicated in the environment
+ with contextlib.suppress(TypeError):
+ yield pathlib.Path(os.getenv("DIST_EXTRA_CONFIG"))
- def parse_config_files(self, filenames=None):
+ def parse_config_files(self, filenames=None): # noqa: C901
from configparser import ConfigParser
# Ignore install directory options if we have a venv
if sys.prefix != sys.base_prefix:
ignore_options = [
- 'install-base', 'install-platbase', 'install-lib',
- 'install-platlib', 'install-purelib', 'install-headers',
- 'install-scripts', 'install-data', 'prefix', 'exec-prefix',
- 'home', 'user', 'root']
+ 'install-base',
+ 'install-platbase',
+ 'install-lib',
+ 'install-platlib',
+ 'install-purelib',
+ 'install-headers',
+ 'install-scripts',
+ 'install-data',
+ 'prefix',
+ 'exec-prefix',
+ 'home',
+ 'user',
+ 'root',
+ ]
else:
ignore_options = []
@@ -411,7 +402,7 @@ Common commands: (see '--help-commands' for more)
for opt in options:
if opt != '__name__' and opt not in ignore_options:
- val = parser.get(section,opt)
+ val = parser.get(section, opt)
opt = opt.replace('-', '_')
opt_dict[opt] = (filename, val)
@@ -428,7 +419,7 @@ Common commands: (see '--help-commands' for more)
try:
if alias:
setattr(self, alias, not strtobool(val))
- elif opt in ('verbose', 'dry_run'): # ugh!
+ elif opt in ('verbose', 'dry_run'): # ugh!
setattr(self, opt, strtobool(val))
else:
setattr(self, opt, val)
@@ -475,14 +466,14 @@ Common commands: (see '--help-commands' for more)
parser.set_aliases({'licence': 'license'})
args = parser.getopt(args=self.script_args, object=self)
option_order = parser.get_option_order()
- log.set_verbosity(self.verbose)
+ logging.getLogger().setLevel(logging.WARN - 10 * self.verbose)
# for display options we return immediately
if self.handle_display_options(option_order):
return
while args:
args = self._parse_command_opts(parser, args)
- if args is None: # user asked for help (and got it)
+ if args is None: # user asked for help (and got it)
return
# Handle the cases of --help as a "global" option, ie.
@@ -492,9 +483,9 @@ Common commands: (see '--help-commands' for more)
# latter, we omit the display-only options and show help for
# each command listed on the command line.
if self.help:
- self._show_help(parser,
- display_options=len(self.commands) == 0,
- commands=self.commands)
+ self._show_help(
+ parser, display_options=len(self.commands) == 0, commands=self.commands
+ )
return
# Oops, no commands found -- an end-user error
@@ -511,11 +502,14 @@ Common commands: (see '--help-commands' for more)
level as well as options recognized for commands.
"""
return self.global_options + [
- ("command-packages=", None,
- "list of packages that provide distutils commands"),
- ]
+ (
+ "command-packages=",
+ None,
+ "list of packages that provide distutils commands",
+ ),
+ ]
- def _parse_command_opts(self, parser, args):
+ def _parse_command_opts(self, parser, args): # noqa: C901
"""Parse the command-line options for a single command.
'parser' must be a FancyGetopt instance; 'args' must be the list
of arguments, starting with the current command (whose options
@@ -545,14 +539,19 @@ Common commands: (see '--help-commands' for more)
# to be sure that the basic "command" interface is implemented.
if not issubclass(cmd_class, Command):
raise DistutilsClassError(
- "command class %s must subclass Command" % cmd_class)
+ "command class %s must subclass Command" % cmd_class
+ )
# Also make sure that the command object provides a list of its
# known options.
- if not (hasattr(cmd_class, 'user_options') and
- isinstance(cmd_class.user_options, list)):
- msg = ("command class %s must provide "
- "'user_options' attribute (a list of tuples)")
+ if not (
+ hasattr(cmd_class, 'user_options')
+ and isinstance(cmd_class.user_options, list)
+ ):
+ msg = (
+ "command class %s must provide "
+ "'user_options' attribute (a list of tuples)"
+ )
raise DistutilsClassError(msg % cmd_class)
# If the command class has a list of negative alias options,
@@ -564,36 +563,39 @@ Common commands: (see '--help-commands' for more)
# Check for help_options in command class. They have a different
# format (tuple of four) so we need to preprocess them here.
- if (hasattr(cmd_class, 'help_options') and
- isinstance(cmd_class.help_options, list)):
+ if hasattr(cmd_class, 'help_options') and isinstance(
+ cmd_class.help_options, list
+ ):
help_options = fix_help_options(cmd_class.help_options)
else:
help_options = []
# All commands support the global options too, just by adding
# in 'global_options'.
- parser.set_option_table(self.global_options +
- cmd_class.user_options +
- help_options)
+ parser.set_option_table(
+ self.global_options + cmd_class.user_options + help_options
+ )
parser.set_negative_aliases(negative_opt)
(args, opts) = parser.getopt(args[1:])
if hasattr(opts, 'help') and opts.help:
self._show_help(parser, display_options=0, commands=[cmd_class])
return
- if (hasattr(cmd_class, 'help_options') and
- isinstance(cmd_class.help_options, list)):
- help_option_found=0
+ if hasattr(cmd_class, 'help_options') and isinstance(
+ cmd_class.help_options, list
+ ):
+ help_option_found = 0
for (help_option, short, desc, func) in cmd_class.help_options:
if hasattr(opts, parser.get_attr_name(help_option)):
- help_option_found=1
+ help_option_found = 1
if callable(func):
func()
else:
raise DistutilsClassError(
"invalid help function %r for help option '%s': "
"must be a callable object (function, etc.)"
- % (func, help_option))
+ % (func, help_option)
+ )
if help_option_found:
return
@@ -619,8 +621,7 @@ Common commands: (see '--help-commands' for more)
value = [elm.strip() for elm in value.split(',')]
setattr(self.metadata, attr, value)
- def _show_help(self, parser, global_options=1, display_options=1,
- commands=[]):
+ def _show_help(self, parser, global_options=1, display_options=1, commands=[]):
"""Show help for the setup script command-line in the form of
several lists of command-line options. 'parser' should be a
FancyGetopt instance; do not expect it to be returned in the
@@ -649,8 +650,9 @@ Common commands: (see '--help-commands' for more)
if display_options:
parser.set_option_table(self.display_options)
parser.print_help(
- "Information display options (just display " +
- "information, ignore any commands)")
+ "Information display options (just display "
+ + "information, ignore any commands)"
+ )
print('')
for command in self.commands:
@@ -658,10 +660,10 @@ Common commands: (see '--help-commands' for more)
klass = command
else:
klass = self.get_command_class(command)
- if (hasattr(klass, 'help_options') and
- isinstance(klass.help_options, list)):
- parser.set_option_table(klass.user_options +
- fix_help_options(klass.help_options))
+ if hasattr(klass, 'help_options') and isinstance(klass.help_options, list):
+ parser.set_option_table(
+ klass.user_options + fix_help_options(klass.help_options)
+ )
else:
parser.set_option_table(klass.user_options)
parser.print_help("Options for '%s' command:" % klass.__name__)
@@ -697,11 +699,10 @@ Common commands: (see '--help-commands' for more)
for (opt, val) in option_order:
if val and is_display_option.get(opt):
opt = translate_longopt(opt)
- value = getattr(self.metadata, "get_"+opt)()
+ value = getattr(self.metadata, "get_" + opt)()
if opt in ['keywords', 'platforms']:
print(','.join(value))
- elif opt in ('classifiers', 'provides', 'requires',
- 'obsoletes'):
+ elif opt in ('classifiers', 'provides', 'requires', 'obsoletes'):
print('\n'.join(value))
else:
print(value)
@@ -735,6 +736,7 @@ Common commands: (see '--help-commands' for more)
'description'.
"""
import distutils.command
+
std_commands = distutils.command.__all__
is_std = {}
for cmd in std_commands:
@@ -746,18 +748,14 @@ Common commands: (see '--help-commands' for more)
extra_commands.append(cmd)
max_length = 0
- for cmd in (std_commands + extra_commands):
+ for cmd in std_commands + extra_commands:
if len(cmd) > max_length:
max_length = len(cmd)
- self.print_command_list(std_commands,
- "Standard commands",
- max_length)
+ self.print_command_list(std_commands, "Standard commands", max_length)
if extra_commands:
print()
- self.print_command_list(extra_commands,
- "Extra commands",
- max_length)
+ self.print_command_list(extra_commands, "Extra commands", max_length)
def get_command_list(self):
"""Get a list of (command, description) tuples.
@@ -769,6 +767,7 @@ Common commands: (see '--help-commands' for more)
# Currently this is only used on Mac OS, for the Mac-only GUI
# Distutils interface (by Jack Jansen)
import distutils.command
+
std_commands = distutils.command.__all__
is_std = {}
for cmd in std_commands:
@@ -780,7 +779,7 @@ Common commands: (see '--help-commands' for more)
extra_commands.append(cmd)
rv = []
- for cmd in (std_commands + extra_commands):
+ for cmd in std_commands + extra_commands:
klass = self.cmdclass.get(cmd)
if not klass:
klass = self.get_command_class(cmd)
@@ -822,7 +821,7 @@ Common commands: (see '--help-commands' for more)
return klass
for pkgname in self.get_command_packages():
- module_name = "%s.%s" % (pkgname, command)
+ module_name = "{}.{}".format(pkgname, command)
klass_name = command
try:
@@ -836,7 +835,8 @@ Common commands: (see '--help-commands' for more)
except AttributeError:
raise DistutilsModuleError(
"invalid command '%s' (no class '%s' in module '%s')"
- % (command, klass_name, module_name))
+ % (command, klass_name, module_name)
+ )
self.cmdclass[command] = klass
return klass
@@ -852,8 +852,10 @@ Common commands: (see '--help-commands' for more)
cmd_obj = self.command_obj.get(command)
if not cmd_obj and create:
if DEBUG:
- self.announce("Distribution.get_command_obj(): "
- "creating '%s' command object" % command)
+ self.announce(
+ "Distribution.get_command_obj(): "
+ "creating '%s' command object" % command
+ )
klass = self.get_command_class(command)
cmd_obj = self.command_obj[command] = klass(self)
@@ -870,7 +872,7 @@ Common commands: (see '--help-commands' for more)
return cmd_obj
- def _set_command_options(self, command_obj, option_dict=None):
+ def _set_command_options(self, command_obj, option_dict=None): # noqa: C901
"""Set the options for 'command_obj' from 'option_dict'. Basically
this means copying elements of a dictionary ('option_dict') to
attributes of an instance ('command').
@@ -887,11 +889,9 @@ Common commands: (see '--help-commands' for more)
self.announce(" setting options for '%s' command:" % command_name)
for (option, (source, value)) in option_dict.items():
if DEBUG:
- self.announce(" %s = %s (from %s)" % (option, value,
- source))
+ self.announce(" {} = {} (from {})".format(option, value, source))
try:
- bool_opts = [translate_longopt(o)
- for o in command_obj.boolean_options]
+ bool_opts = [translate_longopt(o) for o in command_obj.boolean_options]
except AttributeError:
bool_opts = []
try:
@@ -910,7 +910,8 @@ Common commands: (see '--help-commands' for more)
else:
raise DistutilsOptionError(
"error in %s: command '%s' has no such option '%s'"
- % (source, command_name, option))
+ % (source, command_name, option)
+ )
except ValueError as msg:
raise DistutilsOptionError(msg)
@@ -934,6 +935,7 @@ Common commands: (see '--help-commands' for more)
Returns the reinitialized command object.
"""
from distutils.cmd import Command
+
if not isinstance(command, Command):
command_name = command
command = self.get_command_obj(command_name)
@@ -955,7 +957,7 @@ Common commands: (see '--help-commands' for more)
# -- Methods that operate on the Distribution ----------------------
- def announce(self, msg, level=log.INFO):
+ def announce(self, msg, level=logging.INFO):
log.log(level, msg)
def run_commands(self):
@@ -1010,9 +1012,11 @@ Common commands: (see '--help-commands' for more)
return self.data_files and len(self.data_files) > 0
def is_pure(self):
- return (self.has_pure_modules() and
- not self.has_ext_modules() and
- not self.has_c_libraries())
+ return (
+ self.has_pure_modules()
+ and not self.has_ext_modules()
+ and not self.has_c_libraries()
+ )
# -- Metadata query methods ----------------------------------------
@@ -1021,19 +1025,35 @@ Common commands: (see '--help-commands' for more)
# to self.metadata.get_XXX. The actual code is in the
# DistributionMetadata class, below.
+
class DistributionMetadata:
"""Dummy class to hold the distribution meta-data: name, version,
author, and so forth.
"""
- _METHOD_BASENAMES = ("name", "version", "author", "author_email",
- "maintainer", "maintainer_email", "url",
- "license", "description", "long_description",
- "keywords", "platforms", "fullname", "contact",
- "contact_email", "classifiers", "download_url",
- # PEP 314
- "provides", "requires", "obsoletes",
- )
+ _METHOD_BASENAMES = (
+ "name",
+ "version",
+ "author",
+ "author_email",
+ "maintainer",
+ "maintainer_email",
+ "url",
+ "license",
+ "description",
+ "long_description",
+ "keywords",
+ "platforms",
+ "fullname",
+ "contact",
+ "contact_email",
+ "classifiers",
+ "download_url",
+ # PEP 314
+ "provides",
+ "requires",
+ "obsoletes",
+ )
def __init__(self, path=None):
if path is not None:
@@ -1064,9 +1084,8 @@ class DistributionMetadata:
def _read_field(name):
value = msg[name]
- if value == 'UNKNOWN':
- return None
- return value
+ if value and value != "UNKNOWN":
+ return value
def _read_list(name):
values = msg.get_all(name, None)
@@ -1111,37 +1130,42 @@ class DistributionMetadata:
self.obsoletes = None
def write_pkg_info(self, base_dir):
- """Write the PKG-INFO file into the release tree.
- """
- with open(os.path.join(base_dir, 'PKG-INFO'), 'w',
- encoding='UTF-8') as pkg_info:
+ """Write the PKG-INFO file into the release tree."""
+ with open(
+ os.path.join(base_dir, 'PKG-INFO'), 'w', encoding='UTF-8'
+ ) as pkg_info:
self.write_pkg_file(pkg_info)
def write_pkg_file(self, file):
- """Write the PKG-INFO format data to a file object.
- """
+ """Write the PKG-INFO format data to a file object."""
version = '1.0'
- if (self.provides or self.requires or self.obsoletes or
- self.classifiers or self.download_url):
+ if (
+ self.provides
+ or self.requires
+ or self.obsoletes
+ or self.classifiers
+ or self.download_url
+ ):
version = '1.1'
+ # required fields
file.write('Metadata-Version: %s\n' % version)
file.write('Name: %s\n' % self.get_name())
file.write('Version: %s\n' % self.get_version())
- file.write('Summary: %s\n' % self.get_description())
- file.write('Home-page: %s\n' % self.get_url())
- file.write('Author: %s\n' % self.get_contact())
- file.write('Author-email: %s\n' % self.get_contact_email())
- file.write('License: %s\n' % self.get_license())
- if self.download_url:
- file.write('Download-URL: %s\n' % self.download_url)
- long_desc = rfc822_escape(self.get_long_description())
- file.write('Description: %s\n' % long_desc)
+ def maybe_write(header, val):
+ if val:
+ file.write(f"{header}: {val}\n")
- keywords = ','.join(self.get_keywords())
- if keywords:
- file.write('Keywords: %s\n' % keywords)
+ # optional fields
+ maybe_write("Summary", self.get_description())
+ maybe_write("Home-page", self.get_url())
+ maybe_write("Author", self.get_contact())
+ maybe_write("Author-email", self.get_contact_email())
+ maybe_write("License", self.get_license())
+ maybe_write("Download-URL", self.download_url)
+ maybe_write("Description", rfc822_escape(self.get_long_description() or ""))
+ maybe_write("Keywords", ",".join(self.get_keywords()))
self._write_list(file, 'Platform', self.get_platforms())
self._write_list(file, 'Classifier', self.get_classifiers())
@@ -1152,8 +1176,9 @@ class DistributionMetadata:
self._write_list(file, 'Obsoletes', self.get_obsoletes())
def _write_list(self, file, name, values):
+ values = values or []
for value in values:
- file.write('%s: %s\n' % (name, value))
+ file.write('{}: {}\n'.format(name, value))
# -- Metadata query methods ----------------------------------------
@@ -1164,38 +1189,39 @@ class DistributionMetadata:
return self.version or "0.0.0"
def get_fullname(self):
- return "%s-%s" % (self.get_name(), self.get_version())
+ return "{}-{}".format(self.get_name(), self.get_version())
def get_author(self):
- return self.author or "UNKNOWN"
+ return self.author
def get_author_email(self):
- return self.author_email or "UNKNOWN"
+ return self.author_email
def get_maintainer(self):
- return self.maintainer or "UNKNOWN"
+ return self.maintainer
def get_maintainer_email(self):
- return self.maintainer_email or "UNKNOWN"
+ return self.maintainer_email
def get_contact(self):
- return self.maintainer or self.author or "UNKNOWN"
+ return self.maintainer or self.author
def get_contact_email(self):
- return self.maintainer_email or self.author_email or "UNKNOWN"
+ return self.maintainer_email or self.author_email
def get_url(self):
- return self.url or "UNKNOWN"
+ return self.url
def get_license(self):
- return self.license or "UNKNOWN"
+ return self.license
+
get_licence = get_license
def get_description(self):
- return self.description or "UNKNOWN"
+ return self.description
def get_long_description(self):
- return self.long_description or "UNKNOWN"
+ return self.long_description
def get_keywords(self):
return self.keywords or []
@@ -1204,7 +1230,7 @@ class DistributionMetadata:
self.keywords = _ensure_list(value, 'keywords')
def get_platforms(self):
- return self.platforms or ["UNKNOWN"]
+ return self.platforms
def set_platforms(self, value):
self.platforms = _ensure_list(value, 'platforms')
@@ -1216,7 +1242,7 @@ class DistributionMetadata:
self.classifiers = _ensure_list(value, 'classifiers')
def get_download_url(self):
- return self.download_url or "UNKNOWN"
+ return self.download_url
# PEP 314
def get_requires(self):
@@ -1224,6 +1250,7 @@ class DistributionMetadata:
def set_requires(self, value):
import distutils.versionpredicate
+
for v in value:
distutils.versionpredicate.VersionPredicate(v)
self.requires = list(value)
@@ -1235,6 +1262,7 @@ class DistributionMetadata:
value = [v.strip() for v in value]
for v in value:
import distutils.versionpredicate
+
distutils.versionpredicate.split_provision(v)
self.provides = value
@@ -1243,10 +1271,12 @@ class DistributionMetadata:
def set_obsoletes(self, value):
import distutils.versionpredicate
+
for v in value:
distutils.versionpredicate.VersionPredicate(v)
self.obsoletes = list(value)
+
def fix_help_options(options):
"""Convert a 4-tuple 'help_options' list as found in various command
classes to the 3-tuple form required by FancyGetopt.
diff --git a/setuptools/_distutils/errors.py b/setuptools/_distutils/errors.py
index 8b93059e..626254c3 100644
--- a/setuptools/_distutils/errors.py
+++ b/setuptools/_distutils/errors.py
@@ -8,90 +8,120 @@ usually raised for errors that are obviously the end-user's fault
This module is safe to use in "from ... import *" mode; it only exports
symbols whose names start with "Distutils" and end with "Error"."""
-class DistutilsError (Exception):
+
+class DistutilsError(Exception):
"""The root of all Distutils evil."""
+
pass
-class DistutilsModuleError (DistutilsError):
+
+class DistutilsModuleError(DistutilsError):
"""Unable to load an expected module, or to find an expected class
within some module (in particular, command modules and classes)."""
+
pass
-class DistutilsClassError (DistutilsError):
+
+class DistutilsClassError(DistutilsError):
"""Some command class (or possibly distribution class, if anyone
feels a need to subclass Distribution) is found not to be holding
up its end of the bargain, ie. implementing some part of the
"command "interface."""
+
pass
-class DistutilsGetoptError (DistutilsError):
+
+class DistutilsGetoptError(DistutilsError):
"""The option table provided to 'fancy_getopt()' is bogus."""
+
pass
-class DistutilsArgError (DistutilsError):
+
+class DistutilsArgError(DistutilsError):
"""Raised by fancy_getopt in response to getopt.error -- ie. an
error in the command line usage."""
+
pass
-class DistutilsFileError (DistutilsError):
+
+class DistutilsFileError(DistutilsError):
"""Any problems in the filesystem: expected file not found, etc.
Typically this is for problems that we detect before OSError
could be raised."""
+
pass
-class DistutilsOptionError (DistutilsError):
+
+class DistutilsOptionError(DistutilsError):
"""Syntactic/semantic errors in command options, such as use of
mutually conflicting options, or inconsistent options,
badly-spelled values, etc. No distinction is made between option
values originating in the setup script, the command line, config
files, or what-have-you -- but if we *know* something originated in
the setup script, we'll raise DistutilsSetupError instead."""
+
pass
-class DistutilsSetupError (DistutilsError):
+
+class DistutilsSetupError(DistutilsError):
"""For errors that can be definitely blamed on the setup script,
such as invalid keyword arguments to 'setup()'."""
+
pass
-class DistutilsPlatformError (DistutilsError):
+
+class DistutilsPlatformError(DistutilsError):
"""We don't know how to do something on the current platform (but
we do know how to do it on some platform) -- eg. trying to compile
C files on a platform not supported by a CCompiler subclass."""
+
pass
-class DistutilsExecError (DistutilsError):
+
+class DistutilsExecError(DistutilsError):
"""Any problems executing an external program (such as the C
compiler, when compiling C files)."""
+
pass
-class DistutilsInternalError (DistutilsError):
+
+class DistutilsInternalError(DistutilsError):
"""Internal inconsistencies or impossibilities (obviously, this
should never be seen if the code is working!)."""
+
pass
-class DistutilsTemplateError (DistutilsError):
+
+class DistutilsTemplateError(DistutilsError):
"""Syntax error in a file list template."""
+
class DistutilsByteCompileError(DistutilsError):
"""Byte compile error."""
+
# Exception classes used by the CCompiler implementation classes
-class CCompilerError (Exception):
+class CCompilerError(Exception):
"""Some compile/link operation failed."""
-class PreprocessError (CCompilerError):
+
+class PreprocessError(CCompilerError):
"""Failure to preprocess one or more C/C++ files."""
-class CompileError (CCompilerError):
+
+class CompileError(CCompilerError):
"""Failure to compile one or more C/C++ source files."""
-class LibError (CCompilerError):
+
+class LibError(CCompilerError):
"""Failure to create a static library from one or more C/C++ object
files."""
-class LinkError (CCompilerError):
+
+class LinkError(CCompilerError):
"""Failure to link one or more C/C++ object files into an executable
or shared library file."""
-class UnknownFileError (CCompilerError):
+
+class UnknownFileError(CCompilerError):
"""Attempt to process an unknown file type."""
diff --git a/setuptools/_distutils/extension.py b/setuptools/_distutils/extension.py
index c507da36..6b8575de 100644
--- a/setuptools/_distutils/extension.py
+++ b/setuptools/_distutils/extension.py
@@ -16,6 +16,7 @@ import warnings
# import that large-ish module (indirectly, through distutils.core) in
# order to do anything.
+
class Extension:
"""Just a collection of attributes that describes an extension
module and everything needed to build it (hopefully in a portable
@@ -83,27 +84,29 @@ class Extension:
# When adding arguments to this constructor, be sure to update
# setup_keywords in core.py.
- def __init__(self, name, sources,
- include_dirs=None,
- define_macros=None,
- undef_macros=None,
- library_dirs=None,
- libraries=None,
- runtime_library_dirs=None,
- extra_objects=None,
- extra_compile_args=None,
- extra_link_args=None,
- export_symbols=None,
- swig_opts = None,
- depends=None,
- language=None,
- optional=None,
- **kw # To catch unknown keywords
- ):
+ def __init__(
+ self,
+ name,
+ sources,
+ include_dirs=None,
+ define_macros=None,
+ undef_macros=None,
+ library_dirs=None,
+ libraries=None,
+ runtime_library_dirs=None,
+ extra_objects=None,
+ extra_compile_args=None,
+ extra_link_args=None,
+ export_symbols=None,
+ swig_opts=None,
+ depends=None,
+ language=None,
+ optional=None,
+ **kw # To catch unknown keywords
+ ):
if not isinstance(name, str):
raise AssertionError("'name' must be a string")
- if not (isinstance(sources, list) and
- all(isinstance(v, str) for v in sources)):
+ if not (isinstance(sources, list) and all(isinstance(v, str) for v in sources)):
raise AssertionError("'sources' must be a list of strings")
self.name = name
@@ -131,17 +134,17 @@ class Extension:
warnings.warn(msg)
def __repr__(self):
- return '<%s.%s(%r) at %#x>' % (
+ return '<{}.{}({!r}) at {:#x}>'.format(
self.__class__.__module__,
self.__class__.__qualname__,
self.name,
- id(self))
+ id(self),
+ )
-def read_setup_file(filename):
+def read_setup_file(filename): # noqa: C901
"""Reads a Setup file and returns Extension instances."""
- from distutils.sysconfig import (parse_makefile, expand_makefile_vars,
- _variable_rx)
+ from distutils.sysconfig import parse_makefile, expand_makefile_vars, _variable_rx
from distutils.text_file import TextFile
from distutils.util import split_quoted
@@ -151,17 +154,22 @@ def read_setup_file(filename):
# Second pass to gobble up the real content: lines of the form
# <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
- file = TextFile(filename,
- strip_comments=1, skip_blanks=1, join_lines=1,
- lstrip_ws=1, rstrip_ws=1)
+ file = TextFile(
+ filename,
+ strip_comments=1,
+ skip_blanks=1,
+ join_lines=1,
+ lstrip_ws=1,
+ rstrip_ws=1,
+ )
try:
extensions = []
while True:
line = file.readline()
- if line is None: # eof
+ if line is None: # eof
break
- if _variable_rx.match(line): # VAR=VALUE, handled in first pass
+ if _variable_rx.match(line): # VAR=VALUE, handled in first pass
continue
if line[0] == line[-1] == "*":
@@ -188,7 +196,8 @@ def read_setup_file(filename):
continue
suffix = os.path.splitext(word)[1]
- switch = word[0:2] ; value = word[2:]
+ switch = word[0:2]
+ value = word[2:]
if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
# hmm, should we do something about C vs. C++ sources?
@@ -199,14 +208,13 @@ def read_setup_file(filename):
ext.include_dirs.append(value)
elif switch == "-D":
equals = value.find("=")
- if equals == -1: # bare "-DFOO" -- no value
+ if equals == -1: # bare "-DFOO" -- no value
ext.define_macros.append((value, None))
- else: # "-DFOO=blah"
- ext.define_macros.append((value[0:equals],
- value[equals+2:]))
+ else: # "-DFOO=blah"
+ ext.define_macros.append((value[0:equals], value[equals + 2 :]))
elif switch == "-U":
ext.undef_macros.append(value)
- elif switch == "-C": # only here 'cause makesetup has it!
+ elif switch == "-C": # only here 'cause makesetup has it!
ext.extra_compile_args.append(word)
elif switch == "-l":
ext.libraries.append(value)
diff --git a/setuptools/_distutils/fancy_getopt.py b/setuptools/_distutils/fancy_getopt.py
index 7d170dd2..6abb884d 100644
--- a/setuptools/_distutils/fancy_getopt.py
+++ b/setuptools/_distutils/fancy_getopt.py
@@ -8,9 +8,11 @@ additional features:
* options set attributes of a passed-in object
"""
-import sys, string, re
+import sys
+import string
+import re
import getopt
-from distutils.errors import *
+from .errors import DistutilsGetoptError, DistutilsArgError
# Much like command_re in distutils.core, this is close to but not quite
# the same as a Python NAME -- except, in the spirit of most GNU
@@ -20,12 +22,13 @@ longopt_pat = r'[a-zA-Z](?:[a-zA-Z0-9-]*)'
longopt_re = re.compile(r'^%s$' % longopt_pat)
# For recognizing "negative alias" options, eg. "quiet=!verbose"
-neg_alias_re = re.compile("^(%s)=!(%s)$" % (longopt_pat, longopt_pat))
+neg_alias_re = re.compile("^({})=!({})$".format(longopt_pat, longopt_pat))
# This is used to translate long options to legitimate Python identifiers
# (for use as attributes of some object).
longopt_xlate = str.maketrans('-', '_')
+
class FancyGetopt:
"""Wrapper around the standard 'getopt()' module that provides some
handy extra functionality:
@@ -90,7 +93,8 @@ class FancyGetopt:
def add_option(self, long_option, short_option=None, help_string=None):
if long_option in self.option_index:
raise DistutilsGetoptError(
- "option conflict: already an option '%s'" % long_option)
+ "option conflict: already an option '%s'" % long_option
+ )
else:
option = (long_option, short_option, help_string)
self.option_table.append(option)
@@ -111,11 +115,15 @@ class FancyGetopt:
assert isinstance(aliases, dict)
for (alias, opt) in aliases.items():
if alias not in self.option_index:
- raise DistutilsGetoptError(("invalid %s '%s': "
- "option '%s' not defined") % (what, alias, alias))
+ raise DistutilsGetoptError(
+ ("invalid %s '%s': " "option '%s' not defined")
+ % (what, alias, alias)
+ )
if opt not in self.option_index:
- raise DistutilsGetoptError(("invalid %s '%s': "
- "aliased option '%s' not defined") % (what, alias, opt))
+ raise DistutilsGetoptError(
+ ("invalid %s '%s': " "aliased option '%s' not defined")
+ % (what, alias, opt)
+ )
def set_aliases(self, alias):
"""Set the aliases for this option parser."""
@@ -130,7 +138,7 @@ class FancyGetopt:
self._check_alias_dict(negative_alias, "negative alias")
self.negative_alias = negative_alias
- def _grok_option_table(self):
+ def _grok_option_table(self): # noqa: C901
"""Populate the various data structures that keep tabs on the
option table. Called by 'getopt()' before it can do anything
worthwhile.
@@ -149,23 +157,27 @@ class FancyGetopt:
else:
# the option table is part of the code, so simply
# assert that it is correct
- raise ValueError("invalid option tuple: %r" % (option,))
+ raise ValueError("invalid option tuple: {!r}".format(option))
# Type- and value-check the option names
if not isinstance(long, str) or len(long) < 2:
- raise DistutilsGetoptError(("invalid long option '%s': "
- "must be a string of length >= 2") % long)
+ raise DistutilsGetoptError(
+ ("invalid long option '%s': " "must be a string of length >= 2")
+ % long
+ )
- if (not ((short is None) or
- (isinstance(short, str) and len(short) == 1))):
- raise DistutilsGetoptError("invalid short option '%s': "
- "must a single character or None" % short)
+ if not ((short is None) or (isinstance(short, str) and len(short) == 1)):
+ raise DistutilsGetoptError(
+ "invalid short option '%s': "
+ "must a single character or None" % short
+ )
self.repeat[long] = repeat
self.long_opts.append(long)
- if long[-1] == '=': # option takes an argument?
- if short: short = short + ':'
+ if long[-1] == '=': # option takes an argument?
+ if short:
+ short = short + ':'
long = long[0:-1]
self.takes_arg[long] = 1
else:
@@ -175,11 +187,11 @@ class FancyGetopt:
if alias_to is not None:
if self.takes_arg[alias_to]:
raise DistutilsGetoptError(
- "invalid negative alias '%s': "
- "aliased option '%s' takes a value"
- % (long, alias_to))
+ "invalid negative alias '%s': "
+ "aliased option '%s' takes a value" % (long, alias_to)
+ )
- self.long_opts[-1] = long # XXX redundant?!
+ self.long_opts[-1] = long # XXX redundant?!
self.takes_arg[long] = 0
# If this is an alias option, make sure its "takes arg" flag is
@@ -188,10 +200,10 @@ class FancyGetopt:
if alias_to is not None:
if self.takes_arg[long] != self.takes_arg[alias_to]:
raise DistutilsGetoptError(
- "invalid alias '%s': inconsistent with "
- "aliased option '%s' (one of them takes a value, "
- "the other doesn't"
- % (long, alias_to))
+ "invalid alias '%s': inconsistent with "
+ "aliased option '%s' (one of them takes a value, "
+ "the other doesn't" % (long, alias_to)
+ )
# Now enforce some bondage on the long option name, so we can
# later translate it to an attribute name on some object. Have
@@ -199,15 +211,16 @@ class FancyGetopt:
# '='.
if not longopt_re.match(long):
raise DistutilsGetoptError(
- "invalid long option name '%s' "
- "(must be letters, numbers, hyphens only" % long)
+ "invalid long option name '%s' "
+ "(must be letters, numbers, hyphens only" % long
+ )
self.attr_name[long] = self.get_attr_name(long)
if short:
self.short_opts.append(short)
self.short2long[short[0]] = long
- def getopt(self, args=None, object=None):
+ def getopt(self, args=None, object=None): # noqa: C901
"""Parse command-line options in args. Store as attributes on object.
If 'args' is None or not supplied, uses 'sys.argv[1:]'. If
@@ -235,7 +248,7 @@ class FancyGetopt:
raise DistutilsArgError(msg)
for opt, val in opts:
- if len(opt) == 2 and opt[0] == '-': # it's a short option
+ if len(opt) == 2 and opt[0] == '-': # it's a short option
opt = self.short2long[opt[1]]
else:
assert len(opt) > 2 and opt[:2] == '--'
@@ -245,7 +258,7 @@ class FancyGetopt:
if alias:
opt = alias
- if not self.takes_arg[opt]: # boolean option?
+ if not self.takes_arg[opt]: # boolean option?
assert val == '', "boolean option can't have value"
alias = self.negative_alias.get(opt)
if alias:
@@ -278,7 +291,7 @@ class FancyGetopt:
else:
return self.option_order
- def generate_help(self, header=None):
+ def generate_help(self, header=None): # noqa: C901
"""Generate help text (a list of strings, one per suggested line of
output) from the option table for this FancyGetopt object.
"""
@@ -290,15 +303,15 @@ class FancyGetopt:
for option in self.option_table:
long = option[0]
short = option[1]
- l = len(long)
+ ell = len(long)
if long[-1] == '=':
- l = l - 1
+ ell = ell - 1
if short is not None:
- l = l + 5 # " (-x)" where short == 'x'
- if l > max_opt:
- max_opt = l
+ ell = ell + 5 # " (-x)" where short == 'x'
+ if ell > max_opt:
+ max_opt = ell
- opt_width = max_opt + 2 + 2 + 2 # room for indent + dashes + gutter
+ opt_width = max_opt + 2 + 2 + 2 # room for indent + dashes + gutter
# Typical help block looks like this:
# --foo controls foonabulation
@@ -346,15 +359,14 @@ class FancyGetopt:
# Case 2: we have a short option, so we have to include it
# just after the long option
else:
- opt_names = "%s (-%s)" % (long, short)
+ opt_names = "{} (-{})".format(long, short)
if text:
- lines.append(" --%-*s %s" %
- (max_opt, opt_names, text[0]))
+ lines.append(" --%-*s %s" % (max_opt, opt_names, text[0]))
else:
lines.append(" --%-*s" % opt_names)
- for l in text[1:]:
- lines.append(big_indent + l)
+ for ell in text[1:]:
+ lines.append(big_indent + ell)
return lines
def print_help(self, header=None, file=None):
@@ -370,7 +382,8 @@ def fancy_getopt(options, negative_opt, object, args):
return parser.getopt(args, object)
-WS_TRANS = {ord(_wschar) : ' ' for _wschar in string.whitespace}
+WS_TRANS = {ord(_wschar): ' ' for _wschar in string.whitespace}
+
def wrap_text(text, width):
"""wrap_text(text : string, width : int) -> [string]
@@ -386,26 +399,26 @@ def wrap_text(text, width):
text = text.expandtabs()
text = text.translate(WS_TRANS)
chunks = re.split(r'( +|-+)', text)
- chunks = [ch for ch in chunks if ch] # ' - ' results in empty strings
+ chunks = [ch for ch in chunks if ch] # ' - ' results in empty strings
lines = []
while chunks:
- cur_line = [] # list of chunks (to-be-joined)
- cur_len = 0 # length of current line
+ cur_line = [] # list of chunks (to-be-joined)
+ cur_len = 0 # length of current line
while chunks:
- l = len(chunks[0])
- if cur_len + l <= width: # can squeeze (at least) this chunk in
+ ell = len(chunks[0])
+ if cur_len + ell <= width: # can squeeze (at least) this chunk in
cur_line.append(chunks[0])
del chunks[0]
- cur_len = cur_len + l
- else: # this line is full
+ cur_len = cur_len + ell
+ else: # this line is full
# drop last chunk if all space
if cur_line and cur_line[-1][0] == ' ':
del cur_line[-1]
break
- if chunks: # any chunks left to process?
+ if chunks: # any chunks left to process?
# if the current line is still empty, then we had a single
# chunk that's too big too fit on a line -- so we break
# down and break it up at the line width
diff --git a/setuptools/_distutils/file_util.py b/setuptools/_distutils/file_util.py
index b3fee35a..1b7cd53b 100644
--- a/setuptools/_distutils/file_util.py
+++ b/setuptools/_distutils/file_util.py
@@ -4,16 +4,14 @@ Utility functions for operating on single files.
"""
import os
-from distutils.errors import DistutilsFileError
-from distutils import log
+from .errors import DistutilsFileError
+from ._log import log
# for generating verbose output in 'copy_file()'
-_copy_action = { None: 'copying',
- 'hard': 'hard linking',
- 'sym': 'symbolically linking' }
+_copy_action = {None: 'copying', 'hard': 'hard linking', 'sym': 'symbolically linking'}
-def _copy_file_contents(src, dst, buffer_size=16*1024):
+def _copy_file_contents(src, dst, buffer_size=16 * 1024): # noqa: C901
"""Copy the file 'src' to 'dst'; both must be filenames. Any error
opening either file, reading from 'src', or writing to 'dst', raises
DistutilsFileError. Data is read/written in chunks of 'buffer_size'
@@ -28,27 +26,30 @@ def _copy_file_contents(src, dst, buffer_size=16*1024):
try:
fsrc = open(src, 'rb')
except OSError as e:
- raise DistutilsFileError("could not open '%s': %s" % (src, e.strerror))
+ raise DistutilsFileError("could not open '{}': {}".format(src, e.strerror))
if os.path.exists(dst):
try:
os.unlink(dst)
except OSError as e:
raise DistutilsFileError(
- "could not delete '%s': %s" % (dst, e.strerror))
+ "could not delete '{}': {}".format(dst, e.strerror)
+ )
try:
fdst = open(dst, 'wb')
except OSError as e:
raise DistutilsFileError(
- "could not create '%s': %s" % (dst, e.strerror))
+ "could not create '{}': {}".format(dst, e.strerror)
+ )
while True:
try:
buf = fsrc.read(buffer_size)
except OSError as e:
raise DistutilsFileError(
- "could not read from '%s': %s" % (src, e.strerror))
+ "could not read from '{}': {}".format(src, e.strerror)
+ )
if not buf:
break
@@ -57,15 +58,25 @@ def _copy_file_contents(src, dst, buffer_size=16*1024):
fdst.write(buf)
except OSError as e:
raise DistutilsFileError(
- "could not write to '%s': %s" % (dst, e.strerror))
+ "could not write to '{}': {}".format(dst, e.strerror)
+ )
finally:
if fdst:
fdst.close()
if fsrc:
fsrc.close()
-def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
- link=None, verbose=1, dry_run=0):
+
+def copy_file( # noqa: C901
+ src,
+ dst,
+ preserve_mode=1,
+ preserve_times=1,
+ update=0,
+ link=None,
+ verbose=1,
+ dry_run=0,
+):
"""Copy a file 'src' to 'dst'. If 'dst' is a directory, then 'src' is
copied there with the same name; otherwise, it must be a filename. (If
the file exists, it will be ruthlessly clobbered.) If 'preserve_mode'
@@ -102,7 +113,8 @@ def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
if not os.path.isfile(src):
raise DistutilsFileError(
- "can't copy '%s': doesn't exist or not a regular file" % src)
+ "can't copy '%s': doesn't exist or not a regular file" % src
+ )
if os.path.isdir(dst):
dir = dst
@@ -163,9 +175,7 @@ def copy_file(src, dst, preserve_mode=1, preserve_times=1, update=0,
# XXX I suspect this is Unix-specific -- need porting help!
-def move_file (src, dst,
- verbose=1,
- dry_run=0):
+def move_file(src, dst, verbose=1, dry_run=0): # noqa: C901
"""Move a file 'src' to 'dst'. If 'dst' is a directory, the file will
be moved into it with the same name; otherwise, 'src' is just renamed
@@ -190,13 +200,13 @@ def move_file (src, dst,
dst = os.path.join(dst, basename(src))
elif exists(dst):
raise DistutilsFileError(
- "can't move '%s': destination '%s' already exists" %
- (src, dst))
+ "can't move '{}': destination '{}' already exists".format(src, dst)
+ )
if not isdir(dirname(dst)):
raise DistutilsFileError(
- "can't move '%s': destination '%s' not a valid path" %
- (src, dst))
+ "can't move '{}': destination '{}' not a valid path".format(src, dst)
+ )
copy_it = False
try:
@@ -207,7 +217,8 @@ def move_file (src, dst,
copy_it = True
else:
raise DistutilsFileError(
- "couldn't move '%s' to '%s': %s" % (src, dst, msg))
+ "couldn't move '{}' to '{}': {}".format(src, dst, msg)
+ )
if copy_it:
copy_file(src, dst, verbose=verbose)
@@ -220,13 +231,13 @@ def move_file (src, dst,
except OSError:
pass
raise DistutilsFileError(
- "couldn't move '%s' to '%s' by copy/delete: "
- "delete '%s' failed: %s"
- % (src, dst, src, msg))
+ "couldn't move '%s' to '%s' by copy/delete: "
+ "delete '%s' failed: %s" % (src, dst, src, msg)
+ )
return dst
-def write_file (filename, contents):
+def write_file(filename, contents):
"""Create a file with the specified name and write 'contents' (a
sequence of strings without line terminators) to it.
"""
diff --git a/setuptools/_distutils/filelist.py b/setuptools/_distutils/filelist.py
index 82a77384..6dadf923 100644
--- a/setuptools/_distutils/filelist.py
+++ b/setuptools/_distutils/filelist.py
@@ -9,9 +9,9 @@ import re
import fnmatch
import functools
-from distutils.util import convert_path
-from distutils.errors import DistutilsTemplateError, DistutilsInternalError
-from distutils import log
+from .util import convert_path
+from .errors import DistutilsTemplateError, DistutilsInternalError
+from ._log import log
class FileList:
@@ -46,6 +46,7 @@ class FileList:
DISTUTILS_DEBUG environment variable) flag is true.
"""
from distutils.debug import DEBUG
+
if DEBUG:
print(msg)
@@ -80,29 +81,31 @@ class FileList:
patterns = dir = dir_pattern = None
- if action in ('include', 'exclude',
- 'global-include', 'global-exclude'):
+ if action in ('include', 'exclude', 'global-include', 'global-exclude'):
if len(words) < 2:
raise DistutilsTemplateError(
- "'%s' expects <pattern1> <pattern2> ..." % action)
+ "'%s' expects <pattern1> <pattern2> ..." % action
+ )
patterns = [convert_path(w) for w in words[1:]]
elif action in ('recursive-include', 'recursive-exclude'):
if len(words) < 3:
raise DistutilsTemplateError(
- "'%s' expects <dir> <pattern1> <pattern2> ..." % action)
+ "'%s' expects <dir> <pattern1> <pattern2> ..." % action
+ )
dir = convert_path(words[1])
patterns = [convert_path(w) for w in words[2:]]
elif action in ('graft', 'prune'):
if len(words) != 2:
raise DistutilsTemplateError(
- "'%s' expects a single <dir_pattern>" % action)
+ "'%s' expects a single <dir_pattern>" % action
+ )
dir_pattern = convert_path(words[1])
else:
raise DistutilsTemplateError("unknown action '%s'" % action)
return (action, patterns, dir, dir_pattern)
- def process_template_line(self, line):
+ def process_template_line(self, line): # noqa: C901
# Parse the line: split it up, make sure the right number of words
# is there, and return the relevant words. 'action' is always
# defined: it's the first word of the line. Which of the other
@@ -117,65 +120,82 @@ class FileList:
self.debug_print("include " + ' '.join(patterns))
for pattern in patterns:
if not self.include_pattern(pattern, anchor=1):
- log.warn("warning: no files found matching '%s'",
- pattern)
+ log.warning("warning: no files found matching '%s'", pattern)
elif action == 'exclude':
self.debug_print("exclude " + ' '.join(patterns))
for pattern in patterns:
if not self.exclude_pattern(pattern, anchor=1):
- log.warn(("warning: no previously-included files "
- "found matching '%s'"), pattern)
+ log.warning(
+ (
+ "warning: no previously-included files "
+ "found matching '%s'"
+ ),
+ pattern,
+ )
elif action == 'global-include':
self.debug_print("global-include " + ' '.join(patterns))
for pattern in patterns:
if not self.include_pattern(pattern, anchor=0):
- log.warn(("warning: no files found matching '%s' "
- "anywhere in distribution"), pattern)
+ log.warning(
+ (
+ "warning: no files found matching '%s' "
+ "anywhere in distribution"
+ ),
+ pattern,
+ )
elif action == 'global-exclude':
self.debug_print("global-exclude " + ' '.join(patterns))
for pattern in patterns:
if not self.exclude_pattern(pattern, anchor=0):
- log.warn(("warning: no previously-included files matching "
- "'%s' found anywhere in distribution"),
- pattern)
+ log.warning(
+ (
+ "warning: no previously-included files matching "
+ "'%s' found anywhere in distribution"
+ ),
+ pattern,
+ )
elif action == 'recursive-include':
- self.debug_print("recursive-include %s %s" %
- (dir, ' '.join(patterns)))
+ self.debug_print("recursive-include {} {}".format(dir, ' '.join(patterns)))
for pattern in patterns:
if not self.include_pattern(pattern, prefix=dir):
msg = (
- "warning: no files found matching '%s' "
- "under directory '%s'"
+ "warning: no files found matching '%s' " "under directory '%s'"
)
- log.warn(msg, pattern, dir)
+ log.warning(msg, pattern, dir)
elif action == 'recursive-exclude':
- self.debug_print("recursive-exclude %s %s" %
- (dir, ' '.join(patterns)))
+ self.debug_print("recursive-exclude {} {}".format(dir, ' '.join(patterns)))
for pattern in patterns:
if not self.exclude_pattern(pattern, prefix=dir):
- log.warn(("warning: no previously-included files matching "
- "'%s' found under directory '%s'"),
- pattern, dir)
+ log.warning(
+ (
+ "warning: no previously-included files matching "
+ "'%s' found under directory '%s'"
+ ),
+ pattern,
+ dir,
+ )
elif action == 'graft':
self.debug_print("graft " + dir_pattern)
if not self.include_pattern(None, prefix=dir_pattern):
- log.warn("warning: no directories found matching '%s'",
- dir_pattern)
+ log.warning("warning: no directories found matching '%s'", dir_pattern)
elif action == 'prune':
self.debug_print("prune " + dir_pattern)
if not self.exclude_pattern(None, prefix=dir_pattern):
- log.warn(("no previously-included directories found "
- "matching '%s'"), dir_pattern)
+ log.warning(
+ ("no previously-included directories found " "matching '%s'"),
+ dir_pattern,
+ )
else:
raise DistutilsInternalError(
- "this cannot happen: invalid action '%s'" % action)
+ "this cannot happen: invalid action '%s'" % action
+ )
# Filtering/selection methods
@@ -207,8 +227,7 @@ class FileList:
# XXX docstring lying about what the special chars are?
files_found = False
pattern_re = translate_pattern(pattern, anchor, prefix, is_regex)
- self.debug_print("include_pattern: applying regex r'%s'" %
- pattern_re.pattern)
+ self.debug_print("include_pattern: applying regex r'%s'" % pattern_re.pattern)
# delayed loading of allfiles list
if self.allfiles is None:
@@ -221,8 +240,7 @@ class FileList:
files_found = True
return files_found
- def exclude_pattern(
- self, pattern, anchor=1, prefix=None, is_regex=0):
+ def exclude_pattern(self, pattern, anchor=1, prefix=None, is_regex=0):
"""Remove strings (presumably filenames) from 'files' that match
'pattern'. Other parameters are the same as for
'include_pattern()', above.
@@ -231,9 +249,8 @@ class FileList:
"""
files_found = False
pattern_re = translate_pattern(pattern, anchor, prefix, is_regex)
- self.debug_print("exclude_pattern: applying regex r'%s'" %
- pattern_re.pattern)
- for i in range(len(self.files)-1, -1, -1):
+ self.debug_print("exclude_pattern: applying regex r'%s'" % pattern_re.pattern)
+ for i in range(len(self.files) - 1, -1, -1):
if pattern_re.search(self.files[i]):
self.debug_print(" removing " + self.files[i])
del self.files[i]
@@ -243,15 +260,14 @@ class FileList:
# Utility functions
+
def _find_all_simple(path):
"""
Find all files under 'path'
"""
all_unique = _UniqueDirs.filter(os.walk(path, followlinks=True))
results = (
- os.path.join(base, file)
- for base, dirs, files in all_unique
- for file in files
+ os.path.join(base, file) for base, dirs, files in all_unique for file in files
)
return filter(os.path.isfile, results)
@@ -262,6 +278,7 @@ class _UniqueDirs(set):
avoiding infinite recursion.
Ref https://bugs.python.org/issue44497.
"""
+
def __call__(self, walk_item):
"""
Given an item from an os.walk result, determine
@@ -341,15 +358,14 @@ def translate_pattern(pattern, anchor=1, prefix=None, is_regex=0):
if prefix is not None:
prefix_re = glob_to_re(prefix)
assert prefix_re.startswith(start) and prefix_re.endswith(end)
- prefix_re = prefix_re[len(start): len(prefix_re) - len(end)]
+ prefix_re = prefix_re[len(start) : len(prefix_re) - len(end)]
sep = os.sep
if os.sep == '\\':
sep = r'\\'
- pattern_re = pattern_re[len(start): len(pattern_re) - len(end)]
- pattern_re = r'%s\A%s%s.*%s%s' % (
- start, prefix_re, sep, pattern_re, end)
- else: # no prefix -- respect anchor flag
+ pattern_re = pattern_re[len(start) : len(pattern_re) - len(end)]
+ pattern_re = r'{}\A{}{}.*{}{}'.format(start, prefix_re, sep, pattern_re, end)
+ else: # no prefix -- respect anchor flag
if anchor:
- pattern_re = r'%s\A%s' % (start, pattern_re[len(start):])
+ pattern_re = r'{}\A{}'.format(start, pattern_re[len(start) :])
return re.compile(pattern_re)
diff --git a/setuptools/_distutils/log.py b/setuptools/_distutils/log.py
index a68b156b..239f3158 100644
--- a/setuptools/_distutils/log.py
+++ b/setuptools/_distutils/log.py
@@ -1,81 +1,57 @@
-"""A simple log mechanism styled after PEP 282."""
+"""
+A simple log mechanism styled after PEP 282.
-# The class here is styled after PEP 282 so that it could later be
-# replaced with a standard Python logging implementation.
+Retained for compatibility and should not be used.
+"""
-import sys
+import logging
+import warnings
-DEBUG = 1
-INFO = 2
-WARN = 3
-ERROR = 4
-FATAL = 5
+from ._log import log as _global_log
-class Log:
+DEBUG = logging.DEBUG
+INFO = logging.INFO
+WARN = logging.WARN
+ERROR = logging.ERROR
+FATAL = logging.FATAL
- def __init__(self, threshold=WARN):
- self.threshold = threshold
-
- def _log(self, level, msg, args):
- if level not in (DEBUG, INFO, WARN, ERROR, FATAL):
- raise ValueError('%s wrong log level' % str(level))
-
- if level >= self.threshold:
- if args:
- msg = msg % args
- if level in (WARN, ERROR, FATAL):
- stream = sys.stderr
- else:
- stream = sys.stdout
- try:
- stream.write('%s\n' % msg)
- except UnicodeEncodeError:
- # emulate backslashreplace error handler
- encoding = stream.encoding
- msg = msg.encode(encoding, "backslashreplace").decode(encoding)
- stream.write('%s\n' % msg)
- stream.flush()
-
- def log(self, level, msg, *args):
- self._log(level, msg, args)
-
- def debug(self, msg, *args):
- self._log(DEBUG, msg, args)
-
- def info(self, msg, *args):
- self._log(INFO, msg, args)
-
- def warn(self, msg, *args):
- self._log(WARN, msg, args)
-
- def error(self, msg, *args):
- self._log(ERROR, msg, args)
-
- def fatal(self, msg, *args):
- self._log(FATAL, msg, args)
-
-
-_global_log = Log()
log = _global_log.log
debug = _global_log.debug
info = _global_log.info
-warn = _global_log.warn
+warn = _global_log.warning
error = _global_log.error
fatal = _global_log.fatal
def set_threshold(level):
- # return the old threshold for use from tests
- old = _global_log.threshold
- _global_log.threshold = level
- return old
+ orig = _global_log.level
+ _global_log.setLevel(level)
+ return orig
def set_verbosity(v):
if v <= 0:
- set_threshold(WARN)
+ set_threshold(logging.WARN)
elif v == 1:
- set_threshold(INFO)
+ set_threshold(logging.INFO)
elif v >= 2:
- set_threshold(DEBUG)
+ set_threshold(logging.DEBUG)
+
+
+class Log(logging.Logger):
+ """distutils.log.Log is deprecated, please use an alternative from `logging`."""
+
+ def __init__(self, threshold=WARN):
+ warnings.warn(Log.__doc__) # avoid DeprecationWarning to ensure warn is shown
+ super().__init__(__name__, level=threshold)
+
+ @property
+ def threshold(self):
+ return self.level
+
+ @threshold.setter
+ def threshold(self, level):
+ self.setLevel(level)
+
+ warn = logging.Logger.warning
diff --git a/setuptools/_distutils/msvc9compiler.py b/setuptools/_distutils/msvc9compiler.py
index 6b627383..a4714a55 100644
--- a/setuptools/_distutils/msvc9compiler.py
+++ b/setuptools/_distutils/msvc9compiler.py
@@ -16,26 +16,41 @@ import os
import subprocess
import sys
import re
-
-from distutils.errors import DistutilsExecError, DistutilsPlatformError, \
- CompileError, LibError, LinkError
-from distutils.ccompiler import CCompiler, gen_lib_options
-from distutils import log
-from distutils.util import get_platform
+import warnings
+
+from .errors import (
+ DistutilsExecError,
+ DistutilsPlatformError,
+ CompileError,
+ LibError,
+ LinkError,
+)
+from .ccompiler import CCompiler, gen_lib_options
+from ._log import log
+from .util import get_platform
import winreg
+warnings.warn(
+ "msvc9compiler is deprecated and slated to be removed "
+ "in the future. Please discontinue use or file an issue "
+ "with pypa/distutils describing your use case.",
+ DeprecationWarning,
+)
+
RegOpenKeyEx = winreg.OpenKeyEx
RegEnumKey = winreg.EnumKey
RegEnumValue = winreg.EnumValue
RegError = winreg.error
-HKEYS = (winreg.HKEY_USERS,
- winreg.HKEY_CURRENT_USER,
- winreg.HKEY_LOCAL_MACHINE,
- winreg.HKEY_CLASSES_ROOT)
+HKEYS = (
+ winreg.HKEY_USERS,
+ winreg.HKEY_CURRENT_USER,
+ winreg.HKEY_LOCAL_MACHINE,
+ winreg.HKEY_CLASSES_ROOT,
+)
-NATIVE_WIN64 = (sys.platform == 'win32' and sys.maxsize > 2**32)
+NATIVE_WIN64 = sys.platform == 'win32' and sys.maxsize > 2**32
if NATIVE_WIN64:
# Visual C++ is a 32-bit application, so we need to look in
# the corresponding registry branch, if we're running a
@@ -52,13 +67,13 @@ else:
# 'vcvarsall.bat'. Note a cross-compile may combine these (eg, 'x86_amd64' is
# the param to cross-compile on x86 targeting amd64.)
PLAT_TO_VCVARS = {
- 'win32' : 'x86',
- 'win-amd64' : 'amd64',
+ 'win32': 'x86',
+ 'win-amd64': 'amd64',
}
+
class Reg:
- """Helper class to read values from the registry
- """
+ """Helper class to read values from the registry"""
def get_value(cls, path, key):
for base in HKEYS:
@@ -66,6 +81,7 @@ class Reg:
if d and key in d:
return d[key]
raise KeyError(key)
+
get_value = classmethod(get_value)
def read_keys(cls, base, key):
@@ -84,6 +100,7 @@ class Reg:
L.append(k)
i += 1
return L
+
read_keys = classmethod(read_keys)
def read_values(cls, base, key):
@@ -106,6 +123,7 @@ class Reg:
d[cls.convert_mbcs(name)] = cls.convert_mbcs(value)
i += 1
return d
+
read_values = classmethod(read_values)
def convert_mbcs(s):
@@ -116,10 +134,11 @@ class Reg:
except UnicodeError:
pass
return s
+
convert_mbcs = staticmethod(convert_mbcs)
-class MacroExpander:
+class MacroExpander:
def __init__(self, version):
self.macros = {}
self.vsbase = VS_BASE % version
@@ -134,16 +153,16 @@ class MacroExpander:
self.set_macro("FrameworkDir", NET_BASE, "installroot")
try:
if version >= 8.0:
- self.set_macro("FrameworkSDKDir", NET_BASE,
- "sdkinstallrootv2.0")
+ self.set_macro("FrameworkSDKDir", NET_BASE, "sdkinstallrootv2.0")
else:
raise KeyError("sdkinstallrootv2.0")
except KeyError:
raise DistutilsPlatformError(
- """Python was built with Visual Studio 2008;
+ """Python was built with Visual Studio 2008;
extensions must be built with a compiler than can generate compatible binaries.
Visual Studio 2008 was not found on this system. If you have Cygwin installed,
-you can try compiling with MingW32, by passing "-c mingw32" to setup.py.""")
+you can try compiling with MingW32, by passing "-c mingw32" to setup.py."""
+ )
if version >= 9.0:
self.set_macro("FrameworkVersion", self.vsbase, "clr version")
@@ -156,7 +175,7 @@ you can try compiling with MingW32, by passing "-c mingw32" to setup.py.""")
except RegError:
continue
key = RegEnumKey(h, 0)
- d = Reg.get_value(base, r"%s\%s" % (p, key))
+ d = Reg.get_value(base, r"{}\{}".format(p, key))
self.macros["$(FrameworkVersion)"] = d["version"]
def sub(self, s):
@@ -164,6 +183,7 @@ you can try compiling with MingW32, by passing "-c mingw32" to setup.py.""")
s = s.replace(k, v)
return s
+
def get_build_version():
"""Return the version of MSVC that was used to build Python.
@@ -189,6 +209,7 @@ def get_build_version():
# else we don't know what version of the compiler this is
return None
+
def normalize_and_reduce_paths(paths):
"""Return a list of normalized paths with duplicates removed.
@@ -203,9 +224,9 @@ def normalize_and_reduce_paths(paths):
reduced_paths.append(np)
return reduced_paths
+
def removeDuplicates(variable):
- """Remove duplicate values of an environment variable.
- """
+ """Remove duplicate values of an environment variable."""
oldList = variable.split(os.pathsep)
newList = []
for i in oldList:
@@ -214,6 +235,7 @@ def removeDuplicates(variable):
newVariable = os.pathsep.join(newList)
return newVariable
+
def find_vcvarsall(version):
"""Find the vcvarsall.bat file
@@ -222,8 +244,7 @@ def find_vcvarsall(version):
"""
vsbase = VS_BASE % version
try:
- productdir = Reg.get_value(r"%s\Setup\VC" % vsbase,
- "productdir")
+ productdir = Reg.get_value(r"%s\Setup\VC" % vsbase, "productdir")
except KeyError:
log.debug("Unable to find productdir in registry")
productdir = None
@@ -249,9 +270,9 @@ def find_vcvarsall(version):
log.debug("Unable to find vcvarsall.bat")
return None
+
def query_vcvarsall(version, arch="x86"):
- """Launch vcvarsall.bat and read the settings from its environment
- """
+ """Launch vcvarsall.bat and read the settings from its environment"""
vcvarsall = find_vcvarsall(version)
interesting = {"include", "lib", "libpath", "path"}
result = {}
@@ -259,9 +280,11 @@ def query_vcvarsall(version, arch="x86"):
if vcvarsall is None:
raise DistutilsPlatformError("Unable to find vcvarsall.bat")
log.debug("Calling 'vcvarsall.bat %s' (version=%s)", arch, version)
- popen = subprocess.Popen('"%s" %s & set' % (vcvarsall, arch),
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
+ popen = subprocess.Popen(
+ '"{}" {} & set'.format(vcvarsall, arch),
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ )
try:
stdout, stderr = popen.communicate()
if popen.wait() != 0:
@@ -289,13 +312,15 @@ def query_vcvarsall(version, arch="x86"):
return result
+
# More globals
VERSION = get_build_version()
# MACROS = MacroExpander(VERSION)
-class MSVCCompiler(CCompiler) :
+
+class MSVCCompiler(CCompiler):
"""Concrete class that implements an interface to Microsoft Visual C++,
- as defined by the CCompiler abstract class."""
+ as defined by the CCompiler abstract class."""
compiler_type = 'msvc'
@@ -314,8 +339,7 @@ class MSVCCompiler(CCompiler) :
# Needed for the filename generation methods provided by the
# base class, CCompiler.
- src_extensions = (_c_extensions + _cpp_extensions +
- _rc_extensions + _mc_extensions)
+ src_extensions = _c_extensions + _cpp_extensions + _rc_extensions + _mc_extensions
res_extension = '.res'
obj_extension = '.obj'
static_lib_extension = '.lib'
@@ -331,23 +355,30 @@ class MSVCCompiler(CCompiler) :
self.__paths = []
# target platform (.plat_name is consistent with 'bdist')
self.plat_name = None
- self.__arch = None # deprecated name
+ self.__arch = None # deprecated name
self.initialized = False
- def initialize(self, plat_name=None):
+ def initialize(self, plat_name=None): # noqa: C901
# multi-init means we would need to check platform same each time...
assert not self.initialized, "don't init multiple times"
if self.__version < 8.0:
- raise DistutilsPlatformError("VC %0.1f is not supported by this module" % self.__version)
+ raise DistutilsPlatformError(
+ "VC %0.1f is not supported by this module" % self.__version
+ )
if plat_name is None:
plat_name = get_platform()
# sanity check for platforms to prevent obscure errors later.
ok_plats = 'win32', 'win-amd64'
if plat_name not in ok_plats:
- raise DistutilsPlatformError("--plat-name must be one of %s" %
- (ok_plats,))
-
- if "DISTUTILS_USE_SDK" in os.environ and "MSSdk" in os.environ and self.find_exe("cl.exe"):
+ raise DistutilsPlatformError(
+ "--plat-name must be one of {}".format(ok_plats)
+ )
+
+ if (
+ "DISTUTILS_USE_SDK" in os.environ
+ and "MSSdk" in os.environ
+ and self.find_exe("cl.exe")
+ ):
# Assume that the SDK set up everything alright; don't try to be
# smarter
self.cc = "cl.exe"
@@ -365,8 +396,9 @@ class MSVCCompiler(CCompiler) :
plat_spec = PLAT_TO_VCVARS[plat_name]
else:
# cross compile from win32 -> some 64bit
- plat_spec = PLAT_TO_VCVARS[get_platform()] + '_' + \
- PLAT_TO_VCVARS[plat_name]
+ plat_spec = (
+ PLAT_TO_VCVARS[get_platform()] + '_' + PLAT_TO_VCVARS[plat_name]
+ )
vc_env = query_vcvarsall(VERSION, plat_spec)
@@ -375,18 +407,19 @@ class MSVCCompiler(CCompiler) :
os.environ['include'] = vc_env['include']
if len(self.__paths) == 0:
- raise DistutilsPlatformError("Python was built with %s, "
- "and extensions need to be built with the same "
- "version of the compiler, but it isn't installed."
- % self.__product)
+ raise DistutilsPlatformError(
+ "Python was built with %s, "
+ "and extensions need to be built with the same "
+ "version of the compiler, but it isn't installed." % self.__product
+ )
self.cc = self.find_exe("cl.exe")
self.linker = self.find_exe("link.exe")
self.lib = self.find_exe("lib.exe")
- self.rc = self.find_exe("rc.exe") # resource compiler
- self.mc = self.find_exe("mc.exe") # message compiler
- #self.set_path_env_var('lib')
- #self.set_path_env_var('include')
+ self.rc = self.find_exe("rc.exe") # resource compiler
+ self.mc = self.find_exe("mc.exe") # message compiler
+ # self.set_path_env_var('lib')
+ # self.set_path_env_var('include')
# extend the MSVC path with the current path
try:
@@ -399,71 +432,83 @@ class MSVCCompiler(CCompiler) :
self.preprocess_options = None
if self.__arch == "x86":
- self.compile_options = [ '/nologo', '/O2', '/MD', '/W3',
- '/DNDEBUG']
- self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3',
- '/Z7', '/D_DEBUG']
+ self.compile_options = ['/nologo', '/O2', '/MD', '/W3', '/DNDEBUG']
+ self.compile_options_debug = [
+ '/nologo',
+ '/Od',
+ '/MDd',
+ '/W3',
+ '/Z7',
+ '/D_DEBUG',
+ ]
else:
# Win64
- self.compile_options = [ '/nologo', '/O2', '/MD', '/W3', '/GS-' ,
- '/DNDEBUG']
- self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GS-',
- '/Z7', '/D_DEBUG']
+ self.compile_options = ['/nologo', '/O2', '/MD', '/W3', '/GS-', '/DNDEBUG']
+ self.compile_options_debug = [
+ '/nologo',
+ '/Od',
+ '/MDd',
+ '/W3',
+ '/GS-',
+ '/Z7',
+ '/D_DEBUG',
+ ]
self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO']
if self.__version >= 7:
- self.ldflags_shared_debug = [
- '/DLL', '/nologo', '/INCREMENTAL:no', '/DEBUG'
- ]
- self.ldflags_static = [ '/nologo']
+ self.ldflags_shared_debug = ['/DLL', '/nologo', '/INCREMENTAL:no', '/DEBUG']
+ self.ldflags_static = ['/nologo']
self.initialized = True
# -- Worker methods ------------------------------------------------
- def object_filenames(self,
- source_filenames,
- strip_dir=0,
- output_dir=''):
+ def object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
# Copied from ccompiler.py, extended to return .res as 'object'-file
# for .rc input file
- if output_dir is None: output_dir = ''
+ if output_dir is None:
+ output_dir = ''
obj_names = []
for src_name in source_filenames:
- (base, ext) = os.path.splitext (src_name)
- base = os.path.splitdrive(base)[1] # Chop off the drive
- base = base[os.path.isabs(base):] # If abs, chop off leading /
+ (base, ext) = os.path.splitext(src_name)
+ base = os.path.splitdrive(base)[1] # Chop off the drive
+ base = base[os.path.isabs(base) :] # If abs, chop off leading /
if ext not in self.src_extensions:
# Better to raise an exception instead of silently continuing
# and later complain about sources and targets having
# different lengths
- raise CompileError ("Don't know how to compile %s" % src_name)
+ raise CompileError("Don't know how to compile %s" % src_name)
if strip_dir:
- base = os.path.basename (base)
+ base = os.path.basename(base)
if ext in self._rc_extensions:
- obj_names.append (os.path.join (output_dir,
- base + self.res_extension))
+ obj_names.append(os.path.join(output_dir, base + self.res_extension))
elif ext in self._mc_extensions:
- obj_names.append (os.path.join (output_dir,
- base + self.res_extension))
+ obj_names.append(os.path.join(output_dir, base + self.res_extension))
else:
- obj_names.append (os.path.join (output_dir,
- base + self.obj_extension))
+ obj_names.append(os.path.join(output_dir, base + self.obj_extension))
return obj_names
-
- def compile(self, sources,
- output_dir=None, macros=None, include_dirs=None, debug=0,
- extra_preargs=None, extra_postargs=None, depends=None):
+ def compile( # noqa: C901
+ self,
+ sources,
+ output_dir=None,
+ macros=None,
+ include_dirs=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ depends=None,
+ ):
if not self.initialized:
self.initialize()
- compile_info = self._setup_compile(output_dir, macros, include_dirs,
- sources, depends, extra_postargs)
+ compile_info = self._setup_compile(
+ output_dir, macros, include_dirs, sources, depends, extra_postargs
+ )
macros, objects, extra_postargs, pp_opts, build = compile_info
compile_opts = extra_preargs or []
- compile_opts.append ('/c')
+ compile_opts.append('/c')
if debug:
compile_opts.extend(self.compile_options_debug)
else:
@@ -489,8 +534,7 @@ class MSVCCompiler(CCompiler) :
input_opt = src
output_opt = "/fo" + obj
try:
- self.spawn([self.rc] + pp_opts +
- [output_opt] + [input_opt])
+ self.spawn([self.rc] + pp_opts + [output_opt] + [input_opt])
except DistutilsExecError as msg:
raise CompileError(msg)
continue
@@ -510,50 +554,48 @@ class MSVCCompiler(CCompiler) :
rc_dir = os.path.dirname(obj)
try:
# first compile .MC to .RC and .H file
- self.spawn([self.mc] +
- ['-h', h_dir, '-r', rc_dir] + [src])
- base, _ = os.path.splitext (os.path.basename (src))
- rc_file = os.path.join (rc_dir, base + '.rc')
+ self.spawn([self.mc] + ['-h', h_dir, '-r', rc_dir] + [src])
+ base, _ = os.path.splitext(os.path.basename(src))
+ rc_file = os.path.join(rc_dir, base + '.rc')
# then compile .RC to .RES file
- self.spawn([self.rc] +
- ["/fo" + obj] + [rc_file])
+ self.spawn([self.rc] + ["/fo" + obj] + [rc_file])
except DistutilsExecError as msg:
raise CompileError(msg)
continue
else:
# how to handle this file?
- raise CompileError("Don't know how to compile %s to %s"
- % (src, obj))
+ raise CompileError(
+ "Don't know how to compile {} to {}".format(src, obj)
+ )
output_opt = "/Fo" + obj
try:
- self.spawn([self.cc] + compile_opts + pp_opts +
- [input_opt, output_opt] +
- extra_postargs)
+ self.spawn(
+ [self.cc]
+ + compile_opts
+ + pp_opts
+ + [input_opt, output_opt]
+ + extra_postargs
+ )
except DistutilsExecError as msg:
raise CompileError(msg)
return objects
-
- def create_static_lib(self,
- objects,
- output_libname,
- output_dir=None,
- debug=0,
- target_lang=None):
+ def create_static_lib(
+ self, objects, output_libname, output_dir=None, debug=0, target_lang=None
+ ):
if not self.initialized:
self.initialize()
(objects, output_dir) = self._fix_object_args(objects, output_dir)
- output_filename = self.library_filename(output_libname,
- output_dir=output_dir)
+ output_filename = self.library_filename(output_libname, output_dir=output_dir)
if self._need_link(objects, output_filename):
lib_args = objects + ['/OUT:' + output_filename]
if debug:
- pass # XXX what goes here?
+ pass # XXX what goes here?
try:
self.spawn([self.lib] + lib_args)
except DistutilsExecError as msg:
@@ -561,36 +603,36 @@ class MSVCCompiler(CCompiler) :
else:
log.debug("skipping %s (up-to-date)", output_filename)
-
- def link(self,
- target_desc,
- objects,
- output_filename,
- output_dir=None,
- libraries=None,
- library_dirs=None,
- runtime_library_dirs=None,
- export_symbols=None,
- debug=0,
- extra_preargs=None,
- extra_postargs=None,
- build_temp=None,
- target_lang=None):
+ def link( # noqa: C901
+ self,
+ target_desc,
+ objects,
+ output_filename,
+ output_dir=None,
+ libraries=None,
+ library_dirs=None,
+ runtime_library_dirs=None,
+ export_symbols=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ build_temp=None,
+ target_lang=None,
+ ):
if not self.initialized:
self.initialize()
(objects, output_dir) = self._fix_object_args(objects, output_dir)
- fixed_args = self._fix_lib_args(libraries, library_dirs,
- runtime_library_dirs)
+ fixed_args = self._fix_lib_args(libraries, library_dirs, runtime_library_dirs)
(libraries, library_dirs, runtime_library_dirs) = fixed_args
if runtime_library_dirs:
- self.warn ("I don't know what to do with 'runtime_library_dirs': "
- + str (runtime_library_dirs))
+ self.warn(
+ "I don't know what to do with 'runtime_library_dirs': "
+ + str(runtime_library_dirs)
+ )
- lib_opts = gen_lib_options(self,
- library_dirs, runtime_library_dirs,
- libraries)
+ lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs, libraries)
if output_dir is not None:
output_filename = os.path.join(output_dir, output_filename)
@@ -607,11 +649,12 @@ class MSVCCompiler(CCompiler) :
ldflags = self.ldflags_shared
export_opts = []
- for sym in (export_symbols or []):
+ for sym in export_symbols or []:
export_opts.append("/EXPORT:" + sym)
- ld_args = (ldflags + lib_opts + export_opts +
- objects + ['/OUT:' + output_filename])
+ ld_args = (
+ ldflags + lib_opts + export_opts + objects + ['/OUT:' + output_filename]
+ )
# The MSVC linker generates .lib and .exp files, which cannot be
# suppressed by any linker switches. The .lib files may even be
@@ -621,11 +664,10 @@ class MSVCCompiler(CCompiler) :
build_temp = os.path.dirname(objects[0])
if export_symbols is not None:
(dll_name, dll_ext) = os.path.splitext(
- os.path.basename(output_filename))
- implib_file = os.path.join(
- build_temp,
- self.library_filename(dll_name))
- ld_args.append ('/IMPLIB:' + implib_file)
+ os.path.basename(output_filename)
+ )
+ implib_file = os.path.join(build_temp, self.library_filename(dll_name))
+ ld_args.append('/IMPLIB:' + implib_file)
self.manifest_setup_ldargs(output_filename, build_temp, ld_args)
@@ -648,10 +690,9 @@ class MSVCCompiler(CCompiler) :
mfinfo = self.manifest_get_embed_info(target_desc, ld_args)
if mfinfo is not None:
mffilename, mfid = mfinfo
- out_arg = '-outputresource:%s;%s' % (output_filename, mfid)
+ out_arg = '-outputresource:{};{}'.format(output_filename, mfid)
try:
- self.spawn(['mt.exe', '-nologo', '-manifest',
- mffilename, out_arg])
+ self.spawn(['mt.exe', '-nologo', '-manifest', mffilename, out_arg])
except DistutilsExecError as msg:
raise LinkError(msg)
else:
@@ -665,8 +706,8 @@ class MSVCCompiler(CCompiler) :
# Ask the linker to generate the manifest in the temp dir, so
# we can check it, and possibly embed it, later.
temp_manifest = os.path.join(
- build_temp,
- os.path.basename(output_filename) + ".manifest")
+ build_temp, os.path.basename(output_filename) + ".manifest"
+ )
ld_args.append('/MANIFESTFILE:' + temp_manifest)
def manifest_get_embed_info(self, target_desc, ld_args):
@@ -709,9 +750,10 @@ class MSVCCompiler(CCompiler) :
finally:
manifest_f.close()
pattern = re.compile(
- r"""<assemblyIdentity.*?name=("|')Microsoft\."""\
+ r"""<assemblyIdentity.*?name=("|')Microsoft\."""
r"""VC\d{2}\.CRT("|').*?(/>|</assemblyIdentity>)""",
- re.DOTALL)
+ re.DOTALL,
+ )
manifest_buf = re.sub(pattern, "", manifest_buf)
pattern = r"<dependentAssembly>\s*</dependentAssembly>"
manifest_buf = re.sub(pattern, "", manifest_buf)
@@ -719,7 +761,9 @@ class MSVCCompiler(CCompiler) :
# don't want a manifest embedded.
pattern = re.compile(
r"""<assemblyIdentity.*?name=(?:"|')(.+?)(?:"|')"""
- r""".*?(?:/>|</assemblyIdentity>)""", re.DOTALL)
+ r""".*?(?:/>|</assemblyIdentity>)""",
+ re.DOTALL,
+ )
if re.search(pattern, manifest_buf) is None:
return None
@@ -741,12 +785,12 @@ class MSVCCompiler(CCompiler) :
def runtime_library_dir_option(self, dir):
raise DistutilsPlatformError(
- "don't know how to set runtime library search path for MSVC++")
+ "don't know how to set runtime library search path for MSVC++"
+ )
def library_option(self, lib):
return self.library_filename(lib)
-
def find_library_file(self, dirs, lib, debug=0):
# Prefer a debugging library if found (and requested), but deal
# with it if we don't have one.
@@ -756,7 +800,7 @@ class MSVCCompiler(CCompiler) :
try_names = [lib]
for dir in dirs:
for name in try_names:
- libfile = os.path.join(dir, self.library_filename (name))
+ libfile = os.path.join(dir, self.library_filename(name))
if os.path.exists(libfile):
return libfile
else:
@@ -781,7 +825,7 @@ class MSVCCompiler(CCompiler) :
# didn't find it; try existing path
for p in os.environ['Path'].split(';'):
- fn = os.path.join(os.path.abspath(p),exe)
+ fn = os.path.join(os.path.abspath(p), exe)
if os.path.isfile(fn):
return fn
diff --git a/setuptools/_distutils/msvccompiler.py b/setuptools/_distutils/msvccompiler.py
index e1367b89..59ebe99c 100644
--- a/setuptools/_distutils/msvccompiler.py
+++ b/setuptools/_distutils/msvccompiler.py
@@ -8,13 +8,18 @@ for the Microsoft Visual Studio.
# hacked by Robin Becker and Thomas Heller to do a better job of
# finding DevStudio (through the registry)
-import sys, os
-from distutils.errors import \
- DistutilsExecError, DistutilsPlatformError, \
- CompileError, LibError, LinkError
-from distutils.ccompiler import \
- CCompiler, gen_lib_options
-from distutils import log
+import sys
+import os
+import warnings
+from .errors import (
+ DistutilsExecError,
+ DistutilsPlatformError,
+ CompileError,
+ LibError,
+ LinkError,
+)
+from .ccompiler import CCompiler, gen_lib_options
+from ._log import log
_can_read_reg = False
try:
@@ -32,6 +37,7 @@ except ImportError:
try:
import win32api
import win32con
+
_can_read_reg = True
hkey_mod = win32con
@@ -40,17 +46,30 @@ except ImportError:
RegEnumValue = win32api.RegEnumValue
RegError = win32api.error
except ImportError:
- log.info("Warning: Can't read registry to find the "
- "necessary compiler setting\n"
- "Make sure that Python modules winreg, "
- "win32api or win32con are installed.")
+ log.info(
+ "Warning: Can't read registry to find the "
+ "necessary compiler setting\n"
+ "Make sure that Python modules winreg, "
+ "win32api or win32con are installed."
+ )
pass
if _can_read_reg:
- HKEYS = (hkey_mod.HKEY_USERS,
- hkey_mod.HKEY_CURRENT_USER,
- hkey_mod.HKEY_LOCAL_MACHINE,
- hkey_mod.HKEY_CLASSES_ROOT)
+ HKEYS = (
+ hkey_mod.HKEY_USERS,
+ hkey_mod.HKEY_CURRENT_USER,
+ hkey_mod.HKEY_LOCAL_MACHINE,
+ hkey_mod.HKEY_CLASSES_ROOT,
+ )
+
+
+warnings.warn(
+ "msvccompiler is deprecated and slated to be removed "
+ "in the future. Please discontinue use or file an issue "
+ "with pypa/distutils describing your use case.",
+ DeprecationWarning,
+)
+
def read_keys(base, key):
"""Return list of registry keys."""
@@ -69,6 +88,7 @@ def read_keys(base, key):
i += 1
return L
+
def read_values(base, key):
"""Return dict of registry keys and values.
@@ -90,6 +110,7 @@ def read_values(base, key):
i += 1
return d
+
def convert_mbcs(s):
dec = getattr(s, "decode", None)
if dec is not None:
@@ -99,6 +120,7 @@ def convert_mbcs(s):
pass
return s
+
class MacroExpander:
def __init__(self, version):
self.macros = {}
@@ -122,12 +144,13 @@ class MacroExpander:
self.set_macro("FrameworkSDKDir", net, "sdkinstallrootv1.1")
else:
self.set_macro("FrameworkSDKDir", net, "sdkinstallroot")
- except KeyError as exc: #
+ except KeyError:
raise DistutilsPlatformError(
- """Python was built with Visual Studio 2003;
+ """Python was built with Visual Studio 2003;
extensions must be built with a compiler than can generate compatible binaries.
Visual Studio 2003 was not found on this system. If you have Cygwin installed,
-you can try compiling with MingW32, by passing "-c mingw32" to setup.py.""")
+you can try compiling with MingW32, by passing "-c mingw32" to setup.py."""
+ )
p = r"Software\Microsoft\NET Framework Setup\Product"
for base in HKEYS:
@@ -136,7 +159,7 @@ you can try compiling with MingW32, by passing "-c mingw32" to setup.py.""")
except RegError:
continue
key = RegEnumKey(h, 0)
- d = read_values(base, r"%s\%s" % (p, key))
+ d = read_values(base, r"{}\{}".format(p, key))
self.macros["$(FrameworkVersion)"] = d["version"]
def sub(self, s):
@@ -144,6 +167,7 @@ you can try compiling with MingW32, by passing "-c mingw32" to setup.py.""")
s = s.replace(k, v)
return s
+
def get_build_version():
"""Return the version of MSVC that was used to build Python.
@@ -169,6 +193,7 @@ def get_build_version():
# else we don't know what version of the compiler this is
return None
+
def get_build_architecture():
"""Return the processor architecture.
@@ -180,7 +205,8 @@ def get_build_architecture():
if i == -1:
return "Intel"
j = sys.version.find(")", i)
- return sys.version[i+len(prefix):j]
+ return sys.version[i + len(prefix) : j]
+
def normalize_and_reduce_paths(paths):
"""Return a list of normalized paths with duplicates removed.
@@ -197,9 +223,9 @@ def normalize_and_reduce_paths(paths):
return reduced_paths
-class MSVCCompiler(CCompiler) :
+class MSVCCompiler(CCompiler):
"""Concrete class that implements an interface to Microsoft Visual C++,
- as defined by the CCompiler abstract class."""
+ as defined by the CCompiler abstract class."""
compiler_type = 'msvc'
@@ -218,8 +244,7 @@ class MSVCCompiler(CCompiler) :
# Needed for the filename generation methods provided by the
# base class, CCompiler.
- src_extensions = (_c_extensions + _cpp_extensions +
- _rc_extensions + _mc_extensions)
+ src_extensions = _c_extensions + _cpp_extensions + _rc_extensions + _mc_extensions
res_extension = '.res'
obj_extension = '.obj'
static_lib_extension = '.lib'
@@ -247,7 +272,11 @@ class MSVCCompiler(CCompiler) :
def initialize(self):
self.__paths = []
- if "DISTUTILS_USE_SDK" in os.environ and "MSSdk" in os.environ and self.find_exe("cl.exe"):
+ if (
+ "DISTUTILS_USE_SDK" in os.environ
+ and "MSSdk" in os.environ
+ and self.find_exe("cl.exe")
+ ):
# Assume that the SDK set up everything alright; don't try to be
# smarter
self.cc = "cl.exe"
@@ -259,16 +288,17 @@ class MSVCCompiler(CCompiler) :
self.__paths = self.get_msvc_paths("path")
if len(self.__paths) == 0:
- raise DistutilsPlatformError("Python was built with %s, "
- "and extensions need to be built with the same "
- "version of the compiler, but it isn't installed."
- % self.__product)
+ raise DistutilsPlatformError(
+ "Python was built with %s, "
+ "and extensions need to be built with the same "
+ "version of the compiler, but it isn't installed." % self.__product
+ )
self.cc = self.find_exe("cl.exe")
self.linker = self.find_exe("link.exe")
self.lib = self.find_exe("lib.exe")
- self.rc = self.find_exe("rc.exe") # resource compiler
- self.mc = self.find_exe("mc.exe") # message compiler
+ self.rc = self.find_exe("rc.exe") # resource compiler
+ self.mc = self.find_exe("mc.exe") # message compiler
self.set_path_env_var('lib')
self.set_path_env_var('include')
@@ -283,75 +313,92 @@ class MSVCCompiler(CCompiler) :
self.preprocess_options = None
if self.__arch == "Intel":
- self.compile_options = [ '/nologo', '/O2', '/MD', '/W3', '/GX' ,
- '/DNDEBUG']
- self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GX',
- '/Z7', '/D_DEBUG']
+ self.compile_options = ['/nologo', '/O2', '/MD', '/W3', '/GX', '/DNDEBUG']
+ self.compile_options_debug = [
+ '/nologo',
+ '/Od',
+ '/MDd',
+ '/W3',
+ '/GX',
+ '/Z7',
+ '/D_DEBUG',
+ ]
else:
# Win64
- self.compile_options = [ '/nologo', '/O2', '/MD', '/W3', '/GS-' ,
- '/DNDEBUG']
- self.compile_options_debug = ['/nologo', '/Od', '/MDd', '/W3', '/GS-',
- '/Z7', '/D_DEBUG']
+ self.compile_options = ['/nologo', '/O2', '/MD', '/W3', '/GS-', '/DNDEBUG']
+ self.compile_options_debug = [
+ '/nologo',
+ '/Od',
+ '/MDd',
+ '/W3',
+ '/GS-',
+ '/Z7',
+ '/D_DEBUG',
+ ]
self.ldflags_shared = ['/DLL', '/nologo', '/INCREMENTAL:NO']
if self.__version >= 7:
- self.ldflags_shared_debug = [
- '/DLL', '/nologo', '/INCREMENTAL:no', '/DEBUG'
- ]
+ self.ldflags_shared_debug = ['/DLL', '/nologo', '/INCREMENTAL:no', '/DEBUG']
else:
self.ldflags_shared_debug = [
- '/DLL', '/nologo', '/INCREMENTAL:no', '/pdb:None', '/DEBUG'
- ]
- self.ldflags_static = [ '/nologo']
+ '/DLL',
+ '/nologo',
+ '/INCREMENTAL:no',
+ '/pdb:None',
+ '/DEBUG',
+ ]
+ self.ldflags_static = ['/nologo']
self.initialized = True
# -- Worker methods ------------------------------------------------
- def object_filenames(self,
- source_filenames,
- strip_dir=0,
- output_dir=''):
+ def object_filenames(self, source_filenames, strip_dir=0, output_dir=''):
# Copied from ccompiler.py, extended to return .res as 'object'-file
# for .rc input file
- if output_dir is None: output_dir = ''
+ if output_dir is None:
+ output_dir = ''
obj_names = []
for src_name in source_filenames:
- (base, ext) = os.path.splitext (src_name)
- base = os.path.splitdrive(base)[1] # Chop off the drive
- base = base[os.path.isabs(base):] # If abs, chop off leading /
+ (base, ext) = os.path.splitext(src_name)
+ base = os.path.splitdrive(base)[1] # Chop off the drive
+ base = base[os.path.isabs(base) :] # If abs, chop off leading /
if ext not in self.src_extensions:
# Better to raise an exception instead of silently continuing
# and later complain about sources and targets having
# different lengths
- raise CompileError ("Don't know how to compile %s" % src_name)
+ raise CompileError("Don't know how to compile %s" % src_name)
if strip_dir:
- base = os.path.basename (base)
+ base = os.path.basename(base)
if ext in self._rc_extensions:
- obj_names.append (os.path.join (output_dir,
- base + self.res_extension))
+ obj_names.append(os.path.join(output_dir, base + self.res_extension))
elif ext in self._mc_extensions:
- obj_names.append (os.path.join (output_dir,
- base + self.res_extension))
+ obj_names.append(os.path.join(output_dir, base + self.res_extension))
else:
- obj_names.append (os.path.join (output_dir,
- base + self.obj_extension))
+ obj_names.append(os.path.join(output_dir, base + self.obj_extension))
return obj_names
-
- def compile(self, sources,
- output_dir=None, macros=None, include_dirs=None, debug=0,
- extra_preargs=None, extra_postargs=None, depends=None):
+ def compile( # noqa: C901
+ self,
+ sources,
+ output_dir=None,
+ macros=None,
+ include_dirs=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ depends=None,
+ ):
if not self.initialized:
self.initialize()
- compile_info = self._setup_compile(output_dir, macros, include_dirs,
- sources, depends, extra_postargs)
+ compile_info = self._setup_compile(
+ output_dir, macros, include_dirs, sources, depends, extra_postargs
+ )
macros, objects, extra_postargs, pp_opts, build = compile_info
compile_opts = extra_preargs or []
- compile_opts.append ('/c')
+ compile_opts.append('/c')
if debug:
compile_opts.extend(self.compile_options_debug)
else:
@@ -377,8 +424,7 @@ class MSVCCompiler(CCompiler) :
input_opt = src
output_opt = "/fo" + obj
try:
- self.spawn([self.rc] + pp_opts +
- [output_opt] + [input_opt])
+ self.spawn([self.rc] + pp_opts + [output_opt] + [input_opt])
except DistutilsExecError as msg:
raise CompileError(msg)
continue
@@ -398,50 +444,48 @@ class MSVCCompiler(CCompiler) :
rc_dir = os.path.dirname(obj)
try:
# first compile .MC to .RC and .H file
- self.spawn([self.mc] +
- ['-h', h_dir, '-r', rc_dir] + [src])
- base, _ = os.path.splitext (os.path.basename (src))
- rc_file = os.path.join (rc_dir, base + '.rc')
+ self.spawn([self.mc] + ['-h', h_dir, '-r', rc_dir] + [src])
+ base, _ = os.path.splitext(os.path.basename(src))
+ rc_file = os.path.join(rc_dir, base + '.rc')
# then compile .RC to .RES file
- self.spawn([self.rc] +
- ["/fo" + obj] + [rc_file])
+ self.spawn([self.rc] + ["/fo" + obj] + [rc_file])
except DistutilsExecError as msg:
raise CompileError(msg)
continue
else:
# how to handle this file?
- raise CompileError("Don't know how to compile %s to %s"
- % (src, obj))
+ raise CompileError(
+ "Don't know how to compile {} to {}".format(src, obj)
+ )
output_opt = "/Fo" + obj
try:
- self.spawn([self.cc] + compile_opts + pp_opts +
- [input_opt, output_opt] +
- extra_postargs)
+ self.spawn(
+ [self.cc]
+ + compile_opts
+ + pp_opts
+ + [input_opt, output_opt]
+ + extra_postargs
+ )
except DistutilsExecError as msg:
raise CompileError(msg)
return objects
-
- def create_static_lib(self,
- objects,
- output_libname,
- output_dir=None,
- debug=0,
- target_lang=None):
+ def create_static_lib(
+ self, objects, output_libname, output_dir=None, debug=0, target_lang=None
+ ):
if not self.initialized:
self.initialize()
(objects, output_dir) = self._fix_object_args(objects, output_dir)
- output_filename = self.library_filename(output_libname,
- output_dir=output_dir)
+ output_filename = self.library_filename(output_libname, output_dir=output_dir)
if self._need_link(objects, output_filename):
lib_args = objects + ['/OUT:' + output_filename]
if debug:
- pass # XXX what goes here?
+ pass # XXX what goes here?
try:
self.spawn([self.lib] + lib_args)
except DistutilsExecError as msg:
@@ -449,36 +493,36 @@ class MSVCCompiler(CCompiler) :
else:
log.debug("skipping %s (up-to-date)", output_filename)
-
- def link(self,
- target_desc,
- objects,
- output_filename,
- output_dir=None,
- libraries=None,
- library_dirs=None,
- runtime_library_dirs=None,
- export_symbols=None,
- debug=0,
- extra_preargs=None,
- extra_postargs=None,
- build_temp=None,
- target_lang=None):
+ def link( # noqa: C901
+ self,
+ target_desc,
+ objects,
+ output_filename,
+ output_dir=None,
+ libraries=None,
+ library_dirs=None,
+ runtime_library_dirs=None,
+ export_symbols=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ build_temp=None,
+ target_lang=None,
+ ):
if not self.initialized:
self.initialize()
(objects, output_dir) = self._fix_object_args(objects, output_dir)
- fixed_args = self._fix_lib_args(libraries, library_dirs,
- runtime_library_dirs)
+ fixed_args = self._fix_lib_args(libraries, library_dirs, runtime_library_dirs)
(libraries, library_dirs, runtime_library_dirs) = fixed_args
if runtime_library_dirs:
- self.warn ("I don't know what to do with 'runtime_library_dirs': "
- + str (runtime_library_dirs))
+ self.warn(
+ "I don't know what to do with 'runtime_library_dirs': "
+ + str(runtime_library_dirs)
+ )
- lib_opts = gen_lib_options(self,
- library_dirs, runtime_library_dirs,
- libraries)
+ lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs, libraries)
if output_dir is not None:
output_filename = os.path.join(output_dir, output_filename)
@@ -495,11 +539,12 @@ class MSVCCompiler(CCompiler) :
ldflags = self.ldflags_shared
export_opts = []
- for sym in (export_symbols or []):
+ for sym in export_symbols or []:
export_opts.append("/EXPORT:" + sym)
- ld_args = (ldflags + lib_opts + export_opts +
- objects + ['/OUT:' + output_filename])
+ ld_args = (
+ ldflags + lib_opts + export_opts + objects + ['/OUT:' + output_filename]
+ )
# The MSVC linker generates .lib and .exp files, which cannot be
# suppressed by any linker switches. The .lib files may even be
@@ -508,11 +553,12 @@ class MSVCCompiler(CCompiler) :
# builds, they can go into the same directory.
if export_symbols is not None:
(dll_name, dll_ext) = os.path.splitext(
- os.path.basename(output_filename))
+ os.path.basename(output_filename)
+ )
implib_file = os.path.join(
- os.path.dirname(objects[0]),
- self.library_filename(dll_name))
- ld_args.append ('/IMPLIB:' + implib_file)
+ os.path.dirname(objects[0]), self.library_filename(dll_name)
+ )
+ ld_args.append('/IMPLIB:' + implib_file)
if extra_preargs:
ld_args[:0] = extra_preargs
@@ -528,7 +574,6 @@ class MSVCCompiler(CCompiler) :
else:
log.debug("skipping %s (up-to-date)", output_filename)
-
# -- Miscellaneous methods -----------------------------------------
# These are all used by the 'gen_lib_options() function, in
# ccompiler.py.
@@ -538,12 +583,12 @@ class MSVCCompiler(CCompiler) :
def runtime_library_dir_option(self, dir):
raise DistutilsPlatformError(
- "don't know how to set runtime library search path for MSVC++")
+ "don't know how to set runtime library search path for MSVC++"
+ )
def library_option(self, lib):
return self.library_filename(lib)
-
def find_library_file(self, dirs, lib, debug=0):
# Prefer a debugging library if found (and requested), but deal
# with it if we don't have one.
@@ -553,7 +598,7 @@ class MSVCCompiler(CCompiler) :
try_names = [lib]
for dir in dirs:
for name in try_names:
- libfile = os.path.join(dir, self.library_filename (name))
+ libfile = os.path.join(dir, self.library_filename(name))
if os.path.exists(libfile):
return libfile
else:
@@ -578,7 +623,7 @@ class MSVCCompiler(CCompiler) :
# didn't find it; try existing path
for p in os.environ['Path'].split(';'):
- fn = os.path.join(os.path.abspath(p),exe)
+ fn = os.path.join(os.path.abspath(p), exe)
if os.path.isfile(fn):
return fn
@@ -595,11 +640,15 @@ class MSVCCompiler(CCompiler) :
path = path + " dirs"
if self.__version >= 7:
- key = (r"%s\%0.1f\VC\VC_OBJECTS_PLATFORM_INFO\Win32\Directories"
- % (self.__root, self.__version))
+ key = r"{}\{:0.1f}\VC\VC_OBJECTS_PLATFORM_INFO\Win32\Directories".format(
+ self.__root,
+ self.__version,
+ )
else:
- key = (r"%s\6.0\Build System\Components\Platforms"
- r"\Win32 (%s)\Directories" % (self.__root, platform))
+ key = (
+ r"%s\6.0\Build System\Components\Platforms"
+ r"\Win32 (%s)\Directories" % (self.__root, platform)
+ )
for base in HKEYS:
d = read_values(base, key)
@@ -613,10 +662,12 @@ class MSVCCompiler(CCompiler) :
if self.__version == 6:
for base in HKEYS:
if read_values(base, r"%s\6.0" % self.__root) is not None:
- self.warn("It seems you have Visual Studio 6 installed, "
+ self.warn(
+ "It seems you have Visual Studio 6 installed, "
"but the expected registry settings are not present.\n"
"You must at least run the Visual Studio GUI once "
- "so that these entries are created.")
+ "so that these entries are created."
+ )
break
return []
@@ -639,5 +690,6 @@ if get_build_version() >= 8.0:
log.debug("Importing new compiler from distutils.msvc9compiler")
OldMSVCCompiler = MSVCCompiler
from distutils.msvc9compiler import MSVCCompiler
+
# get_build_architecture not really relevant now we support cross-compile
- from distutils.msvc9compiler import MacroExpander
+ from distutils.msvc9compiler import MacroExpander # noqa: F811
diff --git a/setuptools/_distutils/py35compat.py b/setuptools/_distutils/py35compat.py
deleted file mode 100644
index 79b2e7f3..00000000
--- a/setuptools/_distutils/py35compat.py
+++ /dev/null
@@ -1,19 +0,0 @@
-import sys
-import subprocess
-
-
-def __optim_args_from_interpreter_flags():
- """Return a list of command-line arguments reproducing the current
- optimization settings in sys.flags."""
- args = []
- value = sys.flags.optimize
- if value > 0:
- args.append("-" + "O" * value)
- return args
-
-
-_optim_args_from_interpreter_flags = getattr(
- subprocess,
- "_optim_args_from_interpreter_flags",
- __optim_args_from_interpreter_flags,
-)
diff --git a/setuptools/_distutils/py38compat.py b/setuptools/_distutils/py38compat.py
index 7dbe8cef..59224e71 100644
--- a/setuptools/_distutils/py38compat.py
+++ b/setuptools/_distutils/py38compat.py
@@ -1,7 +1,8 @@
def aix_platform(osname, version, release):
try:
import _aix_support
+
return _aix_support.aix_platform()
except ImportError:
pass
- return "%s-%s.%s" % (osname, version, release)
+ return "{}-{}.{}".format(osname, version, release)
diff --git a/setuptools/_distutils/py39compat.py b/setuptools/_distutils/py39compat.py
new file mode 100644
index 00000000..c43e5f10
--- /dev/null
+++ b/setuptools/_distutils/py39compat.py
@@ -0,0 +1,22 @@
+import sys
+import platform
+
+
+def add_ext_suffix_39(vars):
+ """
+ Ensure vars contains 'EXT_SUFFIX'. pypa/distutils#130
+ """
+ import _imp
+
+ ext_suffix = _imp.extension_suffixes()[0]
+ vars.update(
+ EXT_SUFFIX=ext_suffix,
+ # sysconfig sets SO to match EXT_SUFFIX, so maintain
+ # that expectation.
+ # https://github.com/python/cpython/blob/785cc6770588de087d09e89a69110af2542be208/Lib/sysconfig.py#L671-L673
+ SO=ext_suffix,
+ )
+
+
+needs_ext_suffix = sys.version_info < (3, 10) and platform.system() == 'Windows'
+add_ext_suffix = add_ext_suffix_39 if needs_ext_suffix else lambda vars: None
diff --git a/setuptools/_distutils/spawn.py b/setuptools/_distutils/spawn.py
index b2d10e39..afefe525 100644
--- a/setuptools/_distutils/spawn.py
+++ b/setuptools/_distutils/spawn.py
@@ -10,12 +10,12 @@ import sys
import os
import subprocess
-from distutils.errors import DistutilsExecError
-from distutils.debug import DEBUG
-from distutils import log
+from .errors import DistutilsExecError
+from .debug import DEBUG
+from ._log import log
-def spawn(cmd, search_path=1, verbose=0, dry_run=0, env=None):
+def spawn(cmd, search_path=1, verbose=0, dry_run=0, env=None): # noqa: C901
"""Run another program, specified as a command list 'cmd', in a new process.
'cmd' is just the argument list for the new process, ie.
@@ -48,6 +48,7 @@ def spawn(cmd, search_path=1, verbose=0, dry_run=0, env=None):
if sys.platform == 'darwin':
from distutils.util import MACOSX_VERSION_VAR, get_macosx_target_ver
+
macosx_target_ver = get_macosx_target_ver()
if macosx_target_ver:
env[MACOSX_VERSION_VAR] = macosx_target_ver
@@ -60,13 +61,15 @@ def spawn(cmd, search_path=1, verbose=0, dry_run=0, env=None):
if not DEBUG:
cmd = cmd[0]
raise DistutilsExecError(
- "command %r failed: %s" % (cmd, exc.args[-1])) from exc
+ "command {!r} failed: {}".format(cmd, exc.args[-1])
+ ) from exc
if exitcode:
if not DEBUG:
cmd = cmd[0]
raise DistutilsExecError(
- "command %r failed with exit code %s" % (cmd, exitcode))
+ "command {!r} failed with exit code {}".format(cmd, exitcode)
+ )
def find_executable(executable, path=None):
diff --git a/setuptools/_distutils/sysconfig.py b/setuptools/_distutils/sysconfig.py
index 4a77a431..0ec69366 100644
--- a/setuptools/_distutils/sysconfig.py
+++ b/setuptools/_distutils/sysconfig.py
@@ -9,13 +9,15 @@ Written by: Fred L. Drake, Jr.
Email: <fdrake@acm.org>
"""
-import _imp
import os
import re
import sys
import sysconfig
+import pathlib
from .errors import DistutilsPlatformError
+from . import py39compat
+from ._functools import pass_none
IS_PYPY = '__pypy__' in sys.builtin_module_names
@@ -39,31 +41,48 @@ else:
project_base = os.getcwd()
-# python_build: (Boolean) if true, we're either building Python or
-# building an extension with an un-installed Python, so we use
-# different (hard-wired) directories.
def _is_python_source_dir(d):
- for fn in ("Setup", "Setup.local"):
- if os.path.isfile(os.path.join(d, "Modules", fn)):
- return True
- return False
+ """
+ Return True if the target directory appears to point to an
+ un-installed Python.
+ """
+ modules = pathlib.Path(d).joinpath('Modules')
+ return any(modules.joinpath(fn).is_file() for fn in ('Setup', 'Setup.local'))
+
_sys_home = getattr(sys, '_home', None)
+
+def _is_parent(dir_a, dir_b):
+ """
+ Return True if a is a parent of b.
+ """
+ return os.path.normcase(dir_a).startswith(os.path.normcase(dir_b))
+
+
if os.name == 'nt':
+
+ @pass_none
def _fix_pcbuild(d):
- if d and os.path.normcase(d).startswith(
- os.path.normcase(os.path.join(PREFIX, "PCbuild"))):
- return PREFIX
- return d
+ # In a venv, sys._home will be inside BASE_PREFIX rather than PREFIX.
+ prefixes = PREFIX, BASE_PREFIX
+ matched = (
+ prefix
+ for prefix in prefixes
+ if _is_parent(d, os.path.join(prefix, "PCbuild"))
+ )
+ return next(matched, d)
+
project_base = _fix_pcbuild(project_base)
_sys_home = _fix_pcbuild(_sys_home)
+
def _python_build():
if _sys_home:
return _is_python_source_dir(_sys_home)
return _is_python_source_dir(project_base)
+
python_build = _python_build()
@@ -79,6 +98,7 @@ except AttributeError:
# this attribute, which is fine.
pass
+
def get_python_version():
"""Return a string containing the major and minor Python version,
leaving off the patchlevel. Sample return values could be '1.5'
@@ -98,36 +118,83 @@ def get_python_inc(plat_specific=0, prefix=None):
If 'prefix' is supplied, use it instead of sys.base_prefix or
sys.base_exec_prefix -- i.e., ignore 'plat_specific'.
"""
- if prefix is None:
- prefix = plat_specific and BASE_EXEC_PREFIX or BASE_PREFIX
- if os.name == "posix":
- if IS_PYPY and sys.version_info < (3, 8):
- return os.path.join(prefix, 'include')
- if python_build:
- # Assume the executable is in the build directory. The
- # pyconfig.h file should be in the same directory. Since
- # the build directory may not be the source directory, we
- # must use "srcdir" from the makefile to find the "Include"
- # directory.
- if plat_specific:
- return _sys_home or project_base
- else:
- incdir = os.path.join(get_config_var('srcdir'), 'Include')
- return os.path.normpath(incdir)
- implementation = 'pypy' if IS_PYPY else 'python'
- python_dir = implementation + get_python_version() + build_flags
- return os.path.join(prefix, "include", python_dir)
- elif os.name == "nt":
- if python_build:
- # Include both the include and PC dir to ensure we can find
- # pyconfig.h
- return (os.path.join(prefix, "include") + os.path.pathsep +
- os.path.join(prefix, "PC"))
- return os.path.join(prefix, "include")
- else:
+ default_prefix = BASE_EXEC_PREFIX if plat_specific else BASE_PREFIX
+ resolved_prefix = prefix if prefix is not None else default_prefix
+ try:
+ getter = globals()[f'_get_python_inc_{os.name}']
+ except KeyError:
raise DistutilsPlatformError(
"I don't know where Python installs its C header files "
- "on platform '%s'" % os.name)
+ "on platform '%s'" % os.name
+ )
+ return getter(resolved_prefix, prefix, plat_specific)
+
+
+def _get_python_inc_posix(prefix, spec_prefix, plat_specific):
+ if IS_PYPY and sys.version_info < (3, 8):
+ return os.path.join(prefix, 'include')
+ return (
+ _get_python_inc_posix_python(plat_specific)
+ or _get_python_inc_from_config(plat_specific, spec_prefix)
+ or _get_python_inc_posix_prefix(prefix)
+ )
+
+
+def _get_python_inc_posix_python(plat_specific):
+ """
+ Assume the executable is in the build directory. The
+ pyconfig.h file should be in the same directory. Since
+ the build directory may not be the source directory,
+ use "srcdir" from the makefile to find the "Include"
+ directory.
+ """
+ if not python_build:
+ return
+ if plat_specific:
+ return _sys_home or project_base
+ incdir = os.path.join(get_config_var('srcdir'), 'Include')
+ return os.path.normpath(incdir)
+
+
+def _get_python_inc_from_config(plat_specific, spec_prefix):
+ """
+ If no prefix was explicitly specified, provide the include
+ directory from the config vars. Useful when
+ cross-compiling, since the config vars may come from
+ the host
+ platform Python installation, while the current Python
+ executable is from the build platform installation.
+
+ >>> monkeypatch = getfixture('monkeypatch')
+ >>> gpifc = _get_python_inc_from_config
+ >>> monkeypatch.setitem(gpifc.__globals__, 'get_config_var', str.lower)
+ >>> gpifc(False, '/usr/bin/')
+ >>> gpifc(False, '')
+ >>> gpifc(False, None)
+ 'includepy'
+ >>> gpifc(True, None)
+ 'confincludepy'
+ """
+ if spec_prefix is None:
+ return get_config_var('CONF' * plat_specific + 'INCLUDEPY')
+
+
+def _get_python_inc_posix_prefix(prefix):
+ implementation = 'pypy' if IS_PYPY else 'python'
+ python_dir = implementation + get_python_version() + build_flags
+ return os.path.join(prefix, "include", python_dir)
+
+
+def _get_python_inc_nt(prefix, spec_prefix, plat_specific):
+ if python_build:
+ # Include both the include and PC dir to ensure we can find
+ # pyconfig.h
+ return (
+ os.path.join(prefix, "include")
+ + os.path.pathsep
+ + os.path.join(prefix, "PC")
+ )
+ return os.path.join(prefix, "include")
# allow this behavior to be monkey-patched. Ref pypa/distutils#2.
@@ -178,8 +245,7 @@ def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
# Pure Python
libdir = "lib"
implementation = 'pypy' if IS_PYPY else 'python'
- libpython = os.path.join(prefix, libdir,
- implementation + get_python_version())
+ libpython = os.path.join(prefix, libdir, implementation + get_python_version())
return _posix_lib(standard_lib, libpython, early_prefix, prefix)
elif os.name == "nt":
if standard_lib:
@@ -189,11 +255,11 @@ def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
else:
raise DistutilsPlatformError(
"I don't know where Python installs its library "
- "on platform '%s'" % os.name)
+ "on platform '%s'" % os.name
+ )
-
-def customize_compiler(compiler):
+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
@@ -213,20 +279,36 @@ def customize_compiler(compiler):
# 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'
- (cc, cxx, cflags, ccshared, ldshared, shlib_suffix, ar, ar_flags) = \
- get_config_vars('CC', 'CXX', 'CFLAGS',
- 'CCSHARED', 'LDSHARED', 'SHLIB_SUFFIX', 'AR', 'ARFLAGS')
+ (
+ cc,
+ cxx,
+ cflags,
+ ccshared,
+ ldshared,
+ shlib_suffix,
+ ar,
+ ar_flags,
+ ) = get_config_vars(
+ 'CC',
+ 'CXX',
+ 'CFLAGS',
+ 'CCSHARED',
+ 'LDSHARED',
+ 'SHLIB_SUFFIX',
+ 'AR',
+ 'ARFLAGS',
+ )
if 'CC' in os.environ:
newcc = os.environ['CC']
- if('LDSHARED' not in os.environ
- and ldshared.startswith(cc)):
+ if 'LDSHARED' not in os.environ and ldshared.startswith(cc):
# If CC is overridden, use that as the default
# command for LDSHARED as well
- ldshared = newcc + ldshared[len(cc):]
+ ldshared = newcc + ldshared[len(cc) :]
cc = newcc
if 'CXX' in os.environ:
cxx = os.environ['CXX']
@@ -235,7 +317,7 @@ def customize_compiler(compiler):
if 'CPP' in os.environ:
cpp = os.environ['CPP']
else:
- cpp = cc + " -E" # not always
+ cpp = cc + " -E" # not always
if 'LDFLAGS' in os.environ:
ldshared = ldshared + ' ' + os.environ['LDFLAGS']
if 'CFLAGS' in os.environ:
@@ -260,7 +342,8 @@ def customize_compiler(compiler):
compiler_cxx=cxx,
linker_so=ldshared,
linker_exe=cc,
- archiver=archiver)
+ archiver=archiver,
+ )
if 'RANLIB' in os.environ and compiler.executables.get('ranlib', None):
compiler.set_executables(ranlib=os.environ['RANLIB'])
@@ -280,7 +363,6 @@ def get_config_h_filename():
return sysconfig.get_config_h_filename()
-
def get_makefile_filename():
"""Return full pathname of installed Makefile from the Python build."""
return sysconfig.get_makefile_filename()
@@ -302,7 +384,8 @@ _variable_rx = re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
-def parse_makefile(fn, g=None):
+
+def parse_makefile(fn, g=None): # noqa: C901
"""Parse a Makefile-style file.
A dictionary containing name/value pairs is returned. If an
@@ -310,7 +393,10 @@ def parse_makefile(fn, g=None):
used instead of a new dictionary.
"""
from distutils.text_file import TextFile
- fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape")
+
+ fp = TextFile(
+ fn, strip_comments=1, skip_blanks=1, join_lines=1, errors="surrogateescape"
+ )
if g is None:
g = {}
@@ -319,7 +405,7 @@ def parse_makefile(fn, g=None):
while True:
line = fp.readline()
- if line is None: # eof
+ if line is None: # eof
break
m = _variable_rx.match(line)
if m:
@@ -374,20 +460,20 @@ def parse_makefile(fn, g=None):
else:
done[n] = item = ""
if found:
- after = value[m.end():]
- value = value[:m.start()] + item + after
+ after = value[m.end() :]
+ value = value[: m.start()] + item + after
if "$" in after:
notdone[name] = value
else:
- try: value = int(value)
+ try:
+ value = int(value)
except ValueError:
done[name] = value.strip()
else:
done[name] = value
del notdone[name]
- if name.startswith('PY_') \
- and name[3:] in renamed_variables:
+ if name.startswith('PY_') and name[3:] in renamed_variables:
name = name[3:]
if name not in done:
@@ -436,51 +522,6 @@ def expand_makefile_vars(s, vars):
_config_vars = None
-_sysconfig_name_tmpl = '_sysconfigdata_{abi}_{platform}_{multiarch}'
-
-
-def _init_posix():
- """Initialize the module as appropriate for POSIX systems."""
- # _sysconfigdata is generated at build time, see the sysconfig module
- name = os.environ.get(
- '_PYTHON_SYSCONFIGDATA_NAME',
- _sysconfig_name_tmpl.format(
- abi=sys.abiflags,
- platform=sys.platform,
- multiarch=getattr(sys.implementation, '_multiarch', ''),
- ),
- )
- try:
- _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
- except ImportError:
- # Python 3.5 and pypy 7.3.1
- _temp = __import__(
- '_sysconfigdata', globals(), locals(), ['build_time_vars'], 0)
- build_time_vars = _temp.build_time_vars
- global _config_vars
- _config_vars = {}
- _config_vars.update(build_time_vars)
-
-
-def _init_nt():
- """Initialize the module as appropriate for NT"""
- g = {}
- # set basic install directories
- g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
- g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
-
- # XXX hmmm.. a normal install puts include files here
- g['INCLUDEPY'] = get_python_inc(plat_specific=0)
-
- g['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
- g['EXE'] = ".exe"
- g['VERSION'] = get_python_version().replace(".", "")
- g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
-
- global _config_vars
- _config_vars = g
-
-
def get_config_vars(*args):
"""With no arguments, return a dictionary of all configuration
variables relevant for the current platform. Generally this includes
@@ -493,68 +534,11 @@ def get_config_vars(*args):
"""
global _config_vars
if _config_vars is None:
- func = globals().get("_init_" + os.name)
- if func:
- func()
- else:
- _config_vars = {}
-
- # Normalized versions of prefix and exec_prefix are handy to have;
- # in fact, these are the standard versions used most places in the
- # Distutils.
- _config_vars['prefix'] = PREFIX
- _config_vars['exec_prefix'] = EXEC_PREFIX
-
- if not IS_PYPY:
- # For backward compatibility, see issue19555
- SO = _config_vars.get('EXT_SUFFIX')
- if SO is not None:
- _config_vars['SO'] = SO
-
- # Always convert srcdir to an absolute path
- srcdir = _config_vars.get('srcdir', project_base)
- if os.name == 'posix':
- if python_build:
- # If srcdir is a relative path (typically '.' or '..')
- # then it should be interpreted relative to the directory
- # containing Makefile.
- base = os.path.dirname(get_makefile_filename())
- srcdir = os.path.join(base, srcdir)
- else:
- # srcdir is not meaningful since the installation is
- # spread about the filesystem. We choose the
- # directory containing the Makefile since we know it
- # exists.
- srcdir = os.path.dirname(get_makefile_filename())
- _config_vars['srcdir'] = os.path.abspath(os.path.normpath(srcdir))
-
- # Convert srcdir into an absolute path if it appears necessary.
- # Normally it is relative to the build directory. However, during
- # testing, for example, we might be running a non-installed python
- # from a different directory.
- if python_build and os.name == "posix":
- base = project_base
- if (not os.path.isabs(_config_vars['srcdir']) and
- base != os.getcwd()):
- # srcdir is relative and we are not in the same directory
- # as the executable. Assume executable is in the build
- # directory and make srcdir absolute.
- srcdir = os.path.join(base, _config_vars['srcdir'])
- _config_vars['srcdir'] = os.path.normpath(srcdir)
-
- # OS X platforms require special customization to handle
- # multi-architecture, multi-os-version installers
- if sys.platform == 'darwin':
- import _osx_support
- _osx_support.customize_config_vars(_config_vars)
-
- if args:
- vals = []
- for name in args:
- vals.append(_config_vars.get(name))
- return vals
- else:
- return _config_vars
+ _config_vars = sysconfig.get_config_vars().copy()
+ py39compat.add_ext_suffix(_config_vars)
+
+ return [_config_vars.get(name) for name in args] if args else _config_vars
+
def get_config_var(name):
"""Return the value of a single variable using the dictionary
@@ -563,5 +547,6 @@ def get_config_var(name):
"""
if name == 'SO':
import warnings
+
warnings.warn('SO is deprecated, use EXT_SUFFIX', DeprecationWarning, 2)
return get_config_vars().get(name)
diff --git a/setuptools/_distutils/tests/__init__.py b/setuptools/_distutils/tests/__init__.py
index c7dcc7ec..27e73393 100644
--- a/setuptools/_distutils/tests/__init__.py
+++ b/setuptools/_distutils/tests/__init__.py
@@ -1,42 +1,8 @@
-"""Test suite for distutils.
-
-This test suite consists of a collection of test modules in the
-distutils.tests package. Each test module has a name starting with
-'test' and contains a function test_suite(). The function is expected
-to return an initialized unittest.TestSuite instance.
+"""
+Test suite for distutils.
Tests for the command classes in the distutils.command package are
included in distutils.tests as well, instead of using a separate
distutils.command.tests package, since command identification is done
by import rather than matching pre-defined names.
-
"""
-
-import os
-import sys
-import unittest
-from test.support import run_unittest
-
-from .py38compat import save_restore_warnings_filters
-
-
-here = os.path.dirname(__file__) or os.curdir
-
-
-def test_suite():
- suite = unittest.TestSuite()
- for fn in os.listdir(here):
- if fn.startswith("test") and fn.endswith(".py"):
- modname = "distutils.tests." + fn[:-3]
- # bpo-40055: Save/restore warnings filters to leave them unchanged.
- # Importing tests imports docutils which imports pkg_resources
- # which adds a warnings filter.
- with save_restore_warnings_filters():
- __import__(modname)
- module = sys.modules[modname]
- suite.addTest(module.test_suite())
- return suite
-
-
-if __name__ == "__main__":
- run_unittest(test_suite())
diff --git a/setuptools/_distutils/tests/py35compat.py b/setuptools/_distutils/tests/py35compat.py
deleted file mode 100644
index 0c755261..00000000
--- a/setuptools/_distutils/tests/py35compat.py
+++ /dev/null
@@ -1,77 +0,0 @@
-"""
-Backward compatibility support for Python 3.5
-"""
-
-import sys
-import test.support
-import subprocess
-
-
-# copied from Python 3.9 test.support module
-def _missing_compiler_executable(cmd_names=[]):
- """Check if the compiler components used to build the interpreter exist.
-
- Check for the existence of the compiler executables whose names are listed
- in 'cmd_names' or all the compiler executables when 'cmd_names' is empty
- and return the first missing executable or None when none is found
- missing.
-
- """
- from distutils import ccompiler, sysconfig, spawn
- compiler = ccompiler.new_compiler()
- sysconfig.customize_compiler(compiler)
- for name in compiler.executables:
- if cmd_names and name not in cmd_names:
- continue
- cmd = getattr(compiler, name)
- if cmd_names:
- assert cmd is not None, \
- "the '%s' executable is not configured" % name
- elif not cmd:
- continue
- if spawn.find_executable(cmd[0]) is None:
- return cmd[0]
-
-
-missing_compiler_executable = vars(test.support).setdefault(
- 'missing_compiler_executable',
- _missing_compiler_executable,
-)
-
-
-try:
- from test.support import unix_shell
-except ImportError:
- # Adapted from Python 3.9 test.support module
- is_android = hasattr(sys, 'getandroidapilevel')
- unix_shell = (
- None if sys.platform == 'win32' else
- '/system/bin/sh' if is_android else
- '/bin/sh'
- )
-
-
-# copied from Python 3.9 subprocess module
-def _optim_args_from_interpreter_flags():
- """Return a list of command-line arguments reproducing the current
- optimization settings in sys.flags."""
- args = []
- value = sys.flags.optimize
- if value > 0:
- args.append('-' + 'O' * value)
- return args
-
-
-vars(subprocess).setdefault(
- '_optim_args_from_interpreter_flags',
- _optim_args_from_interpreter_flags,
-)
-
-
-def adapt_glob(regex):
- """
- Supply legacy expectation on Python 3.5
- """
- if sys.version_info > (3, 6):
- return regex
- return regex.replace('(?s:', '').replace(r')\Z', r'\Z(?ms)')
diff --git a/setuptools/_distutils/tests/py37compat.py b/setuptools/_distutils/tests/py37compat.py
new file mode 100644
index 00000000..e5d406a3
--- /dev/null
+++ b/setuptools/_distutils/tests/py37compat.py
@@ -0,0 +1,18 @@
+import os
+import sys
+import platform
+
+
+def subprocess_args_compat(*args):
+ return list(map(os.fspath, args))
+
+
+def subprocess_args_passthrough(*args):
+ return list(args)
+
+
+subprocess_args = (
+ subprocess_args_compat
+ if platform.system() == "Windows" and sys.version_info < (3, 8)
+ else subprocess_args_passthrough
+)
diff --git a/setuptools/_distutils/tests/py38compat.py b/setuptools/_distutils/tests/py38compat.py
index c949f58e..211d3a6c 100644
--- a/setuptools/_distutils/tests/py38compat.py
+++ b/setuptools/_distutils/tests/py38compat.py
@@ -18,44 +18,32 @@ except (ModuleNotFoundError, ImportError):
try:
from test.support.os_helper import (
- change_cwd,
rmtree,
EnvironmentVarGuard,
- TESTFN,
unlink,
skip_unless_symlink,
temp_dir,
- create_empty_file,
- temp_cwd,
)
except (ModuleNotFoundError, ImportError):
from test.support import (
- change_cwd,
rmtree,
EnvironmentVarGuard,
- TESTFN,
unlink,
skip_unless_symlink,
temp_dir,
- create_empty_file,
- temp_cwd,
)
-# From Python 3.9
-@contextlib.contextmanager
-def _save_restore_warnings_filters():
- old_filters = warnings.filters[:]
- try:
- yield
- finally:
- warnings.filters[:] = old_filters
-
-
try:
- from test.support.warnings_helper import save_restore_warnings_filters
+ from test.support.import_helper import (
+ DirsOnSysPath,
+ CleanImport,
+ )
except (ModuleNotFoundError, ImportError):
- save_restore_warnings_filters = _save_restore_warnings_filters
+ from test.support import (
+ DirsOnSysPath,
+ CleanImport,
+ )
if sys.version_info < (3, 9):
diff --git a/setuptools/_distutils/tests/support.py b/setuptools/_distutils/tests/support.py
index b4410fc9..fd4b11bf 100644
--- a/setuptools/_distutils/tests/support.py
+++ b/setuptools/_distutils/tests/support.py
@@ -3,69 +3,21 @@ import os
import sys
import shutil
import tempfile
-import unittest
import sysconfig
-from copy import deepcopy
+import itertools
+import pathlib
-from . import py38compat as os_helper
+import pytest
+from more_itertools import always_iterable
-from distutils import log
-from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL
from distutils.core import Distribution
-class LoggingSilencer(object):
-
- def setUp(self):
- super().setUp()
- self.threshold = log.set_threshold(log.FATAL)
- # catching warnings
- # when log will be replaced by logging
- # we won't need such monkey-patch anymore
- self._old_log = log.Log._log
- log.Log._log = self._log
- self.logs = []
-
- def tearDown(self):
- log.set_threshold(self.threshold)
- log.Log._log = self._old_log
- super().tearDown()
-
- def _log(self, level, msg, args):
- if level not in (DEBUG, INFO, WARN, ERROR, FATAL):
- raise ValueError('%s wrong log level' % str(level))
- if not isinstance(msg, str):
- raise TypeError("msg should be str, not '%.200s'"
- % (type(msg).__name__))
- self.logs.append((level, msg, args))
-
- def get_logs(self, *levels):
- return [msg % args for level, msg, args
- in self.logs if level in levels]
-
- def clear_logs(self):
- self.logs = []
-
-
-class TempdirManager(object):
- """Mix-in class that handles temporary directories for test cases.
-
- This is intended to be used with unittest.TestCase.
+@pytest.mark.usefixtures('distutils_managed_tempdir')
+class TempdirManager:
+ """
+ Mix-in class that handles temporary directories for test cases.
"""
-
- def setUp(self):
- super().setUp()
- self.old_cwd = os.getcwd()
- self.tempdirs = []
-
- def tearDown(self):
- # Restore working dir, for Solaris and derivatives, where rmdir()
- # on the current directory fails.
- os.chdir(self.old_cwd)
- super().tearDown()
- while self.tempdirs:
- tmpdir = self.tempdirs.pop()
- os_helper.rmtree(tmpdir)
def mkdtemp(self):
"""Create a temporary directory that will be cleaned up.
@@ -79,16 +31,9 @@ class TempdirManager(object):
def write_file(self, path, content='xxx'):
"""Writes a file in the given path.
-
path can be a string or a sequence.
"""
- if isinstance(path, (list, tuple)):
- path = os.path.join(*path)
- f = open(path, 'w')
- try:
- f.write(content)
- finally:
- f.close()
+ pathlib.Path(*always_iterable(path)).write_text(content)
def create_dist(self, pkg_name='foo', **kw):
"""Will generate a test environment.
@@ -112,31 +57,12 @@ class DummyCommand:
"""Class to store options for retrieval via set_undefined_options()."""
def __init__(self, **kwargs):
- for kw, val in kwargs.items():
- setattr(self, kw, val)
+ vars(self).update(kwargs)
def ensure_finalized(self):
pass
-class EnvironGuard(object):
-
- def setUp(self):
- super(EnvironGuard, self).setUp()
- self.old_environ = deepcopy(os.environ)
-
- def tearDown(self):
- for key, value in self.old_environ.items():
- if os.environ.get(key) != value:
- os.environ[key] = value
-
- for key in tuple(os.environ.keys()):
- if key not in self.old_environ:
- del os.environ[key]
-
- super(EnvironGuard, self).tearDown()
-
-
def copy_xxmodule_c(directory):
"""Helper for tests that need the xxmodule.c source file.
@@ -149,28 +75,12 @@ def copy_xxmodule_c(directory):
If the source file can be found, it will be copied to *directory*. If not,
the test will be skipped. Errors during copy are not caught.
"""
- filename = _get_xxmodule_path()
- if filename is None:
- raise unittest.SkipTest('cannot find xxmodule.c (test must run in '
- 'the python build dir)')
- shutil.copy(filename, directory)
+ shutil.copy(_get_xxmodule_path(), os.path.join(directory, 'xxmodule.c'))
def _get_xxmodule_path():
- srcdir = sysconfig.get_config_var('srcdir')
- candidates = [
- # use installed copy if available
- os.path.join(os.path.dirname(__file__), 'xxmodule.c'),
- # otherwise try using copy from build directory
- os.path.join(srcdir, 'Modules', 'xxmodule.c'),
- # srcdir mysteriously can be $srcdir/Lib/distutils/tests when
- # this file is run from its parent directory, so walk up the
- # tree to find the real srcdir
- os.path.join(srcdir, '..', '..', '..', 'Modules', 'xxmodule.c'),
- ]
- for path in candidates:
- if os.path.exists(path):
- return path
+ source_name = 'xxmodule.c' if sys.version_info > (3, 9) else 'xxmodule-3.8.c'
+ return os.path.join(os.path.dirname(__file__), source_name)
def fixup_build_ext(cmd):
@@ -208,3 +118,17 @@ def fixup_build_ext(cmd):
else:
name, equals, value = runshared.partition('=')
cmd.library_dirs = [d for d in value.split(os.pathsep) if d]
+
+
+def combine_markers(cls):
+ """
+ pytest will honor markers as found on the class, but when
+ markers are on multiple subclasses, only one appears. Use
+ this decorator to combine those markers.
+ """
+ cls.pytestmark = [
+ mark
+ for base in itertools.chain([cls], cls.__bases__)
+ for mark in getattr(base, 'pytestmark', [])
+ ]
+ return cls
diff --git a/setuptools/_distutils/tests/test_archive_util.py b/setuptools/_distutils/tests/test_archive_util.py
index 800b9018..7778c3ad 100644
--- a/setuptools/_distutils/tests/test_archive_util.py
+++ b/setuptools/_distutils/tests/test_archive_util.py
@@ -1,47 +1,32 @@
-# -*- coding: utf-8 -*-
"""Tests for distutils.archive_util."""
-import unittest
import os
import sys
import tarfile
from os.path import splitdrive
import warnings
+import functools
+import operator
+import pathlib
+
+import pytest
+import path
from distutils import archive_util
-from distutils.archive_util import (check_archive_formats, make_tarball,
- make_zipfile, make_archive,
- ARCHIVE_FORMATS)
-from distutils.spawn import find_executable, spawn
+from distutils.archive_util import (
+ check_archive_formats,
+ make_tarball,
+ make_zipfile,
+ make_archive,
+ ARCHIVE_FORMATS,
+)
+from distutils.spawn import spawn
from distutils.tests import support
-from test.support import run_unittest, patch
+from test.support import patch
from .unix_compat import require_unix_id, require_uid_0, grp, pwd, UID_0_SUPPORT
-from .py38compat import change_cwd
from .py38compat import check_warnings
-try:
- import zipfile
- ZIP_SUPPORT = True
-except ImportError:
- ZIP_SUPPORT = find_executable('zip')
-
-try:
- import zlib
- ZLIB_SUPPORT = True
-except ImportError:
- ZLIB_SUPPORT = False
-
-try:
- import bz2
-except ImportError:
- bz2 = None
-
-try:
- import lzma
-except ImportError:
- lzma = None
-
def can_fs_encode(filename):
"""
Return True if the filename can be saved in the file system.
@@ -55,11 +40,16 @@ def can_fs_encode(filename):
return True
-class ArchiveUtilTestCase(support.TempdirManager,
- support.LoggingSilencer,
- unittest.TestCase):
+def all_equal(values):
+ return functools.reduce(operator.eq, values)
+
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
+def same_drive(*paths):
+ return all_equal(pathlib.Path(path).drive for path in paths)
+
+
+class ArchiveUtilTestCase(support.TempdirManager):
+ @pytest.mark.usefixtures('needs_zlib')
def test_make_tarball(self, name='archive'):
# creating something to tar
tmpdir = self._create_files()
@@ -67,53 +57,51 @@ class ArchiveUtilTestCase(support.TempdirManager,
# trying an uncompressed one
self._make_tarball(tmpdir, name, '.tar', compress=None)
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
+ @pytest.mark.usefixtures('needs_zlib')
def test_make_tarball_gzip(self):
tmpdir = self._create_files()
self._make_tarball(tmpdir, 'archive', '.tar.gz', compress='gzip')
- @unittest.skipUnless(bz2, 'Need bz2 support to run')
def test_make_tarball_bzip2(self):
+ pytest.importorskip('bz2')
tmpdir = self._create_files()
self._make_tarball(tmpdir, 'archive', '.tar.bz2', compress='bzip2')
- @unittest.skipUnless(lzma, 'Need lzma support to run')
def test_make_tarball_xz(self):
+ pytest.importorskip('lzma')
tmpdir = self._create_files()
self._make_tarball(tmpdir, 'archive', '.tar.xz', compress='xz')
- @unittest.skipUnless(can_fs_encode('årchiv'),
- 'File system cannot handle this filename')
+ @pytest.mark.skipif("not can_fs_encode('årchiv')")
def test_make_tarball_latin1(self):
"""
Mirror test_make_tarball, except filename contains latin characters.
"""
- self.test_make_tarball('årchiv') # note this isn't a real word
+ self.test_make_tarball('årchiv') # note this isn't a real word
- @unittest.skipUnless(can_fs_encode('のアーカイブ'),
- 'File system cannot handle this filename')
+ @pytest.mark.skipif("not can_fs_encode('のアーカイブ')")
def test_make_tarball_extended(self):
"""
Mirror test_make_tarball, except filename contains extended
characters outside the latin charset.
"""
- self.test_make_tarball('のアーカイブ') # japanese for archive
+ self.test_make_tarball('のアーカイブ') # japanese for archive
def _make_tarball(self, tmpdir, target_name, suffix, **kwargs):
tmpdir2 = self.mkdtemp()
- unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0],
- "source and target should be on same drive")
+ if same_drive(tmpdir, tmpdir2):
+ pytest.skip("source and target should be on same drive")
base_name = os.path.join(tmpdir2, target_name)
# working with relative paths to avoid tar warnings
- with change_cwd(tmpdir):
+ with path.Path(tmpdir):
make_tarball(splitdrive(base_name)[1], 'dist', **kwargs)
# check if the compressed tarball was created
tarball = base_name + suffix
- self.assertTrue(os.path.exists(tarball))
- self.assertEqual(self._tarinfo(tarball), self._created_files)
+ assert os.path.exists(tarball)
+ assert self._tarinfo(tarball) == self._created_files
def _tarinfo(self, path):
tar = tarfile.open(path)
@@ -124,8 +112,14 @@ class ArchiveUtilTestCase(support.TempdirManager,
finally:
tar.close()
- _zip_created_files = ['dist/', 'dist/file1', 'dist/file2',
- 'dist/sub/', 'dist/sub/file3', 'dist/sub2/']
+ _zip_created_files = [
+ 'dist/',
+ 'dist/file1',
+ 'dist/file2',
+ 'dist/sub/',
+ 'dist/sub/file3',
+ 'dist/sub2/',
+ ]
_created_files = [p.rstrip('/') for p in _zip_created_files]
def _create_files(self):
@@ -140,11 +134,10 @@ class ArchiveUtilTestCase(support.TempdirManager,
os.mkdir(os.path.join(dist, 'sub2'))
return tmpdir
- @unittest.skipUnless(find_executable('tar') and find_executable('gzip')
- and ZLIB_SUPPORT,
- 'Need the tar, gzip and zlib command to run')
+ @pytest.mark.usefixtures('needs_zlib')
+ @pytest.mark.skipif("not (find_executable('tar') and find_executable('gzip'))")
def test_tarfile_vs_tar(self):
- tmpdir = self._create_files()
+ tmpdir = self._create_files()
tmpdir2 = self.mkdtemp()
base_name = os.path.join(tmpdir2, 'archive')
old_dir = os.getcwd()
@@ -156,7 +149,7 @@ class ArchiveUtilTestCase(support.TempdirManager,
# check if the compressed tarball was created
tarball = base_name + '.tar.gz'
- self.assertTrue(os.path.exists(tarball))
+ assert os.path.exists(tarball)
# now create another tarball using `tar`
tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
@@ -170,10 +163,10 @@ class ArchiveUtilTestCase(support.TempdirManager,
finally:
os.chdir(old_dir)
- self.assertTrue(os.path.exists(tarball2))
+ assert os.path.exists(tarball2)
# let's compare both tarballs
- self.assertEqual(self._tarinfo(tarball), self._created_files)
- self.assertEqual(self._tarinfo(tarball2), self._created_files)
+ assert self._tarinfo(tarball) == self._created_files
+ assert self._tarinfo(tarball2) == self._created_files
# trying an uncompressed one
base_name = os.path.join(tmpdir2, 'archive')
@@ -184,7 +177,7 @@ class ArchiveUtilTestCase(support.TempdirManager,
finally:
os.chdir(old_dir)
tarball = base_name + '.tar'
- self.assertTrue(os.path.exists(tarball))
+ assert os.path.exists(tarball)
# now for a dry_run
base_name = os.path.join(tmpdir2, 'archive')
@@ -195,15 +188,14 @@ class ArchiveUtilTestCase(support.TempdirManager,
finally:
os.chdir(old_dir)
tarball = base_name + '.tar'
- self.assertTrue(os.path.exists(tarball))
+ assert os.path.exists(tarball)
- @unittest.skipUnless(find_executable('compress'),
- 'The compress program is required')
+ @pytest.mark.skipif("not find_executable('compress')")
def test_compress_deprecated(self):
- tmpdir = self._create_files()
+ tmpdir = self._create_files()
base_name = os.path.join(self.mkdtemp(), 'archive')
- # using compress and testing the PendingDeprecationWarning
+ # using compress and testing the DeprecationWarning
old_dir = os.getcwd()
os.chdir(tmpdir)
try:
@@ -213,8 +205,8 @@ class ArchiveUtilTestCase(support.TempdirManager,
finally:
os.chdir(old_dir)
tarball = base_name + '.tar.Z'
- self.assertTrue(os.path.exists(tarball))
- self.assertEqual(len(w.warnings), 1)
+ assert os.path.exists(tarball)
+ assert len(w.warnings) == 1
# same test with dry_run
os.remove(tarball)
@@ -223,34 +215,34 @@ class ArchiveUtilTestCase(support.TempdirManager,
try:
with check_warnings() as w:
warnings.simplefilter("always")
- make_tarball(base_name, 'dist', compress='compress',
- dry_run=True)
+ make_tarball(base_name, 'dist', compress='compress', dry_run=True)
finally:
os.chdir(old_dir)
- self.assertFalse(os.path.exists(tarball))
- self.assertEqual(len(w.warnings), 1)
+ assert not os.path.exists(tarball)
+ assert len(w.warnings) == 1
- @unittest.skipUnless(ZIP_SUPPORT and ZLIB_SUPPORT,
- 'Need zip and zlib support to run')
+ @pytest.mark.usefixtures('needs_zlib')
def test_make_zipfile(self):
+ zipfile = pytest.importorskip('zipfile')
# creating something to tar
tmpdir = self._create_files()
base_name = os.path.join(self.mkdtemp(), 'archive')
- with change_cwd(tmpdir):
+ with path.Path(tmpdir):
make_zipfile(base_name, 'dist')
# check if the compressed tarball was created
tarball = base_name + '.zip'
- self.assertTrue(os.path.exists(tarball))
+ assert os.path.exists(tarball)
with zipfile.ZipFile(tarball) as zf:
- self.assertEqual(sorted(zf.namelist()), self._zip_created_files)
+ assert sorted(zf.namelist()) == self._zip_created_files
- @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
def test_make_zipfile_no_zlib(self):
+ zipfile = pytest.importorskip('zipfile')
patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError
called = []
zipfile_class = zipfile.ZipFile
+
def fake_zipfile(*a, **kw):
if kw.get('compression', None) == zipfile.ZIP_STORED:
called.append((a, kw))
@@ -261,75 +253,78 @@ class ArchiveUtilTestCase(support.TempdirManager,
# create something to tar and compress
tmpdir = self._create_files()
base_name = os.path.join(self.mkdtemp(), 'archive')
- with change_cwd(tmpdir):
+ with path.Path(tmpdir):
make_zipfile(base_name, 'dist')
tarball = base_name + '.zip'
- self.assertEqual(called,
- [((tarball, "w"), {'compression': zipfile.ZIP_STORED})])
- self.assertTrue(os.path.exists(tarball))
+ assert called == [((tarball, "w"), {'compression': zipfile.ZIP_STORED})]
+ assert os.path.exists(tarball)
with zipfile.ZipFile(tarball) as zf:
- self.assertEqual(sorted(zf.namelist()), self._zip_created_files)
+ assert sorted(zf.namelist()) == self._zip_created_files
def test_check_archive_formats(self):
- self.assertEqual(check_archive_formats(['gztar', 'xxx', 'zip']),
- 'xxx')
- self.assertIsNone(check_archive_formats(['gztar', 'bztar', 'xztar',
- 'ztar', 'tar', 'zip']))
+ assert check_archive_formats(['gztar', 'xxx', 'zip']) == 'xxx'
+ assert (
+ check_archive_formats(['gztar', 'bztar', 'xztar', 'ztar', 'tar', 'zip'])
+ is None
+ )
def test_make_archive(self):
tmpdir = self.mkdtemp()
base_name = os.path.join(tmpdir, 'archive')
- self.assertRaises(ValueError, make_archive, base_name, 'xxx')
+ with pytest.raises(ValueError):
+ make_archive(base_name, 'xxx')
def test_make_archive_cwd(self):
current_dir = os.getcwd()
+
def _breaks(*args, **kw):
raise RuntimeError()
+
ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file')
try:
try:
make_archive('xxx', 'xxx', root_dir=self.mkdtemp())
- except:
+ except Exception:
pass
- self.assertEqual(os.getcwd(), current_dir)
+ assert os.getcwd() == current_dir
finally:
del ARCHIVE_FORMATS['xxx']
def test_make_archive_tar(self):
- base_dir = self._create_files()
- base_name = os.path.join(self.mkdtemp() , 'archive')
+ base_dir = self._create_files()
+ base_name = os.path.join(self.mkdtemp(), 'archive')
res = make_archive(base_name, 'tar', base_dir, 'dist')
- self.assertTrue(os.path.exists(res))
- self.assertEqual(os.path.basename(res), 'archive.tar')
- self.assertEqual(self._tarinfo(res), self._created_files)
+ assert os.path.exists(res)
+ assert os.path.basename(res) == 'archive.tar'
+ assert self._tarinfo(res) == self._created_files
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
+ @pytest.mark.usefixtures('needs_zlib')
def test_make_archive_gztar(self):
- base_dir = self._create_files()
- base_name = os.path.join(self.mkdtemp() , 'archive')
+ base_dir = self._create_files()
+ base_name = os.path.join(self.mkdtemp(), 'archive')
res = make_archive(base_name, 'gztar', base_dir, 'dist')
- self.assertTrue(os.path.exists(res))
- self.assertEqual(os.path.basename(res), 'archive.tar.gz')
- self.assertEqual(self._tarinfo(res), self._created_files)
+ assert os.path.exists(res)
+ assert os.path.basename(res) == 'archive.tar.gz'
+ assert self._tarinfo(res) == self._created_files
- @unittest.skipUnless(bz2, 'Need bz2 support to run')
def test_make_archive_bztar(self):
- base_dir = self._create_files()
- base_name = os.path.join(self.mkdtemp() , 'archive')
+ pytest.importorskip('bz2')
+ base_dir = self._create_files()
+ base_name = os.path.join(self.mkdtemp(), 'archive')
res = make_archive(base_name, 'bztar', base_dir, 'dist')
- self.assertTrue(os.path.exists(res))
- self.assertEqual(os.path.basename(res), 'archive.tar.bz2')
- self.assertEqual(self._tarinfo(res), self._created_files)
+ assert os.path.exists(res)
+ assert os.path.basename(res) == 'archive.tar.bz2'
+ assert self._tarinfo(res) == self._created_files
- @unittest.skipUnless(lzma, 'Need xz support to run')
def test_make_archive_xztar(self):
- base_dir = self._create_files()
- base_name = os.path.join(self.mkdtemp() , 'archive')
+ pytest.importorskip('lzma')
+ base_dir = self._create_files()
+ base_name = os.path.join(self.mkdtemp(), 'archive')
res = make_archive(base_name, 'xztar', base_dir, 'dist')
- self.assertTrue(os.path.exists(res))
- self.assertEqual(os.path.basename(res), 'archive.tar.xz')
- self.assertEqual(self._tarinfo(res), self._created_files)
+ assert os.path.exists(res)
+ assert os.path.basename(res) == 'archive.tar.xz'
+ assert self._tarinfo(res) == self._created_files
def test_make_archive_owner_group(self):
# testing make_archive with owner and group, with various combinations
@@ -340,54 +335,52 @@ class ArchiveUtilTestCase(support.TempdirManager,
else:
group = owner = 'root'
- base_dir = self._create_files()
+ base_dir = self._create_files()
root_dir = self.mkdtemp()
- base_name = os.path.join(self.mkdtemp() , 'archive')
- res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner,
- group=group)
- self.assertTrue(os.path.exists(res))
+ base_name = os.path.join(self.mkdtemp(), 'archive')
+ res = make_archive(
+ base_name, 'zip', root_dir, base_dir, owner=owner, group=group
+ )
+ assert os.path.exists(res)
res = make_archive(base_name, 'zip', root_dir, base_dir)
- self.assertTrue(os.path.exists(res))
+ assert os.path.exists(res)
- res = make_archive(base_name, 'tar', root_dir, base_dir,
- owner=owner, group=group)
- self.assertTrue(os.path.exists(res))
+ res = make_archive(
+ base_name, 'tar', root_dir, base_dir, owner=owner, group=group
+ )
+ assert os.path.exists(res)
- res = make_archive(base_name, 'tar', root_dir, base_dir,
- owner='kjhkjhkjg', group='oihohoh')
- self.assertTrue(os.path.exists(res))
+ res = make_archive(
+ base_name, 'tar', root_dir, base_dir, owner='kjhkjhkjg', group='oihohoh'
+ )
+ assert os.path.exists(res)
- @unittest.skipUnless(ZLIB_SUPPORT, "Requires zlib")
+ @pytest.mark.usefixtures('needs_zlib')
@require_unix_id
@require_uid_0
def test_tarfile_root_owner(self):
- tmpdir = self._create_files()
+ tmpdir = self._create_files()
base_name = os.path.join(self.mkdtemp(), 'archive')
old_dir = os.getcwd()
os.chdir(tmpdir)
group = grp.getgrgid(0)[0]
owner = pwd.getpwuid(0)[0]
try:
- archive_name = make_tarball(base_name, 'dist', compress=None,
- owner=owner, group=group)
+ archive_name = make_tarball(
+ base_name, 'dist', compress=None, owner=owner, group=group
+ )
finally:
os.chdir(old_dir)
# check if the compressed tarball was created
- self.assertTrue(os.path.exists(archive_name))
+ assert os.path.exists(archive_name)
# now checks the rights
archive = tarfile.open(archive_name)
try:
for member in archive.getmembers():
- self.assertEqual(member.uid, 0)
- self.assertEqual(member.gid, 0)
+ assert member.uid == 0
+ assert member.gid == 0
finally:
archive.close()
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(ArchiveUtilTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
diff --git a/setuptools/_distutils/tests/test_bdist.py b/setuptools/_distutils/tests/test_bdist.py
index 8b7498e3..af330a06 100644
--- a/setuptools/_distutils/tests/test_bdist.py
+++ b/setuptools/_distutils/tests/test_bdist.py
@@ -1,30 +1,30 @@
"""Tests for distutils.command.bdist."""
-import os
-import unittest
-from test.support import run_unittest
-import warnings
-
from distutils.command.bdist import bdist
from distutils.tests import support
-class BuildTestCase(support.TempdirManager,
- unittest.TestCase):
-
+class TestBuild(support.TempdirManager):
def test_formats(self):
# let's create a command and make sure
# we can set the format
dist = self.create_dist()[1]
cmd = bdist(dist)
- cmd.formats = ['msi']
+ cmd.formats = ['gztar']
cmd.ensure_finalized()
- self.assertEqual(cmd.formats, ['msi'])
+ assert cmd.formats == ['gztar']
# what formats does bdist offer?
- formats = ['bztar', 'gztar', 'msi', 'rpm', 'tar',
- 'wininst', 'xztar', 'zip', 'ztar']
- found = sorted(cmd.format_command)
- self.assertEqual(found, formats)
+ formats = [
+ 'bztar',
+ 'gztar',
+ 'rpm',
+ 'tar',
+ 'xztar',
+ 'zip',
+ 'ztar',
+ ]
+ found = sorted(cmd.format_commands)
+ assert found == formats
def test_skip_build(self):
# bug #10946: bdist --skip-build should trickle down to subcommands
@@ -34,24 +34,13 @@ class BuildTestCase(support.TempdirManager,
cmd.ensure_finalized()
dist.command_obj['bdist'] = cmd
- names = ['bdist_dumb', 'bdist_wininst'] # bdist_rpm does not support --skip-build
- if os.name == 'nt':
- names.append('bdist_msi')
+ names = [
+ 'bdist_dumb',
+ ] # bdist_rpm does not support --skip-build
for name in names:
- with warnings.catch_warnings():
- warnings.filterwarnings('ignore', 'bdist_wininst command is deprecated',
- DeprecationWarning)
- subcmd = cmd.get_finalized_command(name)
+ subcmd = cmd.get_finalized_command(name)
if getattr(subcmd, '_unsupported', False):
# command is not supported on this build
continue
- self.assertTrue(subcmd.skip_build,
- '%s should take --skip-build from bdist' % name)
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(BuildTestCase)
-
-if __name__ == '__main__':
- run_unittest(test_suite())
+ assert subcmd.skip_build, '%s should take --skip-build from bdist' % name
diff --git a/setuptools/_distutils/tests/test_bdist_dumb.py b/setuptools/_distutils/tests/test_bdist_dumb.py
index bb860c8a..b9bec051 100644
--- a/setuptools/_distutils/tests/test_bdist_dumb.py
+++ b/setuptools/_distutils/tests/test_bdist_dumb.py
@@ -3,8 +3,8 @@
import os
import sys
import zipfile
-import unittest
-from test.support import run_unittest
+
+import pytest
from distutils.core import Distribution
from distutils.command.bdist_dumb import bdist_dumb
@@ -19,30 +19,15 @@ setup(name='foo', version='0.1', py_modules=['foo'],
"""
-try:
- import zlib
- ZLIB_SUPPORT = True
-except ImportError:
- ZLIB_SUPPORT = False
-
-
-class BuildDumbTestCase(support.TempdirManager,
- support.LoggingSilencer,
- support.EnvironGuard,
- unittest.TestCase):
-
- def setUp(self):
- super(BuildDumbTestCase, self).setUp()
- self.old_location = os.getcwd()
- self.old_sys_argv = sys.argv, sys.argv[:]
- def tearDown(self):
- os.chdir(self.old_location)
- sys.argv = self.old_sys_argv[0]
- sys.argv[:] = self.old_sys_argv[1]
- super(BuildDumbTestCase, self).tearDown()
-
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
+@support.combine_markers
+@pytest.mark.usefixtures('save_env')
+@pytest.mark.usefixtures('save_argv')
+@pytest.mark.usefixtures('save_cwd')
+class TestBuildDumb(
+ support.TempdirManager,
+):
+ @pytest.mark.usefixtures('needs_zlib')
def test_simple_built(self):
# let's create a simple package
@@ -54,10 +39,16 @@ class BuildDumbTestCase(support.TempdirManager,
self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
self.write_file((pkg_dir, 'README'), '')
- dist = Distribution({'name': 'foo', 'version': '0.1',
- 'py_modules': ['foo'],
- 'url': 'xxx', 'author': 'xxx',
- 'author_email': 'xxx'})
+ dist = Distribution(
+ {
+ 'name': 'foo',
+ 'version': '0.1',
+ 'py_modules': ['foo'],
+ 'url': 'xxx',
+ 'author': 'xxx',
+ 'author_email': 'xxx',
+ }
+ )
dist.script_name = 'setup.py'
os.chdir(pkg_dir)
@@ -73,9 +64,9 @@ class BuildDumbTestCase(support.TempdirManager,
# see what we have
dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
- base = "%s.%s.zip" % (dist.get_fullname(), cmd.plat_name)
+ base = "{}.{}.zip".format(dist.get_fullname(), cmd.plat_name)
- self.assertEqual(dist_created, [base])
+ assert dist_created == [base]
# now let's check what we have in the zip file
fp = zipfile.ZipFile(os.path.join('dist', base))
@@ -88,10 +79,4 @@ class BuildDumbTestCase(support.TempdirManager,
wanted = ['foo-0.1-py%s.%s.egg-info' % sys.version_info[:2], 'foo.py']
if not sys.dont_write_bytecode:
wanted.append('foo.%s.pyc' % sys.implementation.cache_tag)
- self.assertEqual(contents, sorted(wanted))
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(BuildDumbTestCase)
-
-if __name__ == '__main__':
- run_unittest(test_suite())
+ assert contents == sorted(wanted)
diff --git a/setuptools/_distutils/tests/test_bdist_msi.py b/setuptools/_distutils/tests/test_bdist_msi.py
deleted file mode 100644
index b1831ef2..00000000
--- a/setuptools/_distutils/tests/test_bdist_msi.py
+++ /dev/null
@@ -1,28 +0,0 @@
-"""Tests for distutils.command.bdist_msi."""
-import sys
-import unittest
-from test.support import run_unittest
-from distutils.tests import support
-
-from .py38compat import check_warnings
-
-
-@unittest.skipUnless(sys.platform == 'win32', 'these tests require Windows')
-class BDistMSITestCase(support.TempdirManager,
- support.LoggingSilencer,
- unittest.TestCase):
-
- def test_minimal(self):
- # minimal test XXX need more tests
- from distutils.command.bdist_msi import bdist_msi
- project_dir, dist = self.create_dist()
- with check_warnings(("", DeprecationWarning)):
- cmd = bdist_msi(dist)
- cmd.ensure_finalized()
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(BDistMSITestCase)
-
-if __name__ == '__main__':
- run_unittest(test_suite())
diff --git a/setuptools/_distutils/tests/test_bdist_rpm.py b/setuptools/_distutils/tests/test_bdist_rpm.py
index 08a7cb46..4a702fb9 100644
--- a/setuptools/_distutils/tests/test_bdist_rpm.py
+++ b/setuptools/_distutils/tests/test_bdist_rpm.py
@@ -1,14 +1,14 @@
"""Tests for distutils.command.bdist_rpm."""
-import unittest
import sys
import os
-from test.support import run_unittest
+
+import pytest
from distutils.core import Distribution
from distutils.command.bdist_rpm import bdist_rpm
from distutils.tests import support
-from distutils.spawn import find_executable
+from distutils.spawn import find_executable # noqa: F401
from .py38compat import requires_zlib
@@ -22,40 +22,35 @@ setup(name='foo', version='0.1', py_modules=['foo'],
"""
-class BuildRpmTestCase(support.TempdirManager,
- support.EnvironGuard,
- support.LoggingSilencer,
- unittest.TestCase):
-
- def setUp(self):
- try:
- sys.executable.encode("UTF-8")
- except UnicodeEncodeError:
- raise unittest.SkipTest("sys.executable is not encodable to UTF-8")
-
- super(BuildRpmTestCase, self).setUp()
- self.old_location = os.getcwd()
- self.old_sys_argv = sys.argv, sys.argv[:]
-
- def tearDown(self):
- os.chdir(self.old_location)
- sys.argv = self.old_sys_argv[0]
- sys.argv[:] = self.old_sys_argv[1]
- super(BuildRpmTestCase, self).tearDown()
-
- # XXX I am unable yet to make this test work without
- # spurious sdtout/stderr output under Mac OS X
- @unittest.skipUnless(sys.platform.startswith('linux'),
- 'spurious sdtout/stderr output under Mac OS X')
+
+@pytest.fixture(autouse=True)
+def sys_executable_encodable():
+ try:
+ sys.executable.encode('UTF-8')
+ except UnicodeEncodeError:
+ pytest.skip("sys.executable is not encodable to UTF-8")
+
+
+mac_woes = pytest.mark.skipif(
+ "not sys.platform.startswith('linux')",
+ reason='spurious sdtout/stderr output under macOS',
+)
+
+
+@pytest.mark.usefixtures('save_env')
+@pytest.mark.usefixtures('save_argv')
+@pytest.mark.usefixtures('save_cwd')
+class TestBuildRpm(
+ support.TempdirManager,
+):
+ @mac_woes
@requires_zlib()
- @unittest.skipIf(find_executable('rpm') is None,
- 'the rpm command is not found')
- @unittest.skipIf(find_executable('rpmbuild') is None,
- 'the rpmbuild command is not found')
+ @pytest.mark.skipif("not find_executable('rpm')")
+ @pytest.mark.skipif("not find_executable('rpmbuild')")
def test_quiet(self):
# let's create a package
tmp_dir = self.mkdtemp()
- os.environ['HOME'] = tmp_dir # to confine dir '.rpmdb' creation
+ os.environ['HOME'] = tmp_dir # to confine dir '.rpmdb' creation
pkg_dir = os.path.join(tmp_dir, 'foo')
os.mkdir(pkg_dir)
self.write_file((pkg_dir, 'setup.py'), SETUP_PY)
@@ -63,10 +58,16 @@ class BuildRpmTestCase(support.TempdirManager,
self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
self.write_file((pkg_dir, 'README'), '')
- dist = Distribution({'name': 'foo', 'version': '0.1',
- 'py_modules': ['foo'],
- 'url': 'xxx', 'author': 'xxx',
- 'author_email': 'xxx'})
+ dist = Distribution(
+ {
+ 'name': 'foo',
+ 'version': '0.1',
+ 'py_modules': ['foo'],
+ 'url': 'xxx',
+ 'author': 'xxx',
+ 'author_email': 'xxx',
+ }
+ )
dist.script_name = 'setup.py'
os.chdir(pkg_dir)
@@ -80,26 +81,21 @@ class BuildRpmTestCase(support.TempdirManager,
cmd.run()
dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
- self.assertIn('foo-0.1-1.noarch.rpm', dist_created)
+ assert 'foo-0.1-1.noarch.rpm' in dist_created
# bug #2945: upload ignores bdist_rpm files
- self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.src.rpm'), dist.dist_files)
- self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.noarch.rpm'), dist.dist_files)
+ assert ('bdist_rpm', 'any', 'dist/foo-0.1-1.src.rpm') in dist.dist_files
+ assert ('bdist_rpm', 'any', 'dist/foo-0.1-1.noarch.rpm') in dist.dist_files
- # XXX I am unable yet to make this test work without
- # spurious sdtout/stderr output under Mac OS X
- @unittest.skipUnless(sys.platform.startswith('linux'),
- 'spurious sdtout/stderr output under Mac OS X')
+ @mac_woes
@requires_zlib()
# http://bugs.python.org/issue1533164
- @unittest.skipIf(find_executable('rpm') is None,
- 'the rpm command is not found')
- @unittest.skipIf(find_executable('rpmbuild') is None,
- 'the rpmbuild command is not found')
+ @pytest.mark.skipif("not find_executable('rpm')")
+ @pytest.mark.skipif("not find_executable('rpmbuild')")
def test_no_optimize_flag(self):
# let's create a package that breaks bdist_rpm
tmp_dir = self.mkdtemp()
- os.environ['HOME'] = tmp_dir # to confine dir '.rpmdb' creation
+ os.environ['HOME'] = tmp_dir # to confine dir '.rpmdb' creation
pkg_dir = os.path.join(tmp_dir, 'foo')
os.mkdir(pkg_dir)
self.write_file((pkg_dir, 'setup.py'), SETUP_PY)
@@ -107,10 +103,16 @@ class BuildRpmTestCase(support.TempdirManager,
self.write_file((pkg_dir, 'MANIFEST.in'), 'include foo.py')
self.write_file((pkg_dir, 'README'), '')
- dist = Distribution({'name': 'foo', 'version': '0.1',
- 'py_modules': ['foo'],
- 'url': 'xxx', 'author': 'xxx',
- 'author_email': 'xxx'})
+ dist = Distribution(
+ {
+ 'name': 'foo',
+ 'version': '0.1',
+ 'py_modules': ['foo'],
+ 'url': 'xxx',
+ 'author': 'xxx',
+ 'author_email': 'xxx',
+ }
+ )
dist.script_name = 'setup.py'
os.chdir(pkg_dir)
@@ -123,16 +125,10 @@ class BuildRpmTestCase(support.TempdirManager,
cmd.run()
dist_created = os.listdir(os.path.join(pkg_dir, 'dist'))
- self.assertIn('foo-0.1-1.noarch.rpm', dist_created)
+ assert 'foo-0.1-1.noarch.rpm' in dist_created
# bug #2945: upload ignores bdist_rpm files
- self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.src.rpm'), dist.dist_files)
- self.assertIn(('bdist_rpm', 'any', 'dist/foo-0.1-1.noarch.rpm'), dist.dist_files)
+ assert ('bdist_rpm', 'any', 'dist/foo-0.1-1.src.rpm') in dist.dist_files
+ assert ('bdist_rpm', 'any', 'dist/foo-0.1-1.noarch.rpm') in dist.dist_files
os.remove(os.path.join(pkg_dir, 'dist', 'foo-0.1-1.noarch.rpm'))
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(BuildRpmTestCase)
-
-if __name__ == '__main__':
- run_unittest(test_suite())
diff --git a/setuptools/_distutils/tests/test_bdist_wininst.py b/setuptools/_distutils/tests/test_bdist_wininst.py
deleted file mode 100644
index 59f25167..00000000
--- a/setuptools/_distutils/tests/test_bdist_wininst.py
+++ /dev/null
@@ -1,40 +0,0 @@
-"""Tests for distutils.command.bdist_wininst."""
-import sys
-import platform
-import unittest
-from test.support import run_unittest
-
-from .py38compat import check_warnings
-
-from distutils.command.bdist_wininst import bdist_wininst
-from distutils.tests import support
-
-@unittest.skipIf(sys.platform == 'win32' and platform.machine() == 'ARM64',
- 'bdist_wininst is not supported in this install')
-@unittest.skipIf(getattr(bdist_wininst, '_unsupported', False),
- 'bdist_wininst is not supported in this install')
-class BuildWinInstTestCase(support.TempdirManager,
- support.LoggingSilencer,
- unittest.TestCase):
-
- def test_get_exe_bytes(self):
-
- # issue5731: command was broken on non-windows platforms
- # this test makes sure it works now for every platform
- # let's create a command
- pkg_pth, dist = self.create_dist()
- with check_warnings(("", DeprecationWarning)):
- cmd = bdist_wininst(dist)
- cmd.ensure_finalized()
-
- # let's run the code that finds the right wininst*.exe file
- # and make sure it finds it and returns its content
- # no matter what platform we have
- exe_file = cmd.get_exe_bytes()
- self.assertGreater(len(exe_file), 10)
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(BuildWinInstTestCase)
-
-if __name__ == '__main__':
- run_unittest(test_suite())
diff --git a/setuptools/_distutils/tests/test_build.py b/setuptools/_distutils/tests/test_build.py
index 83a9e4f4..66d8af50 100644
--- a/setuptools/_distutils/tests/test_build.py
+++ b/setuptools/_distutils/tests/test_build.py
@@ -1,56 +1,45 @@
"""Tests for distutils.command.build."""
-import unittest
import os
import sys
-from test.support import run_unittest
from distutils.command.build import build
from distutils.tests import support
from sysconfig import get_platform
-class BuildTestCase(support.TempdirManager,
- support.LoggingSilencer,
- unittest.TestCase):
+class TestBuild(support.TempdirManager):
def test_finalize_options(self):
pkg_dir, dist = self.create_dist()
cmd = build(dist)
cmd.finalize_options()
# if not specified, plat_name gets the current platform
- self.assertEqual(cmd.plat_name, get_platform())
+ assert cmd.plat_name == get_platform()
# build_purelib is build + lib
wanted = os.path.join(cmd.build_base, 'lib')
- self.assertEqual(cmd.build_purelib, wanted)
+ assert cmd.build_purelib == wanted
- # build_platlib is 'build/lib.platform-x.x[-pydebug]'
+ # build_platlib is 'build/lib.platform-cache_tag[-pydebug]'
# examples:
- # build/lib.macosx-10.3-i386-2.7
- plat_spec = '.%s-%d.%d' % (cmd.plat_name, *sys.version_info[:2])
+ # build/lib.macosx-10.3-i386-cpython39
+ plat_spec = '.{}-{}'.format(cmd.plat_name, sys.implementation.cache_tag)
if hasattr(sys, 'gettotalrefcount'):
- self.assertTrue(cmd.build_platlib.endswith('-pydebug'))
+ assert cmd.build_platlib.endswith('-pydebug')
plat_spec += '-pydebug'
wanted = os.path.join(cmd.build_base, 'lib' + plat_spec)
- self.assertEqual(cmd.build_platlib, wanted)
+ assert cmd.build_platlib == wanted
# by default, build_lib = build_purelib
- self.assertEqual(cmd.build_lib, cmd.build_purelib)
+ assert cmd.build_lib == cmd.build_purelib
# build_temp is build/temp.<plat>
wanted = os.path.join(cmd.build_base, 'temp' + plat_spec)
- self.assertEqual(cmd.build_temp, wanted)
+ assert cmd.build_temp == wanted
# build_scripts is build/scripts-x.x
- wanted = os.path.join(cmd.build_base,
- 'scripts-%d.%d' % sys.version_info[:2])
- self.assertEqual(cmd.build_scripts, wanted)
+ wanted = os.path.join(cmd.build_base, 'scripts-%d.%d' % sys.version_info[:2])
+ assert cmd.build_scripts == wanted
# executable is os.path.normpath(sys.executable)
- self.assertEqual(cmd.executable, os.path.normpath(sys.executable))
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(BuildTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert cmd.executable == os.path.normpath(sys.executable)
diff --git a/setuptools/_distutils/tests/test_build_clib.py b/setuptools/_distutils/tests/test_build_clib.py
index d50ead7c..709d0b7d 100644
--- a/setuptools/_distutils/tests/test_build_clib.py
+++ b/setuptools/_distutils/tests/test_build_clib.py
@@ -1,45 +1,44 @@
"""Tests for distutils.command.build_clib."""
-import unittest
import os
-import sys
-from test.support import run_unittest
+from test.support import missing_compiler_executable
-from .py35compat import missing_compiler_executable
+import pytest
from distutils.command.build_clib import build_clib
from distutils.errors import DistutilsSetupError
from distutils.tests import support
-class BuildCLibTestCase(support.TempdirManager,
- support.LoggingSilencer,
- unittest.TestCase):
+class TestBuildCLib(support.TempdirManager):
def test_check_library_dist(self):
pkg_dir, dist = self.create_dist()
cmd = build_clib(dist)
# 'libraries' option must be a list
- self.assertRaises(DistutilsSetupError, cmd.check_library_list, 'foo')
+ with pytest.raises(DistutilsSetupError):
+ cmd.check_library_list('foo')
# each element of 'libraries' must a 2-tuple
- self.assertRaises(DistutilsSetupError, cmd.check_library_list,
- ['foo1', 'foo2'])
+ with pytest.raises(DistutilsSetupError):
+ cmd.check_library_list(['foo1', 'foo2'])
# first element of each tuple in 'libraries'
# must be a string (the library name)
- self.assertRaises(DistutilsSetupError, cmd.check_library_list,
- [(1, 'foo1'), ('name', 'foo2')])
+ with pytest.raises(DistutilsSetupError):
+ cmd.check_library_list([(1, 'foo1'), ('name', 'foo2')])
# library name may not contain directory separators
- self.assertRaises(DistutilsSetupError, cmd.check_library_list,
- [('name', 'foo1'),
- ('another/name', 'foo2')])
+ with pytest.raises(DistutilsSetupError):
+ cmd.check_library_list(
+ [('name', 'foo1'), ('another/name', 'foo2')],
+ )
# second element of each tuple must be a dictionary (build info)
- self.assertRaises(DistutilsSetupError, cmd.check_library_list,
- [('name', {}),
- ('another', 'foo2')])
+ with pytest.raises(DistutilsSetupError):
+ cmd.check_library_list(
+ [('name', {}), ('another', 'foo2')],
+ )
# those work
libs = [('name', {}), ('name', {'ok': 'good'})]
@@ -52,35 +51,42 @@ class BuildCLibTestCase(support.TempdirManager,
# "in 'libraries' option 'sources' must be present and must be
# a list of source filenames
cmd.libraries = [('name', {})]
- self.assertRaises(DistutilsSetupError, cmd.get_source_files)
+ with pytest.raises(DistutilsSetupError):
+ cmd.get_source_files()
cmd.libraries = [('name', {'sources': 1})]
- self.assertRaises(DistutilsSetupError, cmd.get_source_files)
+ with pytest.raises(DistutilsSetupError):
+ cmd.get_source_files()
cmd.libraries = [('name', {'sources': ['a', 'b']})]
- self.assertEqual(cmd.get_source_files(), ['a', 'b'])
+ assert cmd.get_source_files() == ['a', 'b']
cmd.libraries = [('name', {'sources': ('a', 'b')})]
- self.assertEqual(cmd.get_source_files(), ['a', 'b'])
+ assert cmd.get_source_files() == ['a', 'b']
- cmd.libraries = [('name', {'sources': ('a', 'b')}),
- ('name2', {'sources': ['c', 'd']})]
- self.assertEqual(cmd.get_source_files(), ['a', 'b', 'c', 'd'])
+ cmd.libraries = [
+ ('name', {'sources': ('a', 'b')}),
+ ('name2', {'sources': ['c', 'd']}),
+ ]
+ assert cmd.get_source_files() == ['a', 'b', 'c', 'd']
def test_build_libraries(self):
pkg_dir, dist = self.create_dist()
cmd = build_clib(dist)
+
class FakeCompiler:
def compile(*args, **kw):
pass
+
create_static_lib = compile
cmd.compiler = FakeCompiler()
# build_libraries is also doing a bit of typo checking
lib = [('name', {'sources': 'notvalid'})]
- self.assertRaises(DistutilsSetupError, cmd.build_libraries, lib)
+ with pytest.raises(DistutilsSetupError):
+ cmd.build_libraries(lib)
lib = [('name', {'sources': list()})]
cmd.build_libraries(lib)
@@ -94,16 +100,17 @@ class BuildCLibTestCase(support.TempdirManager,
cmd.include_dirs = 'one-dir'
cmd.finalize_options()
- self.assertEqual(cmd.include_dirs, ['one-dir'])
+ assert cmd.include_dirs == ['one-dir']
cmd.include_dirs = None
cmd.finalize_options()
- self.assertEqual(cmd.include_dirs, [])
+ assert cmd.include_dirs == []
cmd.distribution.libraries = 'WONTWORK'
- self.assertRaises(DistutilsSetupError, cmd.finalize_options)
+ with pytest.raises(DistutilsSetupError):
+ cmd.finalize_options()
- @unittest.skipIf(sys.platform == 'win32', "can't test on Windows")
+ @pytest.mark.skipif('platform.system() == "Windows"')
def test_run(self):
pkg_dir, dist = self.create_dist()
cmd = build_clib(dist)
@@ -127,10 +134,4 @@ class BuildCLibTestCase(support.TempdirManager,
cmd.run()
# let's check the result
- self.assertIn('libfoo.a', os.listdir(build_temp))
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(BuildCLibTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert 'libfoo.a' in os.listdir(build_temp)
diff --git a/setuptools/_distutils/tests/test_build_ext.py b/setuptools/_distutils/tests/test_build_ext.py
index 920e4dc8..f5058487 100644
--- a/setuptools/_distutils/tests/test_build_ext.py
+++ b/setuptools/_distutils/tests/test_build_ext.py
@@ -2,62 +2,94 @@ import sys
import os
from io import StringIO
import textwrap
+import site
+import contextlib
+import platform
+import tempfile
+import importlib
+import shutil
+import re
+
+import path
+import pytest
from distutils.core import Distribution
from distutils.command.build_ext import build_ext
from distutils import sysconfig
-from distutils.tests.support import (TempdirManager, LoggingSilencer,
- copy_xxmodule_c, fixup_build_ext)
+from distutils.tests.support import (
+ TempdirManager,
+ copy_xxmodule_c,
+ fixup_build_ext,
+)
from distutils.extension import Extension
from distutils.errors import (
- CompileError, DistutilsPlatformError, DistutilsSetupError,
- UnknownFileError)
+ CompileError,
+ DistutilsPlatformError,
+ DistutilsSetupError,
+ UnknownFileError,
+)
-import unittest
from test import support
-from . import py38compat as os_helper
-from test.support.script_helper import assert_python_ok
+from . import py38compat as import_helper
-# http://bugs.python.org/issue4373
-# Don't load the xx module more than once.
-ALREADY_TESTED = False
+@pytest.fixture()
+def user_site_dir(request):
+ self = request.instance
+ self.tmp_dir = self.mkdtemp()
+ from distutils.command import build_ext
+
+ orig_user_base = site.USER_BASE
+
+ site.USER_BASE = self.mkdtemp()
+ build_ext.USER_BASE = site.USER_BASE
+
+ # bpo-30132: On Windows, a .pdb file may be created in the current
+ # working directory. Create a temporary working directory to cleanup
+ # everything at the end of the test.
+ with path.Path(self.tmp_dir):
+ yield
+
+ site.USER_BASE = orig_user_base
+ build_ext.USER_BASE = orig_user_base
-class BuildExtTestCase(TempdirManager,
- LoggingSilencer,
- unittest.TestCase):
- def setUp(self):
- # Create a simple test environment
- super(BuildExtTestCase, self).setUp()
- self.tmp_dir = self.mkdtemp()
- import site
- self.old_user_base = site.USER_BASE
- site.USER_BASE = self.mkdtemp()
- from distutils.command import build_ext
- build_ext.USER_BASE = site.USER_BASE
-
- # bpo-30132: On Windows, a .pdb file may be created in the current
- # working directory. Create a temporary working directory to cleanup
- # everything at the end of the test.
- change_cwd = os_helper.change_cwd(self.tmp_dir)
- change_cwd.__enter__()
- self.addCleanup(change_cwd.__exit__, None, None, None)
-
- def tearDown(self):
- import site
- site.USER_BASE = self.old_user_base
- from distutils.command import build_ext
- build_ext.USER_BASE = self.old_user_base
- super(BuildExtTestCase, self).tearDown()
+@contextlib.contextmanager
+def safe_extension_import(name, path):
+ with import_helper.CleanImport(name):
+ with extension_redirect(name, path) as new_path:
+ with import_helper.DirsOnSysPath(new_path):
+ yield
+
+
+@contextlib.contextmanager
+def extension_redirect(mod, path):
+ """
+ Tests will fail to tear down an extension module if it's been imported.
+
+ Before importing, copy the file to a temporary directory that won't
+ be cleaned up. Yield the new path.
+ """
+ if platform.system() != "Windows" and sys.platform != "cygwin":
+ yield path
+ return
+ with import_helper.DirsOnSysPath(path):
+ spec = importlib.util.find_spec(mod)
+ filename = os.path.basename(spec.origin)
+ trash_dir = tempfile.mkdtemp(prefix='deleteme')
+ dest = os.path.join(trash_dir, os.path.basename(filename))
+ shutil.copy(spec.origin, dest)
+ yield trash_dir
+ # TODO: can the file be scheduled for deletion?
+
+
+@pytest.mark.usefixtures('user_site_dir')
+class TestBuildExt(TempdirManager):
def build_ext(self, *args, **kwargs):
return build_ext(*args, **kwargs)
def test_build_ext(self):
cmd = support.missing_compiler_executable()
- if cmd is not None:
- self.skipTest('The %r command is not found' % cmd)
- global ALREADY_TESTED
copy_xxmodule_c(self.tmp_dir)
xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
xx_ext = Extension('xx', [xx_c])
@@ -78,47 +110,33 @@ class BuildExtTestCase(TempdirManager,
finally:
sys.stdout = old_stdout
- if ALREADY_TESTED:
- self.skipTest('Already tested in %s' % ALREADY_TESTED)
- else:
- ALREADY_TESTED = type(self).__name__
-
- code = textwrap.dedent("""
- tmp_dir = {self.tmp_dir!r}
-
- import sys
- import unittest
- from test import support
-
- sys.path.insert(0, tmp_dir)
- import xx
-
- class Tests(unittest.TestCase):
- def test_xx(self):
- for attr in ('error', 'foo', 'new', 'roj'):
- self.assertTrue(hasattr(xx, attr))
+ with safe_extension_import('xx', self.tmp_dir):
+ self._test_xx()
- self.assertEqual(xx.foo(2, 5), 7)
- self.assertEqual(xx.foo(13,15), 28)
- self.assertEqual(xx.new().demo(), None)
- if support.HAVE_DOCSTRINGS:
- doc = 'This is a template module just for instruction.'
- self.assertEqual(xx.__doc__, doc)
- self.assertIsInstance(xx.Null(), xx.Null)
- self.assertIsInstance(xx.Str(), xx.Str)
+ @staticmethod
+ def _test_xx():
+ import xx
+ for attr in ('error', 'foo', 'new', 'roj'):
+ assert hasattr(xx, attr)
- unittest.main()
- """.format(**locals()))
- assert_python_ok('-c', code)
+ assert xx.foo(2, 5) == 7
+ assert xx.foo(13, 15) == 28
+ assert xx.new().demo() is None
+ if support.HAVE_DOCSTRINGS:
+ doc = 'This is a template module just for instruction.'
+ assert xx.__doc__ == doc
+ assert isinstance(xx.Null(), xx.Null)
+ assert isinstance(xx.Str(), xx.Str)
def test_solaris_enable_shared(self):
dist = Distribution({'name': 'xx'})
cmd = self.build_ext(dist)
old = sys.platform
- sys.platform = 'sunos' # fooling finalize_options
- from distutils.sysconfig import _config_vars
+ sys.platform = 'sunos' # fooling finalize_options
+ from distutils.sysconfig import _config_vars
+
old_var = _config_vars.get('Py_ENABLE_SHARED')
_config_vars['Py_ENABLE_SHARED'] = 1
try:
@@ -131,17 +149,17 @@ class BuildExtTestCase(TempdirManager,
_config_vars['Py_ENABLE_SHARED'] = old_var
# make sure we get some library dirs under solaris
- self.assertGreater(len(cmd.library_dirs), 0)
+ assert len(cmd.library_dirs) > 0
def test_user_site(self):
import site
+
dist = Distribution({'name': 'xx'})
cmd = self.build_ext(dist)
# making sure the user option is there
- options = [name for name, short, lable in
- cmd.user_options]
- self.assertIn('user', options)
+ options = [name for name, short, lable in cmd.user_options]
+ assert 'user' in options
# setting a value
cmd.user = 1
@@ -157,9 +175,9 @@ class BuildExtTestCase(TempdirManager,
# see if include_dirs and library_dirs
# were set
- self.assertIn(lib, cmd.library_dirs)
- self.assertIn(lib, cmd.rpath)
- self.assertIn(incl, cmd.include_dirs)
+ assert lib in cmd.library_dirs
+ assert lib in cmd.rpath
+ assert incl in cmd.include_dirs
def test_optional_extension(self):
@@ -169,8 +187,8 @@ class BuildExtTestCase(TempdirManager,
dist = Distribution({'name': 'xx', 'ext_modules': modules})
cmd = self.build_ext(dist)
cmd.ensure_finalized()
- self.assertRaises((UnknownFileError, CompileError),
- cmd.run) # should raise an error
+ with pytest.raises((UnknownFileError, CompileError)):
+ cmd.run() # should raise an error
modules = [Extension('foo', ['xxx'], optional=True)]
dist = Distribution({'name': 'xx', 'ext_modules': modules})
@@ -188,40 +206,40 @@ class BuildExtTestCase(TempdirManager,
py_include = sysconfig.get_python_inc()
for p in py_include.split(os.path.pathsep):
- self.assertIn(p, cmd.include_dirs)
+ assert p in cmd.include_dirs
plat_py_include = sysconfig.get_python_inc(plat_specific=1)
for p in plat_py_include.split(os.path.pathsep):
- self.assertIn(p, cmd.include_dirs)
+ assert p in cmd.include_dirs
# make sure cmd.libraries is turned into a list
# if it's a string
cmd = self.build_ext(dist)
cmd.libraries = 'my_lib, other_lib lastlib'
cmd.finalize_options()
- self.assertEqual(cmd.libraries, ['my_lib', 'other_lib', 'lastlib'])
+ assert cmd.libraries == ['my_lib', 'other_lib', 'lastlib']
# make sure cmd.library_dirs is turned into a list
# if it's a string
cmd = self.build_ext(dist)
cmd.library_dirs = 'my_lib_dir%sother_lib_dir' % os.pathsep
cmd.finalize_options()
- self.assertIn('my_lib_dir', cmd.library_dirs)
- self.assertIn('other_lib_dir', cmd.library_dirs)
+ assert 'my_lib_dir' in cmd.library_dirs
+ assert 'other_lib_dir' in cmd.library_dirs
# make sure rpath is turned into a list
# if it's a string
cmd = self.build_ext(dist)
cmd.rpath = 'one%stwo' % os.pathsep
cmd.finalize_options()
- self.assertEqual(cmd.rpath, ['one', 'two'])
+ assert cmd.rpath == ['one', 'two']
# make sure cmd.link_objects is turned into a list
# if it's a string
cmd = build_ext(dist)
cmd.link_objects = 'one two,three'
cmd.finalize_options()
- self.assertEqual(cmd.link_objects, ['one', 'two', 'three'])
+ assert cmd.link_objects == ['one', 'two', 'three']
# XXX more tests to perform for win32
@@ -230,80 +248,92 @@ class BuildExtTestCase(TempdirManager,
cmd = self.build_ext(dist)
cmd.define = 'one,two'
cmd.finalize_options()
- self.assertEqual(cmd.define, [('one', '1'), ('two', '1')])
+ assert cmd.define == [('one', '1'), ('two', '1')]
# make sure undef is turned into a list of
# strings if they are ','-separated strings
cmd = self.build_ext(dist)
cmd.undef = 'one,two'
cmd.finalize_options()
- self.assertEqual(cmd.undef, ['one', 'two'])
+ assert cmd.undef == ['one', 'two']
# make sure swig_opts is turned into a list
cmd = self.build_ext(dist)
cmd.swig_opts = None
cmd.finalize_options()
- self.assertEqual(cmd.swig_opts, [])
+ assert cmd.swig_opts == []
cmd = self.build_ext(dist)
cmd.swig_opts = '1 2'
cmd.finalize_options()
- self.assertEqual(cmd.swig_opts, ['1', '2'])
+ assert cmd.swig_opts == ['1', '2']
def test_check_extensions_list(self):
dist = Distribution()
cmd = self.build_ext(dist)
cmd.finalize_options()
- #'extensions' option must be a list of Extension instances
- self.assertRaises(DistutilsSetupError,
- cmd.check_extensions_list, 'foo')
+ # 'extensions' option must be a list of Extension instances
+ with pytest.raises(DistutilsSetupError):
+ cmd.check_extensions_list('foo')
# each element of 'ext_modules' option must be an
# Extension instance or 2-tuple
exts = [('bar', 'foo', 'bar'), 'foo']
- self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
+ with pytest.raises(DistutilsSetupError):
+ cmd.check_extensions_list(exts)
# first element of each tuple in 'ext_modules'
# must be the extension name (a string) and match
# a python dotted-separated name
exts = [('foo-bar', '')]
- self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
+ with pytest.raises(DistutilsSetupError):
+ cmd.check_extensions_list(exts)
# second element of each tuple in 'ext_modules'
# must be a dictionary (build info)
exts = [('foo.bar', '')]
- self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
+ with pytest.raises(DistutilsSetupError):
+ cmd.check_extensions_list(exts)
# ok this one should pass
- exts = [('foo.bar', {'sources': [''], 'libraries': 'foo',
- 'some': 'bar'})]
+ exts = [('foo.bar', {'sources': [''], 'libraries': 'foo', 'some': 'bar'})]
cmd.check_extensions_list(exts)
ext = exts[0]
- self.assertIsInstance(ext, Extension)
+ assert isinstance(ext, Extension)
# check_extensions_list adds in ext the values passed
# when they are in ('include_dirs', 'library_dirs', 'libraries'
# 'extra_objects', 'extra_compile_args', 'extra_link_args')
- self.assertEqual(ext.libraries, 'foo')
- self.assertFalse(hasattr(ext, 'some'))
+ assert ext.libraries == 'foo'
+ assert not hasattr(ext, 'some')
# 'macros' element of build info dict must be 1- or 2-tuple
- exts = [('foo.bar', {'sources': [''], 'libraries': 'foo',
- 'some': 'bar', 'macros': [('1', '2', '3'), 'foo']})]
- self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
+ exts = [
+ (
+ 'foo.bar',
+ {
+ 'sources': [''],
+ 'libraries': 'foo',
+ 'some': 'bar',
+ 'macros': [('1', '2', '3'), 'foo'],
+ },
+ )
+ ]
+ with pytest.raises(DistutilsSetupError):
+ cmd.check_extensions_list(exts)
exts[0][1]['macros'] = [('1', '2'), ('3',)]
cmd.check_extensions_list(exts)
- self.assertEqual(exts[0].undef_macros, ['3'])
- self.assertEqual(exts[0].define_macros, [('1', '2')])
+ assert exts[0].undef_macros == ['3']
+ assert exts[0].define_macros == [('1', '2')]
def test_get_source_files(self):
modules = [Extension('foo', ['xxx'], optional=False)]
dist = Distribution({'name': 'xx', 'ext_modules': modules})
cmd = self.build_ext(dist)
cmd.ensure_finalized()
- self.assertEqual(cmd.get_source_files(), ['xxx'])
+ assert cmd.get_source_files() == ['xxx']
def test_unicode_module_names(self):
modules = [
@@ -313,10 +343,10 @@ class BuildExtTestCase(TempdirManager,
dist = Distribution({'name': 'xx', 'ext_modules': modules})
cmd = self.build_ext(dist)
cmd.ensure_finalized()
- self.assertRegex(cmd.get_ext_filename(modules[0].name), r'foo(_d)?\..*')
- self.assertRegex(cmd.get_ext_filename(modules[1].name), r'föö(_d)?\..*')
- self.assertEqual(cmd.get_export_symbols(modules[0]), ['PyInit_foo'])
- self.assertEqual(cmd.get_export_symbols(modules[1]), ['PyInitU_f_1gaa'])
+ assert re.search(r'foo(_d)?\..*', cmd.get_ext_filename(modules[0].name))
+ assert re.search(r'föö(_d)?\..*', cmd.get_ext_filename(modules[1].name))
+ assert cmd.get_export_symbols(modules[0]) == ['PyInit_foo']
+ assert cmd.get_export_symbols(modules[1]) == ['PyInitU_f_1gaa']
def test_compiler_option(self):
# cmd.compiler is an option and
@@ -327,22 +357,19 @@ class BuildExtTestCase(TempdirManager,
cmd.compiler = 'unix'
cmd.ensure_finalized()
cmd.run()
- self.assertEqual(cmd.compiler, 'unix')
+ assert cmd.compiler == 'unix'
def test_get_outputs(self):
cmd = support.missing_compiler_executable()
- if cmd is not None:
- self.skipTest('The %r command is not found' % cmd)
tmp_dir = self.mkdtemp()
c_file = os.path.join(tmp_dir, 'foo.c')
self.write_file(c_file, 'void PyInit_foo(void) {}\n')
ext = Extension('foo', [c_file], optional=False)
- dist = Distribution({'name': 'xx',
- 'ext_modules': [ext]})
+ dist = Distribution({'name': 'xx', 'ext_modules': [ext]})
cmd = self.build_ext(dist)
fixup_build_ext(cmd)
cmd.ensure_finalized()
- self.assertEqual(len(cmd.get_outputs()), 1)
+ assert len(cmd.get_outputs()) == 1
cmd.build_lib = os.path.join(self.tmp_dir, 'build')
cmd.build_temp = os.path.join(self.tmp_dir, 'tempt')
@@ -358,20 +385,20 @@ class BuildExtTestCase(TempdirManager,
so_file = cmd.get_outputs()[0]
finally:
os.chdir(old_wd)
- self.assertTrue(os.path.exists(so_file))
+ assert os.path.exists(so_file)
ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')
- self.assertTrue(so_file.endswith(ext_suffix))
+ assert so_file.endswith(ext_suffix)
so_dir = os.path.dirname(so_file)
- self.assertEqual(so_dir, other_tmp_dir)
+ assert so_dir == other_tmp_dir
cmd.inplace = 0
cmd.compiler = None
cmd.run()
so_file = cmd.get_outputs()[0]
- self.assertTrue(os.path.exists(so_file))
- self.assertTrue(so_file.endswith(ext_suffix))
+ assert os.path.exists(so_file)
+ assert so_file.endswith(ext_suffix)
so_dir = os.path.dirname(so_file)
- self.assertEqual(so_dir, cmd.build_lib)
+ assert so_dir == cmd.build_lib
# inplace = 0, cmd.package = 'bar'
build_py = cmd.get_finalized_command('build_py')
@@ -379,7 +406,7 @@ class BuildExtTestCase(TempdirManager,
path = cmd.get_ext_fullpath('foo')
# checking that the last directory is the build_dir
path = os.path.split(path)[0]
- self.assertEqual(path, cmd.build_lib)
+ assert path == cmd.build_lib
# inplace = 1, cmd.package = 'bar'
cmd.inplace = 1
@@ -393,14 +420,14 @@ class BuildExtTestCase(TempdirManager,
# checking that the last directory is bar
path = os.path.split(path)[0]
lastdir = os.path.split(path)[-1]
- self.assertEqual(lastdir, 'bar')
+ assert lastdir == 'bar'
def test_ext_fullpath(self):
ext = sysconfig.get_config_var('EXT_SUFFIX')
# building lxml.etree inplace
- #etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c')
- #etree_ext = Extension('lxml.etree', [etree_c])
- #dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]})
+ # etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c')
+ # etree_ext = Extension('lxml.etree', [etree_c])
+ # dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]})
dist = Distribution()
cmd = self.build_ext(dist)
cmd.inplace = 1
@@ -409,46 +436,47 @@ class BuildExtTestCase(TempdirManager,
curdir = os.getcwd()
wanted = os.path.join(curdir, 'src', 'lxml', 'etree' + ext)
path = cmd.get_ext_fullpath('lxml.etree')
- self.assertEqual(wanted, path)
+ assert wanted == path
# building lxml.etree not inplace
cmd.inplace = 0
cmd.build_lib = os.path.join(curdir, 'tmpdir')
wanted = os.path.join(curdir, 'tmpdir', 'lxml', 'etree' + ext)
path = cmd.get_ext_fullpath('lxml.etree')
- self.assertEqual(wanted, path)
+ assert wanted == path
# building twisted.runner.portmap not inplace
build_py = cmd.get_finalized_command('build_py')
build_py.package_dir = {}
cmd.distribution.packages = ['twisted', 'twisted.runner.portmap']
path = cmd.get_ext_fullpath('twisted.runner.portmap')
- wanted = os.path.join(curdir, 'tmpdir', 'twisted', 'runner',
- 'portmap' + ext)
- self.assertEqual(wanted, path)
+ wanted = os.path.join(curdir, 'tmpdir', 'twisted', 'runner', 'portmap' + ext)
+ assert wanted == path
# building twisted.runner.portmap inplace
cmd.inplace = 1
path = cmd.get_ext_fullpath('twisted.runner.portmap')
wanted = os.path.join(curdir, 'twisted', 'runner', 'portmap' + ext)
- self.assertEqual(wanted, path)
+ assert wanted == path
-
- @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for MacOSX')
+ @pytest.mark.skipif('platform.system() != "Darwin"')
+ @pytest.mark.usefixtures('save_env')
def test_deployment_target_default(self):
# Issue 9516: Test that, in the absence of the environment variable,
# an extension module is compiled with the same deployment target as
# the interpreter.
self._try_compile_deployment_target('==', None)
- @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for MacOSX')
+ @pytest.mark.skipif('platform.system() != "Darwin"')
+ @pytest.mark.usefixtures('save_env')
def test_deployment_target_too_low(self):
# Issue 9516: Test that an extension module is not allowed to be
# compiled with a deployment target less than that of the interpreter.
- self.assertRaises(DistutilsPlatformError,
- self._try_compile_deployment_target, '>', '10.1')
+ with pytest.raises(DistutilsPlatformError):
+ self._try_compile_deployment_target('>', '10.1')
- @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for MacOSX')
+ @pytest.mark.skipif('platform.system() != "Darwin"')
+ @pytest.mark.usefixtures('save_env')
def test_deployment_target_higher_ok(self):
# Issue 9516: Test that an extension module can be compiled with a
# deployment target higher than that of the interpreter: the ext
@@ -462,10 +490,6 @@ class BuildExtTestCase(TempdirManager,
self._try_compile_deployment_target('<', deptarget)
def _try_compile_deployment_target(self, operator, target):
- orig_environ = os.environ
- os.environ = orig_environ.copy()
- self.addCleanup(setattr, os, 'environ', orig_environ)
-
if target is None:
if os.environ.get('MACOSX_DEPLOYMENT_TARGET'):
del os.environ['MACOSX_DEPLOYMENT_TARGET']
@@ -475,7 +499,9 @@ class BuildExtTestCase(TempdirManager,
deptarget_c = os.path.join(self.tmp_dir, 'deptargetmodule.c')
with open(deptarget_c, 'w') as fp:
- fp.write(textwrap.dedent('''\
+ fp.write(
+ textwrap.dedent(
+ '''\
#include <AvailabilityMacros.h>
int dummy;
@@ -485,7 +511,10 @@ class BuildExtTestCase(TempdirManager,
#error "Unexpected target"
#endif
- ''' % operator))
+ '''
+ % operator
+ )
+ )
# get the deployment target that the interpreter was built with
target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
@@ -506,12 +535,9 @@ class BuildExtTestCase(TempdirManager,
deptarget_ext = Extension(
'deptarget',
[deptarget_c],
- extra_compile_args=['-DTARGET=%s'%(target,)],
+ extra_compile_args=['-DTARGET={}'.format(target)],
)
- dist = Distribution({
- 'name': 'deptarget',
- 'ext_modules': [deptarget_ext]
- })
+ dist = Distribution({'name': 'deptarget', 'ext_modules': [deptarget_ext]})
dist.package_dir = self.tmp_dir
cmd = self.build_ext(dist)
cmd.build_lib = self.tmp_dir
@@ -532,19 +558,8 @@ class BuildExtTestCase(TempdirManager,
self.fail("Wrong deployment target during compilation")
-class ParallelBuildExtTestCase(BuildExtTestCase):
-
+class TestParallelBuildExt(TestBuildExt):
def build_ext(self, *args, **kwargs):
build_ext = super().build_ext(*args, **kwargs)
build_ext.parallel = True
return build_ext
-
-
-def test_suite():
- suite = unittest.TestSuite()
- suite.addTest(unittest.TestLoader().loadTestsFromTestCase(BuildExtTestCase))
- suite.addTest(unittest.TestLoader().loadTestsFromTestCase(ParallelBuildExtTestCase))
- return suite
-
-if __name__ == '__main__':
- support.run_unittest(__name__)
diff --git a/setuptools/_distutils/tests/test_build_py.py b/setuptools/_distutils/tests/test_build_py.py
index a590a485..3bef9d79 100644
--- a/setuptools/_distutils/tests/test_build_py.py
+++ b/setuptools/_distutils/tests/test_build_py.py
@@ -2,20 +2,18 @@
import os
import sys
-import unittest
+
+import pytest
from distutils.command.build_py import build_py
from distutils.core import Distribution
from distutils.errors import DistutilsFileError
from distutils.tests import support
-from test.support import run_unittest
-
-class BuildPyTestCase(support.TempdirManager,
- support.LoggingSilencer,
- unittest.TestCase):
+@support.combine_markers
+class TestBuildPy(support.TempdirManager):
def test_package_data(self):
sources = self.mkdtemp()
f = open(os.path.join(sources, "__init__.py"), "w")
@@ -31,13 +29,10 @@ class BuildPyTestCase(support.TempdirManager,
destination = self.mkdtemp()
- dist = Distribution({"packages": ["pkg"],
- "package_dir": {"pkg": sources}})
+ dist = Distribution({"packages": ["pkg"], "package_dir": {"pkg": sources}})
# script_name need not exist, it just need to be initialized
dist.script_name = os.path.join(sources, "setup.py")
- dist.command_obj["build"] = support.DummyCommand(
- force=0,
- build_lib=destination)
+ dist.command_obj["build"] = support.DummyCommand(force=0, build_lib=destination)
dist.packages = ["pkg"]
dist.package_data = {"pkg": ["README.txt"]}
dist.package_dir = {"pkg": sources}
@@ -45,25 +40,24 @@ class BuildPyTestCase(support.TempdirManager,
cmd = build_py(dist)
cmd.compile = 1
cmd.ensure_finalized()
- self.assertEqual(cmd.package_data, dist.package_data)
+ assert cmd.package_data == dist.package_data
cmd.run()
# This makes sure the list of outputs includes byte-compiled
# files for Python modules but not for package data files
# (there shouldn't *be* byte-code files for those!).
- self.assertEqual(len(cmd.get_outputs()), 3)
+ assert len(cmd.get_outputs()) == 3
pkgdest = os.path.join(destination, "pkg")
files = os.listdir(pkgdest)
pycache_dir = os.path.join(pkgdest, "__pycache__")
- self.assertIn("__init__.py", files)
- self.assertIn("README.txt", files)
+ assert "__init__.py" in files
+ assert "README.txt" in files
if sys.dont_write_bytecode:
- self.assertFalse(os.path.exists(pycache_dir))
+ assert not os.path.exists(pycache_dir)
else:
pyc_files = os.listdir(pycache_dir)
- self.assertIn("__init__.%s.pyc" % sys.implementation.cache_tag,
- pyc_files)
+ assert "__init__.%s.pyc" % sys.implementation.cache_tag in pyc_files
def test_empty_package_dir(self):
# See bugs #1668596/#1720897
@@ -75,9 +69,13 @@ class BuildPyTestCase(support.TempdirManager,
open(os.path.join(testdir, "testfile"), "w").close()
os.chdir(sources)
- dist = Distribution({"packages": ["pkg"],
- "package_dir": {"pkg": ""},
- "package_data": {"pkg": ["doc/*"]}})
+ dist = Distribution(
+ {
+ "packages": ["pkg"],
+ "package_dir": {"pkg": ""},
+ "package_data": {"pkg": ["doc/*"]},
+ }
+ )
# script_name need not exist, it just need to be initialized
dist.script_name = os.path.join(sources, "setup.py")
dist.script_args = ["build"]
@@ -88,7 +86,7 @@ class BuildPyTestCase(support.TempdirManager,
except DistutilsFileError:
self.fail("failed package_data test when package_dir is ''")
- @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
+ @pytest.mark.skipif('sys.dont_write_bytecode')
def test_byte_compile(self):
project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
os.chdir(project_dir)
@@ -100,12 +98,11 @@ class BuildPyTestCase(support.TempdirManager,
cmd.run()
found = os.listdir(cmd.build_lib)
- self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py'])
+ assert sorted(found) == ['__pycache__', 'boiledeggs.py']
found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
- self.assertEqual(found,
- ['boiledeggs.%s.pyc' % sys.implementation.cache_tag])
+ assert found == ['boiledeggs.%s.pyc' % sys.implementation.cache_tag]
- @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
+ @pytest.mark.skipif('sys.dont_write_bytecode')
def test_byte_compile_optimized(self):
project_dir, dist = self.create_dist(py_modules=['boiledeggs'])
os.chdir(project_dir)
@@ -118,10 +115,10 @@ class BuildPyTestCase(support.TempdirManager,
cmd.run()
found = os.listdir(cmd.build_lib)
- self.assertEqual(sorted(found), ['__pycache__', 'boiledeggs.py'])
+ assert sorted(found) == ['__pycache__', 'boiledeggs.py']
found = os.listdir(os.path.join(cmd.build_lib, '__pycache__'))
- expect = 'boiledeggs.{}.opt-1.pyc'.format(sys.implementation.cache_tag)
- self.assertEqual(sorted(found), [expect])
+ expect = f'boiledeggs.{sys.implementation.cache_tag}.opt-1.pyc'
+ assert sorted(found) == [expect]
def test_dir_in_package_data(self):
"""
@@ -142,8 +139,7 @@ class BuildPyTestCase(support.TempdirManager,
os.mkdir(os.path.join(docdir, 'otherdir'))
os.chdir(sources)
- dist = Distribution({"packages": ["pkg"],
- "package_data": {"pkg": ["doc/*"]}})
+ dist = Distribution({"packages": ["pkg"], "package_data": {"pkg": ["doc/*"]}})
# script_name need not exist, it just need to be initialized
dist.script_name = os.path.join(sources, "setup.py")
dist.script_args = ["build"]
@@ -154,7 +150,7 @@ class BuildPyTestCase(support.TempdirManager,
except DistutilsFileError:
self.fail("failed package_data when data dir includes a dir")
- def test_dont_write_bytecode(self):
+ def test_dont_write_bytecode(self, caplog):
# makes sure byte_compile is not used
dist = self.create_dist()[1]
cmd = build_py(dist)
@@ -168,12 +164,40 @@ class BuildPyTestCase(support.TempdirManager,
finally:
sys.dont_write_bytecode = old_dont_write_bytecode
- self.assertIn('byte-compiling is disabled',
- self.logs[0][1] % self.logs[0][2])
+ assert 'byte-compiling is disabled' in caplog.records[0].message
+ def test_namespace_package_does_not_warn(self, caplog):
+ """
+ Originally distutils implementation did not account for PEP 420
+ and included warns for package directories that did not contain
+ ``__init__.py`` files.
+ After the acceptance of PEP 420, these warnings don't make more sense
+ so we want to ensure there are not displayed to not confuse the users.
+ """
+ # Create a fake project structure with a package namespace:
+ tmp = self.mkdtemp()
+ os.chdir(tmp)
+ os.makedirs("ns/pkg")
+ open("ns/pkg/module.py", "w").close()
+
+ # Configure the package:
+ attrs = {
+ "name": "ns.pkg",
+ "packages": ["ns", "ns.pkg"],
+ "script_name": "setup.py",
+ }
+ dist = Distribution(attrs)
+
+ # Run code paths that would trigger the trap:
+ cmd = dist.get_command_obj("build_py")
+ cmd.finalize_options()
+ modules = cmd.find_all_modules()
+ assert len(modules) == 1
+ module_path = modules[0][-1]
+ assert module_path.replace(os.sep, "/") == "ns/pkg/module.py"
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(BuildPyTestCase)
+ cmd.run()
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert not any(
+ "package init file" in msg and "not found" in msg for msg in caplog.messages
+ )
diff --git a/setuptools/_distutils/tests/test_build_scripts.py b/setuptools/_distutils/tests/test_build_scripts.py
index f299e51e..1a5753c7 100644
--- a/setuptools/_distutils/tests/test_build_scripts.py
+++ b/setuptools/_distutils/tests/test_build_scripts.py
@@ -1,73 +1,74 @@
"""Tests for distutils.command.build_scripts."""
import os
-import unittest
from distutils.command.build_scripts import build_scripts
from distutils.core import Distribution
from distutils import sysconfig
from distutils.tests import support
-from test.support import run_unittest
-class BuildScriptsTestCase(support.TempdirManager,
- support.LoggingSilencer,
- unittest.TestCase):
-
+class TestBuildScripts(support.TempdirManager):
def test_default_settings(self):
cmd = self.get_build_scripts_cmd("/foo/bar", [])
- self.assertFalse(cmd.force)
- self.assertIsNone(cmd.build_dir)
+ assert not cmd.force
+ assert cmd.build_dir is None
cmd.finalize_options()
- self.assertTrue(cmd.force)
- self.assertEqual(cmd.build_dir, "/foo/bar")
+ assert cmd.force
+ assert cmd.build_dir == "/foo/bar"
def test_build(self):
source = self.mkdtemp()
target = self.mkdtemp()
expected = self.write_sample_scripts(source)
- cmd = self.get_build_scripts_cmd(target,
- [os.path.join(source, fn)
- for fn in expected])
+ cmd = self.get_build_scripts_cmd(
+ target, [os.path.join(source, fn) for fn in expected]
+ )
cmd.finalize_options()
cmd.run()
built = os.listdir(target)
for name in expected:
- self.assertIn(name, built)
+ assert name in built
def get_build_scripts_cmd(self, target, scripts):
import sys
+
dist = Distribution()
dist.scripts = scripts
dist.command_obj["build"] = support.DummyCommand(
- build_scripts=target,
- force=1,
- executable=sys.executable
- )
+ build_scripts=target, force=1, executable=sys.executable
+ )
return build_scripts(dist)
def write_sample_scripts(self, dir):
expected = []
expected.append("script1.py")
- self.write_script(dir, "script1.py",
- ("#! /usr/bin/env python2.3\n"
- "# bogus script w/ Python sh-bang\n"
- "pass\n"))
+ self.write_script(
+ dir,
+ "script1.py",
+ (
+ "#! /usr/bin/env python2.3\n"
+ "# bogus script w/ Python sh-bang\n"
+ "pass\n"
+ ),
+ )
expected.append("script2.py")
- self.write_script(dir, "script2.py",
- ("#!/usr/bin/python\n"
- "# bogus script w/ Python sh-bang\n"
- "pass\n"))
+ self.write_script(
+ dir,
+ "script2.py",
+ ("#!/usr/bin/python\n" "# bogus script w/ Python sh-bang\n" "pass\n"),
+ )
expected.append("shell.sh")
- self.write_script(dir, "shell.sh",
- ("#!/bin/sh\n"
- "# bogus shell script w/ sh-bang\n"
- "exit 0\n"))
+ self.write_script(
+ dir,
+ "shell.sh",
+ ("#!/bin/sh\n" "# bogus shell script w/ sh-bang\n" "exit 0\n"),
+ )
return expected
def write_script(self, dir, name, text):
@@ -82,10 +83,9 @@ class BuildScriptsTestCase(support.TempdirManager,
target = self.mkdtemp()
expected = self.write_sample_scripts(source)
-
- cmd = self.get_build_scripts_cmd(target,
- [os.path.join(source, fn)
- for fn in expected])
+ cmd = self.get_build_scripts_cmd(
+ target, [os.path.join(source, fn) for fn in expected]
+ )
cmd.finalize_options()
# http://bugs.python.org/issue4524
@@ -103,10 +103,4 @@ class BuildScriptsTestCase(support.TempdirManager,
built = os.listdir(target)
for name in expected:
- self.assertIn(name, built)
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(BuildScriptsTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert name in built
diff --git a/setuptools/_distutils/tests/test_ccompiler.py b/setuptools/_distutils/tests/test_ccompiler.py
new file mode 100644
index 00000000..da1879f2
--- /dev/null
+++ b/setuptools/_distutils/tests/test_ccompiler.py
@@ -0,0 +1,55 @@
+import os
+import sys
+import platform
+import textwrap
+import sysconfig
+
+import pytest
+
+from distutils import ccompiler
+
+
+def _make_strs(paths):
+ """
+ Convert paths to strings for legacy compatibility.
+ """
+ if sys.version_info > (3, 8) and platform.system() != "Windows":
+ return paths
+ return list(map(os.fspath, paths))
+
+
+@pytest.fixture
+def c_file(tmp_path):
+ c_file = tmp_path / 'foo.c'
+ gen_headers = ('Python.h',)
+ is_windows = platform.system() == "Windows"
+ plat_headers = ('windows.h',) * is_windows
+ all_headers = gen_headers + plat_headers
+ headers = '\n'.join(f'#include <{header}>\n' for header in all_headers)
+ payload = (
+ textwrap.dedent(
+ """
+ #headers
+ void PyInit_foo(void) {}
+ """
+ )
+ .lstrip()
+ .replace('#headers', headers)
+ )
+ c_file.write_text(payload)
+ return c_file
+
+
+def test_set_include_dirs(c_file):
+ """
+ Extensions should build even if set_include_dirs is invoked.
+ In particular, compiler-specific paths should not be overridden.
+ """
+ compiler = ccompiler.new_compiler()
+ python = sysconfig.get_paths()['include']
+ compiler.set_include_dirs([python])
+ compiler.compile(_make_strs([c_file]))
+
+ # do it again, setting include dirs after any initialization
+ compiler.set_include_dirs([python])
+ compiler.compile(_make_strs([c_file]))
diff --git a/setuptools/_distutils/tests/test_check.py b/setuptools/_distutils/tests/test_check.py
index 91bcdceb..54654067 100644
--- a/setuptools/_distutils/tests/test_check.py
+++ b/setuptools/_distutils/tests/test_check.py
@@ -1,10 +1,10 @@
"""Tests for distutils.command.check."""
import os
import textwrap
-import unittest
-from test.support import run_unittest
-from distutils.command.check import check, HAS_DOCUTILS
+import pytest
+
+from distutils.command.check import check
from distutils.tests import support
from distutils.errors import DistutilsSetupError
@@ -17,10 +17,8 @@ except ImportError:
HERE = os.path.dirname(__file__)
-class CheckTestCase(support.LoggingSilencer,
- support.TempdirManager,
- unittest.TestCase):
-
+@support.combine_markers
+class TestCheck(support.TempdirManager):
def _run(self, metadata=None, cwd=None, **options):
if metadata is None:
metadata = {}
@@ -43,97 +41,142 @@ class CheckTestCase(support.LoggingSilencer,
# by default, check is checking the metadata
# should have some warnings
cmd = self._run()
- self.assertEqual(cmd._warnings, 2)
+ assert cmd._warnings == 1
# now let's add the required fields
# and run it again, to make sure we don't get
# any warning anymore
- metadata = {'url': 'xxx', 'author': 'xxx',
- 'author_email': 'xxx',
- 'name': 'xxx', 'version': 'xxx'}
+ metadata = {
+ 'url': 'xxx',
+ 'author': 'xxx',
+ 'author_email': 'xxx',
+ 'name': 'xxx',
+ 'version': 'xxx',
+ }
cmd = self._run(metadata)
- self.assertEqual(cmd._warnings, 0)
+ assert cmd._warnings == 0
# now with the strict mode, we should
# get an error if there are missing metadata
- self.assertRaises(DistutilsSetupError, self._run, {}, **{'strict': 1})
+ with pytest.raises(DistutilsSetupError):
+ self._run({}, **{'strict': 1})
# and of course, no error when all metadata are present
cmd = self._run(metadata, strict=1)
- self.assertEqual(cmd._warnings, 0)
+ assert cmd._warnings == 0
# now a test with non-ASCII characters
- metadata = {'url': 'xxx', 'author': '\u00c9ric',
- 'author_email': 'xxx', 'name': 'xxx',
- 'version': 'xxx',
- 'description': 'Something about esszet \u00df',
- 'long_description': 'More things about esszet \u00df'}
+ metadata = {
+ 'url': 'xxx',
+ 'author': '\u00c9ric',
+ 'author_email': 'xxx',
+ 'name': 'xxx',
+ 'version': 'xxx',
+ 'description': 'Something about esszet \u00df',
+ 'long_description': 'More things about esszet \u00df',
+ }
cmd = self._run(metadata)
- self.assertEqual(cmd._warnings, 0)
+ assert cmd._warnings == 0
+
+ def test_check_author_maintainer(self):
+ for kind in ("author", "maintainer"):
+ # ensure no warning when author_email or maintainer_email is given
+ # (the spec allows these fields to take the form "Name <email>")
+ metadata = {
+ 'url': 'xxx',
+ kind + '_email': 'Name <name@email.com>',
+ 'name': 'xxx',
+ 'version': 'xxx',
+ }
+ cmd = self._run(metadata)
+ assert cmd._warnings == 0
+
+ # the check should not warn if only email is given
+ metadata[kind + '_email'] = 'name@email.com'
+ cmd = self._run(metadata)
+ assert cmd._warnings == 0
+
+ # the check should not warn if only the name is given
+ metadata[kind] = "Name"
+ del metadata[kind + '_email']
+ cmd = self._run(metadata)
+ assert cmd._warnings == 0
- @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
def test_check_document(self):
+ pytest.importorskip('docutils')
pkg_info, dist = self.create_dist()
cmd = check(dist)
# let's see if it detects broken rest
broken_rest = 'title\n===\n\ntest'
msgs = cmd._check_rst_data(broken_rest)
- self.assertEqual(len(msgs), 1)
+ assert len(msgs) == 1
# and non-broken rest
rest = 'title\n=====\n\ntest'
msgs = cmd._check_rst_data(rest)
- self.assertEqual(len(msgs), 0)
+ assert len(msgs) == 0
- @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
def test_check_restructuredtext(self):
+ pytest.importorskip('docutils')
# let's see if it detects broken rest in long_description
broken_rest = 'title\n===\n\ntest'
pkg_info, dist = self.create_dist(long_description=broken_rest)
cmd = check(dist)
cmd.check_restructuredtext()
- self.assertEqual(cmd._warnings, 1)
+ assert cmd._warnings == 1
# let's see if we have an error with strict=1
- metadata = {'url': 'xxx', 'author': 'xxx',
- 'author_email': 'xxx',
- 'name': 'xxx', 'version': 'xxx',
- 'long_description': broken_rest}
- self.assertRaises(DistutilsSetupError, self._run, metadata,
- **{'strict': 1, 'restructuredtext': 1})
+ metadata = {
+ 'url': 'xxx',
+ 'author': 'xxx',
+ 'author_email': 'xxx',
+ 'name': 'xxx',
+ 'version': 'xxx',
+ 'long_description': broken_rest,
+ }
+ with pytest.raises(DistutilsSetupError):
+ self._run(metadata, **{'strict': 1, 'restructuredtext': 1})
# and non-broken rest, including a non-ASCII character to test #12114
metadata['long_description'] = 'title\n=====\n\ntest \u00df'
cmd = self._run(metadata, strict=1, restructuredtext=1)
- self.assertEqual(cmd._warnings, 0)
+ assert cmd._warnings == 0
# check that includes work to test #31292
metadata['long_description'] = 'title\n=====\n\n.. include:: includetest.rst'
cmd = self._run(metadata, cwd=HERE, strict=1, restructuredtext=1)
- self.assertEqual(cmd._warnings, 0)
+ assert cmd._warnings == 0
- @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
def test_check_restructuredtext_with_syntax_highlight(self):
+ pytest.importorskip('docutils')
# Don't fail if there is a `code` or `code-block` directive
example_rst_docs = []
- example_rst_docs.append(textwrap.dedent("""\
+ example_rst_docs.append(
+ textwrap.dedent(
+ """\
Here's some code:
.. code:: python
def foo():
pass
- """))
- example_rst_docs.append(textwrap.dedent("""\
+ """
+ )
+ )
+ example_rst_docs.append(
+ textwrap.dedent(
+ """\
Here's some code:
.. code-block:: python
def foo():
pass
- """))
+ """
+ )
+ )
for rest_with_code in example_rst_docs:
pkg_info, dist = self.create_dist(long_description=rest_with_code)
@@ -141,23 +184,14 @@ class CheckTestCase(support.LoggingSilencer,
cmd.check_restructuredtext()
msgs = cmd._check_rst_data(rest_with_code)
if pygments is not None:
- self.assertEqual(len(msgs), 0)
+ assert len(msgs) == 0
else:
- self.assertEqual(len(msgs), 1)
- self.assertEqual(
- str(msgs[0][1]),
- 'Cannot analyze code. Pygments package not found.'
+ assert len(msgs) == 1
+ assert (
+ str(msgs[0][1])
+ == 'Cannot analyze code. Pygments package not found.'
)
def test_check_all(self):
-
- metadata = {'url': 'xxx', 'author': 'xxx'}
- self.assertRaises(DistutilsSetupError, self._run,
- {}, **{'strict': 1,
- 'restructuredtext': 1})
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(CheckTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ with pytest.raises(DistutilsSetupError):
+ self._run({}, **{'strict': 1, 'restructuredtext': 1})
diff --git a/setuptools/_distutils/tests/test_clean.py b/setuptools/_distutils/tests/test_clean.py
index 92367499..157b60a1 100644
--- a/setuptools/_distutils/tests/test_clean.py
+++ b/setuptools/_distutils/tests/test_clean.py
@@ -1,23 +1,26 @@
"""Tests for distutils.command.clean."""
import os
-import unittest
from distutils.command.clean import clean
from distutils.tests import support
-from test.support import run_unittest
-class cleanTestCase(support.TempdirManager,
- support.LoggingSilencer,
- unittest.TestCase):
+class TestClean(support.TempdirManager):
def test_simple_run(self):
pkg_dir, dist = self.create_dist()
cmd = clean(dist)
# let's add some elements clean should remove
- dirs = [(d, os.path.join(pkg_dir, d))
- for d in ('build_temp', 'build_lib', 'bdist_base',
- 'build_scripts', 'build_base')]
+ dirs = [
+ (d, os.path.join(pkg_dir, d))
+ for d in (
+ 'build_temp',
+ 'build_lib',
+ 'bdist_base',
+ 'build_scripts',
+ 'build_base',
+ )
+ ]
for name, path in dirs:
os.mkdir(path)
@@ -34,16 +37,9 @@ class cleanTestCase(support.TempdirManager,
# make sure the files where removed
for name, path in dirs:
- self.assertFalse(os.path.exists(path),
- '%s was not removed' % path)
+ assert not os.path.exists(path), '%s was not removed' % path
# let's run the command again (should spit warnings but succeed)
cmd.all = 1
cmd.ensure_finalized()
cmd.run()
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(cleanTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
diff --git a/setuptools/_distutils/tests/test_cmd.py b/setuptools/_distutils/tests/test_cmd.py
index 2319214a..3aac448d 100644
--- a/setuptools/_distutils/tests/test_cmd.py
+++ b/setuptools/_distutils/tests/test_cmd.py
@@ -1,26 +1,25 @@
"""Tests for distutils.cmd."""
-import unittest
import os
-from test.support import captured_stdout, run_unittest
from distutils.cmd import Command
from distutils.dist import Distribution
from distutils.errors import DistutilsOptionError
from distutils import debug
+import pytest
+
class MyCmd(Command):
def initialize_options(self):
pass
-class CommandTestCase(unittest.TestCase):
- def setUp(self):
- dist = Distribution()
- self.cmd = MyCmd(dist)
+@pytest.fixture
+def cmd(request):
+ return MyCmd(Distribution())
- def test_ensure_string_list(self):
- cmd = self.cmd
+class TestCommand:
+ def test_ensure_string_list(self, cmd):
cmd.not_string_list = ['one', 2, 'three']
cmd.yes_string_list = ['one', 'two', 'three']
cmd.not_string_list2 = object()
@@ -28,99 +27,81 @@ class CommandTestCase(unittest.TestCase):
cmd.ensure_string_list('yes_string_list')
cmd.ensure_string_list('yes_string_list2')
- self.assertRaises(DistutilsOptionError,
- cmd.ensure_string_list, 'not_string_list')
+ with pytest.raises(DistutilsOptionError):
+ cmd.ensure_string_list('not_string_list')
- self.assertRaises(DistutilsOptionError,
- cmd.ensure_string_list, 'not_string_list2')
+ with pytest.raises(DistutilsOptionError):
+ cmd.ensure_string_list('not_string_list2')
cmd.option1 = 'ok,dok'
cmd.ensure_string_list('option1')
- self.assertEqual(cmd.option1, ['ok', 'dok'])
+ assert cmd.option1 == ['ok', 'dok']
cmd.option2 = ['xxx', 'www']
cmd.ensure_string_list('option2')
cmd.option3 = ['ok', 2]
- self.assertRaises(DistutilsOptionError, cmd.ensure_string_list,
- 'option3')
-
-
- def test_make_file(self):
-
- cmd = self.cmd
+ with pytest.raises(DistutilsOptionError):
+ cmd.ensure_string_list('option3')
+ def test_make_file(self, cmd):
# making sure it raises when infiles is not a string or a list/tuple
- self.assertRaises(TypeError, cmd.make_file,
- infiles=1, outfile='', func='func', args=())
+ with pytest.raises(TypeError):
+ cmd.make_file(infiles=1, outfile='', func='func', args=())
# making sure execute gets called properly
def _execute(func, args, exec_msg, level):
- self.assertEqual(exec_msg, 'generating out from in')
+ assert exec_msg == 'generating out from in'
+
cmd.force = True
cmd.execute = _execute
cmd.make_file(infiles='in', outfile='out', func='func', args=())
- def test_dump_options(self):
+ def test_dump_options(self, cmd):
msgs = []
+
def _announce(msg, level):
msgs.append(msg)
- cmd = self.cmd
+
cmd.announce = _announce
cmd.option1 = 1
cmd.option2 = 1
cmd.user_options = [('option1', '', ''), ('option2', '', '')]
cmd.dump_options()
- wanted = ["command options for 'MyCmd':", ' option1 = 1',
- ' option2 = 1']
- self.assertEqual(msgs, wanted)
+ wanted = ["command options for 'MyCmd':", ' option1 = 1', ' option2 = 1']
+ assert msgs == wanted
- def test_ensure_string(self):
- cmd = self.cmd
+ def test_ensure_string(self, cmd):
cmd.option1 = 'ok'
cmd.ensure_string('option1')
cmd.option2 = None
cmd.ensure_string('option2', 'xxx')
- self.assertTrue(hasattr(cmd, 'option2'))
+ assert hasattr(cmd, 'option2')
cmd.option3 = 1
- self.assertRaises(DistutilsOptionError, cmd.ensure_string, 'option3')
+ with pytest.raises(DistutilsOptionError):
+ cmd.ensure_string('option3')
- def test_ensure_filename(self):
- cmd = self.cmd
+ def test_ensure_filename(self, cmd):
cmd.option1 = __file__
cmd.ensure_filename('option1')
cmd.option2 = 'xxx'
- self.assertRaises(DistutilsOptionError, cmd.ensure_filename, 'option2')
+ with pytest.raises(DistutilsOptionError):
+ cmd.ensure_filename('option2')
- def test_ensure_dirname(self):
- cmd = self.cmd
+ def test_ensure_dirname(self, cmd):
cmd.option1 = os.path.dirname(__file__) or os.curdir
cmd.ensure_dirname('option1')
cmd.option2 = 'xxx'
- self.assertRaises(DistutilsOptionError, cmd.ensure_dirname, 'option2')
-
- def test_debug_print(self):
- cmd = self.cmd
- with captured_stdout() as stdout:
- cmd.debug_print('xxx')
- stdout.seek(0)
- self.assertEqual(stdout.read(), '')
-
- debug.DEBUG = True
- try:
- with captured_stdout() as stdout:
- cmd.debug_print('xxx')
- stdout.seek(0)
- self.assertEqual(stdout.read(), 'xxx\n')
- finally:
- debug.DEBUG = False
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(CommandTestCase)
-
-if __name__ == '__main__':
- run_unittest(test_suite())
+ with pytest.raises(DistutilsOptionError):
+ cmd.ensure_dirname('option2')
+
+ def test_debug_print(self, cmd, capsys, monkeypatch):
+ cmd.debug_print('xxx')
+ assert capsys.readouterr().out == ''
+ monkeypatch.setattr(debug, 'DEBUG', True)
+ cmd.debug_print('xxx')
+ assert capsys.readouterr().out == 'xxx\n'
diff --git a/setuptools/_distutils/tests/test_config.py b/setuptools/_distutils/tests/test_config.py
index 27bd9d44..1ae615db 100644
--- a/setuptools/_distutils/tests/test_config.py
+++ b/setuptools/_distutils/tests/test_config.py
@@ -1,14 +1,9 @@
"""Tests for distutils.pypirc.pypirc."""
import os
-import unittest
-from distutils.core import PyPIRCCommand
-from distutils.core import Distribution
-from distutils.log import set_threshold
-from distutils.log import WARN
+import pytest
from distutils.tests import support
-from test.support import run_unittest
PYPIRC = """\
[distutils]
@@ -50,38 +45,13 @@ password:xxx
"""
-class BasePyPIRCCommandTestCase(support.TempdirManager,
- support.LoggingSilencer,
- support.EnvironGuard,
- unittest.TestCase):
-
- def setUp(self):
- """Patches the environment."""
- super(BasePyPIRCCommandTestCase, self).setUp()
- self.tmp_dir = self.mkdtemp()
- os.environ['HOME'] = self.tmp_dir
- os.environ['USERPROFILE'] = self.tmp_dir
- self.rc = os.path.join(self.tmp_dir, '.pypirc')
- self.dist = Distribution()
-
- class command(PyPIRCCommand):
- def __init__(self, dist):
- super().__init__(dist)
- def initialize_options(self):
- pass
- finalize_options = initialize_options
-
- self._cmd = command
- self.old_threshold = set_threshold(WARN)
-
- def tearDown(self):
- """Removes the patch."""
- set_threshold(self.old_threshold)
- super(BasePyPIRCCommandTestCase, self).tearDown()
+@support.combine_markers
+@pytest.mark.usefixtures('pypirc')
+class BasePyPIRCCommandTestCase(support.TempdirManager):
+ pass
class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase):
-
def test_server_registration(self):
# This test makes sure PyPIRCCommand knows how to:
# 1. handle several sections in .pypirc
@@ -93,30 +63,38 @@ class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase):
config = cmd._read_pypirc()
config = list(sorted(config.items()))
- waited = [('password', 'secret'), ('realm', 'pypi'),
- ('repository', 'https://upload.pypi.org/legacy/'),
- ('server', 'server1'), ('username', 'me')]
- self.assertEqual(config, waited)
+ waited = [
+ ('password', 'secret'),
+ ('realm', 'pypi'),
+ ('repository', 'https://upload.pypi.org/legacy/'),
+ ('server', 'server1'),
+ ('username', 'me'),
+ ]
+ assert config == waited
# old format
self.write_file(self.rc, PYPIRC_OLD)
config = cmd._read_pypirc()
config = list(sorted(config.items()))
- waited = [('password', 'secret'), ('realm', 'pypi'),
- ('repository', 'https://upload.pypi.org/legacy/'),
- ('server', 'server-login'), ('username', 'tarek')]
- self.assertEqual(config, waited)
+ waited = [
+ ('password', 'secret'),
+ ('realm', 'pypi'),
+ ('repository', 'https://upload.pypi.org/legacy/'),
+ ('server', 'server-login'),
+ ('username', 'tarek'),
+ ]
+ assert config == waited
def test_server_empty_registration(self):
cmd = self._cmd(self.dist)
rc = cmd._get_rc_file()
- self.assertFalse(os.path.exists(rc))
+ assert not os.path.exists(rc)
cmd._store_pypirc('tarek', 'xxx')
- self.assertTrue(os.path.exists(rc))
+ assert os.path.exists(rc)
f = open(rc)
try:
content = f.read()
- self.assertEqual(content, WANTED)
+ assert content == WANTED
finally:
f.close()
@@ -128,14 +106,11 @@ class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase):
config = cmd._read_pypirc()
config = list(sorted(config.items()))
- waited = [('password', 'yh^%#rest-of-my-password'), ('realm', 'pypi'),
- ('repository', 'https://upload.pypi.org/legacy/'),
- ('server', 'server3'), ('username', 'cbiggles')]
- self.assertEqual(config, waited)
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(PyPIRCCommandTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ waited = [
+ ('password', 'yh^%#rest-of-my-password'),
+ ('realm', 'pypi'),
+ ('repository', 'https://upload.pypi.org/legacy/'),
+ ('server', 'server3'),
+ ('username', 'cbiggles'),
+ ]
+ assert config == waited
diff --git a/setuptools/_distutils/tests/test_config_cmd.py b/setuptools/_distutils/tests/test_config_cmd.py
index 2c84719a..e72a7c5f 100644
--- a/setuptools/_distutils/tests/test_config_cmd.py
+++ b/setuptools/_distutils/tests/test_config_cmd.py
@@ -1,33 +1,28 @@
"""Tests for distutils.command.config."""
-import unittest
import os
import sys
-from test.support import run_unittest
+from test.support import missing_compiler_executable
-from .py35compat import missing_compiler_executable
+import pytest
from distutils.command.config import dump_file, config
from distutils.tests import support
-from distutils import log
+from distutils._log import log
-class ConfigTestCase(support.LoggingSilencer,
- support.TempdirManager,
- unittest.TestCase):
+@pytest.fixture(autouse=True)
+def info_log(request, monkeypatch):
+ self = request.instance
+ self._logs = []
+ monkeypatch.setattr(log, 'info', self._info)
+
+
+@support.combine_markers
+class TestConfig(support.TempdirManager):
def _info(self, msg, *args):
for line in msg.splitlines():
self._logs.append(line)
- def setUp(self):
- super(ConfigTestCase, self).setUp()
- self._logs = []
- self.old_log = log.info
- log.info = self._info
-
- def tearDown(self):
- log.info = self.old_log
- super(ConfigTestCase, self).tearDown()
-
def test_dump_file(self):
this_file = os.path.splitext(__file__)[0] + '.py'
f = open(this_file)
@@ -37,9 +32,9 @@ class ConfigTestCase(support.LoggingSilencer,
f.close()
dump_file(this_file, 'I am the header')
- self.assertEqual(len(self._logs), numlines+1)
+ assert len(self._logs) == numlines + 1
- @unittest.skipIf(sys.platform == 'win32', "can't test on Windows")
+ @pytest.mark.skipif('platform.system() == "Windows"')
def test_search_cpp(self):
cmd = missing_compiler_executable(['preprocessor'])
if cmd is not None:
@@ -49,14 +44,16 @@ class ConfigTestCase(support.LoggingSilencer,
cmd._check_compiler()
compiler = cmd.compiler
if sys.platform[:3] == "aix" and "xlc" in compiler.preprocessor[0].lower():
- self.skipTest('xlc: The -E option overrides the -P, -o, and -qsyntaxonly options')
+ self.skipTest(
+ 'xlc: The -E option overrides the -P, -o, and -qsyntaxonly options'
+ )
# simple pattern searches
match = cmd.search_cpp(pattern='xxx', body='/* xxx */')
- self.assertEqual(match, 0)
+ assert match == 0
match = cmd.search_cpp(pattern='_configtest', body='/* xxx */')
- self.assertEqual(match, 1)
+ assert match == 1
def test_finalize_options(self):
# finalize_options does a bit of transformation
@@ -68,9 +65,9 @@ class ConfigTestCase(support.LoggingSilencer,
cmd.library_dirs = 'three%sfour' % os.pathsep
cmd.ensure_finalized()
- self.assertEqual(cmd.include_dirs, ['one', 'two'])
- self.assertEqual(cmd.libraries, ['one'])
- self.assertEqual(cmd.library_dirs, ['three', 'four'])
+ assert cmd.include_dirs == ['one', 'two']
+ assert cmd.libraries == ['one']
+ assert cmd.library_dirs == ['three', 'four']
def test_clean(self):
# _clean removes files
@@ -82,17 +79,11 @@ class ConfigTestCase(support.LoggingSilencer,
self.write_file(f2, 'xxx')
for f in (f1, f2):
- self.assertTrue(os.path.exists(f))
+ assert os.path.exists(f)
pkg_dir, dist = self.create_dist()
cmd = config(dist)
cmd._clean(f1, f2)
for f in (f1, f2):
- self.assertFalse(os.path.exists(f))
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(ConfigTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert not os.path.exists(f)
diff --git a/setuptools/_distutils/tests/test_core.py b/setuptools/_distutils/tests/test_core.py
index 7270d699..2c11ff76 100644
--- a/setuptools/_distutils/tests/test_core.py
+++ b/setuptools/_distutils/tests/test_core.py
@@ -3,13 +3,10 @@
import io
import distutils.core
import os
-import shutil
import sys
-from test.support import captured_stdout, run_unittest
-from . import py38compat as os_helper
-import unittest
-from distutils.tests import support
-from distutils import log
+
+import pytest
+
from distutils.dist import Distribution
# setup script that uses __file__
@@ -56,110 +53,79 @@ if __name__ == "__main__":
main()
"""
-class CoreTestCase(support.EnvironGuard, unittest.TestCase):
-
- def setUp(self):
- super(CoreTestCase, self).setUp()
- self.old_stdout = sys.stdout
- self.cleanup_testfn()
- self.old_argv = sys.argv, sys.argv[:]
- self.addCleanup(log.set_threshold, log._global_log.threshold)
-
- def tearDown(self):
- sys.stdout = self.old_stdout
- self.cleanup_testfn()
- sys.argv = self.old_argv[0]
- sys.argv[:] = self.old_argv[1]
- super(CoreTestCase, self).tearDown()
-
- def cleanup_testfn(self):
- path = os_helper.TESTFN
- if os.path.isfile(path):
- os.remove(path)
- elif os.path.isdir(path):
- shutil.rmtree(path)
-
- def write_setup(self, text, path=os_helper.TESTFN):
- f = open(path, "w")
- try:
- f.write(text)
- finally:
- f.close()
- return path
-
- def test_run_setup_provides_file(self):
+
+@pytest.fixture(autouse=True)
+def save_stdout(monkeypatch):
+ monkeypatch.setattr(sys, 'stdout', sys.stdout)
+
+
+@pytest.fixture
+def temp_file(tmp_path):
+ return tmp_path / 'file'
+
+
+@pytest.mark.usefixtures('save_env')
+@pytest.mark.usefixtures('save_argv')
+class TestCore:
+ def test_run_setup_provides_file(self, temp_file):
# Make sure the script can use __file__; if that's missing, the test
# setup.py script will raise NameError.
- distutils.core.run_setup(
- self.write_setup(setup_using___file__))
+ temp_file.write_text(setup_using___file__)
+ distutils.core.run_setup(temp_file)
- def test_run_setup_preserves_sys_argv(self):
+ def test_run_setup_preserves_sys_argv(self, temp_file):
# Make sure run_setup does not clobber sys.argv
argv_copy = sys.argv.copy()
- distutils.core.run_setup(
- self.write_setup(setup_does_nothing))
- self.assertEqual(sys.argv, argv_copy)
+ temp_file.write_text(setup_does_nothing)
+ distutils.core.run_setup(temp_file)
+ assert sys.argv == argv_copy
- def test_run_setup_defines_subclass(self):
+ def test_run_setup_defines_subclass(self, temp_file):
# Make sure the script can use __file__; if that's missing, the test
# setup.py script will raise NameError.
- dist = distutils.core.run_setup(
- self.write_setup(setup_defines_subclass))
+ temp_file.write_text(setup_defines_subclass)
+ dist = distutils.core.run_setup(temp_file)
install = dist.get_command_obj('install')
- self.assertIn('cmd', install.sub_commands)
+ assert 'cmd' in install.sub_commands
- def test_run_setup_uses_current_dir(self):
- # This tests that the setup script is run with the current directory
- # as its own current directory; this was temporarily broken by a
- # previous patch when TESTFN did not use the current directory.
+ def test_run_setup_uses_current_dir(self, tmp_path):
+ """
+ Test that the setup script is run with the current directory
+ as its own current directory.
+ """
sys.stdout = io.StringIO()
cwd = os.getcwd()
# Create a directory and write the setup.py file there:
- os.mkdir(os_helper.TESTFN)
- setup_py = os.path.join(os_helper.TESTFN, "setup.py")
- distutils.core.run_setup(
- self.write_setup(setup_prints_cwd, path=setup_py))
+ setup_py = tmp_path / 'setup.py'
+ setup_py.write_text(setup_prints_cwd)
+ distutils.core.run_setup(setup_py)
output = sys.stdout.getvalue()
if output.endswith("\n"):
output = output[:-1]
- self.assertEqual(cwd, output)
+ assert cwd == output
- def test_run_setup_within_if_main(self):
- dist = distutils.core.run_setup(
- self.write_setup(setup_within_if_main), stop_after="config")
- self.assertIsInstance(dist, Distribution)
- self.assertEqual(dist.get_name(), "setup_within_if_main")
+ def test_run_setup_within_if_main(self, temp_file):
+ temp_file.write_text(setup_within_if_main)
+ dist = distutils.core.run_setup(temp_file, stop_after="config")
+ assert isinstance(dist, Distribution)
+ assert dist.get_name() == "setup_within_if_main"
- def test_run_commands(self):
+ def test_run_commands(self, temp_file):
sys.argv = ['setup.py', 'build']
- dist = distutils.core.run_setup(
- self.write_setup(setup_within_if_main), stop_after="commandline")
- self.assertNotIn('build', dist.have_run)
+ temp_file.write_text(setup_within_if_main)
+ dist = distutils.core.run_setup(temp_file, stop_after="commandline")
+ assert 'build' not in dist.have_run
distutils.core.run_commands(dist)
- self.assertIn('build', dist.have_run)
+ assert 'build' in dist.have_run
- def test_debug_mode(self):
+ def test_debug_mode(self, capsys, monkeypatch):
# this covers the code called when DEBUG is set
sys.argv = ['setup.py', '--name']
- with captured_stdout() as stdout:
- distutils.core.setup(name='bar')
- stdout.seek(0)
- self.assertEqual(stdout.read(), 'bar\n')
-
- distutils.core.DEBUG = True
- try:
- with captured_stdout() as stdout:
- distutils.core.setup(name='bar')
- finally:
- distutils.core.DEBUG = False
- stdout.seek(0)
+ distutils.core.setup(name='bar')
+ capsys.readouterr().out == 'bar\n'
+ monkeypatch.setattr(distutils.core, 'DEBUG', True)
+ distutils.core.setup(name='bar')
wanted = "options (after parsing config files):\n"
- self.assertEqual(stdout.readlines()[0], wanted)
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(CoreTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert capsys.readouterr().out.startswith(wanted)
diff --git a/setuptools/_distutils/tests/test_cygwinccompiler.py b/setuptools/_distutils/tests/test_cygwinccompiler.py
index 8715a535..ef01ae21 100644
--- a/setuptools/_distutils/tests/test_cygwinccompiler.py
+++ b/setuptools/_distutils/tests/test_cygwinccompiler.py
@@ -1,96 +1,118 @@
"""Tests for distutils.cygwinccompiler."""
-import unittest
import sys
import os
-from test.support import run_unittest
-from distutils.cygwinccompiler import (check_config_h,
- CONFIG_H_OK, CONFIG_H_NOTOK,
- CONFIG_H_UNCERTAIN,
- get_msvcr)
-from distutils.tests import support
+import pytest
+from distutils.cygwinccompiler import (
+ check_config_h,
+ CONFIG_H_OK,
+ CONFIG_H_NOTOK,
+ CONFIG_H_UNCERTAIN,
+ get_msvcr,
+)
+from distutils.tests import support
+from distutils import sysconfig
-class CygwinCCompilerTestCase(support.TempdirManager,
- unittest.TestCase):
- def setUp(self):
- super(CygwinCCompilerTestCase, self).setUp()
- self.version = sys.version
- self.python_h = os.path.join(self.mkdtemp(), 'python.h')
- from distutils import sysconfig
- self.old_get_config_h_filename = sysconfig.get_config_h_filename
- sysconfig.get_config_h_filename = self._get_config_h_filename
+@pytest.fixture(autouse=True)
+def stuff(request, monkeypatch, distutils_managed_tempdir):
+ self = request.instance
+ self.python_h = os.path.join(self.mkdtemp(), 'python.h')
+ monkeypatch.setattr(sysconfig, 'get_config_h_filename', self._get_config_h_filename)
+ monkeypatch.setattr(sys, 'version', sys.version)
- def tearDown(self):
- sys.version = self.version
- from distutils import sysconfig
- sysconfig.get_config_h_filename = self.old_get_config_h_filename
- super(CygwinCCompilerTestCase, self).tearDown()
+class TestCygwinCCompiler(support.TempdirManager):
def _get_config_h_filename(self):
return self.python_h
+ @pytest.mark.skipif('sys.platform != "cygwin"')
+ @pytest.mark.skipif('not os.path.exists("/usr/lib/libbash.dll.a")')
+ def test_find_library_file(self):
+ from distutils.cygwinccompiler import CygwinCCompiler
+
+ compiler = CygwinCCompiler()
+ link_name = "bash"
+ linkable_file = compiler.find_library_file(["/usr/lib"], link_name)
+ assert linkable_file is not None
+ assert os.path.exists(linkable_file)
+ assert linkable_file == f"/usr/lib/lib{link_name:s}.dll.a"
+
+ @pytest.mark.skipif('sys.platform != "cygwin"')
+ def test_runtime_library_dir_option(self):
+ from distutils.cygwinccompiler import CygwinCCompiler
+
+ compiler = CygwinCCompiler()
+ assert compiler.runtime_library_dir_option('/foo') == []
+
def test_check_config_h(self):
# check_config_h looks for "GCC" in sys.version first
# returns CONFIG_H_OK if found
- sys.version = ('2.6.1 (r261:67515, Dec 6 2008, 16:42:21) \n[GCC '
- '4.0.1 (Apple Computer, Inc. build 5370)]')
+ sys.version = (
+ '2.6.1 (r261:67515, Dec 6 2008, 16:42:21) \n[GCC '
+ '4.0.1 (Apple Computer, Inc. build 5370)]'
+ )
- self.assertEqual(check_config_h()[0], CONFIG_H_OK)
+ assert check_config_h()[0] == CONFIG_H_OK
# then it tries to see if it can find "__GNUC__" in pyconfig.h
sys.version = 'something without the *CC word'
# if the file doesn't exist it returns CONFIG_H_UNCERTAIN
- self.assertEqual(check_config_h()[0], CONFIG_H_UNCERTAIN)
+ assert check_config_h()[0] == CONFIG_H_UNCERTAIN
# if it exists but does not contain __GNUC__, it returns CONFIG_H_NOTOK
self.write_file(self.python_h, 'xxx')
- self.assertEqual(check_config_h()[0], CONFIG_H_NOTOK)
+ assert check_config_h()[0] == CONFIG_H_NOTOK
# and CONFIG_H_OK if __GNUC__ is found
self.write_file(self.python_h, 'xxx __GNUC__ xxx')
- self.assertEqual(check_config_h()[0], CONFIG_H_OK)
+ assert check_config_h()[0] == CONFIG_H_OK
def test_get_msvcr(self):
# none
- sys.version = ('2.6.1 (r261:67515, Dec 6 2008, 16:42:21) '
- '\n[GCC 4.0.1 (Apple Computer, Inc. build 5370)]')
- self.assertEqual(get_msvcr(), None)
+ sys.version = (
+ '2.6.1 (r261:67515, Dec 6 2008, 16:42:21) '
+ '\n[GCC 4.0.1 (Apple Computer, Inc. build 5370)]'
+ )
+ assert get_msvcr() is None
# MSVC 7.0
- sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
- '[MSC v.1300 32 bits (Intel)]')
- self.assertEqual(get_msvcr(), ['msvcr70'])
+ sys.version = (
+ '2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' '[MSC v.1300 32 bits (Intel)]'
+ )
+ assert get_msvcr() == ['msvcr70']
# MSVC 7.1
- sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
- '[MSC v.1310 32 bits (Intel)]')
- self.assertEqual(get_msvcr(), ['msvcr71'])
+ sys.version = (
+ '2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' '[MSC v.1310 32 bits (Intel)]'
+ )
+ assert get_msvcr() == ['msvcr71']
# VS2005 / MSVC 8.0
- sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
- '[MSC v.1400 32 bits (Intel)]')
- self.assertEqual(get_msvcr(), ['msvcr80'])
+ sys.version = (
+ '2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' '[MSC v.1400 32 bits (Intel)]'
+ )
+ assert get_msvcr() == ['msvcr80']
# VS2008 / MSVC 9.0
- sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
- '[MSC v.1500 32 bits (Intel)]')
- self.assertEqual(get_msvcr(), ['msvcr90'])
-
- sys.version = '3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 18:46:30) [MSC v.1929 32 bit (Intel)]'
- self.assertEqual(get_msvcr(), ['ucrt', 'vcruntime140'])
-
- # unknown
- sys.version = ('2.5.1 (r251:54863, Apr 18 2007, 08:51:08) '
- '[MSC v.2000 32 bits (Intel)]')
- self.assertRaises(ValueError, get_msvcr)
+ sys.version = (
+ '2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' '[MSC v.1500 32 bits (Intel)]'
+ )
+ assert get_msvcr() == ['msvcr90']
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(CygwinCCompilerTestCase)
+ sys.version = (
+ '3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 18:46:30) '
+ '[MSC v.1929 32 bit (Intel)]'
+ )
+ assert get_msvcr() == ['ucrt', 'vcruntime140']
-if __name__ == '__main__':
- run_unittest(test_suite())
+ # unknown
+ sys.version = (
+ '2.5.1 (r251:54863, Apr 18 2007, 08:51:08) ' '[MSC v.2000 32 bits (Intel)]'
+ )
+ with pytest.raises(ValueError):
+ get_msvcr()
diff --git a/setuptools/_distutils/tests/test_dep_util.py b/setuptools/_distutils/tests/test_dep_util.py
index 0d52740a..2dcce1dd 100644
--- a/setuptools/_distutils/tests/test_dep_util.py
+++ b/setuptools/_distutils/tests/test_dep_util.py
@@ -1,14 +1,13 @@
"""Tests for distutils.dep_util."""
-import unittest
import os
from distutils.dep_util import newer, newer_pairwise, newer_group
from distutils.errors import DistutilsFileError
from distutils.tests import support
-from test.support import run_unittest
+import pytest
-class DepUtilTestCase(support.TempdirManager, unittest.TestCase):
+class TestDepUtil(support.TempdirManager):
def test_newer(self):
tmpdir = self.mkdtemp()
@@ -16,17 +15,18 @@ class DepUtilTestCase(support.TempdirManager, unittest.TestCase):
old_file = os.path.abspath(__file__)
# Raise DistutilsFileError if 'new_file' does not exist.
- self.assertRaises(DistutilsFileError, newer, new_file, old_file)
+ with pytest.raises(DistutilsFileError):
+ newer(new_file, old_file)
# Return true if 'new_file' exists and is more recently modified than
# 'old_file', or if 'new_file' exists and 'old_file' doesn't.
self.write_file(new_file)
- self.assertTrue(newer(new_file, 'I_dont_exist'))
- self.assertTrue(newer(new_file, old_file))
+ assert newer(new_file, 'I_dont_exist')
+ assert newer(new_file, old_file)
# Return false if both exist and 'old_file' is the same age or younger
# than 'new_file'.
- self.assertFalse(newer(old_file, new_file))
+ assert not newer(old_file, new_file)
def test_newer_pairwise(self):
tmpdir = self.mkdtemp()
@@ -36,14 +36,13 @@ class DepUtilTestCase(support.TempdirManager, unittest.TestCase):
os.mkdir(targets)
one = os.path.join(sources, 'one')
two = os.path.join(sources, 'two')
- three = os.path.abspath(__file__) # I am the old file
+ three = os.path.abspath(__file__) # I am the old file
four = os.path.join(targets, 'four')
self.write_file(one)
self.write_file(two)
self.write_file(four)
- self.assertEqual(newer_pairwise([one, two], [three, four]),
- ([one],[three]))
+ assert newer_pairwise([one, two], [three, four]) == ([one], [three])
def test_newer_group(self):
tmpdir = self.mkdtemp()
@@ -59,22 +58,14 @@ class DepUtilTestCase(support.TempdirManager, unittest.TestCase):
self.write_file(one)
self.write_file(two)
self.write_file(three)
- self.assertTrue(newer_group([one, two, three], old_file))
- self.assertFalse(newer_group([one, two, old_file], three))
+ assert newer_group([one, two, three], old_file)
+ assert not newer_group([one, two, old_file], three)
# missing handling
os.remove(one)
- self.assertRaises(OSError, newer_group, [one, two, old_file], three)
+ with pytest.raises(OSError):
+ newer_group([one, two, old_file], three)
- self.assertFalse(newer_group([one, two, old_file], three,
- missing='ignore'))
+ assert not newer_group([one, two, old_file], three, missing='ignore')
- self.assertTrue(newer_group([one, two, old_file], three,
- missing='newer'))
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(DepUtilTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert newer_group([one, two, old_file], three, missing='newer')
diff --git a/setuptools/_distutils/tests/test_dir_util.py b/setuptools/_distutils/tests/test_dir_util.py
index 1b1f3bbb..0c6db4af 100644
--- a/setuptools/_distutils/tests/test_dir_util.py
+++ b/setuptools/_distutils/tests/test_dir_util.py
@@ -1,89 +1,73 @@
"""Tests for distutils.dir_util."""
-import unittest
import os
import stat
-import sys
-from unittest.mock import patch
+import unittest.mock as mock
from distutils import dir_util, errors
-from distutils.dir_util import (mkpath, remove_tree, create_tree, copy_tree,
- ensure_relative)
+from distutils.dir_util import (
+ mkpath,
+ remove_tree,
+ create_tree,
+ copy_tree,
+ ensure_relative,
+)
-from distutils import log
from distutils.tests import support
-from test.support import run_unittest
+import pytest
-class DirUtilTestCase(support.TempdirManager, unittest.TestCase):
+@pytest.fixture(autouse=True)
+def stuff(request, monkeypatch, distutils_managed_tempdir):
+ self = request.instance
+ tmp_dir = self.mkdtemp()
+ self.root_target = os.path.join(tmp_dir, 'deep')
+ self.target = os.path.join(self.root_target, 'here')
+ self.target2 = os.path.join(tmp_dir, 'deep2')
- def _log(self, msg, *args):
- if len(args) > 0:
- self._logs.append(msg % args)
- else:
- self._logs.append(msg)
-
- def setUp(self):
- super(DirUtilTestCase, self).setUp()
- self._logs = []
- tmp_dir = self.mkdtemp()
- self.root_target = os.path.join(tmp_dir, 'deep')
- self.target = os.path.join(self.root_target, 'here')
- self.target2 = os.path.join(tmp_dir, 'deep2')
- self.old_log = log.info
- log.info = self._log
-
- def tearDown(self):
- log.info = self.old_log
- super(DirUtilTestCase, self).tearDown()
-
- def test_mkpath_remove_tree_verbosity(self):
+class TestDirUtil(support.TempdirManager):
+ def test_mkpath_remove_tree_verbosity(self, caplog):
mkpath(self.target, verbose=0)
- wanted = []
- self.assertEqual(self._logs, wanted)
+ assert not caplog.records
remove_tree(self.root_target, verbose=0)
mkpath(self.target, verbose=1)
- wanted = ['creating %s' % self.root_target,
- 'creating %s' % self.target]
- self.assertEqual(self._logs, wanted)
- self._logs = []
+ wanted = ['creating %s' % self.root_target, 'creating %s' % self.target]
+ assert caplog.messages == wanted
+ caplog.clear()
remove_tree(self.root_target, verbose=1)
wanted = ["removing '%s' (and everything under it)" % self.root_target]
- self.assertEqual(self._logs, wanted)
+ assert caplog.messages == wanted
- @unittest.skipIf(sys.platform.startswith('win'),
- "This test is only appropriate for POSIX-like systems.")
+ @pytest.mark.skipif("platform.system() == 'Windows'")
def test_mkpath_with_custom_mode(self):
# Get and set the current umask value for testing mode bits.
umask = os.umask(0o002)
os.umask(umask)
mkpath(self.target, 0o700)
- self.assertEqual(
- stat.S_IMODE(os.stat(self.target).st_mode), 0o700 & ~umask)
+ assert stat.S_IMODE(os.stat(self.target).st_mode) == 0o700 & ~umask
mkpath(self.target2, 0o555)
- self.assertEqual(
- stat.S_IMODE(os.stat(self.target2).st_mode), 0o555 & ~umask)
+ assert stat.S_IMODE(os.stat(self.target2).st_mode) == 0o555 & ~umask
- def test_create_tree_verbosity(self):
+ def test_create_tree_verbosity(self, caplog):
create_tree(self.root_target, ['one', 'two', 'three'], verbose=0)
- self.assertEqual(self._logs, [])
+ assert caplog.messages == []
remove_tree(self.root_target, verbose=0)
wanted = ['creating %s' % self.root_target]
create_tree(self.root_target, ['one', 'two', 'three'], verbose=1)
- self.assertEqual(self._logs, wanted)
+ assert caplog.messages == wanted
remove_tree(self.root_target, verbose=0)
- def test_copy_tree_verbosity(self):
+ def test_copy_tree_verbosity(self, caplog):
mkpath(self.target, verbose=0)
copy_tree(self.target, self.target2, verbose=0)
- self.assertEqual(self._logs, [])
+ assert caplog.messages == []
remove_tree(self.root_target, verbose=0)
@@ -92,9 +76,9 @@ class DirUtilTestCase(support.TempdirManager, unittest.TestCase):
with open(a_file, 'w') as f:
f.write('some content')
- wanted = ['copying %s -> %s' % (a_file, self.target2)]
+ wanted = ['copying {} -> {}'.format(a_file, self.target2)]
copy_tree(self.target, self.target2, verbose=1)
- self.assertEqual(self._logs, wanted)
+ assert caplog.messages == wanted
remove_tree(self.root_target, verbose=0)
remove_tree(self.target2, verbose=0)
@@ -109,31 +93,25 @@ class DirUtilTestCase(support.TempdirManager, unittest.TestCase):
fh.write('some content')
copy_tree(self.target, self.target2)
- self.assertEqual(os.listdir(self.target2), ['ok.txt'])
+ assert os.listdir(self.target2) == ['ok.txt']
remove_tree(self.root_target, verbose=0)
remove_tree(self.target2, verbose=0)
def test_ensure_relative(self):
if os.sep == '/':
- self.assertEqual(ensure_relative('/home/foo'), 'home/foo')
- self.assertEqual(ensure_relative('some/path'), 'some/path')
- else: # \\
- self.assertEqual(ensure_relative('c:\\home\\foo'), 'c:home\\foo')
- self.assertEqual(ensure_relative('home\\foo'), 'home\\foo')
+ assert ensure_relative('/home/foo') == 'home/foo'
+ assert ensure_relative('some/path') == 'some/path'
+ else: # \\
+ assert ensure_relative('c:\\home\\foo') == 'c:home\\foo'
+ assert ensure_relative('home\\foo') == 'home\\foo'
def test_copy_tree_exception_in_listdir(self):
"""
An exception in listdir should raise a DistutilsFileError
"""
- with patch("os.listdir", side_effect=OSError()), \
- self.assertRaises(errors.DistutilsFileError):
+ with mock.patch("os.listdir", side_effect=OSError()), pytest.raises(
+ errors.DistutilsFileError
+ ):
src = self.tempdirs[-1]
dir_util.copy_tree(src, None)
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(DirUtilTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
diff --git a/setuptools/_distutils/tests/test_dist.py b/setuptools/_distutils/tests/test_dist.py
index 36155be1..b5e81d03 100644
--- a/setuptools/_distutils/tests/test_dist.py
+++ b/setuptools/_distutils/tests/test_dist.py
@@ -2,21 +2,21 @@
import os
import io
import sys
-import unittest
import warnings
import textwrap
+import functools
+import unittest.mock as mock
-from unittest import mock
+import pytest
+import jaraco.path
from distutils.dist import Distribution, fix_help_options
from distutils.cmd import Command
-from test.support import (
- captured_stdout, captured_stderr, run_unittest
-)
-from .py38compat import TESTFN
from distutils.tests import support
-from distutils import log
+
+
+pydistutils_cfg = '.' * (os.name == 'posix') + 'pydistutils.cfg'
class test_dist(Command):
@@ -42,21 +42,15 @@ class TestDistribution(Distribution):
return self._config_files
-class DistributionTestCase(support.LoggingSilencer,
- support.TempdirManager,
- support.EnvironGuard,
- unittest.TestCase):
+@pytest.fixture
+def clear_argv():
+ del sys.argv[1:]
- def setUp(self):
- super(DistributionTestCase, self).setUp()
- self.argv = sys.argv, sys.argv[:]
- del sys.argv[1:]
-
- def tearDown(self):
- sys.argv = self.argv[0]
- sys.argv[:] = self.argv[1]
- super(DistributionTestCase, self).tearDown()
+@support.combine_markers
+@pytest.mark.usefixtures('save_env')
+@pytest.mark.usefixtures('save_argv')
+class TestDistributionBehavior(support.TempdirManager):
def create_distribution(self, configfiles=()):
d = TestDistribution()
d._config_files = configfiles
@@ -64,57 +58,69 @@ class DistributionTestCase(support.LoggingSilencer,
d.parse_command_line()
return d
- def test_command_packages_unspecified(self):
+ def test_command_packages_unspecified(self, clear_argv):
sys.argv.append("build")
d = self.create_distribution()
- self.assertEqual(d.get_command_packages(), ["distutils.command"])
+ assert d.get_command_packages() == ["distutils.command"]
- def test_command_packages_cmdline(self):
+ def test_command_packages_cmdline(self, clear_argv):
from distutils.tests.test_dist import test_dist
- sys.argv.extend(["--command-packages",
- "foo.bar,distutils.tests",
- "test_dist",
- "-Ssometext",
- ])
+
+ sys.argv.extend(
+ [
+ "--command-packages",
+ "foo.bar,distutils.tests",
+ "test_dist",
+ "-Ssometext",
+ ]
+ )
d = self.create_distribution()
# let's actually try to load our test command:
- self.assertEqual(d.get_command_packages(),
- ["distutils.command", "foo.bar", "distutils.tests"])
+ assert d.get_command_packages() == [
+ "distutils.command",
+ "foo.bar",
+ "distutils.tests",
+ ]
cmd = d.get_command_obj("test_dist")
- self.assertIsInstance(cmd, test_dist)
- self.assertEqual(cmd.sample_option, "sometext")
+ assert isinstance(cmd, test_dist)
+ assert cmd.sample_option == "sometext"
- @unittest.skipIf(
+ @pytest.mark.skipif(
'distutils' not in Distribution.parse_config_files.__module__,
- 'Cannot test when virtualenv has monkey-patched Distribution.',
+ reason='Cannot test when virtualenv has monkey-patched Distribution',
)
- def test_venv_install_options(self):
+ def test_venv_install_options(self, tmp_path):
sys.argv.append("install")
- self.addCleanup(os.unlink, TESTFN)
+ file = str(tmp_path / 'file')
fakepath = '/somedir'
- with open(TESTFN, "w") as f:
- print(("[install]\n"
- "install-base = {0}\n"
- "install-platbase = {0}\n"
- "install-lib = {0}\n"
- "install-platlib = {0}\n"
- "install-purelib = {0}\n"
- "install-headers = {0}\n"
- "install-scripts = {0}\n"
- "install-data = {0}\n"
- "prefix = {0}\n"
- "exec-prefix = {0}\n"
- "home = {0}\n"
- "user = {0}\n"
- "root = {0}").format(fakepath), file=f)
+ jaraco.path.build(
+ {
+ file: f"""
+ [install]
+ install-base = {fakepath}
+ install-platbase = {fakepath}
+ install-lib = {fakepath}
+ install-platlib = {fakepath}
+ install-purelib = {fakepath}
+ install-headers = {fakepath}
+ install-scripts = {fakepath}
+ install-data = {fakepath}
+ prefix = {fakepath}
+ exec-prefix = {fakepath}
+ home = {fakepath}
+ user = {fakepath}
+ root = {fakepath}
+ """,
+ }
+ )
# Base case: Not in a Virtual Environment
- with mock.patch.multiple(sys, prefix='/a', base_prefix='/a') as values:
- d = self.create_distribution([TESTFN])
+ with mock.patch.multiple(sys, prefix='/a', base_prefix='/a'):
+ d = self.create_distribution([file])
- option_tuple = (TESTFN, fakepath)
+ option_tuple = (file, fakepath)
result_dict = {
'install_base': option_tuple,
@@ -132,47 +138,47 @@ class DistributionTestCase(support.LoggingSilencer,
'root': option_tuple,
}
- self.assertEqual(
- sorted(d.command_options.get('install').keys()),
- sorted(result_dict.keys()))
+ assert sorted(d.command_options.get('install').keys()) == sorted(
+ result_dict.keys()
+ )
for (key, value) in d.command_options.get('install').items():
- self.assertEqual(value, result_dict[key])
+ assert value == result_dict[key]
# Test case: In a Virtual Environment
- with mock.patch.multiple(sys, prefix='/a', base_prefix='/b') as values:
- d = self.create_distribution([TESTFN])
+ with mock.patch.multiple(sys, prefix='/a', base_prefix='/b'):
+ d = self.create_distribution([file])
for key in result_dict.keys():
- self.assertNotIn(key, d.command_options.get('install', {}))
+ assert key not in d.command_options.get('install', {})
- def test_command_packages_configfile(self):
+ def test_command_packages_configfile(self, tmp_path, clear_argv):
sys.argv.append("build")
- self.addCleanup(os.unlink, TESTFN)
- f = open(TESTFN, "w")
- try:
- print("[global]", file=f)
- print("command_packages = foo.bar, splat", file=f)
- finally:
- f.close()
-
- d = self.create_distribution([TESTFN])
- self.assertEqual(d.get_command_packages(),
- ["distutils.command", "foo.bar", "splat"])
+ file = str(tmp_path / "file")
+ jaraco.path.build(
+ {
+ file: """
+ [global]
+ command_packages = foo.bar, splat
+ """,
+ }
+ )
+
+ d = self.create_distribution([file])
+ assert d.get_command_packages() == ["distutils.command", "foo.bar", "splat"]
# ensure command line overrides config:
sys.argv[1:] = ["--command-packages", "spork", "build"]
- d = self.create_distribution([TESTFN])
- self.assertEqual(d.get_command_packages(),
- ["distutils.command", "spork"])
+ d = self.create_distribution([file])
+ assert d.get_command_packages() == ["distutils.command", "spork"]
# Setting --command-packages to '' should cause the default to
# be used even if a config file specified something else:
sys.argv[1:] = ["--command-packages", "", "build"]
- d = self.create_distribution([TESTFN])
- self.assertEqual(d.get_command_packages(), ["distutils.command"])
+ d = self.create_distribution([file])
+ assert d.get_command_packages() == ["distutils.command"]
- def test_empty_options(self):
+ def test_empty_options(self, request):
# an empty options dictionary should not stay in the
# list of attributes
@@ -182,328 +188,310 @@ class DistributionTestCase(support.LoggingSilencer,
def _warn(msg):
warns.append(msg)
- self.addCleanup(setattr, warnings, 'warn', warnings.warn)
+ request.addfinalizer(
+ functools.partial(setattr, warnings, 'warn', warnings.warn)
+ )
warnings.warn = _warn
- dist = Distribution(attrs={'author': 'xxx', 'name': 'xxx',
- 'version': 'xxx', 'url': 'xxxx',
- 'options': {}})
-
- self.assertEqual(len(warns), 0)
- self.assertNotIn('options', dir(dist))
+ dist = Distribution(
+ attrs={
+ 'author': 'xxx',
+ 'name': 'xxx',
+ 'version': 'xxx',
+ 'url': 'xxxx',
+ 'options': {},
+ }
+ )
+
+ assert len(warns) == 0
+ assert 'options' not in dir(dist)
def test_finalize_options(self):
- attrs = {'keywords': 'one,two',
- 'platforms': 'one,two'}
+ attrs = {'keywords': 'one,two', 'platforms': 'one,two'}
dist = Distribution(attrs=attrs)
dist.finalize_options()
# finalize_option splits platforms and keywords
- self.assertEqual(dist.metadata.platforms, ['one', 'two'])
- self.assertEqual(dist.metadata.keywords, ['one', 'two'])
+ assert dist.metadata.platforms == ['one', 'two']
+ assert dist.metadata.keywords == ['one', 'two']
- attrs = {'keywords': 'foo bar',
- 'platforms': 'foo bar'}
+ attrs = {'keywords': 'foo bar', 'platforms': 'foo bar'}
dist = Distribution(attrs=attrs)
dist.finalize_options()
- self.assertEqual(dist.metadata.platforms, ['foo bar'])
- self.assertEqual(dist.metadata.keywords, ['foo bar'])
+ assert dist.metadata.platforms == ['foo bar']
+ assert dist.metadata.keywords == ['foo bar']
def test_get_command_packages(self):
dist = Distribution()
- self.assertEqual(dist.command_packages, None)
+ assert dist.command_packages is None
cmds = dist.get_command_packages()
- self.assertEqual(cmds, ['distutils.command'])
- self.assertEqual(dist.command_packages,
- ['distutils.command'])
+ assert cmds == ['distutils.command']
+ assert dist.command_packages == ['distutils.command']
dist.command_packages = 'one,two'
cmds = dist.get_command_packages()
- self.assertEqual(cmds, ['distutils.command', 'one', 'two'])
+ assert cmds == ['distutils.command', 'one', 'two']
def test_announce(self):
# make sure the level is known
dist = Distribution()
- args = ('ok',)
- kwargs = {'level': 'ok2'}
- self.assertRaises(ValueError, dist.announce, args, kwargs)
-
+ with pytest.raises(TypeError):
+ dist.announce('ok', level='ok2')
- def test_find_config_files_disable(self):
+ def test_find_config_files_disable(self, temp_home):
# Ticket #1180: Allow user to disable their home config file.
- temp_home = self.mkdtemp()
- if os.name == 'posix':
- user_filename = os.path.join(temp_home, ".pydistutils.cfg")
- else:
- user_filename = os.path.join(temp_home, "pydistutils.cfg")
-
- with open(user_filename, 'w') as f:
- f.write('[distutils]\n')
-
- def _expander(path):
- return temp_home
+ jaraco.path.build({pydistutils_cfg: '[distutils]\n'}, temp_home)
- old_expander = os.path.expanduser
- os.path.expanduser = _expander
- try:
- d = Distribution()
- all_files = d.find_config_files()
+ d = Distribution()
+ all_files = d.find_config_files()
- d = Distribution(attrs={'script_args': ['--no-user-cfg']})
- files = d.find_config_files()
- finally:
- os.path.expanduser = old_expander
+ d = Distribution(attrs={'script_args': ['--no-user-cfg']})
+ files = d.find_config_files()
# make sure --no-user-cfg disables the user cfg file
- self.assertEqual(len(all_files)-1, len(files))
-
-class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
- unittest.TestCase):
-
- def setUp(self):
- super(MetadataTestCase, self).setUp()
- self.argv = sys.argv, sys.argv[:]
-
- def tearDown(self):
- sys.argv = self.argv[0]
- sys.argv[:] = self.argv[1]
- super(MetadataTestCase, self).tearDown()
+ assert len(all_files) - 1 == len(files)
+ @pytest.mark.skipif(
+ 'platform.system() == "Windows"',
+ reason='Windows does not honor chmod 000',
+ )
+ def test_find_config_files_permission_error(self, fake_home):
+ """
+ Finding config files should not fail when directory is inaccessible.
+ """
+ fake_home.joinpath(pydistutils_cfg).write_text('')
+ fake_home.chmod(0o000)
+ Distribution().find_config_files()
+
+
+@pytest.mark.usefixtures('save_env')
+@pytest.mark.usefixtures('save_argv')
+class TestMetadata(support.TempdirManager):
def format_metadata(self, dist):
sio = io.StringIO()
dist.metadata.write_pkg_file(sio)
return sio.getvalue()
def test_simple_metadata(self):
- attrs = {"name": "package",
- "version": "1.0"}
+ attrs = {"name": "package", "version": "1.0"}
dist = Distribution(attrs)
meta = self.format_metadata(dist)
- self.assertIn("Metadata-Version: 1.0", meta)
- self.assertNotIn("provides:", meta.lower())
- self.assertNotIn("requires:", meta.lower())
- self.assertNotIn("obsoletes:", meta.lower())
+ assert "Metadata-Version: 1.0" in meta
+ assert "provides:" not in meta.lower()
+ assert "requires:" not in meta.lower()
+ assert "obsoletes:" not in meta.lower()
def test_provides(self):
- attrs = {"name": "package",
- "version": "1.0",
- "provides": ["package", "package.sub"]}
+ attrs = {
+ "name": "package",
+ "version": "1.0",
+ "provides": ["package", "package.sub"],
+ }
dist = Distribution(attrs)
- self.assertEqual(dist.metadata.get_provides(),
- ["package", "package.sub"])
- self.assertEqual(dist.get_provides(),
- ["package", "package.sub"])
+ assert dist.metadata.get_provides() == ["package", "package.sub"]
+ assert dist.get_provides() == ["package", "package.sub"]
meta = self.format_metadata(dist)
- self.assertIn("Metadata-Version: 1.1", meta)
- self.assertNotIn("requires:", meta.lower())
- self.assertNotIn("obsoletes:", meta.lower())
+ assert "Metadata-Version: 1.1" in meta
+ assert "requires:" not in meta.lower()
+ assert "obsoletes:" not in meta.lower()
def test_provides_illegal(self):
- self.assertRaises(ValueError, Distribution,
- {"name": "package",
- "version": "1.0",
- "provides": ["my.pkg (splat)"]})
+ with pytest.raises(ValueError):
+ Distribution(
+ {"name": "package", "version": "1.0", "provides": ["my.pkg (splat)"]},
+ )
def test_requires(self):
- attrs = {"name": "package",
- "version": "1.0",
- "requires": ["other", "another (==1.0)"]}
+ attrs = {
+ "name": "package",
+ "version": "1.0",
+ "requires": ["other", "another (==1.0)"],
+ }
dist = Distribution(attrs)
- self.assertEqual(dist.metadata.get_requires(),
- ["other", "another (==1.0)"])
- self.assertEqual(dist.get_requires(),
- ["other", "another (==1.0)"])
+ assert dist.metadata.get_requires() == ["other", "another (==1.0)"]
+ assert dist.get_requires() == ["other", "another (==1.0)"]
meta = self.format_metadata(dist)
- self.assertIn("Metadata-Version: 1.1", meta)
- self.assertNotIn("provides:", meta.lower())
- self.assertIn("Requires: other", meta)
- self.assertIn("Requires: another (==1.0)", meta)
- self.assertNotIn("obsoletes:", meta.lower())
+ assert "Metadata-Version: 1.1" in meta
+ assert "provides:" not in meta.lower()
+ assert "Requires: other" in meta
+ assert "Requires: another (==1.0)" in meta
+ assert "obsoletes:" not in meta.lower()
def test_requires_illegal(self):
- self.assertRaises(ValueError, Distribution,
- {"name": "package",
- "version": "1.0",
- "requires": ["my.pkg (splat)"]})
+ with pytest.raises(ValueError):
+ Distribution(
+ {"name": "package", "version": "1.0", "requires": ["my.pkg (splat)"]},
+ )
def test_requires_to_list(self):
- attrs = {"name": "package",
- "requires": iter(["other"])}
+ attrs = {"name": "package", "requires": iter(["other"])}
dist = Distribution(attrs)
- self.assertIsInstance(dist.metadata.requires, list)
-
+ assert isinstance(dist.metadata.requires, list)
def test_obsoletes(self):
- attrs = {"name": "package",
- "version": "1.0",
- "obsoletes": ["other", "another (<1.0)"]}
+ attrs = {
+ "name": "package",
+ "version": "1.0",
+ "obsoletes": ["other", "another (<1.0)"],
+ }
dist = Distribution(attrs)
- self.assertEqual(dist.metadata.get_obsoletes(),
- ["other", "another (<1.0)"])
- self.assertEqual(dist.get_obsoletes(),
- ["other", "another (<1.0)"])
+ assert dist.metadata.get_obsoletes() == ["other", "another (<1.0)"]
+ assert dist.get_obsoletes() == ["other", "another (<1.0)"]
meta = self.format_metadata(dist)
- self.assertIn("Metadata-Version: 1.1", meta)
- self.assertNotIn("provides:", meta.lower())
- self.assertNotIn("requires:", meta.lower())
- self.assertIn("Obsoletes: other", meta)
- self.assertIn("Obsoletes: another (<1.0)", meta)
+ assert "Metadata-Version: 1.1" in meta
+ assert "provides:" not in meta.lower()
+ assert "requires:" not in meta.lower()
+ assert "Obsoletes: other" in meta
+ assert "Obsoletes: another (<1.0)" in meta
def test_obsoletes_illegal(self):
- self.assertRaises(ValueError, Distribution,
- {"name": "package",
- "version": "1.0",
- "obsoletes": ["my.pkg (splat)"]})
+ with pytest.raises(ValueError):
+ Distribution(
+ {"name": "package", "version": "1.0", "obsoletes": ["my.pkg (splat)"]},
+ )
def test_obsoletes_to_list(self):
- attrs = {"name": "package",
- "obsoletes": iter(["other"])}
+ attrs = {"name": "package", "obsoletes": iter(["other"])}
dist = Distribution(attrs)
- self.assertIsInstance(dist.metadata.obsoletes, list)
+ assert isinstance(dist.metadata.obsoletes, list)
def test_classifier(self):
- attrs = {'name': 'Boa', 'version': '3.0',
- 'classifiers': ['Programming Language :: Python :: 3']}
+ attrs = {
+ 'name': 'Boa',
+ 'version': '3.0',
+ 'classifiers': ['Programming Language :: Python :: 3'],
+ }
dist = Distribution(attrs)
- self.assertEqual(dist.get_classifiers(),
- ['Programming Language :: Python :: 3'])
+ assert dist.get_classifiers() == ['Programming Language :: Python :: 3']
meta = self.format_metadata(dist)
- self.assertIn('Metadata-Version: 1.1', meta)
+ assert 'Metadata-Version: 1.1' in meta
- def test_classifier_invalid_type(self):
- attrs = {'name': 'Boa', 'version': '3.0',
- 'classifiers': ('Programming Language :: Python :: 3',)}
- with captured_stderr() as error:
- d = Distribution(attrs)
+ def test_classifier_invalid_type(self, caplog):
+ attrs = {
+ 'name': 'Boa',
+ 'version': '3.0',
+ 'classifiers': ('Programming Language :: Python :: 3',),
+ }
+ d = Distribution(attrs)
# should have warning about passing a non-list
- self.assertIn('should be a list', error.getvalue())
+ assert 'should be a list' in caplog.messages[0]
# should be converted to a list
- self.assertIsInstance(d.metadata.classifiers, list)
- self.assertEqual(d.metadata.classifiers,
- list(attrs['classifiers']))
+ assert isinstance(d.metadata.classifiers, list)
+ assert d.metadata.classifiers == list(attrs['classifiers'])
def test_keywords(self):
- attrs = {'name': 'Monty', 'version': '1.0',
- 'keywords': ['spam', 'eggs', 'life of brian']}
+ attrs = {
+ 'name': 'Monty',
+ 'version': '1.0',
+ 'keywords': ['spam', 'eggs', 'life of brian'],
+ }
dist = Distribution(attrs)
- self.assertEqual(dist.get_keywords(),
- ['spam', 'eggs', 'life of brian'])
-
- def test_keywords_invalid_type(self):
- attrs = {'name': 'Monty', 'version': '1.0',
- 'keywords': ('spam', 'eggs', 'life of brian')}
- with captured_stderr() as error:
- d = Distribution(attrs)
+ assert dist.get_keywords() == ['spam', 'eggs', 'life of brian']
+
+ def test_keywords_invalid_type(self, caplog):
+ attrs = {
+ 'name': 'Monty',
+ 'version': '1.0',
+ 'keywords': ('spam', 'eggs', 'life of brian'),
+ }
+ d = Distribution(attrs)
# should have warning about passing a non-list
- self.assertIn('should be a list', error.getvalue())
+ assert 'should be a list' in caplog.messages[0]
# should be converted to a list
- self.assertIsInstance(d.metadata.keywords, list)
- self.assertEqual(d.metadata.keywords, list(attrs['keywords']))
+ assert isinstance(d.metadata.keywords, list)
+ assert d.metadata.keywords == list(attrs['keywords'])
def test_platforms(self):
- attrs = {'name': 'Monty', 'version': '1.0',
- 'platforms': ['GNU/Linux', 'Some Evil Platform']}
+ attrs = {
+ 'name': 'Monty',
+ 'version': '1.0',
+ 'platforms': ['GNU/Linux', 'Some Evil Platform'],
+ }
dist = Distribution(attrs)
- self.assertEqual(dist.get_platforms(),
- ['GNU/Linux', 'Some Evil Platform'])
-
- def test_platforms_invalid_types(self):
- attrs = {'name': 'Monty', 'version': '1.0',
- 'platforms': ('GNU/Linux', 'Some Evil Platform')}
- with captured_stderr() as error:
- d = Distribution(attrs)
+ assert dist.get_platforms() == ['GNU/Linux', 'Some Evil Platform']
+
+ def test_platforms_invalid_types(self, caplog):
+ attrs = {
+ 'name': 'Monty',
+ 'version': '1.0',
+ 'platforms': ('GNU/Linux', 'Some Evil Platform'),
+ }
+ d = Distribution(attrs)
# should have warning about passing a non-list
- self.assertIn('should be a list', error.getvalue())
+ assert 'should be a list' in caplog.messages[0]
# should be converted to a list
- self.assertIsInstance(d.metadata.platforms, list)
- self.assertEqual(d.metadata.platforms, list(attrs['platforms']))
+ assert isinstance(d.metadata.platforms, list)
+ assert d.metadata.platforms == list(attrs['platforms'])
def test_download_url(self):
- attrs = {'name': 'Boa', 'version': '3.0',
- 'download_url': 'http://example.org/boa'}
+ attrs = {
+ 'name': 'Boa',
+ 'version': '3.0',
+ 'download_url': 'http://example.org/boa',
+ }
dist = Distribution(attrs)
meta = self.format_metadata(dist)
- self.assertIn('Metadata-Version: 1.1', meta)
+ assert 'Metadata-Version: 1.1' in meta
def test_long_description(self):
- long_desc = textwrap.dedent("""\
+ long_desc = textwrap.dedent(
+ """\
example::
We start here
and continue here
- and end here.""")
- attrs = {"name": "package",
- "version": "1.0",
- "long_description": long_desc}
+ and end here."""
+ )
+ attrs = {"name": "package", "version": "1.0", "long_description": long_desc}
dist = Distribution(attrs)
meta = self.format_metadata(dist)
meta = meta.replace('\n' + 8 * ' ', '\n')
- self.assertIn(long_desc, meta)
-
- def test_custom_pydistutils(self):
- # fixes #2166
- # make sure pydistutils.cfg is found
- if os.name == 'posix':
- user_filename = ".pydistutils.cfg"
- else:
- user_filename = "pydistutils.cfg"
-
- temp_dir = self.mkdtemp()
- user_filename = os.path.join(temp_dir, user_filename)
- f = open(user_filename, 'w')
- try:
- f.write('.')
- finally:
- f.close()
-
- try:
- dist = Distribution()
-
- # linux-style
- if sys.platform in ('linux', 'darwin'):
- os.environ['HOME'] = temp_dir
- files = dist.find_config_files()
- self.assertIn(user_filename, files)
-
- # win32-style
- if sys.platform == 'win32':
- # home drive should be found
- os.environ['USERPROFILE'] = temp_dir
- files = dist.find_config_files()
- self.assertIn(user_filename, files,
- '%r not found in %r' % (user_filename, files))
- finally:
- os.remove(user_filename)
+ assert long_desc in meta
+
+ def test_custom_pydistutils(self, temp_home):
+ """
+ pydistutils.cfg is found
+ """
+ jaraco.path.build({pydistutils_cfg: ''}, temp_home)
+ config_path = temp_home / pydistutils_cfg
+
+ assert str(config_path) in Distribution().find_config_files()
+
+ def test_extra_pydistutils(self, monkeypatch, tmp_path):
+ jaraco.path.build({'overrides.cfg': ''}, tmp_path)
+ filename = tmp_path / 'overrides.cfg'
+ monkeypatch.setenv('DIST_EXTRA_CONFIG', str(filename))
+ assert str(filename) in Distribution().find_config_files()
def test_fix_help_options(self):
help_tuples = [('a', 'b', 'c', 'd'), (1, 2, 3, 4)]
fancy_options = fix_help_options(help_tuples)
- self.assertEqual(fancy_options[0], ('a', 'b', 'c'))
- self.assertEqual(fancy_options[1], (1, 2, 3))
+ assert fancy_options[0] == ('a', 'b', 'c')
+ assert fancy_options[1] == (1, 2, 3)
- def test_show_help(self):
+ def test_show_help(self, request, capsys):
# smoke test, just makes sure some help is displayed
- self.addCleanup(log.set_threshold, log._global_log.threshold)
dist = Distribution()
sys.argv = []
dist.help = 1
dist.script_name = 'setup.py'
- with captured_stdout() as s:
- dist.parse_command_line()
-
- output = [line for line in s.getvalue().split('\n')
- if line.strip() != '']
- self.assertTrue(output)
+ dist.parse_command_line()
+ output = [
+ line for line in capsys.readouterr().out.split('\n') if line.strip() != ''
+ ]
+ assert output
def test_read_metadata(self):
- attrs = {"name": "package",
- "version": "1.0",
- "long_description": "desc",
- "description": "xxx",
- "download_url": "http://example.com",
- "keywords": ['one', 'two'],
- "requires": ['foo']}
+ attrs = {
+ "name": "package",
+ "version": "1.0",
+ "long_description": "desc",
+ "description": "xxx",
+ "download_url": "http://example.com",
+ "keywords": ['one', 'two'],
+ "requires": ['foo'],
+ }
dist = Distribution(attrs)
metadata = dist.metadata
@@ -514,20 +502,11 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
PKG_INFO.seek(0)
metadata.read_pkg_file(PKG_INFO)
- self.assertEqual(metadata.name, "package")
- self.assertEqual(metadata.version, "1.0")
- self.assertEqual(metadata.description, "xxx")
- self.assertEqual(metadata.download_url, 'http://example.com')
- self.assertEqual(metadata.keywords, ['one', 'two'])
- self.assertEqual(metadata.platforms, ['UNKNOWN'])
- self.assertEqual(metadata.obsoletes, None)
- self.assertEqual(metadata.requires, ['foo'])
-
-def test_suite():
- suite = unittest.TestSuite()
- suite.addTest(unittest.TestLoader().loadTestsFromTestCase(DistributionTestCase))
- suite.addTest(unittest.TestLoader().loadTestsFromTestCase(MetadataTestCase))
- return suite
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert metadata.name == "package"
+ assert metadata.version == "1.0"
+ assert metadata.description == "xxx"
+ assert metadata.download_url == 'http://example.com'
+ assert metadata.keywords == ['one', 'two']
+ assert metadata.platforms is None
+ assert metadata.obsoletes is None
+ assert metadata.requires == ['foo']
diff --git a/setuptools/_distutils/tests/test_extension.py b/setuptools/_distutils/tests/test_extension.py
index 78a55daa..f86af073 100644
--- a/setuptools/_distutils/tests/test_extension.py
+++ b/setuptools/_distutils/tests/test_extension.py
@@ -1,15 +1,14 @@
"""Tests for distutils.extension."""
-import unittest
import os
import warnings
-from test.support import run_unittest
from distutils.extension import read_setup_file, Extension
from .py38compat import check_warnings
+import pytest
-class ExtensionTestCase(unittest.TestCase):
+class TestExtension:
def test_read_setup_file(self):
# trying to read a Setup file
# (sample extracted from the PyGame project)
@@ -21,51 +20,85 @@ class ExtensionTestCase(unittest.TestCase):
# here are the extensions read_setup_file should have created
# out of the file
- wanted = ['_arraysurfarray', '_camera', '_numericsndarray',
- '_numericsurfarray', 'base', 'bufferproxy', 'cdrom',
- 'color', 'constants', 'display', 'draw', 'event',
- 'fastevent', 'font', 'gfxdraw', 'image', 'imageext',
- 'joystick', 'key', 'mask', 'mixer', 'mixer_music',
- 'mouse', 'movie', 'overlay', 'pixelarray', 'pypm',
- 'rect', 'rwobject', 'scrap', 'surface', 'surflock',
- 'time', 'transform']
+ wanted = [
+ '_arraysurfarray',
+ '_camera',
+ '_numericsndarray',
+ '_numericsurfarray',
+ 'base',
+ 'bufferproxy',
+ 'cdrom',
+ 'color',
+ 'constants',
+ 'display',
+ 'draw',
+ 'event',
+ 'fastevent',
+ 'font',
+ 'gfxdraw',
+ 'image',
+ 'imageext',
+ 'joystick',
+ 'key',
+ 'mask',
+ 'mixer',
+ 'mixer_music',
+ 'mouse',
+ 'movie',
+ 'overlay',
+ 'pixelarray',
+ 'pypm',
+ 'rect',
+ 'rwobject',
+ 'scrap',
+ 'surface',
+ 'surflock',
+ 'time',
+ 'transform',
+ ]
- self.assertEqual(names, wanted)
+ assert names == wanted
def test_extension_init(self):
# the first argument, which is the name, must be a string
- self.assertRaises(AssertionError, Extension, 1, [])
+ with pytest.raises(AssertionError):
+ Extension(1, [])
ext = Extension('name', [])
- self.assertEqual(ext.name, 'name')
+ assert ext.name == 'name'
# the second argument, which is the list of files, must
# be a list of strings
- self.assertRaises(AssertionError, Extension, 'name', 'file')
- self.assertRaises(AssertionError, Extension, 'name', ['file', 1])
+ with pytest.raises(AssertionError):
+ Extension('name', 'file')
+ with pytest.raises(AssertionError):
+ Extension('name', ['file', 1])
ext = Extension('name', ['file1', 'file2'])
- self.assertEqual(ext.sources, ['file1', 'file2'])
+ assert ext.sources == ['file1', 'file2']
# others arguments have defaults
- for attr in ('include_dirs', 'define_macros', 'undef_macros',
- 'library_dirs', 'libraries', 'runtime_library_dirs',
- 'extra_objects', 'extra_compile_args', 'extra_link_args',
- 'export_symbols', 'swig_opts', 'depends'):
- self.assertEqual(getattr(ext, attr), [])
+ for attr in (
+ 'include_dirs',
+ 'define_macros',
+ 'undef_macros',
+ 'library_dirs',
+ 'libraries',
+ 'runtime_library_dirs',
+ 'extra_objects',
+ 'extra_compile_args',
+ 'extra_link_args',
+ 'export_symbols',
+ 'swig_opts',
+ 'depends',
+ ):
+ assert getattr(ext, attr) == []
- self.assertEqual(ext.language, None)
- self.assertEqual(ext.optional, None)
+ assert ext.language is None
+ assert ext.optional is None
# if there are unknown keyword options, warn about them
with check_warnings() as w:
warnings.simplefilter('always')
ext = Extension('name', ['file1', 'file2'], chic=True)
- self.assertEqual(len(w.warnings), 1)
- self.assertEqual(str(w.warnings[0].message),
- "Unknown Extension options: 'chic'")
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(ExtensionTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert len(w.warnings) == 1
+ assert str(w.warnings[0].message) == "Unknown Extension options: 'chic'"
diff --git a/setuptools/_distutils/tests/test_file_util.py b/setuptools/_distutils/tests/test_file_util.py
index 81b90d6c..9f44f91d 100644
--- a/setuptools/_distutils/tests/test_file_util.py
+++ b/setuptools/_distutils/tests/test_file_util.py
@@ -1,40 +1,26 @@
"""Tests for distutils.file_util."""
-import unittest
import os
import errno
-from unittest.mock import patch
+import unittest.mock as mock
from distutils.file_util import move_file, copy_file
-from distutils import log
from distutils.tests import support
from distutils.errors import DistutilsFileError
-from test.support import run_unittest
from .py38compat import unlink
+import pytest
-class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
+@pytest.fixture(autouse=True)
+def stuff(request, monkeypatch, distutils_managed_tempdir):
+ self = request.instance
+ tmp_dir = self.mkdtemp()
+ self.source = os.path.join(tmp_dir, 'f1')
+ self.target = os.path.join(tmp_dir, 'f2')
+ self.target_dir = os.path.join(tmp_dir, 'd1')
- def _log(self, msg, *args):
- if len(args) > 0:
- self._logs.append(msg % args)
- else:
- self._logs.append(msg)
-
- def setUp(self):
- super(FileUtilTestCase, self).setUp()
- self._logs = []
- self.old_log = log.info
- log.info = self._log
- tmp_dir = self.mkdtemp()
- self.source = os.path.join(tmp_dir, 'f1')
- self.target = os.path.join(tmp_dir, 'f2')
- self.target_dir = os.path.join(tmp_dir, 'd1')
-
- def tearDown(self):
- log.info = self.old_log
- super(FileUtilTestCase, self).tearDown()
- def test_move_file_verbosity(self):
+class TestFileUtil(support.TempdirManager):
+ def test_move_file_verbosity(self, caplog):
f = open(self.source, 'w')
try:
f.write('some content')
@@ -42,39 +28,41 @@ class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
f.close()
move_file(self.source, self.target, verbose=0)
- wanted = []
- self.assertEqual(self._logs, wanted)
+ assert not caplog.messages
# back to original state
move_file(self.target, self.source, verbose=0)
move_file(self.source, self.target, verbose=1)
- wanted = ['moving %s -> %s' % (self.source, self.target)]
- self.assertEqual(self._logs, wanted)
+ wanted = ['moving {} -> {}'.format(self.source, self.target)]
+ assert caplog.messages == wanted
# back to original state
move_file(self.target, self.source, verbose=0)
- self._logs = []
+ caplog.clear()
# now the target is a dir
os.mkdir(self.target_dir)
move_file(self.source, self.target_dir, verbose=1)
- wanted = ['moving %s -> %s' % (self.source, self.target_dir)]
- self.assertEqual(self._logs, wanted)
+ wanted = ['moving {} -> {}'.format(self.source, self.target_dir)]
+ assert caplog.messages == wanted
def test_move_file_exception_unpacking_rename(self):
# see issue 22182
- with patch("os.rename", side_effect=OSError("wrong", 1)), \
- self.assertRaises(DistutilsFileError):
+ with mock.patch("os.rename", side_effect=OSError("wrong", 1)), pytest.raises(
+ DistutilsFileError
+ ):
with open(self.source, 'w') as fobj:
fobj.write('spam eggs')
move_file(self.source, self.target, verbose=0)
def test_move_file_exception_unpacking_unlink(self):
# see issue 22182
- with patch("os.rename", side_effect=OSError(errno.EXDEV, "wrong")), \
- patch("os.unlink", side_effect=OSError("wrong", 1)), \
- self.assertRaises(DistutilsFileError):
+ with mock.patch(
+ "os.rename", side_effect=OSError(errno.EXDEV, "wrong")
+ ), mock.patch("os.unlink", side_effect=OSError("wrong", 1)), pytest.raises(
+ DistutilsFileError
+ ):
with open(self.source, 'w') as fobj:
fobj.write('spam eggs')
move_file(self.source, self.target, verbose=0)
@@ -94,10 +82,10 @@ class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
copy_file(self.source, self.target, link='hard')
st2 = os.stat(self.source)
st3 = os.stat(self.target)
- self.assertTrue(os.path.samestat(st, st2), (st, st2))
- self.assertTrue(os.path.samestat(st2, st3), (st2, st3))
- with open(self.source, 'r') as f:
- self.assertEqual(f.read(), 'some content')
+ assert os.path.samestat(st, st2), (st, st2)
+ assert os.path.samestat(st2, st3), (st2, st3)
+ with open(self.source) as f:
+ assert f.read() == 'some content'
def test_copy_file_hard_link_failure(self):
# If hard linking fails, copy_file() falls back on copying file
@@ -106,19 +94,12 @@ class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
with open(self.source, 'w') as f:
f.write('some content')
st = os.stat(self.source)
- with patch("os.link", side_effect=OSError(0, "linking unsupported")):
+ with mock.patch("os.link", side_effect=OSError(0, "linking unsupported")):
copy_file(self.source, self.target, link='hard')
st2 = os.stat(self.source)
st3 = os.stat(self.target)
- self.assertTrue(os.path.samestat(st, st2), (st, st2))
- self.assertFalse(os.path.samestat(st2, st3), (st2, st3))
+ assert os.path.samestat(st, st2), (st, st2)
+ assert not os.path.samestat(st2, st3), (st2, st3)
for fn in (self.source, self.target):
- with open(fn, 'r') as f:
- self.assertEqual(f.read(), 'some content')
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(FileUtilTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ with open(fn) as f:
+ assert f.read() == 'some content'
diff --git a/setuptools/_distutils/tests/test_filelist.py b/setuptools/_distutils/tests/test_filelist.py
index a90edcf1..2cee42cd 100644
--- a/setuptools/_distutils/tests/test_filelist.py
+++ b/setuptools/_distutils/tests/test_filelist.py
@@ -1,17 +1,16 @@
"""Tests for distutils.filelist."""
import os
import re
-import unittest
+import logging
+
from distutils import debug
-from distutils.log import WARN
from distutils.errors import DistutilsTemplateError
from distutils.filelist import glob_to_re, translate_pattern, FileList
from distutils import filelist
-from test.support import captured_stdout, run_unittest
-from distutils.tests import support
+import pytest
+import jaraco.path
-from .py35compat import adapt_glob
from . import py38compat as os_helper
@@ -36,16 +35,16 @@ def make_local_path(s):
return s.replace('/', os.sep)
-class FileListTestCase(support.LoggingSilencer,
- unittest.TestCase):
-
- def assertNoWarnings(self):
- self.assertEqual(self.get_logs(WARN), [])
- self.clear_logs()
+class TestFileList:
+ def assertNoWarnings(self, caplog):
+ warnings = [rec for rec in caplog.records if rec.levelno == logging.WARNING]
+ assert not warnings
+ caplog.clear()
- def assertWarnings(self):
- self.assertGreater(len(self.get_logs(WARN)), 0)
- self.clear_logs()
+ def assertWarnings(self, caplog):
+ warnings = [rec for rec in caplog.records if rec.levelno == logging.WARNING]
+ assert warnings
+ caplog.clear()
def test_glob_to_re(self):
sep = os.sep
@@ -61,69 +60,70 @@ class FileListTestCase(support.LoggingSilencer,
(r'foo\\*', r'(?s:foo\\\\[^%(sep)s]*)\Z'),
(r'foo\\\*', r'(?s:foo\\\\\\[^%(sep)s]*)\Z'),
('foo????', r'(?s:foo[^%(sep)s][^%(sep)s][^%(sep)s][^%(sep)s])\Z'),
- (r'foo\\??', r'(?s:foo\\\\[^%(sep)s][^%(sep)s])\Z')):
+ (r'foo\\??', r'(?s:foo\\\\[^%(sep)s][^%(sep)s])\Z'),
+ ):
regex = regex % {'sep': sep}
- self.assertEqual(glob_to_re(glob), adapt_glob(regex))
+ assert glob_to_re(glob) == regex
def test_process_template_line(self):
# testing all MANIFEST.in template patterns
file_list = FileList()
- l = make_local_path
+ mlp = make_local_path
# simulated file list
- file_list.allfiles = ['foo.tmp', 'ok', 'xo', 'four.txt',
- 'buildout.cfg',
- # filelist does not filter out VCS directories,
- # it's sdist that does
- l('.hg/last-message.txt'),
- l('global/one.txt'),
- l('global/two.txt'),
- l('global/files.x'),
- l('global/here.tmp'),
- l('f/o/f.oo'),
- l('dir/graft-one'),
- l('dir/dir2/graft2'),
- l('dir3/ok'),
- l('dir3/sub/ok.txt'),
- ]
+ file_list.allfiles = [
+ 'foo.tmp',
+ 'ok',
+ 'xo',
+ 'four.txt',
+ 'buildout.cfg',
+ # filelist does not filter out VCS directories,
+ # it's sdist that does
+ mlp('.hg/last-message.txt'),
+ mlp('global/one.txt'),
+ mlp('global/two.txt'),
+ mlp('global/files.x'),
+ mlp('global/here.tmp'),
+ mlp('f/o/f.oo'),
+ mlp('dir/graft-one'),
+ mlp('dir/dir2/graft2'),
+ mlp('dir3/ok'),
+ mlp('dir3/sub/ok.txt'),
+ ]
for line in MANIFEST_IN.split('\n'):
if line.strip() == '':
continue
file_list.process_template_line(line)
- wanted = ['ok',
- 'buildout.cfg',
- 'four.txt',
- l('.hg/last-message.txt'),
- l('global/one.txt'),
- l('global/two.txt'),
- l('f/o/f.oo'),
- l('dir/graft-one'),
- l('dir/dir2/graft2'),
- ]
-
- self.assertEqual(file_list.files, wanted)
-
- def test_debug_print(self):
+ wanted = [
+ 'ok',
+ 'buildout.cfg',
+ 'four.txt',
+ mlp('.hg/last-message.txt'),
+ mlp('global/one.txt'),
+ mlp('global/two.txt'),
+ mlp('f/o/f.oo'),
+ mlp('dir/graft-one'),
+ mlp('dir/dir2/graft2'),
+ ]
+
+ assert file_list.files == wanted
+
+ def test_debug_print(self, capsys, monkeypatch):
file_list = FileList()
- with captured_stdout() as stdout:
- file_list.debug_print('xxx')
- self.assertEqual(stdout.getvalue(), '')
-
- debug.DEBUG = True
- try:
- with captured_stdout() as stdout:
- file_list.debug_print('xxx')
- self.assertEqual(stdout.getvalue(), 'xxx\n')
- finally:
- debug.DEBUG = False
+ file_list.debug_print('xxx')
+ assert capsys.readouterr().out == ''
+
+ monkeypatch.setattr(debug, 'DEBUG', True)
+ file_list.debug_print('xxx')
+ assert capsys.readouterr().out == 'xxx\n'
def test_set_allfiles(self):
file_list = FileList()
files = ['a', 'b', 'c']
file_list.set_allfiles(files)
- self.assertEqual(file_list.allfiles, files)
+ assert file_list.allfiles == files
def test_remove_duplicates(self):
file_list = FileList()
@@ -131,223 +131,204 @@ class FileListTestCase(support.LoggingSilencer,
# files must be sorted beforehand (sdist does it)
file_list.sort()
file_list.remove_duplicates()
- self.assertEqual(file_list.files, ['a', 'b', 'c', 'g'])
+ assert file_list.files == ['a', 'b', 'c', 'g']
def test_translate_pattern(self):
# not regex
- self.assertTrue(hasattr(
- translate_pattern('a', anchor=True, is_regex=False),
- 'search'))
+ assert hasattr(translate_pattern('a', anchor=True, is_regex=False), 'search')
# is a regex
regex = re.compile('a')
- self.assertEqual(
- translate_pattern(regex, anchor=True, is_regex=True),
- regex)
+ assert translate_pattern(regex, anchor=True, is_regex=True) == regex
# plain string flagged as regex
- self.assertTrue(hasattr(
- translate_pattern('a', anchor=True, is_regex=True),
- 'search'))
+ assert hasattr(translate_pattern('a', anchor=True, is_regex=True), 'search')
# glob support
- self.assertTrue(translate_pattern(
- '*.py', anchor=True, is_regex=False).search('filelist.py'))
+ assert translate_pattern('*.py', anchor=True, is_regex=False).search(
+ 'filelist.py'
+ )
def test_exclude_pattern(self):
# return False if no match
file_list = FileList()
- self.assertFalse(file_list.exclude_pattern('*.py'))
+ assert not file_list.exclude_pattern('*.py')
# return True if files match
file_list = FileList()
file_list.files = ['a.py', 'b.py']
- self.assertTrue(file_list.exclude_pattern('*.py'))
+ assert file_list.exclude_pattern('*.py')
# test excludes
file_list = FileList()
file_list.files = ['a.py', 'a.txt']
file_list.exclude_pattern('*.py')
- self.assertEqual(file_list.files, ['a.txt'])
+ assert file_list.files == ['a.txt']
def test_include_pattern(self):
# return False if no match
file_list = FileList()
file_list.set_allfiles([])
- self.assertFalse(file_list.include_pattern('*.py'))
+ assert not file_list.include_pattern('*.py')
# return True if files match
file_list = FileList()
file_list.set_allfiles(['a.py', 'b.txt'])
- self.assertTrue(file_list.include_pattern('*.py'))
+ assert file_list.include_pattern('*.py')
# test * matches all files
file_list = FileList()
- self.assertIsNone(file_list.allfiles)
+ assert file_list.allfiles is None
file_list.set_allfiles(['a.py', 'b.txt'])
file_list.include_pattern('*')
- self.assertEqual(file_list.allfiles, ['a.py', 'b.txt'])
+ assert file_list.allfiles == ['a.py', 'b.txt']
- def test_process_template(self):
- l = make_local_path
+ def test_process_template(self, caplog):
+ mlp = make_local_path
# invalid lines
file_list = FileList()
- for action in ('include', 'exclude', 'global-include',
- 'global-exclude', 'recursive-include',
- 'recursive-exclude', 'graft', 'prune', 'blarg'):
- self.assertRaises(DistutilsTemplateError,
- file_list.process_template_line, action)
+ for action in (
+ 'include',
+ 'exclude',
+ 'global-include',
+ 'global-exclude',
+ 'recursive-include',
+ 'recursive-exclude',
+ 'graft',
+ 'prune',
+ 'blarg',
+ ):
+ with pytest.raises(DistutilsTemplateError):
+ file_list.process_template_line(action)
# include
file_list = FileList()
- file_list.set_allfiles(['a.py', 'b.txt', l('d/c.py')])
+ file_list.set_allfiles(['a.py', 'b.txt', mlp('d/c.py')])
file_list.process_template_line('include *.py')
- self.assertEqual(file_list.files, ['a.py'])
- self.assertNoWarnings()
+ assert file_list.files == ['a.py']
+ self.assertNoWarnings(caplog)
file_list.process_template_line('include *.rb')
- self.assertEqual(file_list.files, ['a.py'])
- self.assertWarnings()
+ assert file_list.files == ['a.py']
+ self.assertWarnings(caplog)
# exclude
file_list = FileList()
- file_list.files = ['a.py', 'b.txt', l('d/c.py')]
+ file_list.files = ['a.py', 'b.txt', mlp('d/c.py')]
file_list.process_template_line('exclude *.py')
- self.assertEqual(file_list.files, ['b.txt', l('d/c.py')])
- self.assertNoWarnings()
+ assert file_list.files == ['b.txt', mlp('d/c.py')]
+ self.assertNoWarnings(caplog)
file_list.process_template_line('exclude *.rb')
- self.assertEqual(file_list.files, ['b.txt', l('d/c.py')])
- self.assertWarnings()
+ assert file_list.files == ['b.txt', mlp('d/c.py')]
+ self.assertWarnings(caplog)
# global-include
file_list = FileList()
- file_list.set_allfiles(['a.py', 'b.txt', l('d/c.py')])
+ file_list.set_allfiles(['a.py', 'b.txt', mlp('d/c.py')])
file_list.process_template_line('global-include *.py')
- self.assertEqual(file_list.files, ['a.py', l('d/c.py')])
- self.assertNoWarnings()
+ assert file_list.files == ['a.py', mlp('d/c.py')]
+ self.assertNoWarnings(caplog)
file_list.process_template_line('global-include *.rb')
- self.assertEqual(file_list.files, ['a.py', l('d/c.py')])
- self.assertWarnings()
+ assert file_list.files == ['a.py', mlp('d/c.py')]
+ self.assertWarnings(caplog)
# global-exclude
file_list = FileList()
- file_list.files = ['a.py', 'b.txt', l('d/c.py')]
+ file_list.files = ['a.py', 'b.txt', mlp('d/c.py')]
file_list.process_template_line('global-exclude *.py')
- self.assertEqual(file_list.files, ['b.txt'])
- self.assertNoWarnings()
+ assert file_list.files == ['b.txt']
+ self.assertNoWarnings(caplog)
file_list.process_template_line('global-exclude *.rb')
- self.assertEqual(file_list.files, ['b.txt'])
- self.assertWarnings()
+ assert file_list.files == ['b.txt']
+ self.assertWarnings(caplog)
# recursive-include
file_list = FileList()
- file_list.set_allfiles(['a.py', l('d/b.py'), l('d/c.txt'),
- l('d/d/e.py')])
+ file_list.set_allfiles(['a.py', mlp('d/b.py'), mlp('d/c.txt'), mlp('d/d/e.py')])
file_list.process_template_line('recursive-include d *.py')
- self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')])
- self.assertNoWarnings()
+ assert file_list.files == [mlp('d/b.py'), mlp('d/d/e.py')]
+ self.assertNoWarnings(caplog)
file_list.process_template_line('recursive-include e *.py')
- self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')])
- self.assertWarnings()
+ assert file_list.files == [mlp('d/b.py'), mlp('d/d/e.py')]
+ self.assertWarnings(caplog)
# recursive-exclude
file_list = FileList()
- file_list.files = ['a.py', l('d/b.py'), l('d/c.txt'), l('d/d/e.py')]
+ file_list.files = ['a.py', mlp('d/b.py'), mlp('d/c.txt'), mlp('d/d/e.py')]
file_list.process_template_line('recursive-exclude d *.py')
- self.assertEqual(file_list.files, ['a.py', l('d/c.txt')])
- self.assertNoWarnings()
+ assert file_list.files == ['a.py', mlp('d/c.txt')]
+ self.assertNoWarnings(caplog)
file_list.process_template_line('recursive-exclude e *.py')
- self.assertEqual(file_list.files, ['a.py', l('d/c.txt')])
- self.assertWarnings()
+ assert file_list.files == ['a.py', mlp('d/c.txt')]
+ self.assertWarnings(caplog)
# graft
file_list = FileList()
- file_list.set_allfiles(['a.py', l('d/b.py'), l('d/d/e.py'),
- l('f/f.py')])
+ file_list.set_allfiles(['a.py', mlp('d/b.py'), mlp('d/d/e.py'), mlp('f/f.py')])
file_list.process_template_line('graft d')
- self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')])
- self.assertNoWarnings()
+ assert file_list.files == [mlp('d/b.py'), mlp('d/d/e.py')]
+ self.assertNoWarnings(caplog)
file_list.process_template_line('graft e')
- self.assertEqual(file_list.files, [l('d/b.py'), l('d/d/e.py')])
- self.assertWarnings()
+ assert file_list.files == [mlp('d/b.py'), mlp('d/d/e.py')]
+ self.assertWarnings(caplog)
# prune
file_list = FileList()
- file_list.files = ['a.py', l('d/b.py'), l('d/d/e.py'), l('f/f.py')]
+ file_list.files = ['a.py', mlp('d/b.py'), mlp('d/d/e.py'), mlp('f/f.py')]
file_list.process_template_line('prune d')
- self.assertEqual(file_list.files, ['a.py', l('f/f.py')])
- self.assertNoWarnings()
+ assert file_list.files == ['a.py', mlp('f/f.py')]
+ self.assertNoWarnings(caplog)
file_list.process_template_line('prune e')
- self.assertEqual(file_list.files, ['a.py', l('f/f.py')])
- self.assertWarnings()
+ assert file_list.files == ['a.py', mlp('f/f.py')]
+ self.assertWarnings(caplog)
-class FindAllTestCase(unittest.TestCase):
+class TestFindAll:
@os_helper.skip_unless_symlink
- def test_missing_symlink(self):
- with os_helper.temp_cwd():
- os.symlink('foo', 'bar')
- self.assertEqual(filelist.findall(), [])
+ def test_missing_symlink(self, temp_cwd):
+ os.symlink('foo', 'bar')
+ assert filelist.findall() == []
- def test_basic_discovery(self):
+ def test_basic_discovery(self, temp_cwd):
"""
When findall is called with no parameters or with
'.' as the parameter, the dot should be omitted from
the results.
"""
- with os_helper.temp_cwd():
- os.mkdir('foo')
- file1 = os.path.join('foo', 'file1.txt')
- os_helper.create_empty_file(file1)
- os.mkdir('bar')
- file2 = os.path.join('bar', 'file2.txt')
- os_helper.create_empty_file(file2)
- expected = [file2, file1]
- self.assertEqual(sorted(filelist.findall()), expected)
-
- def test_non_local_discovery(self):
+ jaraco.path.build({'foo': {'file1.txt': ''}, 'bar': {'file2.txt': ''}})
+ file1 = os.path.join('foo', 'file1.txt')
+ file2 = os.path.join('bar', 'file2.txt')
+ expected = [file2, file1]
+ assert sorted(filelist.findall()) == expected
+
+ def test_non_local_discovery(self, tmp_path):
"""
When findall is called with another path, the full
path name should be returned.
"""
- with os_helper.temp_dir() as temp_dir:
- file1 = os.path.join(temp_dir, 'file1.txt')
- os_helper.create_empty_file(file1)
- expected = [file1]
- self.assertEqual(filelist.findall(temp_dir), expected)
+ filename = tmp_path / 'file1.txt'
+ filename.write_text('')
+ expected = [str(filename)]
+ assert filelist.findall(tmp_path) == expected
@os_helper.skip_unless_symlink
- def test_symlink_loop(self):
- with os_helper.temp_dir() as temp_dir:
- link = os.path.join(temp_dir, 'link-to-parent')
- content = os.path.join(temp_dir, 'somefile')
- os_helper.create_empty_file(content)
- os.symlink('.', link)
- files = filelist.findall(temp_dir)
- assert len(files) == 1
-
-
-def test_suite():
- return unittest.TestSuite([
- unittest.TestLoader().loadTestsFromTestCase(FileListTestCase),
- unittest.TestLoader().loadTestsFromTestCase(FindAllTestCase),
- ])
-
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ def test_symlink_loop(self, tmp_path):
+ tmp_path.joinpath('link-to-parent').symlink_to('.')
+ tmp_path.joinpath('somefile').write_text('')
+ files = filelist.findall(tmp_path)
+ assert len(files) == 1
diff --git a/setuptools/_distutils/tests/test_install.py b/setuptools/_distutils/tests/test_install.py
index 5dbc06b0..102218bc 100644
--- a/setuptools/_distutils/tests/test_install.py
+++ b/setuptools/_distutils/tests/test_install.py
@@ -2,10 +2,11 @@
import os
import sys
-import unittest
import site
+import pathlib
+import logging
-from test.support import captured_stdout, run_unittest
+import pytest
from distutils import sysconfig
from distutils.command.install import install
@@ -24,11 +25,15 @@ def _make_ext_name(modname):
return modname + sysconfig.get_config_var('EXT_SUFFIX')
-class InstallTestCase(support.TempdirManager,
- support.EnvironGuard,
- support.LoggingSilencer,
- unittest.TestCase):
-
+@support.combine_markers
+@pytest.mark.usefixtures('save_env')
+class TestInstall(
+ support.TempdirManager,
+):
+ @pytest.mark.xfail(
+ 'platform.system() == "Windows" and sys.version_info > (3, 11)',
+ reason="pypa/distutils#148",
+ )
def test_home_installation_scheme(self):
# This ensure two things:
# - that --home generates the desired set of directory names
@@ -42,98 +47,88 @@ class InstallTestCase(support.TempdirManager,
dist.command_obj["build"] = support.DummyCommand(
build_base=builddir,
build_lib=os.path.join(builddir, "lib"),
- )
+ )
cmd = install(dist)
cmd.home = destination
cmd.ensure_finalized()
- self.assertEqual(cmd.install_base, destination)
- self.assertEqual(cmd.install_platbase, destination)
+ assert cmd.install_base == destination
+ assert cmd.install_platbase == destination
def check_path(got, expected):
got = os.path.normpath(got)
expected = os.path.normpath(expected)
- self.assertEqual(got, expected)
+ assert got == expected
- libdir = os.path.join(destination, "lib", "python")
+ impl_name = sys.implementation.name.replace("cpython", "python")
+ libdir = os.path.join(destination, "lib", impl_name)
check_path(cmd.install_lib, libdir)
_platlibdir = getattr(sys, "platlibdir", "lib")
- platlibdir = os.path.join(destination, _platlibdir, "python")
+ platlibdir = os.path.join(destination, _platlibdir, impl_name)
check_path(cmd.install_platlib, platlibdir)
check_path(cmd.install_purelib, libdir)
- check_path(cmd.install_headers,
- os.path.join(destination, "include", "python", "foopkg"))
+ check_path(
+ cmd.install_headers,
+ os.path.join(destination, "include", impl_name, "foopkg"),
+ )
check_path(cmd.install_scripts, os.path.join(destination, "bin"))
check_path(cmd.install_data, destination)
- def test_user_site(self):
+ def test_user_site(self, monkeypatch):
# test install with --user
# preparing the environment for the test
- self.old_user_base = site.USER_BASE
- self.old_user_site = site.USER_SITE
self.tmpdir = self.mkdtemp()
- self.user_base = os.path.join(self.tmpdir, 'B')
- self.user_site = os.path.join(self.tmpdir, 'S')
- site.USER_BASE = self.user_base
- site.USER_SITE = self.user_site
- install_module.USER_BASE = self.user_base
- install_module.USER_SITE = self.user_site
+ orig_site = site.USER_SITE
+ orig_base = site.USER_BASE
+ monkeypatch.setattr(site, 'USER_BASE', os.path.join(self.tmpdir, 'B'))
+ monkeypatch.setattr(site, 'USER_SITE', os.path.join(self.tmpdir, 'S'))
+ monkeypatch.setattr(install_module, 'USER_BASE', site.USER_BASE)
+ monkeypatch.setattr(install_module, 'USER_SITE', site.USER_SITE)
def _expanduser(path):
if path.startswith('~'):
return os.path.normpath(self.tmpdir + path[1:])
return path
- self.old_expand = os.path.expanduser
- os.path.expanduser = _expanduser
-
- def cleanup():
- site.USER_BASE = self.old_user_base
- site.USER_SITE = self.old_user_site
- install_module.USER_BASE = self.old_user_base
- install_module.USER_SITE = self.old_user_site
- os.path.expanduser = self.old_expand
- self.addCleanup(cleanup)
+ monkeypatch.setattr(os.path, 'expanduser', _expanduser)
for key in ('nt_user', 'posix_user'):
- self.assertIn(key, INSTALL_SCHEMES)
+ assert key in INSTALL_SCHEMES
dist = Distribution({'name': 'xx'})
cmd = install(dist)
# making sure the user option is there
- options = [name for name, short, lable in
- cmd.user_options]
- self.assertIn('user', options)
+ options = [name for name, short, lable in cmd.user_options]
+ assert 'user' in options
# setting a value
cmd.user = 1
# user base and site shouldn't be created yet
- self.assertFalse(os.path.exists(self.user_base))
- self.assertFalse(os.path.exists(self.user_site))
+ assert not os.path.exists(site.USER_BASE)
+ assert not os.path.exists(site.USER_SITE)
# let's run finalize
cmd.ensure_finalized()
# now they should
- self.assertTrue(os.path.exists(self.user_base))
- self.assertTrue(os.path.exists(self.user_site))
+ assert os.path.exists(site.USER_BASE)
+ assert os.path.exists(site.USER_SITE)
- self.assertIn('userbase', cmd.config_vars)
- self.assertIn('usersite', cmd.config_vars)
+ assert 'userbase' in cmd.config_vars
+ assert 'usersite' in cmd.config_vars
- actual_headers = os.path.relpath(cmd.install_headers, self.user_base)
+ actual_headers = os.path.relpath(cmd.install_headers, site.USER_BASE)
if os.name == 'nt':
- site_path = os.path.relpath(
- os.path.dirname(self.old_user_site), self.old_user_base)
+ site_path = os.path.relpath(os.path.dirname(orig_site), orig_base)
include = os.path.join(site_path, 'Include')
else:
include = sysconfig.get_python_inc(0, '')
expect_headers = os.path.join(include, 'xx')
- self.assertEqual(os.path.normcase(actual_headers), os.path.normcase(expect_headers))
+ assert os.path.normcase(actual_headers) == os.path.normcase(expect_headers)
def test_handle_extra_path(self):
dist = Distribution({'name': 'xx', 'extra_path': 'path,dirs'})
@@ -141,27 +136,28 @@ class InstallTestCase(support.TempdirManager,
# two elements
cmd.handle_extra_path()
- self.assertEqual(cmd.extra_path, ['path', 'dirs'])
- self.assertEqual(cmd.extra_dirs, 'dirs')
- self.assertEqual(cmd.path_file, 'path')
+ assert cmd.extra_path == ['path', 'dirs']
+ assert cmd.extra_dirs == 'dirs'
+ assert cmd.path_file == 'path'
# one element
cmd.extra_path = ['path']
cmd.handle_extra_path()
- self.assertEqual(cmd.extra_path, ['path'])
- self.assertEqual(cmd.extra_dirs, 'path')
- self.assertEqual(cmd.path_file, 'path')
+ assert cmd.extra_path == ['path']
+ assert cmd.extra_dirs == 'path'
+ assert cmd.path_file == 'path'
# none
dist.extra_path = cmd.extra_path = None
cmd.handle_extra_path()
- self.assertEqual(cmd.extra_path, None)
- self.assertEqual(cmd.extra_dirs, '')
- self.assertEqual(cmd.path_file, None)
+ assert cmd.extra_path is None
+ assert cmd.extra_dirs == ''
+ assert cmd.path_file is None
# three elements (no way !)
cmd.extra_path = 'path,dirs,again'
- self.assertRaises(DistutilsOptionError, cmd.handle_extra_path)
+ with pytest.raises(DistutilsOptionError):
+ cmd.handle_extra_path()
def test_finalize_options(self):
dist = Distribution({'name': 'xx'})
@@ -171,23 +167,25 @@ class InstallTestCase(support.TempdirManager,
# install-base/install-platbase -- not both
cmd.prefix = 'prefix'
cmd.install_base = 'base'
- self.assertRaises(DistutilsOptionError, cmd.finalize_options)
+ with pytest.raises(DistutilsOptionError):
+ cmd.finalize_options()
# must supply either home or prefix/exec-prefix -- not both
cmd.install_base = None
cmd.home = 'home'
- self.assertRaises(DistutilsOptionError, cmd.finalize_options)
+ with pytest.raises(DistutilsOptionError):
+ cmd.finalize_options()
# can't combine user with prefix/exec_prefix/home or
# install_(plat)base
cmd.prefix = None
cmd.user = 'user'
- self.assertRaises(DistutilsOptionError, cmd.finalize_options)
+ with pytest.raises(DistutilsOptionError):
+ cmd.finalize_options()
def test_record(self):
install_dir = self.mkdtemp()
- project_dir, dist = self.create_dist(py_modules=['hello'],
- scripts=['sayhi'])
+ project_dir, dist = self.create_dist(py_modules=['hello'], scripts=['sayhi'])
os.chdir(project_dir)
self.write_file('hello.py', "def main(): print('o hai')")
self.write_file('sayhi', 'from hello import main; main()')
@@ -206,18 +204,22 @@ class InstallTestCase(support.TempdirManager,
f.close()
found = [os.path.basename(line) for line in content.splitlines()]
- expected = ['hello.py', 'hello.%s.pyc' % sys.implementation.cache_tag,
- 'sayhi',
- 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
- self.assertEqual(found, expected)
+ expected = [
+ 'hello.py',
+ 'hello.%s.pyc' % sys.implementation.cache_tag,
+ 'sayhi',
+ 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2],
+ ]
+ assert found == expected
def test_record_extensions(self):
cmd = test_support.missing_compiler_executable()
if cmd is not None:
- self.skipTest('The %r command is not found' % cmd)
+ pytest.skip('The %r command is not found' % cmd)
install_dir = self.mkdtemp()
- project_dir, dist = self.create_dist(ext_modules=[
- Extension('xx', ['xxmodule.c'])])
+ project_dir, dist = self.create_dist(
+ ext_modules=[Extension('xx', ['xxmodule.c'])]
+ )
os.chdir(project_dir)
support.copy_xxmodule_c(project_dir)
@@ -233,31 +235,18 @@ class InstallTestCase(support.TempdirManager,
cmd.ensure_finalized()
cmd.run()
- f = open(cmd.record)
- try:
- content = f.read()
- finally:
- f.close()
+ content = pathlib.Path(cmd.record).read_text()
found = [os.path.basename(line) for line in content.splitlines()]
- expected = [_make_ext_name('xx'),
- 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
- self.assertEqual(found, expected)
+ expected = [
+ _make_ext_name('xx'),
+ 'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2],
+ ]
+ assert found == expected
- def test_debug_mode(self):
+ def test_debug_mode(self, caplog, monkeypatch):
# this covers the code called when DEBUG is set
- old_logs_len = len(self.logs)
- install_module.DEBUG = True
- try:
- with captured_stdout():
- self.test_record()
- finally:
- install_module.DEBUG = False
- self.assertGreater(len(self.logs), old_logs_len)
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(InstallTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ monkeypatch.setattr(install_module, 'DEBUG', True)
+ caplog.set_level(logging.DEBUG)
+ self.test_record()
+ assert any(rec for rec in caplog.records if rec.levelno == logging.DEBUG)
diff --git a/setuptools/_distutils/tests/test_install_data.py b/setuptools/_distutils/tests/test_install_data.py
index 6191d2fa..9badbc26 100644
--- a/setuptools/_distutils/tests/test_install_data.py
+++ b/setuptools/_distutils/tests/test_install_data.py
@@ -1,16 +1,16 @@
"""Tests for distutils.command.install_data."""
import os
-import unittest
+
+import pytest
from distutils.command.install_data import install_data
from distutils.tests import support
-from test.support import run_unittest
-class InstallDataTestCase(support.TempdirManager,
- support.LoggingSilencer,
- support.EnvironGuard,
- unittest.TestCase):
+@pytest.mark.usefixtures('save_env')
+class TestInstallData(
+ support.TempdirManager,
+):
def test_simple_run(self):
pkg_dir, dist = self.create_dist()
cmd = install_data(dist)
@@ -26,18 +26,18 @@ class InstallDataTestCase(support.TempdirManager,
self.write_file(two, 'xxx')
cmd.data_files = [one, (inst2, [two])]
- self.assertEqual(cmd.get_inputs(), [one, (inst2, [two])])
+ assert cmd.get_inputs() == [one, (inst2, [two])]
# let's run the command
cmd.ensure_finalized()
cmd.run()
# let's check the result
- self.assertEqual(len(cmd.get_outputs()), 2)
+ assert len(cmd.get_outputs()) == 2
rtwo = os.path.split(two)[-1]
- self.assertTrue(os.path.exists(os.path.join(inst2, rtwo)))
+ assert os.path.exists(os.path.join(inst2, rtwo))
rone = os.path.split(one)[-1]
- self.assertTrue(os.path.exists(os.path.join(inst, rone)))
+ assert os.path.exists(os.path.join(inst, rone))
cmd.outfiles = []
# let's try with warn_dir one
@@ -46,30 +46,21 @@ class InstallDataTestCase(support.TempdirManager,
cmd.run()
# let's check the result
- self.assertEqual(len(cmd.get_outputs()), 2)
- self.assertTrue(os.path.exists(os.path.join(inst2, rtwo)))
- self.assertTrue(os.path.exists(os.path.join(inst, rone)))
+ assert len(cmd.get_outputs()) == 2
+ assert os.path.exists(os.path.join(inst2, rtwo))
+ assert os.path.exists(os.path.join(inst, rone))
cmd.outfiles = []
# now using root and empty dir
cmd.root = os.path.join(pkg_dir, 'root')
- inst3 = os.path.join(cmd.install_dir, 'inst3')
inst4 = os.path.join(pkg_dir, 'inst4')
three = os.path.join(cmd.install_dir, 'three')
self.write_file(three, 'xx')
- cmd.data_files = [one, (inst2, [two]),
- ('inst3', [three]),
- (inst4, [])]
+ cmd.data_files = [one, (inst2, [two]), ('inst3', [three]), (inst4, [])]
cmd.ensure_finalized()
cmd.run()
# let's check the result
- self.assertEqual(len(cmd.get_outputs()), 4)
- self.assertTrue(os.path.exists(os.path.join(inst2, rtwo)))
- self.assertTrue(os.path.exists(os.path.join(inst, rone)))
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(InstallDataTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert len(cmd.get_outputs()) == 4
+ assert os.path.exists(os.path.join(inst2, rtwo))
+ assert os.path.exists(os.path.join(inst, rone))
diff --git a/setuptools/_distutils/tests/test_install_headers.py b/setuptools/_distutils/tests/test_install_headers.py
index 1aa4d09c..1e8ccf79 100644
--- a/setuptools/_distutils/tests/test_install_headers.py
+++ b/setuptools/_distutils/tests/test_install_headers.py
@@ -1,16 +1,16 @@
"""Tests for distutils.command.install_headers."""
import os
-import unittest
+
+import pytest
from distutils.command.install_headers import install_headers
from distutils.tests import support
-from test.support import run_unittest
-class InstallHeadersTestCase(support.TempdirManager,
- support.LoggingSilencer,
- support.EnvironGuard,
- unittest.TestCase):
+@pytest.mark.usefixtures('save_env')
+class TestInstallHeaders(
+ support.TempdirManager,
+):
def test_simple_run(self):
# we have two headers
header_list = self.mkdtemp()
@@ -22,7 +22,7 @@ class InstallHeadersTestCase(support.TempdirManager,
pkg_dir, dist = self.create_dist(headers=headers)
cmd = install_headers(dist)
- self.assertEqual(cmd.get_inputs(), headers)
+ assert cmd.get_inputs() == headers
# let's run the command
cmd.install_dir = os.path.join(pkg_dir, 'inst')
@@ -30,10 +30,4 @@ class InstallHeadersTestCase(support.TempdirManager,
cmd.run()
# let's check the results
- self.assertEqual(len(cmd.get_outputs()), 2)
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(InstallHeadersTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert len(cmd.get_outputs()) == 2
diff --git a/setuptools/_distutils/tests/test_install_lib.py b/setuptools/_distutils/tests/test_install_lib.py
index 652653f2..0bd67cd0 100644
--- a/setuptools/_distutils/tests/test_install_lib.py
+++ b/setuptools/_distutils/tests/test_install_lib.py
@@ -2,39 +2,41 @@
import sys
import os
import importlib.util
-import unittest
+
+import pytest
from distutils.command.install_lib import install_lib
from distutils.extension import Extension
from distutils.tests import support
from distutils.errors import DistutilsOptionError
-from test.support import run_unittest
-
-class InstallLibTestCase(support.TempdirManager,
- support.LoggingSilencer,
- support.EnvironGuard,
- unittest.TestCase):
+@support.combine_markers
+@pytest.mark.usefixtures('save_env')
+class TestInstallLib(
+ support.TempdirManager,
+):
def test_finalize_options(self):
dist = self.create_dist()[1]
cmd = install_lib(dist)
cmd.finalize_options()
- self.assertEqual(cmd.compile, 1)
- self.assertEqual(cmd.optimize, 0)
+ assert cmd.compile == 1
+ assert cmd.optimize == 0
# optimize must be 0, 1, or 2
cmd.optimize = 'foo'
- self.assertRaises(DistutilsOptionError, cmd.finalize_options)
+ with pytest.raises(DistutilsOptionError):
+ cmd.finalize_options()
cmd.optimize = '4'
- self.assertRaises(DistutilsOptionError, cmd.finalize_options)
+ with pytest.raises(DistutilsOptionError):
+ cmd.finalize_options()
cmd.optimize = '2'
cmd.finalize_options()
- self.assertEqual(cmd.optimize, 2)
+ assert cmd.optimize == 2
- @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
+ @pytest.mark.skipif('sys.dont_write_bytecode')
def test_byte_compile(self):
project_dir, dist = self.create_dist()
os.chdir(project_dir)
@@ -45,10 +47,11 @@ class InstallLibTestCase(support.TempdirManager,
self.write_file(f, '# python file')
cmd.byte_compile([f])
pyc_file = importlib.util.cache_from_source('foo.py', optimization='')
- pyc_opt_file = importlib.util.cache_from_source('foo.py',
- optimization=cmd.optimize)
- self.assertTrue(os.path.exists(pyc_file))
- self.assertTrue(os.path.exists(pyc_opt_file))
+ pyc_opt_file = importlib.util.cache_from_source(
+ 'foo.py', optimization=cmd.optimize
+ )
+ assert os.path.exists(pyc_file)
+ assert os.path.exists(pyc_opt_file)
def test_get_outputs(self):
project_dir, dist = self.create_dist()
@@ -68,7 +71,7 @@ class InstallLibTestCase(support.TempdirManager,
# get_outputs should return 4 elements: spam/__init__.py and .pyc,
# foo.import-tag-abiflags.so / foo.pyd
outputs = cmd.get_outputs()
- self.assertEqual(len(outputs), 4, outputs)
+ assert len(outputs) == 4, outputs
def test_get_inputs(self):
project_dir, dist = self.create_dist()
@@ -88,9 +91,9 @@ class InstallLibTestCase(support.TempdirManager,
# get_inputs should return 2 elements: spam/__init__.py and
# foo.import-tag-abiflags.so / foo.pyd
inputs = cmd.get_inputs()
- self.assertEqual(len(inputs), 2, inputs)
+ assert len(inputs) == 2, inputs
- def test_dont_write_bytecode(self):
+ def test_dont_write_bytecode(self, caplog):
# makes sure byte_compile is not used
dist = self.create_dist()[1]
cmd = install_lib(dist)
@@ -104,12 +107,4 @@ class InstallLibTestCase(support.TempdirManager,
finally:
sys.dont_write_bytecode = old_dont_write_bytecode
- self.assertIn('byte-compiling is disabled',
- self.logs[0][1] % self.logs[0][2])
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(InstallLibTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert 'byte-compiling is disabled' in caplog.messages[0]
diff --git a/setuptools/_distutils/tests/test_install_scripts.py b/setuptools/_distutils/tests/test_install_scripts.py
index 648db3b1..58313f28 100644
--- a/setuptools/_distutils/tests/test_install_scripts.py
+++ b/setuptools/_distutils/tests/test_install_scripts.py
@@ -1,40 +1,34 @@
"""Tests for distutils.command.install_scripts."""
import os
-import unittest
from distutils.command.install_scripts import install_scripts
from distutils.core import Distribution
from distutils.tests import support
-from test.support import run_unittest
-class InstallScriptsTestCase(support.TempdirManager,
- support.LoggingSilencer,
- unittest.TestCase):
-
+class TestInstallScripts(support.TempdirManager):
def test_default_settings(self):
dist = Distribution()
- dist.command_obj["build"] = support.DummyCommand(
- build_scripts="/foo/bar")
+ dist.command_obj["build"] = support.DummyCommand(build_scripts="/foo/bar")
dist.command_obj["install"] = support.DummyCommand(
install_scripts="/splat/funk",
force=1,
skip_build=1,
- )
+ )
cmd = install_scripts(dist)
- self.assertFalse(cmd.force)
- self.assertFalse(cmd.skip_build)
- self.assertIsNone(cmd.build_dir)
- self.assertIsNone(cmd.install_dir)
+ assert not cmd.force
+ assert not cmd.skip_build
+ assert cmd.build_dir is None
+ assert cmd.install_dir is None
cmd.finalize_options()
- self.assertTrue(cmd.force)
- self.assertTrue(cmd.skip_build)
- self.assertEqual(cmd.build_dir, "/foo/bar")
- self.assertEqual(cmd.install_dir, "/splat/funk")
+ assert cmd.force
+ assert cmd.skip_build
+ assert cmd.build_dir == "/foo/bar"
+ assert cmd.install_dir == "/splat/funk"
def test_installation(self):
source = self.mkdtemp()
@@ -48,15 +42,21 @@ class InstallScriptsTestCase(support.TempdirManager,
finally:
f.close()
- write_script("script1.py", ("#! /usr/bin/env python2.3\n"
- "# bogus script w/ Python sh-bang\n"
- "pass\n"))
- write_script("script2.py", ("#!/usr/bin/python\n"
- "# bogus script w/ Python sh-bang\n"
- "pass\n"))
- write_script("shell.sh", ("#!/bin/sh\n"
- "# bogus shell script w/ sh-bang\n"
- "exit 0\n"))
+ write_script(
+ "script1.py",
+ (
+ "#! /usr/bin/env python2.3\n"
+ "# bogus script w/ Python sh-bang\n"
+ "pass\n"
+ ),
+ )
+ write_script(
+ "script2.py",
+ ("#!/usr/bin/python\n" "# bogus script w/ Python sh-bang\n" "pass\n"),
+ )
+ write_script(
+ "shell.sh", ("#!/bin/sh\n" "# bogus shell script w/ sh-bang\n" "exit 0\n")
+ )
target = self.mkdtemp()
dist = Distribution()
@@ -65,18 +65,11 @@ class InstallScriptsTestCase(support.TempdirManager,
install_scripts=target,
force=1,
skip_build=1,
- )
+ )
cmd = install_scripts(dist)
cmd.finalize_options()
cmd.run()
installed = os.listdir(target)
for name in expected:
- self.assertIn(name, installed)
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(InstallScriptsTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert name in installed
diff --git a/setuptools/_distutils/tests/test_log.py b/setuptools/_distutils/tests/test_log.py
index ec2ae028..ec6a0c80 100644
--- a/setuptools/_distutils/tests/test_log.py
+++ b/setuptools/_distutils/tests/test_log.py
@@ -1,46 +1,13 @@
"""Tests for distutils.log"""
-import io
-import sys
-import unittest
-from test.support import swap_attr, run_unittest
+import logging
-from distutils import log
+from distutils._log import log
-class TestLog(unittest.TestCase):
- def test_non_ascii(self):
- # Issues #8663, #34421: test that non-encodable text is escaped with
- # backslashreplace error handler and encodable non-ASCII text is
- # output as is.
- for errors in ('strict', 'backslashreplace', 'surrogateescape',
- 'replace', 'ignore'):
- with self.subTest(errors=errors):
- stdout = io.TextIOWrapper(io.BytesIO(),
- encoding='cp437', errors=errors)
- stderr = io.TextIOWrapper(io.BytesIO(),
- encoding='cp437', errors=errors)
- old_threshold = log.set_threshold(log.DEBUG)
- try:
- with swap_attr(sys, 'stdout', stdout), \
- swap_attr(sys, 'stderr', stderr):
- log.debug('Dεbug\tMėssãge')
- log.fatal('Fαtal\tÈrrōr')
- finally:
- log.set_threshold(old_threshold)
- stdout.seek(0)
- self.assertEqual(stdout.read().rstrip(),
- 'Dεbug\tM?ss?ge' if errors == 'replace' else
- 'Dεbug\tMssge' if errors == 'ignore' else
- 'Dεbug\tM\\u0117ss\\xe3ge')
- stderr.seek(0)
- self.assertEqual(stderr.read().rstrip(),
- 'Fαtal\t?rr?r' if errors == 'replace' else
- 'Fαtal\trrr' if errors == 'ignore' else
- 'Fαtal\t\\xc8rr\\u014dr')
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(TestLog)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+class TestLog:
+ def test_non_ascii(self, caplog):
+ caplog.set_level(logging.DEBUG)
+ log.debug('Dεbug\tMėssãge')
+ log.fatal('Fαtal\tÈrrōr')
+ assert caplog.messages == ['Dεbug\tMėssãge', 'Fαtal\tÈrrōr']
diff --git a/setuptools/_distutils/tests/test_msvc9compiler.py b/setuptools/_distutils/tests/test_msvc9compiler.py
index 6235405e..fe5693e1 100644
--- a/setuptools/_distutils/tests/test_msvc9compiler.py
+++ b/setuptools/_distutils/tests/test_msvc9compiler.py
@@ -1,11 +1,10 @@
"""Tests for distutils.msvc9compiler."""
import sys
-import unittest
import os
from distutils.errors import DistutilsPlatformError
from distutils.tests import support
-from test.support import run_unittest
+import pytest
# A manifest with the only assembly reference being the msvcrt assembly, so
# should have the assembly completely stripped. Note that although the
@@ -90,56 +89,62 @@ _CLEANED_MANIFEST = """\
</dependency>
</assembly>"""
-if sys.platform=="win32":
+if sys.platform == "win32":
from distutils.msvccompiler import get_build_version
- if get_build_version()>=8.0:
+
+ if get_build_version() >= 8.0:
SKIP_MESSAGE = None
else:
SKIP_MESSAGE = "These tests are only for MSVC8.0 or above"
else:
SKIP_MESSAGE = "These tests are only for win32"
-@unittest.skipUnless(SKIP_MESSAGE is None, SKIP_MESSAGE)
-class msvc9compilerTestCase(support.TempdirManager,
- unittest.TestCase):
+@pytest.mark.skipif('SKIP_MESSAGE', reason=SKIP_MESSAGE)
+class Testmsvc9compiler(support.TempdirManager):
def test_no_compiler(self):
# makes sure query_vcvarsall raises
# a DistutilsPlatformError if the compiler
# is not found
from distutils.msvc9compiler import query_vcvarsall
+
def _find_vcvarsall(version):
return None
from distutils import msvc9compiler
+
old_find_vcvarsall = msvc9compiler.find_vcvarsall
msvc9compiler.find_vcvarsall = _find_vcvarsall
try:
- self.assertRaises(DistutilsPlatformError, query_vcvarsall,
- 'wont find this version')
+ with pytest.raises(DistutilsPlatformError):
+ query_vcvarsall('wont find this version')
finally:
msvc9compiler.find_vcvarsall = old_find_vcvarsall
def test_reg_class(self):
from distutils.msvc9compiler import Reg
- self.assertRaises(KeyError, Reg.get_value, 'xxx', 'xxx')
+
+ with pytest.raises(KeyError):
+ Reg.get_value('xxx', 'xxx')
# looking for values that should exist on all
# windows registry versions.
path = r'Control Panel\Desktop'
v = Reg.get_value(path, 'dragfullwindows')
- self.assertIn(v, ('0', '1', '2'))
+ assert v in ('0', '1', '2')
import winreg
+
HKCU = winreg.HKEY_CURRENT_USER
keys = Reg.read_keys(HKCU, 'xxxx')
- self.assertEqual(keys, None)
+ assert keys is None
keys = Reg.read_keys(HKCU, r'Control Panel')
- self.assertIn('Desktop', keys)
+ assert 'Desktop' in keys
def test_remove_visual_c_ref(self):
from distutils.msvc9compiler import MSVCCompiler
+
tempdir = self.mkdtemp()
manifest = os.path.join(tempdir, 'manifest')
f = open(manifest, 'w')
@@ -160,10 +165,11 @@ class msvc9compilerTestCase(support.TempdirManager,
f.close()
# makes sure the manifest was properly cleaned
- self.assertEqual(content, _CLEANED_MANIFEST)
+ assert content == _CLEANED_MANIFEST
def test_remove_entire_manifest(self):
from distutils.msvc9compiler import MSVCCompiler
+
tempdir = self.mkdtemp()
manifest = os.path.join(tempdir, 'manifest')
f = open(manifest, 'w')
@@ -174,11 +180,4 @@ class msvc9compilerTestCase(support.TempdirManager,
compiler = MSVCCompiler()
got = compiler._remove_visual_c_ref(manifest)
- self.assertIsNone(got)
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(msvc9compilerTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert got is None
diff --git a/setuptools/_distutils/tests/test_msvccompiler.py b/setuptools/_distutils/tests/test_msvccompiler.py
index 846e5bb8..f63537b8 100644
--- a/setuptools/_distutils/tests/test_msvccompiler.py
+++ b/setuptools/_distutils/tests/test_msvccompiler.py
@@ -1,23 +1,21 @@
"""Tests for distutils._msvccompiler."""
import sys
-import unittest
import os
import threading
+import unittest.mock as mock
+
+import pytest
from distutils.errors import DistutilsPlatformError
from distutils.tests import support
-from test.support import run_unittest
+from distutils import _msvccompiler
-SKIP_MESSAGE = (None if sys.platform == "win32" else
- "These tests are only for win32")
+needs_winreg = pytest.mark.skipif('not hasattr(_msvccompiler, "winreg")')
-@unittest.skipUnless(SKIP_MESSAGE is None, SKIP_MESSAGE)
-class msvccompilerTestCase(support.TempdirManager,
- unittest.TestCase):
+class Testmsvccompiler(support.TempdirManager):
def test_no_compiler(self):
- import distutils._msvccompiler as _msvccompiler
# makes sure query_vcvarsall raises
# a DistutilsPlatformError if the compiler
# is not found
@@ -27,15 +25,15 @@ class msvccompilerTestCase(support.TempdirManager,
old_find_vcvarsall = _msvccompiler._find_vcvarsall
_msvccompiler._find_vcvarsall = _find_vcvarsall
try:
- self.assertRaises(DistutilsPlatformError,
- _msvccompiler._get_vc_env,
- 'wont find this version')
+ with pytest.raises(DistutilsPlatformError):
+ _msvccompiler._get_vc_env(
+ 'wont find this version',
+ )
finally:
_msvccompiler._find_vcvarsall = old_find_vcvarsall
+ @needs_winreg
def test_get_vc_env_unicode(self):
- import distutils._msvccompiler as _msvccompiler
-
test_var = 'ṰḖṤṪ┅ṼẨṜ'
test_value = '₃⁴₅'
@@ -44,36 +42,25 @@ class msvccompilerTestCase(support.TempdirManager,
os.environ[test_var] = test_value
try:
env = _msvccompiler._get_vc_env('x86')
- self.assertIn(test_var.lower(), env)
- self.assertEqual(test_value, env[test_var.lower()])
+ assert test_var.lower() in env
+ assert test_value == env[test_var.lower()]
finally:
os.environ.pop(test_var)
if old_distutils_use_sdk:
os.environ['DISTUTILS_USE_SDK'] = old_distutils_use_sdk
- def test_get_vc2017(self):
- import distutils._msvccompiler as _msvccompiler
-
- # This function cannot be mocked, so pass it if we find VS 2017
- # and mark it skipped if we do not.
- version, path = _msvccompiler._find_vc2017()
- if version:
- self.assertGreaterEqual(version, 15)
- self.assertTrue(os.path.isdir(path))
- else:
- raise unittest.SkipTest("VS 2017 is not installed")
-
- def test_get_vc2015(self):
- import distutils._msvccompiler as _msvccompiler
-
- # This function cannot be mocked, so pass it if we find VS 2015
- # and mark it skipped if we do not.
- version, path = _msvccompiler._find_vc2015()
- if version:
- self.assertGreaterEqual(version, 14)
- self.assertTrue(os.path.isdir(path))
- else:
- raise unittest.SkipTest("VS 2015 is not installed")
+ @needs_winreg
+ @pytest.mark.parametrize('ver', (2015, 2017))
+ def test_get_vc(self, ver):
+ # This function cannot be mocked, so pass if VC is found
+ # and skip otherwise.
+ lookup = getattr(_msvccompiler, f'_find_vc{ver}')
+ expected_version = {2015: 14, 2017: 15}[ver]
+ version, path = lookup()
+ if not version:
+ pytest.skip(f"VS {ver} is not installed")
+ assert version >= expected_version
+ assert os.path.isdir(path)
class CheckThread(threading.Thread):
@@ -89,20 +76,18 @@ class CheckThread(threading.Thread):
return not self.exc_info
-class TestSpawn(unittest.TestCase):
+class TestSpawn:
def test_concurrent_safe(self):
"""
Concurrent calls to spawn should have consistent results.
"""
- import distutils._msvccompiler as _msvccompiler
compiler = _msvccompiler.MSVCCompiler()
compiler._paths = "expected"
inner_cmd = 'import os; assert os.environ["PATH"] == "expected"'
command = [sys.executable, '-c', inner_cmd]
threads = [
- CheckThread(target=compiler.spawn, args=[command])
- for n in range(100)
+ CheckThread(target=compiler.spawn, args=[command]) for n in range(100)
]
for thread in threads:
thread.start()
@@ -115,8 +100,8 @@ class TestSpawn(unittest.TestCase):
If CCompiler.spawn has been monkey-patched without support
for an env, it should still execute.
"""
- import distutils._msvccompiler as _msvccompiler
from distutils import ccompiler
+
compiler = _msvccompiler.MSVCCompiler()
compiler._paths = "expected"
@@ -124,15 +109,7 @@ class TestSpawn(unittest.TestCase):
"A spawn without an env argument."
assert os.environ["PATH"] == "expected"
- with unittest.mock.patch.object(
- ccompiler.CCompiler, 'spawn', CCompiler_spawn):
+ with mock.patch.object(ccompiler.CCompiler, 'spawn', CCompiler_spawn):
compiler.spawn(["n/a"])
assert os.environ.get("PATH") != "expected"
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(msvccompilerTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
diff --git a/setuptools/_distutils/tests/test_register.py b/setuptools/_distutils/tests/test_register.py
index 5770ed58..a10393b5 100644
--- a/setuptools/_distutils/tests/test_register.py
+++ b/setuptools/_distutils/tests/test_register.py
@@ -1,20 +1,14 @@
"""Tests for distutils.command.register."""
import os
-import unittest
import getpass
import urllib
-import warnings
-
-from test.support import run_unittest
-
-from .py38compat import check_warnings
from distutils.command import register as register_module
from distutils.command.register import register
from distutils.errors import DistutilsSetupError
-from distutils.log import INFO
from distutils.tests.test_config import BasePyPIRCCommandTestCase
+import pytest
try:
import docutils
@@ -41,8 +35,10 @@ username:tarek
password:password
"""
-class Inputs(object):
+
+class Inputs:
"""Fakes user inputs."""
+
def __init__(self, *answers):
self.answers = answers
self.index = 0
@@ -53,8 +49,10 @@ class Inputs(object):
finally:
self.index += 1
-class FakeOpener(object):
+
+class FakeOpener:
"""Fakes a PyPI server"""
+
def __init__(self):
self.reqs = []
@@ -71,33 +69,33 @@ class FakeOpener(object):
def getheader(self, name, default=None):
return {
'content-type': 'text/plain; charset=utf-8',
- }.get(name.lower(), default)
+ }.get(name.lower(), default)
+
+@pytest.fixture(autouse=True)
+def autopass(monkeypatch):
+ monkeypatch.setattr(getpass, 'getpass', lambda prompt: 'password')
-class RegisterTestCase(BasePyPIRCCommandTestCase):
- def setUp(self):
- super(RegisterTestCase, self).setUp()
- # patching the password prompt
- self._old_getpass = getpass.getpass
- def _getpass(prompt):
- return 'password'
- getpass.getpass = _getpass
- urllib.request._opener = None
- self.old_opener = urllib.request.build_opener
- self.conn = urllib.request.build_opener = FakeOpener()
+@pytest.fixture(autouse=True)
+def fake_opener(monkeypatch, request):
+ opener = FakeOpener()
+ monkeypatch.setattr(urllib.request, 'build_opener', opener)
+ monkeypatch.setattr(urllib.request, '_opener', None)
+ request.instance.conn = opener
- def tearDown(self):
- getpass.getpass = self._old_getpass
- urllib.request._opener = None
- urllib.request.build_opener = self.old_opener
- super(RegisterTestCase, self).tearDown()
+class TestRegister(BasePyPIRCCommandTestCase):
def _get_cmd(self, metadata=None):
if metadata is None:
- metadata = {'url': 'xxx', 'author': 'xxx',
- 'author_email': 'xxx',
- 'name': 'xxx', 'version': 'xxx'}
+ metadata = {
+ 'url': 'xxx',
+ 'author': 'xxx',
+ 'author_email': 'xxx',
+ 'name': 'xxx',
+ 'version': 'xxx',
+ 'long_description': 'xxx',
+ }
pkg_info, dist = self.create_dist(**metadata)
return register(dist)
@@ -109,7 +107,7 @@ class RegisterTestCase(BasePyPIRCCommandTestCase):
cmd = self._get_cmd()
# we shouldn't have a .pypirc file yet
- self.assertFalse(os.path.exists(self.rc))
+ assert not os.path.exists(self.rc)
# patching input and getpass.getpass
# so register gets happy
@@ -128,13 +126,13 @@ class RegisterTestCase(BasePyPIRCCommandTestCase):
del register_module.input
# we should have a brand new .pypirc file
- self.assertTrue(os.path.exists(self.rc))
+ assert os.path.exists(self.rc)
# with the content similar to WANTED_PYPIRC
f = open(self.rc)
try:
content = f.read()
- self.assertEqual(content, WANTED_PYPIRC)
+ assert content == WANTED_PYPIRC
finally:
f.close()
@@ -143,6 +141,7 @@ class RegisterTestCase(BasePyPIRCCommandTestCase):
# if we run the command again
def _no_way(prompt=''):
raise AssertionError(prompt)
+
register_module.input = _no_way
cmd.show_response = 1
@@ -150,13 +149,13 @@ class RegisterTestCase(BasePyPIRCCommandTestCase):
# let's see what the server received : we should
# have 2 similar requests
- self.assertEqual(len(self.conn.reqs), 2)
+ assert len(self.conn.reqs) == 2
req1 = dict(self.conn.reqs[0].headers)
req2 = dict(self.conn.reqs[1].headers)
- self.assertEqual(req1['Content-length'], '1374')
- self.assertEqual(req2['Content-length'], '1374')
- self.assertIn(b'xxx', self.conn.reqs[1].data)
+ assert req1['Content-length'] == '1358'
+ assert req2['Content-length'] == '1358'
+ assert b'xxx' in self.conn.reqs[1].data
def test_password_not_in_file(self):
@@ -168,7 +167,7 @@ class RegisterTestCase(BasePyPIRCCommandTestCase):
# dist.password should be set
# therefore used afterwards by other commands
- self.assertEqual(cmd.distribution.password, 'password')
+ assert cmd.distribution.password == 'password'
def test_registering(self):
# this test runs choice 2
@@ -182,11 +181,11 @@ class RegisterTestCase(BasePyPIRCCommandTestCase):
del register_module.input
# we should have send a request
- self.assertEqual(len(self.conn.reqs), 1)
+ assert len(self.conn.reqs) == 1
req = self.conn.reqs[0]
headers = dict(req.headers)
- self.assertEqual(headers['Content-length'], '608')
- self.assertIn(b'tarek', req.data)
+ assert headers['Content-length'] == '608'
+ assert b'tarek' in req.data
def test_password_reset(self):
# this test runs choice 3
@@ -200,35 +199,42 @@ class RegisterTestCase(BasePyPIRCCommandTestCase):
del register_module.input
# we should have send a request
- self.assertEqual(len(self.conn.reqs), 1)
+ assert len(self.conn.reqs) == 1
req = self.conn.reqs[0]
headers = dict(req.headers)
- self.assertEqual(headers['Content-length'], '290')
- self.assertIn(b'tarek', req.data)
+ assert headers['Content-length'] == '290'
+ assert b'tarek' in req.data
- @unittest.skipUnless(docutils is not None, 'needs docutils')
def test_strict(self):
- # testing the script option
+ # testing the strict option
# when on, the register command stops if
# the metadata is incomplete or if
# long_description is not reSt compliant
+ pytest.importorskip('docutils')
+
# empty metadata
cmd = self._get_cmd({})
cmd.ensure_finalized()
cmd.strict = 1
- self.assertRaises(DistutilsSetupError, cmd.run)
+ with pytest.raises(DistutilsSetupError):
+ cmd.run()
# metadata are OK but long_description is broken
- metadata = {'url': 'xxx', 'author': 'xxx',
- 'author_email': 'éxéxé',
- 'name': 'xxx', 'version': 'xxx',
- 'long_description': 'title\n==\n\ntext'}
+ metadata = {
+ 'url': 'xxx',
+ 'author': 'xxx',
+ 'author_email': 'éxéxé',
+ 'name': 'xxx',
+ 'version': 'xxx',
+ 'long_description': 'title\n==\n\ntext',
+ }
cmd = self._get_cmd(metadata)
cmd.ensure_finalized()
cmd.strict = 1
- self.assertRaises(DistutilsSetupError, cmd.run)
+ with pytest.raises(DistutilsSetupError):
+ cmd.run()
# now something that works
metadata['long_description'] = 'title\n=====\n\ntext'
@@ -255,11 +261,15 @@ class RegisterTestCase(BasePyPIRCCommandTestCase):
del register_module.input
# and finally a Unicode test (bug #12114)
- metadata = {'url': 'xxx', 'author': '\u00c9ric',
- 'author_email': 'xxx', 'name': 'xxx',
- 'version': 'xxx',
- 'description': 'Something about esszet \u00df',
- 'long_description': 'More things about esszet \u00df'}
+ metadata = {
+ 'url': 'xxx',
+ 'author': '\u00c9ric',
+ 'author_email': 'xxx',
+ 'name': 'xxx',
+ 'version': 'xxx',
+ 'description': 'Something about esszet \u00df',
+ 'long_description': 'More things about esszet \u00df',
+ }
cmd = self._get_cmd(metadata)
cmd.ensure_finalized()
@@ -272,38 +282,33 @@ class RegisterTestCase(BasePyPIRCCommandTestCase):
finally:
del register_module.input
- @unittest.skipUnless(docutils is not None, 'needs docutils')
- def test_register_invalid_long_description(self):
+ def test_register_invalid_long_description(self, monkeypatch):
+ pytest.importorskip('docutils')
description = ':funkie:`str`' # mimic Sphinx-specific markup
- metadata = {'url': 'xxx', 'author': 'xxx',
- 'author_email': 'xxx',
- 'name': 'xxx', 'version': 'xxx',
- 'long_description': description}
+ metadata = {
+ 'url': 'xxx',
+ 'author': 'xxx',
+ 'author_email': 'xxx',
+ 'name': 'xxx',
+ 'version': 'xxx',
+ 'long_description': description,
+ }
cmd = self._get_cmd(metadata)
cmd.ensure_finalized()
cmd.strict = True
inputs = Inputs('2', 'tarek', 'tarek@ziade.org')
- register_module.input = inputs
- self.addCleanup(delattr, register_module, 'input')
+ monkeypatch.setattr(register_module, 'input', inputs, raising=False)
- self.assertRaises(DistutilsSetupError, cmd.run)
-
- def test_check_metadata_deprecated(self):
- # makes sure make_metadata is deprecated
- cmd = self._get_cmd()
- with check_warnings() as w:
- warnings.simplefilter("always")
- cmd.check_metadata()
- self.assertEqual(len(w.warnings), 1)
+ with pytest.raises(DistutilsSetupError):
+ cmd.run()
- def test_list_classifiers(self):
+ def test_list_classifiers(self, caplog):
cmd = self._get_cmd()
cmd.list_classifiers = 1
cmd.run()
- results = self.get_logs(INFO)
- self.assertEqual(results, ['running check', 'xxx'])
+ assert caplog.messages == ['running check', 'xxx']
- def test_show_response(self):
+ def test_show_response(self, caplog):
# test that the --show-response option return a well formatted response
cmd = self._get_cmd()
inputs = Inputs('1', 'tarek', 'y')
@@ -314,12 +319,4 @@ class RegisterTestCase(BasePyPIRCCommandTestCase):
finally:
del register_module.input
- results = self.get_logs(INFO)
- self.assertEqual(results[3], 75 * '-' + '\nxxx\n' + 75 * '-')
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(RegisterTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert caplog.messages[3] == 75 * '-' + '\nxxx\n' + 75 * '-'
diff --git a/setuptools/_distutils/tests/test_sdist.py b/setuptools/_distutils/tests/test_sdist.py
index 4c51717c..97504722 100644
--- a/setuptools/_distutils/tests/test_sdist.py
+++ b/setuptools/_distutils/tests/test_sdist.py
@@ -1,28 +1,23 @@
"""Tests for distutils.command.sdist."""
import os
import tarfile
-import unittest
import warnings
import zipfile
from os.path import join
from textwrap import dedent
-from test.support import captured_stdout, run_unittest
from .unix_compat import require_unix_id, require_uid_0, pwd, grp
-from .py38compat import check_warnings
+import pytest
+import path
+import jaraco.path
-try:
- import zlib
- ZLIB_SUPPORT = True
-except ImportError:
- ZLIB_SUPPORT = False
+from .py38compat import check_warnings
from distutils.command.sdist import sdist, show_formats
from distutils.core import Distribution
from distutils.tests.test_config import BasePyPIRCCommandTestCase
from distutils.errors import DistutilsOptionError
-from distutils.spawn import find_executable
-from distutils.log import WARN
+from distutils.spawn import find_executable # noqa: F401
from distutils.filelist import FileList
from distutils.archive_util import ARCHIVE_FORMATS
@@ -48,33 +43,35 @@ somecode%(sep)sdoc.dat
somecode%(sep)sdoc.txt
"""
-class SDistTestCase(BasePyPIRCCommandTestCase):
-
- def setUp(self):
- # PyPIRCCommandTestCase creates a temp dir already
- # and put it in self.tmp_dir
- super(SDistTestCase, self).setUp()
- # setting up an environment
- self.old_path = os.getcwd()
- os.mkdir(join(self.tmp_dir, 'somecode'))
- os.mkdir(join(self.tmp_dir, 'dist'))
- # a package, and a README
- self.write_file((self.tmp_dir, 'README'), 'xxx')
- self.write_file((self.tmp_dir, 'somecode', '__init__.py'), '#')
- self.write_file((self.tmp_dir, 'setup.py'), SETUP_PY)
- os.chdir(self.tmp_dir)
-
- def tearDown(self):
- # back to normal
- os.chdir(self.old_path)
- super(SDistTestCase, self).tearDown()
+@pytest.fixture(autouse=True)
+def project_dir(request, pypirc):
+ self = request.instance
+ jaraco.path.build(
+ {
+ 'somecode': {
+ '__init__.py': '#',
+ },
+ 'README': 'xxx',
+ 'setup.py': SETUP_PY,
+ },
+ self.tmp_dir,
+ )
+ with path.Path(self.tmp_dir):
+ yield
+
+
+class TestSDist(BasePyPIRCCommandTestCase):
def get_cmd(self, metadata=None):
"""Returns a cmd"""
if metadata is None:
- metadata = {'name': 'fake', 'version': '1.0',
- 'url': 'xxx', 'author': 'xxx',
- 'author_email': 'xxx'}
+ metadata = {
+ 'name': 'fake',
+ 'version': '1.0',
+ 'url': 'xxx',
+ 'author': 'xxx',
+ 'author_email': 'xxx',
+ }
dist = Distribution(metadata)
dist.script_name = 'setup.py'
dist.packages = ['somecode']
@@ -83,7 +80,7 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
cmd.dist_dir = 'dist'
return dist, cmd
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
+ @pytest.mark.usefixtures('needs_zlib')
def test_prune_file_list(self):
# this test creates a project with some VCS dirs and an NFS rename
# file, then launches sdist to check they get pruned on all systems
@@ -93,12 +90,10 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
self.write_file((self.tmp_dir, 'somecode', '.svn', 'ok.py'), 'xxx')
os.mkdir(join(self.tmp_dir, 'somecode', '.hg'))
- self.write_file((self.tmp_dir, 'somecode', '.hg',
- 'ok'), 'xxx')
+ self.write_file((self.tmp_dir, 'somecode', '.hg', 'ok'), 'xxx')
os.mkdir(join(self.tmp_dir, 'somecode', '.git'))
- self.write_file((self.tmp_dir, 'somecode', '.git',
- 'ok'), 'xxx')
+ self.write_file((self.tmp_dir, 'somecode', '.git', 'ok'), 'xxx')
self.write_file((self.tmp_dir, 'somecode', '.nfs0001'), 'xxx')
@@ -115,7 +110,7 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
# now let's check what we have
dist_folder = join(self.tmp_dir, 'dist')
files = os.listdir(dist_folder)
- self.assertEqual(files, ['fake-1.0.zip'])
+ assert files == ['fake-1.0.zip']
zip_file = zipfile.ZipFile(join(dist_folder, 'fake-1.0.zip'))
try:
@@ -124,15 +119,19 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
zip_file.close()
# making sure everything has been pruned correctly
- expected = ['', 'PKG-INFO', 'README', 'setup.py',
- 'somecode/', 'somecode/__init__.py']
- self.assertEqual(sorted(content), ['fake-1.0/' + x for x in expected])
-
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
- @unittest.skipIf(find_executable('tar') is None,
- "The tar command is not found")
- @unittest.skipIf(find_executable('gzip') is None,
- "The gzip command is not found")
+ expected = [
+ '',
+ 'PKG-INFO',
+ 'README',
+ 'setup.py',
+ 'somecode/',
+ 'somecode/__init__.py',
+ ]
+ assert sorted(content) == ['fake-1.0/' + x for x in expected]
+
+ @pytest.mark.usefixtures('needs_zlib')
+ @pytest.mark.skipif("not find_executable('tar')")
+ @pytest.mark.skipif("not find_executable('gzip')")
def test_make_distribution(self):
# now building a sdist
dist, cmd = self.get_cmd()
@@ -146,7 +145,7 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
dist_folder = join(self.tmp_dir, 'dist')
result = os.listdir(dist_folder)
result.sort()
- self.assertEqual(result, ['fake-1.0.tar', 'fake-1.0.tar.gz'])
+ assert result == ['fake-1.0.tar', 'fake-1.0.tar.gz']
os.remove(join(dist_folder, 'fake-1.0.tar'))
os.remove(join(dist_folder, 'fake-1.0.tar.gz'))
@@ -159,9 +158,9 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
result = os.listdir(dist_folder)
result.sort()
- self.assertEqual(result, ['fake-1.0.tar', 'fake-1.0.tar.gz'])
+ assert result == ['fake-1.0.tar', 'fake-1.0.tar.gz']
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
+ @pytest.mark.usefixtures('needs_zlib')
def test_add_defaults(self):
# http://bugs.python.org/issue2279
@@ -172,8 +171,7 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
# filling data_files by pointing files
# in package_data
- dist.package_data = {'': ['*.cfg', '*.dat'],
- 'somecode': ['*.txt']}
+ dist.package_data = {'': ['*.cfg', '*.dat'], 'somecode': ['*.txt']}
self.write_file((self.tmp_dir, 'somecode', 'doc.txt'), '#')
self.write_file((self.tmp_dir, 'somecode', 'doc.dat'), '#')
@@ -193,12 +191,11 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
self.write_file((some_dir, 'file.txt'), '#')
self.write_file((some_dir, 'other_file.txt'), '#')
- dist.data_files = [('data', ['data/data.dt',
- 'buildout.cfg',
- 'inroot.txt',
- 'notexisting']),
- 'some/file.txt',
- 'some/other_file.txt']
+ dist.data_files = [
+ ('data', ['data/data.dt', 'buildout.cfg', 'inroot.txt', 'notexisting']),
+ 'some/file.txt',
+ 'some/other_file.txt',
+ ]
# adding a script
script_dir = join(self.tmp_dir, 'scripts')
@@ -215,7 +212,7 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
# now let's check what we have
dist_folder = join(self.tmp_dir, 'dist')
files = os.listdir(dist_folder)
- self.assertEqual(files, ['fake-1.0.zip'])
+ assert files == ['fake-1.0.zip']
zip_file = zipfile.ZipFile(join(dist_folder, 'fake-1.0.zip'))
try:
@@ -224,13 +221,26 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
zip_file.close()
# making sure everything was added
- expected = ['', 'PKG-INFO', 'README', 'buildout.cfg',
- 'data/', 'data/data.dt', 'inroot.txt',
- 'scripts/', 'scripts/script.py', 'setup.py',
- 'some/', 'some/file.txt', 'some/other_file.txt',
- 'somecode/', 'somecode/__init__.py', 'somecode/doc.dat',
- 'somecode/doc.txt']
- self.assertEqual(sorted(content), ['fake-1.0/' + x for x in expected])
+ expected = [
+ '',
+ 'PKG-INFO',
+ 'README',
+ 'buildout.cfg',
+ 'data/',
+ 'data/data.dt',
+ 'inroot.txt',
+ 'scripts/',
+ 'scripts/script.py',
+ 'setup.py',
+ 'some/',
+ 'some/file.txt',
+ 'some/other_file.txt',
+ 'somecode/',
+ 'somecode/__init__.py',
+ 'somecode/doc.dat',
+ 'somecode/doc.txt',
+ ]
+ assert sorted(content) == ['fake-1.0/' + x for x in expected]
# checking the MANIFEST
f = open(join(self.tmp_dir, 'MANIFEST'))
@@ -238,10 +248,14 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
manifest = f.read()
finally:
f.close()
- self.assertEqual(manifest, MANIFEST % {'sep': os.sep})
+ assert manifest == MANIFEST % {'sep': os.sep}
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
- def test_metadata_check_option(self):
+ @staticmethod
+ def warnings(messages, prefix='warning: '):
+ return [msg for msg in messages if msg.startswith(prefix)]
+
+ @pytest.mark.usefixtures('needs_zlib')
+ def test_metadata_check_option(self, caplog):
# testing the `medata-check` option
dist, cmd = self.get_cmd(metadata={})
@@ -249,19 +263,15 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
# with the `check` subcommand
cmd.ensure_finalized()
cmd.run()
- warnings = [msg for msg in self.get_logs(WARN) if
- msg.startswith('warning: check:')]
- self.assertEqual(len(warnings), 2)
+ assert len(self.warnings(caplog.messages, 'warning: check: ')) == 1
# trying with a complete set of metadata
- self.clear_logs()
+ caplog.clear()
dist, cmd = self.get_cmd()
cmd.ensure_finalized()
cmd.metadata_check = 0
cmd.run()
- warnings = [msg for msg in self.get_logs(WARN) if
- msg.startswith('warning: check:')]
- self.assertEqual(len(warnings), 0)
+ assert len(self.warnings(caplog.messages, 'warning: check: ')) == 0
def test_check_metadata_deprecated(self):
# makes sure make_metadata is deprecated
@@ -269,65 +279,68 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
with check_warnings() as w:
warnings.simplefilter("always")
cmd.check_metadata()
- self.assertEqual(len(w.warnings), 1)
+ assert len(w.warnings) == 1
- def test_show_formats(self):
- with captured_stdout() as stdout:
- show_formats()
+ def test_show_formats(self, capsys):
+ show_formats()
# the output should be a header line + one line per format
num_formats = len(ARCHIVE_FORMATS.keys())
- output = [line for line in stdout.getvalue().split('\n')
- if line.strip().startswith('--formats=')]
- self.assertEqual(len(output), num_formats)
+ output = [
+ line
+ for line in capsys.readouterr().out.split('\n')
+ if line.strip().startswith('--formats=')
+ ]
+ assert len(output) == num_formats
def test_finalize_options(self):
dist, cmd = self.get_cmd()
cmd.finalize_options()
# default options set by finalize
- self.assertEqual(cmd.manifest, 'MANIFEST')
- self.assertEqual(cmd.template, 'MANIFEST.in')
- self.assertEqual(cmd.dist_dir, 'dist')
+ assert cmd.manifest == 'MANIFEST'
+ assert cmd.template == 'MANIFEST.in'
+ assert cmd.dist_dir == 'dist'
# formats has to be a string splitable on (' ', ',') or
# a stringlist
cmd.formats = 1
- self.assertRaises(DistutilsOptionError, cmd.finalize_options)
+ with pytest.raises(DistutilsOptionError):
+ cmd.finalize_options()
cmd.formats = ['zip']
cmd.finalize_options()
# formats has to be known
cmd.formats = 'supazipa'
- self.assertRaises(DistutilsOptionError, cmd.finalize_options)
+ with pytest.raises(DistutilsOptionError):
+ cmd.finalize_options()
# the following tests make sure there is a nice error message instead
# of a traceback when parsing an invalid manifest template
- def _check_template(self, content):
+ def _check_template(self, content, caplog):
dist, cmd = self.get_cmd()
os.chdir(self.tmp_dir)
self.write_file('MANIFEST.in', content)
cmd.ensure_finalized()
cmd.filelist = FileList()
cmd.read_template()
- warnings = self.get_logs(WARN)
- self.assertEqual(len(warnings), 1)
+ assert len(self.warnings(caplog.messages)) == 1
- def test_invalid_template_unknown_command(self):
- self._check_template('taunt knights *')
+ def test_invalid_template_unknown_command(self, caplog):
+ self._check_template('taunt knights *', caplog)
- def test_invalid_template_wrong_arguments(self):
+ def test_invalid_template_wrong_arguments(self, caplog):
# this manifest command takes one argument
- self._check_template('prune')
+ self._check_template('prune', caplog)
- @unittest.skipIf(os.name != 'nt', 'test relevant for Windows only')
- def test_invalid_template_wrong_path(self):
+ @pytest.mark.skipif("platform.system() != 'Windows'")
+ def test_invalid_template_wrong_path(self, caplog):
# on Windows, trailing slashes are not allowed
# this used to crash instead of raising a warning: #8286
- self._check_template('include examples/')
+ self._check_template('include examples/', caplog)
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
+ @pytest.mark.usefixtures('needs_zlib')
def test_get_file_list(self):
# make sure MANIFEST is recalculated
dist, cmd = self.get_cmd()
@@ -341,12 +354,13 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
f = open(cmd.manifest)
try:
- manifest = [line.strip() for line in f.read().split('\n')
- if line.strip() != '']
+ manifest = [
+ line.strip() for line in f.read().split('\n') if line.strip() != ''
+ ]
finally:
f.close()
- self.assertEqual(len(manifest), 5)
+ assert len(manifest) == 5
# adding a file
self.write_file((self.tmp_dir, 'somecode', 'doc2.txt'), '#')
@@ -360,16 +374,17 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
f = open(cmd.manifest)
try:
- manifest2 = [line.strip() for line in f.read().split('\n')
- if line.strip() != '']
+ manifest2 = [
+ line.strip() for line in f.read().split('\n') if line.strip() != ''
+ ]
finally:
f.close()
# do we have the new file in MANIFEST ?
- self.assertEqual(len(manifest2), 6)
- self.assertIn('doc2.txt', manifest2[-1])
+ assert len(manifest2) == 6
+ assert 'doc2.txt' in manifest2[-1]
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
+ @pytest.mark.usefixtures('needs_zlib')
def test_manifest_marker(self):
# check that autogenerated MANIFESTs have a marker
dist, cmd = self.get_cmd()
@@ -378,22 +393,24 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
f = open(cmd.manifest)
try:
- manifest = [line.strip() for line in f.read().split('\n')
- if line.strip() != '']
+ manifest = [
+ line.strip() for line in f.read().split('\n') if line.strip() != ''
+ ]
finally:
f.close()
- self.assertEqual(manifest[0],
- '# file GENERATED by distutils, do NOT edit')
+ assert manifest[0] == '# file GENERATED by distutils, do NOT edit'
- @unittest.skipUnless(ZLIB_SUPPORT, "Need zlib support to run")
+ @pytest.mark.usefixtures('needs_zlib')
def test_manifest_comments(self):
# make sure comments don't cause exceptions or wrong includes
- contents = dedent("""\
+ contents = dedent(
+ """\
# bad.py
#bad.py
good.py
- """)
+ """
+ )
dist, cmd = self.get_cmd()
cmd.ensure_finalized()
self.write_file((self.tmp_dir, cmd.manifest), contents)
@@ -401,28 +418,31 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
self.write_file((self.tmp_dir, 'bad.py'), "# don't pick me!")
self.write_file((self.tmp_dir, '#bad.py'), "# don't pick me!")
cmd.run()
- self.assertEqual(cmd.filelist.files, ['good.py'])
+ assert cmd.filelist.files == ['good.py']
- @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run')
+ @pytest.mark.usefixtures('needs_zlib')
def test_manual_manifest(self):
# check that a MANIFEST without a marker is left alone
dist, cmd = self.get_cmd()
cmd.formats = ['gztar']
cmd.ensure_finalized()
self.write_file((self.tmp_dir, cmd.manifest), 'README.manual')
- self.write_file((self.tmp_dir, 'README.manual'),
- 'This project maintains its MANIFEST file itself.')
+ self.write_file(
+ (self.tmp_dir, 'README.manual'),
+ 'This project maintains its MANIFEST file itself.',
+ )
cmd.run()
- self.assertEqual(cmd.filelist.files, ['README.manual'])
+ assert cmd.filelist.files == ['README.manual']
f = open(cmd.manifest)
try:
- manifest = [line.strip() for line in f.read().split('\n')
- if line.strip() != '']
+ manifest = [
+ line.strip() for line in f.read().split('\n') if line.strip() != ''
+ ]
finally:
f.close()
- self.assertEqual(manifest, ['README.manual'])
+ assert manifest == ['README.manual']
archive_name = join(self.tmp_dir, 'dist', 'fake-1.0.tar.gz')
archive = tarfile.open(archive_name)
@@ -430,16 +450,17 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
filenames = [tarinfo.name for tarinfo in archive]
finally:
archive.close()
- self.assertEqual(sorted(filenames), ['fake-1.0', 'fake-1.0/PKG-INFO',
- 'fake-1.0/README.manual'])
+ assert sorted(filenames) == [
+ 'fake-1.0',
+ 'fake-1.0/PKG-INFO',
+ 'fake-1.0/README.manual',
+ ]
- @unittest.skipUnless(ZLIB_SUPPORT, "requires zlib")
+ @pytest.mark.usefixtures('needs_zlib')
@require_unix_id
@require_uid_0
- @unittest.skipIf(find_executable('tar') is None,
- "The tar command is not found")
- @unittest.skipIf(find_executable('gzip') is None,
- "The gzip command is not found")
+ @pytest.mark.skipif("not find_executable('tar')")
+ @pytest.mark.skipif("not find_executable('gzip')")
def test_make_distribution_owner_group(self):
# now building a sdist
dist, cmd = self.get_cmd()
@@ -456,8 +477,8 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
archive = tarfile.open(archive_name)
try:
for member in archive.getmembers():
- self.assertEqual(member.uid, 0)
- self.assertEqual(member.gid, 0)
+ assert member.uid == 0
+ assert member.gid == 0
finally:
archive.close()
@@ -478,12 +499,6 @@ class SDistTestCase(BasePyPIRCCommandTestCase):
# rights (see #7408)
try:
for member in archive.getmembers():
- self.assertEqual(member.uid, os.getuid())
+ assert member.uid == os.getuid()
finally:
archive.close()
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(SDistTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
diff --git a/setuptools/_distutils/tests/test_spawn.py b/setuptools/_distutils/tests/test_spawn.py
index c5ed8e2b..08a34ee2 100644
--- a/setuptools/_distutils/tests/test_spawn.py
+++ b/setuptools/_distutils/tests/test_spawn.py
@@ -2,23 +2,23 @@
import os
import stat
import sys
-import unittest.mock
-from test.support import run_unittest
+import unittest.mock as mock
+
+from test.support import unix_shell
+
+import path
-from .py35compat import unix_shell
from . import py38compat as os_helper
from distutils.spawn import find_executable
from distutils.spawn import spawn
from distutils.errors import DistutilsExecError
from distutils.tests import support
+import pytest
-class SpawnTestCase(support.TempdirManager,
- support.LoggingSilencer,
- unittest.TestCase):
- @unittest.skipUnless(os.name in ('nt', 'posix'),
- 'Runs only under posix or nt')
+class TestSpawn(support.TempdirManager):
+ @pytest.mark.skipif("os.name not in ('nt', 'posix')")
def test_spawn(self):
tmpdir = self.mkdtemp()
@@ -32,7 +32,8 @@ class SpawnTestCase(support.TempdirManager,
self.write_file(exe, 'exit 1')
os.chmod(exe, 0o777)
- self.assertRaises(DistutilsExecError, spawn, [exe])
+ with pytest.raises(DistutilsExecError):
+ spawn([exe])
# now something that works
if sys.platform != 'win32':
@@ -45,95 +46,84 @@ class SpawnTestCase(support.TempdirManager,
os.chmod(exe, 0o777)
spawn([exe]) # should work without any error
- def test_find_executable(self):
- with os_helper.temp_dir() as tmp_dir:
- # use TESTFN to get a pseudo-unique filename
- program_noeext = os_helper.TESTFN
- # Give the temporary program an ".exe" suffix for all.
- # It's needed on Windows and not harmful on other platforms.
- program = program_noeext + ".exe"
-
- filename = os.path.join(tmp_dir, program)
- with open(filename, "wb"):
- pass
- os.chmod(filename, stat.S_IXUSR)
-
- # test path parameter
- rv = find_executable(program, path=tmp_dir)
- self.assertEqual(rv, filename)
-
- if sys.platform == 'win32':
- # test without ".exe" extension
- rv = find_executable(program_noeext, path=tmp_dir)
- self.assertEqual(rv, filename)
-
- # test find in the current directory
- with os_helper.change_cwd(tmp_dir):
+ def test_find_executable(self, tmp_path):
+ program_noeext = 'program'
+ # Give the temporary program an ".exe" suffix for all.
+ # It's needed on Windows and not harmful on other platforms.
+ program = program_noeext + ".exe"
+
+ program_path = tmp_path / program
+ program_path.write_text("")
+ program_path.chmod(stat.S_IXUSR)
+ filename = str(program_path)
+ tmp_dir = path.Path(tmp_path)
+
+ # test path parameter
+ rv = find_executable(program, path=tmp_dir)
+ assert rv == filename
+
+ if sys.platform == 'win32':
+ # test without ".exe" extension
+ rv = find_executable(program_noeext, path=tmp_dir)
+ assert rv == filename
+
+ # test find in the current directory
+ with tmp_dir:
+ rv = find_executable(program)
+ assert rv == program
+
+ # test non-existent program
+ dont_exist_program = "dontexist_" + program
+ rv = find_executable(dont_exist_program, path=tmp_dir)
+ assert rv is None
+
+ # PATH='': no match, except in the current directory
+ with os_helper.EnvironmentVarGuard() as env:
+ env['PATH'] = ''
+ with mock.patch(
+ 'distutils.spawn.os.confstr', return_value=tmp_dir, create=True
+ ), mock.patch('distutils.spawn.os.defpath', tmp_dir):
rv = find_executable(program)
- self.assertEqual(rv, program)
-
- # test non-existent program
- dont_exist_program = "dontexist_" + program
- rv = find_executable(dont_exist_program , path=tmp_dir)
- self.assertIsNone(rv)
-
- # PATH='': no match, except in the current directory
- with os_helper.EnvironmentVarGuard() as env:
- env['PATH'] = ''
- with unittest.mock.patch('distutils.spawn.os.confstr',
- return_value=tmp_dir, create=True), \
- unittest.mock.patch('distutils.spawn.os.defpath',
- tmp_dir):
- rv = find_executable(program)
- self.assertIsNone(rv)
-
- # look in current directory
- with os_helper.change_cwd(tmp_dir):
- rv = find_executable(program)
- self.assertEqual(rv, program)
-
- # PATH=':': explicitly looks in the current directory
- with os_helper.EnvironmentVarGuard() as env:
- env['PATH'] = os.pathsep
- with unittest.mock.patch('distutils.spawn.os.confstr',
- return_value='', create=True), \
- unittest.mock.patch('distutils.spawn.os.defpath', ''):
- rv = find_executable(program)
- self.assertIsNone(rv)
-
- # look in current directory
- with os_helper.change_cwd(tmp_dir):
- rv = find_executable(program)
- self.assertEqual(rv, program)
-
- # missing PATH: test os.confstr("CS_PATH") and os.defpath
- with os_helper.EnvironmentVarGuard() as env:
- env.pop('PATH', None)
-
- # without confstr
- with unittest.mock.patch('distutils.spawn.os.confstr',
- side_effect=ValueError,
- create=True), \
- unittest.mock.patch('distutils.spawn.os.defpath',
- tmp_dir):
+ assert rv is None
+
+ # look in current directory
+ with tmp_dir:
rv = find_executable(program)
- self.assertEqual(rv, filename)
+ assert rv == program
+
+ # PATH=':': explicitly looks in the current directory
+ with os_helper.EnvironmentVarGuard() as env:
+ env['PATH'] = os.pathsep
+ with mock.patch(
+ 'distutils.spawn.os.confstr', return_value='', create=True
+ ), mock.patch('distutils.spawn.os.defpath', ''):
+ rv = find_executable(program)
+ assert rv is None
- # with confstr
- with unittest.mock.patch('distutils.spawn.os.confstr',
- return_value=tmp_dir, create=True), \
- unittest.mock.patch('distutils.spawn.os.defpath', ''):
+ # look in current directory
+ with tmp_dir:
rv = find_executable(program)
- self.assertEqual(rv, filename)
+ assert rv == program
- def test_spawn_missing_exe(self):
- with self.assertRaises(DistutilsExecError) as ctx:
- spawn(['does-not-exist'])
- self.assertIn("command 'does-not-exist' failed", str(ctx.exception))
+ # missing PATH: test os.confstr("CS_PATH") and os.defpath
+ with os_helper.EnvironmentVarGuard() as env:
+ env.pop('PATH', None)
+ # without confstr
+ with mock.patch(
+ 'distutils.spawn.os.confstr', side_effect=ValueError, create=True
+ ), mock.patch('distutils.spawn.os.defpath', tmp_dir):
+ rv = find_executable(program)
+ assert rv == filename
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(SpawnTestCase)
+ # with confstr
+ with mock.patch(
+ 'distutils.spawn.os.confstr', return_value=tmp_dir, create=True
+ ), mock.patch('distutils.spawn.os.defpath', ''):
+ rv = find_executable(program)
+ assert rv == filename
-if __name__ == "__main__":
- run_unittest(test_suite())
+ def test_spawn_missing_exe(self):
+ with pytest.raises(DistutilsExecError) as ctx:
+ spawn(['does-not-exist'])
+ assert "command 'does-not-exist' failed" in str(ctx.value)
diff --git a/setuptools/_distutils/tests/test_sysconfig.py b/setuptools/_distutils/tests/test_sysconfig.py
index 9de3cb70..66f92c2a 100644
--- a/setuptools/_distutils/tests/test_sysconfig.py
+++ b/setuptools/_distutils/tests/test_sysconfig.py
@@ -1,91 +1,79 @@
"""Tests for distutils.sysconfig."""
import contextlib
import os
-import shutil
import subprocess
import sys
-import textwrap
-import unittest
+import pathlib
+import pytest
+import jaraco.envs
+import path
+from jaraco.text import trim
+
+import distutils
from distutils import sysconfig
-from distutils.ccompiler import get_default_compiler
+from distutils.ccompiler import get_default_compiler # noqa: F401
from distutils.unixccompiler import UnixCCompiler
-from distutils.tests import support
-from test.support import run_unittest, swap_item
-
-from .py38compat import TESTFN
-from .py38compat import check_warnings
-
+from test.support import swap_item
-class SysconfigTestCase(support.EnvironGuard, unittest.TestCase):
- def setUp(self):
- super(SysconfigTestCase, self).setUp()
- self.makefile = None
+from . import py37compat
- def tearDown(self):
- if self.makefile is not None:
- os.unlink(self.makefile)
- self.cleanup_testfn()
- super(SysconfigTestCase, self).tearDown()
-
- def cleanup_testfn(self):
- if os.path.isfile(TESTFN):
- os.remove(TESTFN)
- elif os.path.isdir(TESTFN):
- shutil.rmtree(TESTFN)
+@pytest.mark.usefixtures('save_env')
+class TestSysconfig:
def test_get_config_h_filename(self):
config_h = sysconfig.get_config_h_filename()
- self.assertTrue(os.path.isfile(config_h), config_h)
+ assert os.path.isfile(config_h)
- @unittest.skipIf(sys.platform == 'win32',
- 'Makefile only exists on Unix like systems')
+ @pytest.mark.skipif("platform.system() == 'Windows'")
+ @pytest.mark.skipif("sys.implementation.name != 'cpython'")
def test_get_makefile_filename(self):
makefile = sysconfig.get_makefile_filename()
- self.assertTrue(os.path.isfile(makefile), makefile)
+ assert os.path.isfile(makefile)
- def test_get_python_lib(self):
- # XXX doesn't work on Linux when Python was never installed before
- #self.assertTrue(os.path.isdir(lib_dir), lib_dir)
- # test for pythonxx.lib?
- self.assertNotEqual(sysconfig.get_python_lib(),
- sysconfig.get_python_lib(prefix=TESTFN))
+ def test_get_python_lib(self, tmp_path):
+ assert sysconfig.get_python_lib() != sysconfig.get_python_lib(prefix=tmp_path)
def test_get_config_vars(self):
cvars = sysconfig.get_config_vars()
- self.assertIsInstance(cvars, dict)
- self.assertTrue(cvars)
-
- @unittest.skip('sysconfig.IS_PYPY')
- def test_srcdir(self):
- # See Issues #15322, #15364.
- srcdir = sysconfig.get_config_var('srcdir')
-
- self.assertTrue(os.path.isabs(srcdir), srcdir)
- self.assertTrue(os.path.isdir(srcdir), srcdir)
-
- if sysconfig.python_build:
- # The python executable has not been installed so srcdir
- # should be a full source checkout.
- Python_h = os.path.join(srcdir, 'Include', 'Python.h')
- self.assertTrue(os.path.exists(Python_h), Python_h)
- self.assertTrue(sysconfig._is_python_source_dir(srcdir))
- elif os.name == 'posix':
- self.assertEqual(
- os.path.dirname(sysconfig.get_makefile_filename()),
- srcdir)
+ assert isinstance(cvars, dict)
+ assert cvars
+
+ @pytest.mark.skipif('sysconfig.IS_PYPY')
+ @pytest.mark.skipif('sysconfig.python_build')
+ @pytest.mark.xfail('platform.system() == "Windows"')
+ def test_srcdir_simple(self):
+ # See #15364.
+ srcdir = pathlib.Path(sysconfig.get_config_var('srcdir'))
+
+ assert srcdir.absolute()
+ assert srcdir.is_dir()
+
+ makefile = pathlib.Path(sysconfig.get_makefile_filename())
+ assert makefile.parent.samefile(srcdir)
+
+ @pytest.mark.skipif('sysconfig.IS_PYPY')
+ @pytest.mark.skipif('not sysconfig.python_build')
+ def test_srcdir_python_build(self):
+ # See #15364.
+ srcdir = pathlib.Path(sysconfig.get_config_var('srcdir'))
+
+ # The python executable has not been installed so srcdir
+ # should be a full source checkout.
+ Python_h = srcdir.joinpath('Include', 'Python.h')
+ assert Python_h.is_file()
+ assert sysconfig._is_python_source_dir(srcdir)
+ assert sysconfig._is_python_source_dir(str(srcdir))
def test_srcdir_independent_of_cwd(self):
- # srcdir should be independent of the current working directory
- # See Issues #15322, #15364.
+ """
+ srcdir should be independent of the current working directory
+ """
+ # See #15364.
srcdir = sysconfig.get_config_var('srcdir')
- cwd = os.getcwd()
- try:
- os.chdir('..')
+ with path.Path('..'):
srcdir2 = sysconfig.get_config_var('srcdir')
- finally:
- os.chdir(cwd)
- self.assertEqual(srcdir, srcdir2)
+ assert srcdir == srcdir2
def customize_compiler(self):
# make sure AR gets caught
@@ -109,7 +97,6 @@ class SysconfigTestCase(support.EnvironGuard, unittest.TestCase):
'CCSHARED': '--sc-ccshared',
'LDSHARED': 'sc_ldshared',
'SHLIB_SUFFIX': 'sc_shutil_suffix',
-
# On macOS, disable _osx_support.customize_compiler()
'CUSTOMIZED_OSX_COMPILER': 'True',
}
@@ -122,8 +109,7 @@ class SysconfigTestCase(support.EnvironGuard, unittest.TestCase):
return comp
- @unittest.skipUnless(get_default_compiler() == 'unix',
- 'not testing if default compiler is not unix')
+ @pytest.mark.skipif("get_default_compiler() != 'unix'")
def test_customize_compiler(self):
# Make sure that sysconfig._config_vars is initialized
sysconfig.get_config_vars()
@@ -140,29 +126,23 @@ class SysconfigTestCase(support.EnvironGuard, unittest.TestCase):
os.environ['RANLIB'] = 'env_ranlib'
comp = self.customize_compiler()
- self.assertEqual(comp.exes['archiver'],
- 'env_ar --env-arflags')
- self.assertEqual(comp.exes['preprocessor'],
- 'env_cpp --env-cppflags')
- self.assertEqual(comp.exes['compiler'],
- 'env_cc --sc-cflags --env-cflags --env-cppflags')
- self.assertEqual(comp.exes['compiler_so'],
- ('env_cc --sc-cflags '
- '--env-cflags ''--env-cppflags --sc-ccshared'))
- self.assertEqual(comp.exes['compiler_cxx'],
- 'env_cxx --env-cxx-flags')
- self.assertEqual(comp.exes['linker_exe'],
- 'env_cc')
- self.assertEqual(comp.exes['linker_so'],
- ('env_ldshared --env-ldflags --env-cflags'
- ' --env-cppflags'))
- self.assertEqual(comp.shared_lib_extension, 'sc_shutil_suffix')
+ assert comp.exes['archiver'] == 'env_ar --env-arflags'
+ assert comp.exes['preprocessor'] == 'env_cpp --env-cppflags'
+ assert comp.exes['compiler'] == 'env_cc --sc-cflags --env-cflags --env-cppflags'
+ assert comp.exes['compiler_so'] == (
+ 'env_cc --sc-cflags ' '--env-cflags ' '--env-cppflags --sc-ccshared'
+ )
+ assert comp.exes['compiler_cxx'] == 'env_cxx --env-cxx-flags'
+ assert comp.exes['linker_exe'] == 'env_cc'
+ assert comp.exes['linker_so'] == (
+ 'env_ldshared --env-ldflags --env-cflags' ' --env-cppflags'
+ )
+ assert comp.shared_lib_extension == 'sc_shutil_suffix'
if sys.platform == "darwin":
- self.assertEqual(comp.exes['ranlib'],
- 'env_ranlib')
+ assert comp.exes['ranlib'] == 'env_ranlib'
else:
- self.assertTrue('ranlib' not in comp.exes)
+ assert 'ranlib' not in comp.exes
del os.environ['AR']
del os.environ['CC']
@@ -176,57 +156,55 @@ class SysconfigTestCase(support.EnvironGuard, unittest.TestCase):
del os.environ['RANLIB']
comp = self.customize_compiler()
- self.assertEqual(comp.exes['archiver'],
- 'sc_ar --sc-arflags')
- self.assertEqual(comp.exes['preprocessor'],
- 'sc_cc -E')
- self.assertEqual(comp.exes['compiler'],
- 'sc_cc --sc-cflags')
- self.assertEqual(comp.exes['compiler_so'],
- 'sc_cc --sc-cflags --sc-ccshared')
- self.assertEqual(comp.exes['compiler_cxx'],
- 'sc_cxx')
- self.assertEqual(comp.exes['linker_exe'],
- 'sc_cc')
- self.assertEqual(comp.exes['linker_so'],
- 'sc_ldshared')
- self.assertEqual(comp.shared_lib_extension, 'sc_shutil_suffix')
- self.assertTrue('ranlib' not in comp.exes)
-
- def test_parse_makefile_base(self):
- self.makefile = TESTFN
- fd = open(self.makefile, 'w')
- try:
- fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=LIB'" '\n')
- fd.write('VAR=$OTHER\nOTHER=foo')
- finally:
- fd.close()
- d = sysconfig.parse_makefile(self.makefile)
- self.assertEqual(d, {'CONFIG_ARGS': "'--arg1=optarg1' 'ENV=LIB'",
- 'OTHER': 'foo'})
-
- def test_parse_makefile_literal_dollar(self):
- self.makefile = TESTFN
- fd = open(self.makefile, 'w')
- try:
- fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=\$$LIB'" '\n')
- fd.write('VAR=$OTHER\nOTHER=foo')
- finally:
- fd.close()
- d = sysconfig.parse_makefile(self.makefile)
- self.assertEqual(d, {'CONFIG_ARGS': r"'--arg1=optarg1' 'ENV=\$LIB'",
- 'OTHER': 'foo'})
-
+ assert comp.exes['archiver'] == 'sc_ar --sc-arflags'
+ assert comp.exes['preprocessor'] == 'sc_cc -E'
+ assert comp.exes['compiler'] == 'sc_cc --sc-cflags'
+ assert comp.exes['compiler_so'] == 'sc_cc --sc-cflags --sc-ccshared'
+ assert comp.exes['compiler_cxx'] == 'sc_cxx'
+ assert comp.exes['linker_exe'] == 'sc_cc'
+ assert comp.exes['linker_so'] == 'sc_ldshared'
+ assert comp.shared_lib_extension == 'sc_shutil_suffix'
+ assert 'ranlib' not in comp.exes
+
+ def test_parse_makefile_base(self, tmp_path):
+ makefile = tmp_path / 'Makefile'
+ makefile.write_text(
+ trim(
+ """
+ CONFIG_ARGS= '--arg1=optarg1' 'ENV=LIB'
+ VAR=$OTHER
+ OTHER=foo
+ """
+ )
+ )
+ d = sysconfig.parse_makefile(makefile)
+ assert d == {'CONFIG_ARGS': "'--arg1=optarg1' 'ENV=LIB'", 'OTHER': 'foo'}
+
+ def test_parse_makefile_literal_dollar(self, tmp_path):
+ makefile = tmp_path / 'Makefile'
+ makefile.write_text(
+ trim(
+ """
+ CONFIG_ARGS= '--arg1=optarg1' 'ENV=\\$$LIB'
+ VAR=$OTHER
+ OTHER=foo
+ """
+ )
+ )
+ d = sysconfig.parse_makefile(makefile)
+ assert d == {'CONFIG_ARGS': r"'--arg1=optarg1' 'ENV=\$LIB'", 'OTHER': 'foo'}
def test_sysconfig_module(self):
import sysconfig as global_sysconfig
- self.assertEqual(global_sysconfig.get_config_var('CFLAGS'),
- sysconfig.get_config_var('CFLAGS'))
- self.assertEqual(global_sysconfig.get_config_var('LDFLAGS'),
- sysconfig.get_config_var('LDFLAGS'))
- @unittest.skipIf(sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'),
- 'compiler flags customized')
+ assert global_sysconfig.get_config_var('CFLAGS') == sysconfig.get_config_var(
+ 'CFLAGS'
+ )
+ assert global_sysconfig.get_config_var('LDFLAGS') == sysconfig.get_config_var(
+ 'LDFLAGS'
+ )
+
+ @pytest.mark.skipif("sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER')")
def test_sysconfig_compiler_vars(self):
# On OS X, binary installers support extension module building on
# various levels of the operating system with differing Xcode
@@ -243,67 +221,79 @@ class SysconfigTestCase(support.EnvironGuard, unittest.TestCase):
# The longer-term solution is to only have one version of sysconfig.
import sysconfig as global_sysconfig
+
if sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'):
- self.skipTest('compiler flags customized')
- self.assertEqual(global_sysconfig.get_config_var('LDSHARED'),
- sysconfig.get_config_var('LDSHARED'))
- self.assertEqual(global_sysconfig.get_config_var('CC'),
- sysconfig.get_config_var('CC'))
-
- @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None,
- 'EXT_SUFFIX required for this test')
+ pytest.skip('compiler flags customized')
+ assert global_sysconfig.get_config_var('LDSHARED') == sysconfig.get_config_var(
+ 'LDSHARED'
+ )
+ assert global_sysconfig.get_config_var('CC') == sysconfig.get_config_var('CC')
+
+ @pytest.mark.skipif("not sysconfig.get_config_var('EXT_SUFFIX')")
def test_SO_deprecation(self):
- self.assertWarns(DeprecationWarning,
- sysconfig.get_config_var, 'SO')
-
- @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None,
- 'EXT_SUFFIX required for this test')
- def test_SO_value(self):
- with check_warnings(('', DeprecationWarning)):
- self.assertEqual(sysconfig.get_config_var('SO'),
- sysconfig.get_config_var('EXT_SUFFIX'))
-
- @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None,
- 'EXT_SUFFIX required for this test')
- def test_SO_in_vars(self):
- vars = sysconfig.get_config_vars()
- self.assertIsNotNone(vars['SO'])
- self.assertEqual(vars['SO'], vars['EXT_SUFFIX'])
-
- def test_customize_compiler_before_get_config_vars(self):
+ with pytest.warns(DeprecationWarning):
+ sysconfig.get_config_var('SO')
+
+ def test_customize_compiler_before_get_config_vars(self, tmp_path):
# Issue #21923: test that a Distribution compiler
# instance can be called without an explicit call to
# get_config_vars().
- with open(TESTFN, 'w') as f:
- f.writelines(textwrap.dedent('''\
+ file = tmp_path / 'file'
+ file.write_text(
+ trim(
+ """
from distutils.core import Distribution
config = Distribution().get_command_obj('config')
# try_compile may pass or it may fail if no compiler
# is found but it should not raise an exception.
rc = config.try_compile('int x;')
- '''))
- p = subprocess.Popen([str(sys.executable), TESTFN],
- stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT,
- universal_newlines=True)
+ """
+ )
+ )
+ p = subprocess.Popen(
+ py37compat.subprocess_args(sys.executable, file),
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ universal_newlines=True,
+ )
outs, errs = p.communicate()
- self.assertEqual(0, p.returncode, "Subprocess failed: " + outs)
+ assert 0 == p.returncode, "Subprocess failed: " + outs
def test_parse_config_h(self):
config_h = sysconfig.get_config_h_filename()
input = {}
with open(config_h, encoding="utf-8") as f:
result = sysconfig.parse_config_h(f, g=input)
- self.assertTrue(input is result)
+ assert input is result
with open(config_h, encoding="utf-8") as f:
result = sysconfig.parse_config_h(f)
- self.assertTrue(isinstance(result, dict))
-
-def test_suite():
- suite = unittest.TestSuite()
- suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SysconfigTestCase))
- return suite
-
-
-if __name__ == '__main__':
- run_unittest(test_suite())
+ assert isinstance(result, dict)
+
+ @pytest.mark.skipif("platform.system() != 'Windows'")
+ @pytest.mark.skipif("sys.implementation.name != 'cpython'")
+ def test_win_ext_suffix(self):
+ assert sysconfig.get_config_var("EXT_SUFFIX").endswith(".pyd")
+ assert sysconfig.get_config_var("EXT_SUFFIX") != ".pyd"
+
+ @pytest.mark.skipif("platform.system() != 'Windows'")
+ @pytest.mark.skipif("sys.implementation.name != 'cpython'")
+ @pytest.mark.skipif(
+ '\\PCbuild\\'.casefold() not in sys.executable.casefold(),
+ reason='Need sys.executable to be in a source tree',
+ )
+ def test_win_build_venv_from_source_tree(self, tmp_path):
+ """Ensure distutils.sysconfig detects venvs from source tree builds."""
+ env = jaraco.envs.VEnv()
+ env.create_opts = env.clean_opts
+ env.root = tmp_path
+ env.ensure_env()
+ cmd = [
+ env.exe(),
+ "-c",
+ "import distutils.sysconfig; print(distutils.sysconfig.python_build)",
+ ]
+ distutils_path = os.path.dirname(os.path.dirname(distutils.__file__))
+ out = subprocess.check_output(
+ cmd, env={**os.environ, "PYTHONPATH": distutils_path}
+ )
+ assert out == "True"
diff --git a/setuptools/_distutils/tests/test_text_file.py b/setuptools/_distutils/tests/test_text_file.py
index ebac3d52..7c8dc5be 100644
--- a/setuptools/_distutils/tests/test_text_file.py
+++ b/setuptools/_distutils/tests/test_text_file.py
@@ -1,9 +1,7 @@
"""Tests for distutils.text_file."""
import os
-import unittest
from distutils.text_file import TextFile
from distutils.tests import support
-from test.support import run_unittest
TEST_DATA = """# test file
@@ -12,32 +10,35 @@ line 3 \\
continues on next line
"""
-class TextFileTestCase(support.TempdirManager, unittest.TestCase):
+class TestTextFile(support.TempdirManager):
def test_class(self):
# old tests moved from text_file.__main__
# so they are really called by the buildbots
# result 1: no fancy options
- result1 = ['# test file\n', '\n', 'line 3 \\\n',
- '# intervening comment\n',
- ' continues on next line\n']
+ result1 = [
+ '# test file\n',
+ '\n',
+ 'line 3 \\\n',
+ '# intervening comment\n',
+ ' continues on next line\n',
+ ]
# result 2: just strip comments
- result2 = ["\n",
- "line 3 \\\n",
- " continues on next line\n"]
+ result2 = ["\n", "line 3 \\\n", " continues on next line\n"]
# result 3: just strip blank lines
- result3 = ["# test file\n",
- "line 3 \\\n",
- "# intervening comment\n",
- " continues on next line\n"]
+ result3 = [
+ "# test file\n",
+ "line 3 \\\n",
+ "# intervening comment\n",
+ " continues on next line\n",
+ ]
# result 4: default, strip comments, blank lines,
# and trailing whitespace
- result4 = ["line 3 \\",
- " continues on next line"]
+ result4 = ["line 3 \\", " continues on next line"]
# result 5: strip comments and blanks, plus join lines (but don't
# "collapse" joined lines
@@ -49,7 +50,7 @@ class TextFileTestCase(support.TempdirManager, unittest.TestCase):
def test_input(count, description, file, expected_result):
result = file.readlines()
- self.assertEqual(result, expected_result)
+ assert result == expected_result
tmpdir = self.mkdtemp()
filename = os.path.join(tmpdir, "test.txt")
@@ -59,22 +60,25 @@ class TextFileTestCase(support.TempdirManager, unittest.TestCase):
finally:
out_file.close()
- in_file = TextFile(filename, strip_comments=0, skip_blanks=0,
- lstrip_ws=0, rstrip_ws=0)
+ in_file = TextFile(
+ filename, strip_comments=0, skip_blanks=0, lstrip_ws=0, rstrip_ws=0
+ )
try:
test_input(1, "no processing", in_file, result1)
finally:
in_file.close()
- in_file = TextFile(filename, strip_comments=1, skip_blanks=0,
- lstrip_ws=0, rstrip_ws=0)
+ in_file = TextFile(
+ filename, strip_comments=1, skip_blanks=0, lstrip_ws=0, rstrip_ws=0
+ )
try:
test_input(2, "strip comments", in_file, result2)
finally:
in_file.close()
- in_file = TextFile(filename, strip_comments=0, skip_blanks=1,
- lstrip_ws=0, rstrip_ws=0)
+ in_file = TextFile(
+ filename, strip_comments=0, skip_blanks=1, lstrip_ws=0, rstrip_ws=0
+ )
try:
test_input(3, "strip blanks", in_file, result3)
finally:
@@ -86,22 +90,23 @@ class TextFileTestCase(support.TempdirManager, unittest.TestCase):
finally:
in_file.close()
- in_file = TextFile(filename, strip_comments=1, skip_blanks=1,
- join_lines=1, rstrip_ws=1)
+ in_file = TextFile(
+ filename, strip_comments=1, skip_blanks=1, join_lines=1, rstrip_ws=1
+ )
try:
test_input(5, "join lines without collapsing", in_file, result5)
finally:
in_file.close()
- in_file = TextFile(filename, strip_comments=1, skip_blanks=1,
- join_lines=1, rstrip_ws=1, collapse_join=1)
+ in_file = TextFile(
+ filename,
+ strip_comments=1,
+ skip_blanks=1,
+ join_lines=1,
+ rstrip_ws=1,
+ collapse_join=1,
+ )
try:
test_input(6, "join lines with collapsing", in_file, result6)
finally:
in_file.close()
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(TextFileTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
diff --git a/setuptools/_distutils/tests/test_unixccompiler.py b/setuptools/_distutils/tests/test_unixccompiler.py
index 4574f77f..3978c239 100644
--- a/setuptools/_distutils/tests/test_unixccompiler.py
+++ b/setuptools/_distutils/tests/test_unixccompiler.py
@@ -1,8 +1,7 @@
"""Tests for distutils.unixccompiler."""
import os
import sys
-import unittest
-from test.support import run_unittest
+import unittest.mock as mock
from .py38compat import EnvironmentVarGuard
@@ -12,27 +11,28 @@ from distutils.unixccompiler import UnixCCompiler
from distutils.util import _clear_cached_macosx_ver
from . import support
+import pytest
-class UnixCCompilerTestCase(support.TempdirManager, unittest.TestCase):
-
- def setUp(self):
- super().setUp()
- self._backup_platform = sys.platform
- self._backup_get_config_var = sysconfig.get_config_var
- self._backup_get_config_vars = sysconfig.get_config_vars
- class CompilerWrapper(UnixCCompiler):
- def rpath_foo(self):
- return self.runtime_library_dir_option('/foo')
- self.cc = CompilerWrapper()
-
- def tearDown(self):
- super().tearDown()
- sys.platform = self._backup_platform
- sysconfig.get_config_var = self._backup_get_config_var
- sysconfig.get_config_vars = self._backup_get_config_vars
-
- @unittest.skipIf(sys.platform == 'win32', "can't test on Windows")
- def test_runtime_libdir_option(self):
+
+@pytest.fixture(autouse=True)
+def save_values(monkeypatch):
+ monkeypatch.setattr(sys, 'platform', sys.platform)
+ monkeypatch.setattr(sysconfig, 'get_config_var', sysconfig.get_config_var)
+ monkeypatch.setattr(sysconfig, 'get_config_vars', sysconfig.get_config_vars)
+
+
+@pytest.fixture(autouse=True)
+def compiler_wrapper(request):
+ class CompilerWrapper(UnixCCompiler):
+ def rpath_foo(self):
+ return self.runtime_library_dir_option('/foo')
+
+ request.instance.cc = CompilerWrapper()
+
+
+class TestUnixCCompiler(support.TempdirManager):
+ @pytest.mark.skipif('platform.system == "Windows"') # noqa: C901
+ def test_runtime_libdir_option(self): # noqa: C901
# Issue #5900; GitHub Issue #37
#
# Ensure RUNPATH is added to extension modules with RPATH if
@@ -48,18 +48,18 @@ class UnixCCompilerTestCase(support.TempdirManager, unittest.TestCase):
# Version value of None generates two tests: as None and as empty string
# Expected flag value of None means an mismatch exception is expected
darwin_test_cases = [
- ((None , None ), darwin_lib_flag),
- ((None , '11' ), darwin_rpath_flag),
- (('10' , None ), darwin_lib_flag),
- (('10.3' , None ), darwin_lib_flag),
- (('10.3.1', None ), darwin_lib_flag),
- (('10.5' , None ), darwin_rpath_flag),
- (('10.5.1', None ), darwin_rpath_flag),
- (('10.3' , '10.3' ), darwin_lib_flag),
- (('10.3' , '10.5' ), darwin_rpath_flag),
- (('10.5' , '10.3' ), darwin_lib_flag),
- (('10.5' , '11' ), darwin_rpath_flag),
- (('10.4' , '10' ), None),
+ ((None, None), darwin_lib_flag),
+ ((None, '11'), darwin_rpath_flag),
+ (('10', None), darwin_lib_flag),
+ (('10.3', None), darwin_lib_flag),
+ (('10.3.1', None), darwin_lib_flag),
+ (('10.5', None), darwin_rpath_flag),
+ (('10.5.1', None), darwin_rpath_flag),
+ (('10.3', '10.3'), darwin_lib_flag),
+ (('10.3', '10.5'), darwin_rpath_flag),
+ (('10.5', '10.3'), darwin_lib_flag),
+ (('10.5', '11'), darwin_rpath_flag),
+ (('10.4', '10'), None),
]
def make_darwin_gcv(syscfg_macosx_ver):
@@ -67,12 +67,15 @@ class UnixCCompilerTestCase(support.TempdirManager, unittest.TestCase):
if var == darwin_ver_var:
return syscfg_macosx_ver
return "xxx"
+
return gcv
def do_darwin_test(syscfg_macosx_ver, env_macosx_ver, expected_flag):
env = os.environ
- msg = "macOS version = (sysconfig=%r, env=%r)" % \
- (syscfg_macosx_ver, env_macosx_ver)
+ msg = "macOS version = (sysconfig={!r}, env={!r})".format(
+ syscfg_macosx_ver,
+ env_macosx_ver,
+ )
# Save
old_gcv = sysconfig.get_config_var
@@ -88,10 +91,11 @@ class UnixCCompilerTestCase(support.TempdirManager, unittest.TestCase):
# Run the test
if expected_flag is not None:
- self.assertEqual(self.cc.rpath_foo(), expected_flag, msg=msg)
+ assert self.cc.rpath_foo() == expected_flag, msg
else:
- with self.assertRaisesRegex(DistutilsPlatformError,
- darwin_ver_var + r' mismatch', msg=msg):
+ with pytest.raises(
+ DistutilsPlatformError, match=darwin_ver_var + r' mismatch'
+ ):
self.cc.rpath_foo()
# Restore
@@ -117,83 +121,98 @@ class UnixCCompilerTestCase(support.TempdirManager, unittest.TestCase):
# hp-ux
sys.platform = 'hp-ux'
+
def gcv(v):
return 'xxx'
+
sysconfig.get_config_var = gcv
- self.assertEqual(self.cc.rpath_foo(), ['+s', '-L/foo'])
+ assert self.cc.rpath_foo() == ['+s', '-L/foo']
def gcv(v):
return 'gcc'
+
sysconfig.get_config_var = gcv
- self.assertEqual(self.cc.rpath_foo(), ['-Wl,+s', '-L/foo'])
+ assert self.cc.rpath_foo() == ['-Wl,+s', '-L/foo']
def gcv(v):
return 'g++'
+
sysconfig.get_config_var = gcv
- self.assertEqual(self.cc.rpath_foo(), ['-Wl,+s', '-L/foo'])
+ assert self.cc.rpath_foo() == ['-Wl,+s', '-L/foo']
sysconfig.get_config_var = old_gcv
# GCC GNULD
sys.platform = 'bar'
+
def gcv(v):
if v == 'CC':
return 'gcc'
elif v == 'GNULD':
return 'yes'
+
sysconfig.get_config_var = gcv
- self.assertEqual(self.cc.rpath_foo(), '-Wl,--enable-new-dtags,-R/foo')
+ assert self.cc.rpath_foo() == '-Wl,--enable-new-dtags,-R/foo'
def gcv(v):
if v == 'CC':
return 'gcc -pthread -B /bar'
elif v == 'GNULD':
return 'yes'
+
sysconfig.get_config_var = gcv
- self.assertEqual(self.cc.rpath_foo(), '-Wl,--enable-new-dtags,-R/foo')
+ assert self.cc.rpath_foo() == '-Wl,--enable-new-dtags,-R/foo'
# GCC non-GNULD
sys.platform = 'bar'
+
def gcv(v):
if v == 'CC':
return 'gcc'
elif v == 'GNULD':
return 'no'
+
sysconfig.get_config_var = gcv
- self.assertEqual(self.cc.rpath_foo(), '-Wl,-R/foo')
+ assert self.cc.rpath_foo() == '-Wl,-R/foo'
# GCC GNULD with fully qualified configuration prefix
# see #7617
sys.platform = 'bar'
+
def gcv(v):
if v == 'CC':
return 'x86_64-pc-linux-gnu-gcc-4.4.2'
elif v == 'GNULD':
return 'yes'
+
sysconfig.get_config_var = gcv
- self.assertEqual(self.cc.rpath_foo(), '-Wl,--enable-new-dtags,-R/foo')
+ assert self.cc.rpath_foo() == '-Wl,--enable-new-dtags,-R/foo'
# non-GCC GNULD
sys.platform = 'bar'
+
def gcv(v):
if v == 'CC':
return 'cc'
elif v == 'GNULD':
return 'yes'
+
sysconfig.get_config_var = gcv
- self.assertEqual(self.cc.rpath_foo(), '-Wl,--enable-new-dtags,-R/foo')
+ assert self.cc.rpath_foo() == '-Wl,--enable-new-dtags,-R/foo'
# non-GCC non-GNULD
sys.platform = 'bar'
+
def gcv(v):
if v == 'CC':
return 'cc'
elif v == 'GNULD':
return 'no'
+
sysconfig.get_config_var = gcv
- self.assertEqual(self.cc.rpath_foo(), '-Wl,-R/foo')
+ assert self.cc.rpath_foo() == '-Wl,-R/foo'
- @unittest.skipIf(sys.platform == 'win32', "can't test on Windows")
+ @pytest.mark.skipif('platform.system == "Windows"')
def test_cc_overrides_ldshared(self):
# Issue #18080:
# ensure that setting CC env variable also changes default linker
@@ -206,15 +225,56 @@ class UnixCCompilerTestCase(support.TempdirManager, unittest.TestCase):
if args:
return list(map(sysconfig.get_config_var, args))
return _orig()
+
sysconfig.get_config_var = gcv
sysconfig.get_config_vars = gcvs
with EnvironmentVarGuard() as env:
env['CC'] = 'my_cc'
del env['LDSHARED']
sysconfig.customize_compiler(self.cc)
- self.assertEqual(self.cc.linker_so[0], 'my_cc')
+ assert self.cc.linker_so[0] == 'my_cc'
+
+ @pytest.mark.skipif('platform.system == "Windows"')
+ def test_cc_overrides_ldshared_for_cxx_correctly(self):
+ """
+ Ensure that setting CC env variable also changes default linker
+ correctly when building C++ extensions.
+
+ pypa/distutils#126
+ """
+
+ def gcv(v):
+ if v == 'LDSHARED':
+ return 'gcc-4.2 -bundle -undefined dynamic_lookup '
+ elif v == 'CXX':
+ return 'g++-4.2'
+ return 'gcc-4.2'
+
+ def gcvs(*args, _orig=sysconfig.get_config_vars):
+ if args:
+ return list(map(sysconfig.get_config_var, args))
+ return _orig()
+
+ sysconfig.get_config_var = gcv
+ sysconfig.get_config_vars = gcvs
+ with mock.patch.object(
+ self.cc, 'spawn', return_value=None
+ ) as mock_spawn, mock.patch.object(
+ self.cc, '_need_link', return_value=True
+ ), mock.patch.object(
+ self.cc, 'mkpath', return_value=None
+ ), EnvironmentVarGuard() as env:
+ env['CC'] = 'ccache my_cc'
+ env['CXX'] = 'my_cxx'
+ del env['LDSHARED']
+ sysconfig.customize_compiler(self.cc)
+ assert self.cc.linker_so[0:2] == ['ccache', 'my_cc']
+ self.cc.link(None, [], 'a.out', target_lang='c++')
+ call_args = mock_spawn.call_args[0][0]
+ expected = ['my_cxx', '-bundle', '-undefined', 'dynamic_lookup']
+ assert call_args[:4] == expected
- @unittest.skipIf(sys.platform == 'win32', "can't test on Windows")
+ @pytest.mark.skipif('platform.system == "Windows"')
def test_explicit_ldshared(self):
# Issue #18080:
# ensure that setting CC env variable does not change
@@ -228,13 +288,14 @@ class UnixCCompilerTestCase(support.TempdirManager, unittest.TestCase):
if args:
return list(map(sysconfig.get_config_var, args))
return _orig()
+
sysconfig.get_config_var = gcv
sysconfig.get_config_vars = gcvs
with EnvironmentVarGuard() as env:
env['CC'] = 'my_cc'
env['LDSHARED'] = 'my_ld -bundle -dynamic'
sysconfig.customize_compiler(self.cc)
- self.assertEqual(self.cc.linker_so[0], 'my_ld')
+ assert self.cc.linker_so[0] == 'my_ld'
def test_has_function(self):
# Issue https://github.com/pypa/distutils/issues/64:
@@ -243,10 +304,3 @@ class UnixCCompilerTestCase(support.TempdirManager, unittest.TestCase):
self.cc.output_dir = 'scratch'
os.chdir(self.mkdtemp())
self.cc.has_function('abort', includes=['stdlib.h'])
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(UnixCCompilerTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
diff --git a/setuptools/_distutils/tests/test_upload.py b/setuptools/_distutils/tests/test_upload.py
index ce3e84a2..9685c065 100644
--- a/setuptools/_distutils/tests/test_upload.py
+++ b/setuptools/_distutils/tests/test_upload.py
@@ -1,18 +1,16 @@
"""Tests for distutils.command.upload."""
import os
-import unittest
import unittest.mock as mock
from urllib.request import HTTPError
-from test.support import run_unittest
from distutils.command import upload as upload_mod
from distutils.command.upload import upload
from distutils.core import Distribution
from distutils.errors import DistutilsError
-from distutils.log import ERROR, INFO
from distutils.tests.test_config import PYPIRC, BasePyPIRCCommandTestCase
+import pytest
PYPIRC_LONG_PASSWORD = """\
[distutils]
@@ -43,8 +41,8 @@ index-servers =
username:me
"""
-class FakeOpen(object):
+class FakeOpen:
def __init__(self, url, msg=None, code=None):
self.url = url
if not isinstance(url, str):
@@ -57,7 +55,7 @@ class FakeOpen(object):
def getheader(self, name, default=None):
return {
'content-type': 'text/plain; charset=utf-8',
- }.get(name.lower(), default)
+ }.get(name.lower(), default)
def read(self):
return b'xyzzy'
@@ -66,20 +64,14 @@ class FakeOpen(object):
return self.code
-class uploadTestCase(BasePyPIRCCommandTestCase):
+@pytest.fixture(autouse=True)
+def urlopen(request, monkeypatch):
+ self = request.instance
+ monkeypatch.setattr(upload_mod, 'urlopen', self._urlopen)
+ self.next_msg = self.next_code = None
- def setUp(self):
- super(uploadTestCase, self).setUp()
- self.old_open = upload_mod.urlopen
- upload_mod.urlopen = self._urlopen
- self.last_open = None
- self.next_msg = None
- self.next_code = None
-
- def tearDown(self):
- upload_mod.urlopen = self.old_open
- super(uploadTestCase, self).tearDown()
+class TestUpload(BasePyPIRCCommandTestCase):
def _urlopen(self, url):
self.last_open = FakeOpen(url, msg=self.next_msg, code=self.next_code)
return self.last_open
@@ -91,10 +83,13 @@ class uploadTestCase(BasePyPIRCCommandTestCase):
dist = Distribution()
cmd = upload(dist)
cmd.finalize_options()
- for attr, waited in (('username', 'me'), ('password', 'secret'),
- ('realm', 'pypi'),
- ('repository', 'https://upload.pypi.org/legacy/')):
- self.assertEqual(getattr(cmd, attr), waited)
+ for attr, waited in (
+ ('username', 'me'),
+ ('password', 'secret'),
+ ('realm', 'pypi'),
+ ('repository', 'https://upload.pypi.org/legacy/'),
+ ):
+ assert getattr(cmd, attr) == waited
def test_saved_password(self):
# file with no password
@@ -104,16 +99,16 @@ class uploadTestCase(BasePyPIRCCommandTestCase):
dist = Distribution()
cmd = upload(dist)
cmd.finalize_options()
- self.assertEqual(cmd.password, None)
+ assert cmd.password is None
# make sure we get it as well, if another command
# initialized it at the dist level
dist.password = 'xxx'
cmd = upload(dist)
cmd.finalize_options()
- self.assertEqual(cmd.password, 'xxx')
+ assert cmd.password == 'xxx'
- def test_upload(self):
+ def test_upload(self, caplog):
tmp = self.mkdtemp()
path = os.path.join(tmp, 'xxx')
self.write_file(path)
@@ -130,34 +125,32 @@ class uploadTestCase(BasePyPIRCCommandTestCase):
# what did we send ?
headers = dict(self.last_open.req.headers)
- self.assertGreaterEqual(int(headers['Content-length']), 2162)
+ assert int(headers['Content-length']) >= 2162
content_type = headers['Content-type']
- self.assertTrue(content_type.startswith('multipart/form-data'))
- self.assertEqual(self.last_open.req.get_method(), 'POST')
+ assert content_type.startswith('multipart/form-data')
+ assert self.last_open.req.get_method() == 'POST'
expected_url = 'https://upload.pypi.org/legacy/'
- self.assertEqual(self.last_open.req.get_full_url(), expected_url)
+ assert self.last_open.req.get_full_url() == expected_url
data = self.last_open.req.data
- self.assertIn(b'xxx',data)
- self.assertIn(b'protocol_version', data)
- self.assertIn(b'sha256_digest', data)
- self.assertIn(
+ assert b'xxx' in data
+ assert b'protocol_version' in data
+ assert b'sha256_digest' in data
+ assert (
b'cd2eb0837c9b4c962c22d2ff8b5441b7b45805887f051d39bf133b583baf'
- b'6860',
- data
+ b'6860' in data
)
if b'md5_digest' in data:
- self.assertIn(b'f561aaf6ef0bf14d4208bb46a4ccb3ad', data)
+ assert b'f561aaf6ef0bf14d4208bb46a4ccb3ad' in data
if b'blake2_256_digest' in data:
- self.assertIn(
+ assert (
b'b6f289a27d4fe90da63c503bfe0a9b761a8f76bb86148565065f040be'
b'6d1c3044cf7ded78ef800509bccb4b648e507d88dc6383d67642aadcc'
- b'ce443f1534330a',
- data
+ b'ce443f1534330a' in data
)
# The PyPI response body was echoed
- results = self.get_logs(INFO)
- self.assertEqual(results[-1], 75 * '-' + '\nxyzzy\n' + 75 * '-')
+ results = caplog.messages
+ assert results[-1] == 75 * '-' + '\nxyzzy\n' + 75 * '-'
# bpo-32304: archives whose last byte was b'\r' were corrupted due to
# normalization intended for Mac OS 9.
@@ -173,8 +166,7 @@ class uploadTestCase(BasePyPIRCCommandTestCase):
# other fields that ended with \r used to be modified, now are
# preserved.
pkg_dir, dist = self.create_dist(
- dist_files=dist_files,
- description='long description\r'
+ dist_files=dist_files, description='long description\r'
)
cmd = upload(dist)
cmd.show_response = 1
@@ -182,15 +174,28 @@ class uploadTestCase(BasePyPIRCCommandTestCase):
cmd.run()
headers = dict(self.last_open.req.headers)
- self.assertGreaterEqual(int(headers['Content-length']), 2172)
- self.assertIn(b'long description\r', self.last_open.req.data)
+ assert int(headers['Content-length']) >= 2172
+ assert b'long description\r' in self.last_open.req.data
- def test_upload_fails(self):
+ def test_upload_fails(self, caplog):
self.next_msg = "Not Found"
self.next_code = 404
- self.assertRaises(DistutilsError, self.test_upload)
+ with pytest.raises(DistutilsError):
+ self.test_upload(caplog)
- def test_wrong_exception_order(self):
+ @pytest.mark.parametrize(
+ 'exception,expected,raised_exception',
+ [
+ (OSError('oserror'), 'oserror', OSError),
+ pytest.param(
+ HTTPError('url', 400, 'httperror', {}, None),
+ 'Upload failed (400): httperror',
+ DistutilsError,
+ id="HTTP 400",
+ ),
+ ],
+ )
+ def test_wrong_exception_order(self, exception, expected, raised_exception, caplog):
tmp = self.mkdtemp()
path = os.path.join(tmp, 'xxx')
self.write_file(path)
@@ -198,26 +203,15 @@ class uploadTestCase(BasePyPIRCCommandTestCase):
self.write_file(self.rc, PYPIRC_LONG_PASSWORD)
pkg_dir, dist = self.create_dist(dist_files=dist_files)
- tests = [
- (OSError('oserror'), 'oserror', OSError),
- (HTTPError('url', 400, 'httperror', {}, None),
- 'Upload failed (400): httperror', DistutilsError),
- ]
- for exception, expected, raised_exception in tests:
- with self.subTest(exception=type(exception).__name__):
- with mock.patch('distutils.command.upload.urlopen',
- new=mock.Mock(side_effect=exception)):
- with self.assertRaises(raised_exception):
- cmd = upload(dist)
- cmd.ensure_finalized()
- cmd.run()
- results = self.get_logs(ERROR)
- self.assertIn(expected, results[-1])
- self.clear_logs()
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(uploadTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+
+ with mock.patch(
+ 'distutils.command.upload.urlopen',
+ new=mock.Mock(side_effect=exception),
+ ):
+ with pytest.raises(raised_exception):
+ cmd = upload(dist)
+ cmd.ensure_finalized()
+ cmd.run()
+ results = caplog.messages
+ assert expected in results[-1]
+ caplog.clear()
diff --git a/setuptools/_distutils/tests/test_util.py b/setuptools/_distutils/tests/test_util.py
index 2738388e..070a2770 100644
--- a/setuptools/_distutils/tests/test_util.py
+++ b/setuptools/_distutils/tests/test_util.py
@@ -1,204 +1,196 @@
"""Tests for distutils.util."""
import os
import sys
-import unittest
import sysconfig as stdlib_sysconfig
+import unittest.mock as mock
from copy import copy
-from test.support import run_unittest
-from unittest import mock
-from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError
-from distutils.util import (get_platform, convert_path, change_root,
- check_environ, split_quoted, strtobool,
- rfc822_escape, byte_compile,
- grok_environment_error, get_host_platform)
-from distutils import util # used to patch _environ_checked
+import pytest
+
+from distutils.util import (
+ get_platform,
+ convert_path,
+ change_root,
+ check_environ,
+ split_quoted,
+ strtobool,
+ rfc822_escape,
+ byte_compile,
+ grok_environment_error,
+ get_host_platform,
+)
+from distutils import util
from distutils import sysconfig
-from distutils.tests import support
-
-class UtilTestCase(support.EnvironGuard, unittest.TestCase):
-
- def setUp(self):
- super(UtilTestCase, self).setUp()
- # saving the environment
- self.name = os.name
- self.platform = sys.platform
- self.version = sys.version
- self.sep = os.sep
- self.join = os.path.join
- self.isabs = os.path.isabs
- self.splitdrive = os.path.splitdrive
- self._config_vars = copy(sysconfig._config_vars)
-
- # patching os.uname
- if hasattr(os, 'uname'):
- self.uname = os.uname
- self._uname = os.uname()
- else:
- self.uname = None
- self._uname = None
-
- os.uname = self._get_uname
-
- def tearDown(self):
- # getting back the environment
- os.name = self.name
- sys.platform = self.platform
- sys.version = self.version
- os.sep = self.sep
- os.path.join = self.join
- os.path.isabs = self.isabs
- os.path.splitdrive = self.splitdrive
- if self.uname is not None:
- os.uname = self.uname
- else:
- del os.uname
- sysconfig._config_vars = copy(self._config_vars)
- super(UtilTestCase, self).tearDown()
-
- def _set_uname(self, uname):
- self._uname = uname
-
- def _get_uname(self):
- return self._uname
+from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError
+
+@pytest.fixture(autouse=True)
+def environment(monkeypatch):
+ monkeypatch.setattr(os, 'name', os.name)
+ monkeypatch.setattr(sys, 'platform', sys.platform)
+ monkeypatch.setattr(sys, 'version', sys.version)
+ monkeypatch.setattr(os, 'sep', os.sep)
+ monkeypatch.setattr(os.path, 'join', os.path.join)
+ monkeypatch.setattr(os.path, 'isabs', os.path.isabs)
+ monkeypatch.setattr(os.path, 'splitdrive', os.path.splitdrive)
+ monkeypatch.setattr(sysconfig, '_config_vars', copy(sysconfig._config_vars))
+
+
+@pytest.mark.usefixtures('save_env')
+class TestUtil:
def test_get_host_platform(self):
- with unittest.mock.patch('os.name', 'nt'):
- with unittest.mock.patch('sys.version', '... [... (ARM64)]'):
- self.assertEqual(get_host_platform(), 'win-arm64')
- with unittest.mock.patch('sys.version', '... [... (ARM)]'):
- self.assertEqual(get_host_platform(), 'win-arm32')
+ with mock.patch('os.name', 'nt'):
+ with mock.patch('sys.version', '... [... (ARM64)]'):
+ assert get_host_platform() == 'win-arm64'
+ with mock.patch('sys.version', '... [... (ARM)]'):
+ assert get_host_platform() == 'win-arm32'
- with unittest.mock.patch('sys.version_info', (3, 9, 0, 'final', 0)):
- self.assertEqual(get_host_platform(), stdlib_sysconfig.get_platform())
+ with mock.patch('sys.version_info', (3, 9, 0, 'final', 0)):
+ assert get_host_platform() == stdlib_sysconfig.get_platform()
def test_get_platform(self):
- with unittest.mock.patch('os.name', 'nt'):
- with unittest.mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'x86'}):
- self.assertEqual(get_platform(), 'win32')
- with unittest.mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'x64'}):
- self.assertEqual(get_platform(), 'win-amd64')
- with unittest.mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'arm'}):
- self.assertEqual(get_platform(), 'win-arm32')
- with unittest.mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'arm64'}):
- self.assertEqual(get_platform(), 'win-arm64')
+ with mock.patch('os.name', 'nt'):
+ with mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'x86'}):
+ assert get_platform() == 'win32'
+ with mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'x64'}):
+ assert get_platform() == 'win-amd64'
+ with mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'arm'}):
+ assert get_platform() == 'win-arm32'
+ with mock.patch.dict('os.environ', {'VSCMD_ARG_TGT_ARCH': 'arm64'}):
+ assert get_platform() == 'win-arm64'
def test_convert_path(self):
# linux/mac
os.sep = '/'
+
def _join(path):
return '/'.join(path)
+
os.path.join = _join
- self.assertEqual(convert_path('/home/to/my/stuff'),
- '/home/to/my/stuff')
+ assert convert_path('/home/to/my/stuff') == '/home/to/my/stuff'
# win
os.sep = '\\'
+
def _join(*path):
return '\\'.join(path)
+
os.path.join = _join
- self.assertRaises(ValueError, convert_path, '/home/to/my/stuff')
- self.assertRaises(ValueError, convert_path, 'home/to/my/stuff/')
+ with pytest.raises(ValueError):
+ convert_path('/home/to/my/stuff')
+ with pytest.raises(ValueError):
+ convert_path('home/to/my/stuff/')
- self.assertEqual(convert_path('home/to/my/stuff'),
- 'home\\to\\my\\stuff')
- self.assertEqual(convert_path('.'),
- os.curdir)
+ assert convert_path('home/to/my/stuff') == 'home\\to\\my\\stuff'
+ assert convert_path('.') == os.curdir
def test_change_root(self):
# linux/mac
os.name = 'posix'
+
def _isabs(path):
return path[0] == '/'
+
os.path.isabs = _isabs
+
def _join(*path):
return '/'.join(path)
+
os.path.join = _join
- self.assertEqual(change_root('/root', '/old/its/here'),
- '/root/old/its/here')
- self.assertEqual(change_root('/root', 'its/here'),
- '/root/its/here')
+ assert change_root('/root', '/old/its/here') == '/root/old/its/here'
+ assert change_root('/root', 'its/here') == '/root/its/here'
# windows
os.name = 'nt'
+
def _isabs(path):
return path.startswith('c:\\')
+
os.path.isabs = _isabs
+
def _splitdrive(path):
if path.startswith('c:'):
return ('', path.replace('c:', ''))
return ('', path)
+
os.path.splitdrive = _splitdrive
+
def _join(*path):
return '\\'.join(path)
+
os.path.join = _join
- self.assertEqual(change_root('c:\\root', 'c:\\old\\its\\here'),
- 'c:\\root\\old\\its\\here')
- self.assertEqual(change_root('c:\\root', 'its\\here'),
- 'c:\\root\\its\\here')
+ assert (
+ change_root('c:\\root', 'c:\\old\\its\\here') == 'c:\\root\\old\\its\\here'
+ )
+ assert change_root('c:\\root', 'its\\here') == 'c:\\root\\its\\here'
# BugsBunny os (it's a great os)
os.name = 'BugsBunny'
- self.assertRaises(DistutilsPlatformError,
- change_root, 'c:\\root', 'its\\here')
+ with pytest.raises(DistutilsPlatformError):
+ change_root('c:\\root', 'its\\here')
# XXX platforms to be covered: mac
def test_check_environ(self):
- util._environ_checked = 0
+ util.check_environ.cache_clear()
os.environ.pop('HOME', None)
check_environ()
- self.assertEqual(os.environ['PLAT'], get_platform())
- self.assertEqual(util._environ_checked, 1)
+ assert os.environ['PLAT'] == get_platform()
- @unittest.skipUnless(os.name == 'posix', 'specific to posix')
+ @pytest.mark.skipif("os.name != 'posix'")
def test_check_environ_getpwuid(self):
- util._environ_checked = 0
+ util.check_environ.cache_clear()
os.environ.pop('HOME', None)
import pwd
# only set pw_dir field, other fields are not used
- result = pwd.struct_passwd((None, None, None, None, None,
- '/home/distutils', None))
+ result = pwd.struct_passwd(
+ (None, None, None, None, None, '/home/distutils', None)
+ )
with mock.patch.object(pwd, 'getpwuid', return_value=result):
check_environ()
- self.assertEqual(os.environ['HOME'], '/home/distutils')
+ assert os.environ['HOME'] == '/home/distutils'
- util._environ_checked = 0
+ util.check_environ.cache_clear()
os.environ.pop('HOME', None)
# bpo-10496: Catch pwd.getpwuid() error
with mock.patch.object(pwd, 'getpwuid', side_effect=KeyError):
check_environ()
- self.assertNotIn('HOME', os.environ)
+ assert 'HOME' not in os.environ
def test_split_quoted(self):
- self.assertEqual(split_quoted('""one"" "two" \'three\' \\four'),
- ['one', 'two', 'three', 'four'])
+ assert split_quoted('""one"" "two" \'three\' \\four') == [
+ 'one',
+ 'two',
+ 'three',
+ 'four',
+ ]
def test_strtobool(self):
yes = ('y', 'Y', 'yes', 'True', 't', 'true', 'True', 'On', 'on', '1')
no = ('n', 'no', 'f', 'false', 'off', '0', 'Off', 'No', 'N')
for y in yes:
- self.assertTrue(strtobool(y))
+ assert strtobool(y)
for n in no:
- self.assertFalse(strtobool(n))
+ assert not strtobool(n)
def test_rfc822_escape(self):
header = 'I am a\npoor\nlonesome\nheader\n'
res = rfc822_escape(header)
- wanted = ('I am a%(8s)spoor%(8s)slonesome%(8s)s'
- 'header%(8s)s') % {'8s': '\n'+8*' '}
- self.assertEqual(res, wanted)
+ wanted = ('I am a%(8s)spoor%(8s)slonesome%(8s)s' 'header%(8s)s') % {
+ '8s': '\n' + 8 * ' '
+ }
+ assert res == wanted
def test_dont_write_bytecode(self):
# makes sure byte_compile raise a DistutilsError
@@ -206,7 +198,8 @@ class UtilTestCase(support.EnvironGuard, unittest.TestCase):
old_dont_write_bytecode = sys.dont_write_bytecode
sys.dont_write_bytecode = True
try:
- self.assertRaises(DistutilsByteCompileError, byte_compile, [])
+ with pytest.raises(DistutilsByteCompileError):
+ byte_compile([])
finally:
sys.dont_write_bytecode = old_dont_write_bytecode
@@ -214,11 +207,4 @@ class UtilTestCase(support.EnvironGuard, unittest.TestCase):
# test obsolete function to ensure backward compat (#4931)
exc = IOError("Unable to find batch file")
msg = grok_environment_error(exc)
- self.assertEqual(msg, "error: Unable to find batch file")
-
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(UtilTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert msg == "error: Unable to find batch file"
diff --git a/setuptools/_distutils/tests/test_version.py b/setuptools/_distutils/tests/test_version.py
index 8405aa3a..ff52ea46 100644
--- a/setuptools/_distutils/tests/test_version.py
+++ b/setuptools/_distutils/tests/test_version.py
@@ -1,44 +1,45 @@
"""Tests for distutils.version."""
-import unittest
+import pytest
+
import distutils
from distutils.version import LooseVersion
from distutils.version import StrictVersion
-from test.support import run_unittest
-class VersionTestCase(unittest.TestCase):
- def setUp(self):
- self.ctx = distutils.version.suppress_known_deprecation()
- self.ctx.__enter__()
+@pytest.fixture(autouse=True)
+def suppress_deprecation():
+ with distutils.version.suppress_known_deprecation():
+ yield
- def tearDown(self):
- self.ctx.__exit__(None, None, None)
+class TestVersion:
def test_prerelease(self):
version = StrictVersion('1.2.3a1')
- self.assertEqual(version.version, (1, 2, 3))
- self.assertEqual(version.prerelease, ('a', 1))
- self.assertEqual(str(version), '1.2.3a1')
+ assert version.version == (1, 2, 3)
+ assert version.prerelease == ('a', 1)
+ assert str(version) == '1.2.3a1'
version = StrictVersion('1.2.0')
- self.assertEqual(str(version), '1.2')
+ assert str(version) == '1.2'
def test_cmp_strict(self):
- versions = (('1.5.1', '1.5.2b2', -1),
- ('161', '3.10a', ValueError),
- ('8.02', '8.02', 0),
- ('3.4j', '1996.07.12', ValueError),
- ('3.2.pl0', '3.1.1.6', ValueError),
- ('2g6', '11g', ValueError),
- ('0.9', '2.2', -1),
- ('1.2.1', '1.2', 1),
- ('1.1', '1.2.2', -1),
- ('1.2', '1.1', 1),
- ('1.2.1', '1.2.2', -1),
- ('1.2.2', '1.2', 1),
- ('1.2', '1.2.2', -1),
- ('0.4.0', '0.4', 0),
- ('1.13++', '5.5.kw', ValueError))
+ versions = (
+ ('1.5.1', '1.5.2b2', -1),
+ ('161', '3.10a', ValueError),
+ ('8.02', '8.02', 0),
+ ('3.4j', '1996.07.12', ValueError),
+ ('3.2.pl0', '3.1.1.6', ValueError),
+ ('2g6', '11g', ValueError),
+ ('0.9', '2.2', -1),
+ ('1.2.1', '1.2', 1),
+ ('1.1', '1.2.2', -1),
+ ('1.2', '1.1', 1),
+ ('1.2.1', '1.2.2', -1),
+ ('1.2.2', '1.2', 1),
+ ('1.2', '1.2.2', -1),
+ ('0.4.0', '0.4', 0),
+ ('1.13++', '5.5.kw', ValueError),
+ )
for v1, v2, wanted in versions:
try:
@@ -47,49 +48,43 @@ class VersionTestCase(unittest.TestCase):
if wanted is ValueError:
continue
else:
- raise AssertionError(("cmp(%s, %s) "
- "shouldn't raise ValueError")
- % (v1, v2))
- self.assertEqual(res, wanted,
- 'cmp(%s, %s) should be %s, got %s' %
- (v1, v2, wanted, res))
+ raise AssertionError(
+ ("cmp(%s, %s) " "shouldn't raise ValueError") % (v1, v2)
+ )
+ assert res == wanted, 'cmp({}, {}) should be {}, got {}'.format(
+ v1, v2, wanted, res
+ )
res = StrictVersion(v1)._cmp(v2)
- self.assertEqual(res, wanted,
- 'cmp(%s, %s) should be %s, got %s' %
- (v1, v2, wanted, res))
+ assert res == wanted, 'cmp({}, {}) should be {}, got {}'.format(
+ v1, v2, wanted, res
+ )
res = StrictVersion(v1)._cmp(object())
- self.assertIs(res, NotImplemented,
- 'cmp(%s, %s) should be NotImplemented, got %s' %
- (v1, v2, res))
-
+ assert (
+ res is NotImplemented
+ ), 'cmp({}, {}) should be NotImplemented, got {}'.format(v1, v2, res)
def test_cmp(self):
- versions = (('1.5.1', '1.5.2b2', -1),
- ('161', '3.10a', 1),
- ('8.02', '8.02', 0),
- ('3.4j', '1996.07.12', -1),
- ('3.2.pl0', '3.1.1.6', 1),
- ('2g6', '11g', -1),
- ('0.960923', '2.2beta29', -1),
- ('1.13++', '5.5.kw', -1))
-
+ versions = (
+ ('1.5.1', '1.5.2b2', -1),
+ ('161', '3.10a', 1),
+ ('8.02', '8.02', 0),
+ ('3.4j', '1996.07.12', -1),
+ ('3.2.pl0', '3.1.1.6', 1),
+ ('2g6', '11g', -1),
+ ('0.960923', '2.2beta29', -1),
+ ('1.13++', '5.5.kw', -1),
+ )
for v1, v2, wanted in versions:
res = LooseVersion(v1)._cmp(LooseVersion(v2))
- self.assertEqual(res, wanted,
- 'cmp(%s, %s) should be %s, got %s' %
- (v1, v2, wanted, res))
+ assert res == wanted, 'cmp({}, {}) should be {}, got {}'.format(
+ v1, v2, wanted, res
+ )
res = LooseVersion(v1)._cmp(v2)
- self.assertEqual(res, wanted,
- 'cmp(%s, %s) should be %s, got %s' %
- (v1, v2, wanted, res))
+ assert res == wanted, 'cmp({}, {}) should be {}, got {}'.format(
+ v1, v2, wanted, res
+ )
res = LooseVersion(v1)._cmp(object())
- self.assertIs(res, NotImplemented,
- 'cmp(%s, %s) should be NotImplemented, got %s' %
- (v1, v2, res))
-
-def test_suite():
- return unittest.TestLoader().loadTestsFromTestCase(VersionTestCase)
-
-if __name__ == "__main__":
- run_unittest(test_suite())
+ assert (
+ res is NotImplemented
+ ), 'cmp({}, {}) should be NotImplemented, got {}'.format(v1, v2, res)
diff --git a/setuptools/_distutils/tests/test_versionpredicate.py b/setuptools/_distutils/tests/test_versionpredicate.py
index 28ae09dc..e69de29b 100644
--- a/setuptools/_distutils/tests/test_versionpredicate.py
+++ b/setuptools/_distutils/tests/test_versionpredicate.py
@@ -1,13 +0,0 @@
-"""Tests harness for distutils.versionpredicate.
-
-"""
-
-import distutils.versionpredicate
-import doctest
-from test.support import run_unittest
-
-def test_suite():
- return doctest.DocTestSuite(distutils.versionpredicate)
-
-if __name__ == '__main__':
- run_unittest(test_suite())
diff --git a/setuptools/_distutils/tests/unix_compat.py b/setuptools/_distutils/tests/unix_compat.py
index b7718c26..95fc8eeb 100644
--- a/setuptools/_distutils/tests/unix_compat.py
+++ b/setuptools/_distutils/tests/unix_compat.py
@@ -1,5 +1,4 @@
import sys
-import unittest
try:
import grp
@@ -7,10 +6,13 @@ try:
except ImportError:
grp = pwd = None
+import pytest
+
UNIX_ID_SUPPORT = grp and pwd
UID_0_SUPPORT = UNIX_ID_SUPPORT and sys.platform != "cygwin"
-require_unix_id = unittest.skipUnless(
- UNIX_ID_SUPPORT, "Requires grp and pwd support")
-require_uid_0 = unittest.skipUnless(UID_0_SUPPORT, "Requires UID 0 support")
+require_unix_id = pytest.mark.skipif(
+ not UNIX_ID_SUPPORT, reason="Requires grp and pwd support"
+)
+require_uid_0 = pytest.mark.skipif(not UID_0_SUPPORT, reason="Requires UID 0 support")
diff --git a/setuptools/_distutils/tests/xxmodule-3.8.c b/setuptools/_distutils/tests/xxmodule-3.8.c
new file mode 100644
index 00000000..0250031d
--- /dev/null
+++ b/setuptools/_distutils/tests/xxmodule-3.8.c
@@ -0,0 +1,411 @@
+
+/* Use this file as a template to start implementing a module that
+ also declares object types. All occurrences of 'Xxo' should be changed
+ to something reasonable for your objects. After that, all other
+ occurrences of 'xx' should be changed to something reasonable for your
+ module. If your module is named foo your sourcefile should be named
+ foomodule.c.
+
+ You will probably want to delete all references to 'x_attr' and add
+ your own types of attributes instead. Maybe you want to name your
+ local variables other than 'self'. If your object type is needed in
+ other files, you'll have to create a file "foobarobject.h"; see
+ floatobject.h for an example. */
+
+/* Xxo objects */
+
+#include "Python.h"
+
+static PyObject *ErrorObject;
+
+typedef struct {
+ PyObject_HEAD
+ PyObject *x_attr; /* Attributes dictionary */
+} XxoObject;
+
+static PyTypeObject Xxo_Type;
+
+#define XxoObject_Check(v) (Py_TYPE(v) == &Xxo_Type)
+
+static XxoObject *
+newXxoObject(PyObject *arg)
+{
+ XxoObject *self;
+ self = PyObject_New(XxoObject, &Xxo_Type);
+ if (self == NULL)
+ return NULL;
+ self->x_attr = NULL;
+ return self;
+}
+
+/* Xxo methods */
+
+static void
+Xxo_dealloc(XxoObject *self)
+{
+ Py_XDECREF(self->x_attr);
+ PyObject_Del(self);
+}
+
+static PyObject *
+Xxo_demo(XxoObject *self, PyObject *args)
+{
+ if (!PyArg_ParseTuple(args, ":demo"))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+static PyMethodDef Xxo_methods[] = {
+ {"demo", (PyCFunction)Xxo_demo, METH_VARARGS,
+ PyDoc_STR("demo() -> None")},
+ {NULL, NULL} /* sentinel */
+};
+
+static PyObject *
+Xxo_getattro(XxoObject *self, PyObject *name)
+{
+ if (self->x_attr != NULL) {
+ PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
+ if (v != NULL) {
+ Py_INCREF(v);
+ return v;
+ }
+ else if (PyErr_Occurred()) {
+ return NULL;
+ }
+ }
+ return PyObject_GenericGetAttr((PyObject *)self, name);
+}
+
+static int
+Xxo_setattr(XxoObject *self, const char *name, PyObject *v)
+{
+ if (self->x_attr == NULL) {
+ self->x_attr = PyDict_New();
+ if (self->x_attr == NULL)
+ return -1;
+ }
+ if (v == NULL) {
+ int rv = PyDict_DelItemString(self->x_attr, name);
+ if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
+ PyErr_SetString(PyExc_AttributeError,
+ "delete non-existing Xxo attribute");
+ return rv;
+ }
+ else
+ return PyDict_SetItemString(self->x_attr, name, v);
+}
+
+static PyTypeObject Xxo_Type = {
+ /* The ob_type field must be initialized in the module init function
+ * to be portable to Windows without using C++. */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "xxmodule.Xxo", /*tp_name*/
+ sizeof(XxoObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)Xxo_dealloc, /*tp_dealloc*/
+ 0, /*tp_vectorcall_offset*/
+ (getattrfunc)0, /*tp_getattr*/
+ (setattrfunc)Xxo_setattr, /*tp_setattr*/
+ 0, /*tp_as_async*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ (getattrofunc)Xxo_getattro, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ Xxo_methods, /*tp_methods*/
+ 0, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ 0, /*tp_init*/
+ 0, /*tp_alloc*/
+ 0, /*tp_new*/
+ 0, /*tp_free*/
+ 0, /*tp_is_gc*/
+};
+/* --------------------------------------------------------------------- */
+
+/* Function of two integers returning integer */
+
+PyDoc_STRVAR(xx_foo_doc,
+"foo(i,j)\n\
+\n\
+Return the sum of i and j.");
+
+static PyObject *
+xx_foo(PyObject *self, PyObject *args)
+{
+ long i, j;
+ long res;
+ if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
+ return NULL;
+ res = i+j; /* XXX Do something here */
+ return PyLong_FromLong(res);
+}
+
+
+/* Function of no arguments returning new Xxo object */
+
+static PyObject *
+xx_new(PyObject *self, PyObject *args)
+{
+ XxoObject *rv;
+
+ if (!PyArg_ParseTuple(args, ":new"))
+ return NULL;
+ rv = newXxoObject(args);
+ if (rv == NULL)
+ return NULL;
+ return (PyObject *)rv;
+}
+
+/* Example with subtle bug from extensions manual ("Thin Ice"). */
+
+static PyObject *
+xx_bug(PyObject *self, PyObject *args)
+{
+ PyObject *list, *item;
+
+ if (!PyArg_ParseTuple(args, "O:bug", &list))
+ return NULL;
+
+ item = PyList_GetItem(list, 0);
+ /* Py_INCREF(item); */
+ PyList_SetItem(list, 1, PyLong_FromLong(0L));
+ PyObject_Print(item, stdout, 0);
+ printf("\n");
+ /* Py_DECREF(item); */
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+/* Test bad format character */
+
+static PyObject *
+xx_roj(PyObject *self, PyObject *args)
+{
+ PyObject *a;
+ long b;
+ if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+
+/* ---------- */
+
+static PyTypeObject Str_Type = {
+ /* The ob_type field must be initialized in the module init function
+ * to be portable to Windows without using C++. */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "xxmodule.Str", /*tp_name*/
+ 0, /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ 0, /*tp_dealloc*/
+ 0, /*tp_vectorcall_offset*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_as_async*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ 0, /*tp_methods*/
+ 0, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /* see PyInit_xx */ /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ 0, /*tp_init*/
+ 0, /*tp_alloc*/
+ 0, /*tp_new*/
+ 0, /*tp_free*/
+ 0, /*tp_is_gc*/
+};
+
+/* ---------- */
+
+static PyObject *
+null_richcompare(PyObject *self, PyObject *other, int op)
+{
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+}
+
+static PyTypeObject Null_Type = {
+ /* The ob_type field must be initialized in the module init function
+ * to be portable to Windows without using C++. */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "xxmodule.Null", /*tp_name*/
+ 0, /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ 0, /*tp_dealloc*/
+ 0, /*tp_vectorcall_offset*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_as_async*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ null_richcompare, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ 0, /*tp_methods*/
+ 0, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /* see PyInit_xx */ /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ 0, /*tp_init*/
+ 0, /*tp_alloc*/
+ PyType_GenericNew, /*tp_new*/
+ 0, /*tp_free*/
+ 0, /*tp_is_gc*/
+};
+
+
+/* ---------- */
+
+
+/* List of functions defined in the module */
+
+static PyMethodDef xx_methods[] = {
+ {"roj", xx_roj, METH_VARARGS,
+ PyDoc_STR("roj(a,b) -> None")},
+ {"foo", xx_foo, METH_VARARGS,
+ xx_foo_doc},
+ {"new", xx_new, METH_VARARGS,
+ PyDoc_STR("new() -> new Xx object")},
+ {"bug", xx_bug, METH_VARARGS,
+ PyDoc_STR("bug(o) -> None")},
+ {NULL, NULL} /* sentinel */
+};
+
+PyDoc_STRVAR(module_doc,
+"This is a template module just for instruction.");
+
+
+static int
+xx_exec(PyObject *m)
+{
+ /* Slot initialization is subject to the rules of initializing globals.
+ C99 requires the initializers to be "address constants". Function
+ designators like 'PyType_GenericNew', with implicit conversion to
+ a pointer, are valid C99 address constants.
+
+ However, the unary '&' operator applied to a non-static variable
+ like 'PyBaseObject_Type' is not required to produce an address
+ constant. Compilers may support this (gcc does), MSVC does not.
+
+ Both compilers are strictly standard conforming in this particular
+ behavior.
+ */
+ Null_Type.tp_base = &PyBaseObject_Type;
+ Str_Type.tp_base = &PyUnicode_Type;
+
+ /* Finalize the type object including setting type of the new type
+ * object; doing it here is required for portability, too. */
+ if (PyType_Ready(&Xxo_Type) < 0)
+ goto fail;
+
+ /* Add some symbolic constants to the module */
+ if (ErrorObject == NULL) {
+ ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
+ if (ErrorObject == NULL)
+ goto fail;
+ }
+ Py_INCREF(ErrorObject);
+ PyModule_AddObject(m, "error", ErrorObject);
+
+ /* Add Str */
+ if (PyType_Ready(&Str_Type) < 0)
+ goto fail;
+ PyModule_AddObject(m, "Str", (PyObject *)&Str_Type);
+
+ /* Add Null */
+ if (PyType_Ready(&Null_Type) < 0)
+ goto fail;
+ PyModule_AddObject(m, "Null", (PyObject *)&Null_Type);
+ return 0;
+ fail:
+ Py_XDECREF(m);
+ return -1;
+}
+
+static struct PyModuleDef_Slot xx_slots[] = {
+ {Py_mod_exec, xx_exec},
+ {0, NULL},
+};
+
+static struct PyModuleDef xxmodule = {
+ PyModuleDef_HEAD_INIT,
+ "xx",
+ module_doc,
+ 0,
+ xx_methods,
+ xx_slots,
+ NULL,
+ NULL,
+ NULL
+};
+
+/* Export function for the module (*must* be called PyInit_xx) */
+
+PyMODINIT_FUNC
+PyInit_xx(void)
+{
+ return PyModuleDef_Init(&xxmodule);
+}
diff --git a/setuptools/_distutils/tests/xxmodule.c b/setuptools/_distutils/tests/xxmodule.c
new file mode 100644
index 00000000..a6e5071d
--- /dev/null
+++ b/setuptools/_distutils/tests/xxmodule.c
@@ -0,0 +1,412 @@
+
+/* Use this file as a template to start implementing a module that
+ also declares object types. All occurrences of 'Xxo' should be changed
+ to something reasonable for your objects. After that, all other
+ occurrences of 'xx' should be changed to something reasonable for your
+ module. If your module is named foo your sourcefile should be named
+ foomodule.c.
+
+ You will probably want to delete all references to 'x_attr' and add
+ your own types of attributes instead. Maybe you want to name your
+ local variables other than 'self'. If your object type is needed in
+ other files, you'll have to create a file "foobarobject.h"; see
+ floatobject.h for an example. */
+
+/* Xxo objects */
+
+#include "Python.h"
+
+static PyObject *ErrorObject;
+
+typedef struct {
+ PyObject_HEAD
+ PyObject *x_attr; /* Attributes dictionary */
+} XxoObject;
+
+static PyTypeObject Xxo_Type;
+
+#define XxoObject_Check(v) Py_IS_TYPE(v, &Xxo_Type)
+
+static XxoObject *
+newXxoObject(PyObject *arg)
+{
+ XxoObject *self;
+ self = PyObject_New(XxoObject, &Xxo_Type);
+ if (self == NULL)
+ return NULL;
+ self->x_attr = NULL;
+ return self;
+}
+
+/* Xxo methods */
+
+static void
+Xxo_dealloc(XxoObject *self)
+{
+ Py_XDECREF(self->x_attr);
+ PyObject_Free(self);
+}
+
+static PyObject *
+Xxo_demo(XxoObject *self, PyObject *args)
+{
+ if (!PyArg_ParseTuple(args, ":demo"))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+static PyMethodDef Xxo_methods[] = {
+ {"demo", (PyCFunction)Xxo_demo, METH_VARARGS,
+ PyDoc_STR("demo() -> None")},
+ {NULL, NULL} /* sentinel */
+};
+
+static PyObject *
+Xxo_getattro(XxoObject *self, PyObject *name)
+{
+ if (self->x_attr != NULL) {
+ PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
+ if (v != NULL) {
+ Py_INCREF(v);
+ return v;
+ }
+ else if (PyErr_Occurred()) {
+ return NULL;
+ }
+ }
+ return PyObject_GenericGetAttr((PyObject *)self, name);
+}
+
+static int
+Xxo_setattr(XxoObject *self, const char *name, PyObject *v)
+{
+ if (self->x_attr == NULL) {
+ self->x_attr = PyDict_New();
+ if (self->x_attr == NULL)
+ return -1;
+ }
+ if (v == NULL) {
+ int rv = PyDict_DelItemString(self->x_attr, name);
+ if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
+ PyErr_SetString(PyExc_AttributeError,
+ "delete non-existing Xxo attribute");
+ return rv;
+ }
+ else
+ return PyDict_SetItemString(self->x_attr, name, v);
+}
+
+static PyTypeObject Xxo_Type = {
+ /* The ob_type field must be initialized in the module init function
+ * to be portable to Windows without using C++. */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "xxmodule.Xxo", /*tp_name*/
+ sizeof(XxoObject), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ (destructor)Xxo_dealloc, /*tp_dealloc*/
+ 0, /*tp_vectorcall_offset*/
+ (getattrfunc)0, /*tp_getattr*/
+ (setattrfunc)Xxo_setattr, /*tp_setattr*/
+ 0, /*tp_as_async*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ (getattrofunc)Xxo_getattro, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ Xxo_methods, /*tp_methods*/
+ 0, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ 0, /*tp_init*/
+ 0, /*tp_alloc*/
+ 0, /*tp_new*/
+ 0, /*tp_free*/
+ 0, /*tp_is_gc*/
+};
+/* --------------------------------------------------------------------- */
+
+/* Function of two integers returning integer */
+
+PyDoc_STRVAR(xx_foo_doc,
+"foo(i,j)\n\
+\n\
+Return the sum of i and j.");
+
+static PyObject *
+xx_foo(PyObject *self, PyObject *args)
+{
+ long i, j;
+ long res;
+ if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
+ return NULL;
+ res = i+j; /* XXX Do something here */
+ return PyLong_FromLong(res);
+}
+
+
+/* Function of no arguments returning new Xxo object */
+
+static PyObject *
+xx_new(PyObject *self, PyObject *args)
+{
+ XxoObject *rv;
+
+ if (!PyArg_ParseTuple(args, ":new"))
+ return NULL;
+ rv = newXxoObject(args);
+ if (rv == NULL)
+ return NULL;
+ return (PyObject *)rv;
+}
+
+/* Example with subtle bug from extensions manual ("Thin Ice"). */
+
+static PyObject *
+xx_bug(PyObject *self, PyObject *args)
+{
+ PyObject *list, *item;
+
+ if (!PyArg_ParseTuple(args, "O:bug", &list))
+ return NULL;
+
+ item = PyList_GetItem(list, 0);
+ /* Py_INCREF(item); */
+ PyList_SetItem(list, 1, PyLong_FromLong(0L));
+ PyObject_Print(item, stdout, 0);
+ printf("\n");
+ /* Py_DECREF(item); */
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+/* Test bad format character */
+
+static PyObject *
+xx_roj(PyObject *self, PyObject *args)
+{
+ PyObject *a;
+ long b;
+ if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+
+/* ---------- */
+
+static PyTypeObject Str_Type = {
+ /* The ob_type field must be initialized in the module init function
+ * to be portable to Windows without using C++. */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "xxmodule.Str", /*tp_name*/
+ 0, /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ 0, /*tp_dealloc*/
+ 0, /*tp_vectorcall_offset*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_as_async*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ 0, /*tp_methods*/
+ 0, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /* see PyInit_xx */ /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ 0, /*tp_init*/
+ 0, /*tp_alloc*/
+ 0, /*tp_new*/
+ 0, /*tp_free*/
+ 0, /*tp_is_gc*/
+};
+
+/* ---------- */
+
+static PyObject *
+null_richcompare(PyObject *self, PyObject *other, int op)
+{
+ Py_INCREF(Py_NotImplemented);
+ return Py_NotImplemented;
+}
+
+static PyTypeObject Null_Type = {
+ /* The ob_type field must be initialized in the module init function
+ * to be portable to Windows without using C++. */
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "xxmodule.Null", /*tp_name*/
+ 0, /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ /* methods */
+ 0, /*tp_dealloc*/
+ 0, /*tp_vectorcall_offset*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_as_async*/
+ 0, /*tp_repr*/
+ 0, /*tp_as_number*/
+ 0, /*tp_as_sequence*/
+ 0, /*tp_as_mapping*/
+ 0, /*tp_hash*/
+ 0, /*tp_call*/
+ 0, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ 0, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ null_richcompare, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ 0, /*tp_methods*/
+ 0, /*tp_members*/
+ 0, /*tp_getset*/
+ 0, /* see PyInit_xx */ /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ 0, /*tp_init*/
+ 0, /*tp_alloc*/
+ PyType_GenericNew, /*tp_new*/
+ 0, /*tp_free*/
+ 0, /*tp_is_gc*/
+};
+
+
+/* ---------- */
+
+
+/* List of functions defined in the module */
+
+static PyMethodDef xx_methods[] = {
+ {"roj", xx_roj, METH_VARARGS,
+ PyDoc_STR("roj(a,b) -> None")},
+ {"foo", xx_foo, METH_VARARGS,
+ xx_foo_doc},
+ {"new", xx_new, METH_VARARGS,
+ PyDoc_STR("new() -> new Xx object")},
+ {"bug", xx_bug, METH_VARARGS,
+ PyDoc_STR("bug(o) -> None")},
+ {NULL, NULL} /* sentinel */
+};
+
+PyDoc_STRVAR(module_doc,
+"This is a template module just for instruction.");
+
+
+static int
+xx_exec(PyObject *m)
+{
+ /* Slot initialization is subject to the rules of initializing globals.
+ C99 requires the initializers to be "address constants". Function
+ designators like 'PyType_GenericNew', with implicit conversion to
+ a pointer, are valid C99 address constants.
+
+ However, the unary '&' operator applied to a non-static variable
+ like 'PyBaseObject_Type' is not required to produce an address
+ constant. Compilers may support this (gcc does), MSVC does not.
+
+ Both compilers are strictly standard conforming in this particular
+ behavior.
+ */
+ Null_Type.tp_base = &PyBaseObject_Type;
+ Str_Type.tp_base = &PyUnicode_Type;
+
+ /* Finalize the type object including setting type of the new type
+ * object; doing it here is required for portability, too. */
+ if (PyType_Ready(&Xxo_Type) < 0) {
+ return -1;
+ }
+
+ /* Add some symbolic constants to the module */
+ if (ErrorObject == NULL) {
+ ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
+ if (ErrorObject == NULL) {
+ return -1;
+ }
+ }
+ int rc = PyModule_AddType(m, (PyTypeObject *)ErrorObject);
+ Py_DECREF(ErrorObject);
+ if (rc < 0) {
+ return -1;
+ }
+
+ /* Add Str and Null types */
+ if (PyModule_AddType(m, &Str_Type) < 0) {
+ return -1;
+ }
+ if (PyModule_AddType(m, &Null_Type) < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+static struct PyModuleDef_Slot xx_slots[] = {
+ {Py_mod_exec, xx_exec},
+ {0, NULL},
+};
+
+static struct PyModuleDef xxmodule = {
+ PyModuleDef_HEAD_INIT,
+ "xx",
+ module_doc,
+ 0,
+ xx_methods,
+ xx_slots,
+ NULL,
+ NULL,
+ NULL
+};
+
+/* Export function for the module (*must* be called PyInit_xx) */
+
+PyMODINIT_FUNC
+PyInit_xx(void)
+{
+ return PyModuleDef_Init(&xxmodule);
+}
diff --git a/setuptools/_distutils/text_file.py b/setuptools/_distutils/text_file.py
index 93abad38..7274d4b1 100644
--- a/setuptools/_distutils/text_file.py
+++ b/setuptools/_distutils/text_file.py
@@ -4,84 +4,87 @@ provides the TextFile class, which gives an interface to text files
that (optionally) takes care of stripping comments, ignoring blank
lines, and joining lines with backslashes."""
-import sys, io
+import sys
class TextFile:
"""Provides a file-like object that takes care of all the things you
- commonly want to do when processing a text file that has some
- line-by-line syntax: strip comments (as long as "#" is your
- comment character), skip blank lines, join adjacent lines by
- escaping the newline (ie. backslash at end of line), strip
- leading and/or trailing whitespace. All of these are optional
- and independently controllable.
-
- Provides a 'warn()' method so you can generate warning messages that
- report physical line number, even if the logical line in question
- spans multiple physical lines. Also provides 'unreadline()' for
- implementing line-at-a-time lookahead.
-
- Constructor is called as:
-
- TextFile (filename=None, file=None, **options)
-
- It bombs (RuntimeError) if both 'filename' and 'file' are None;
- 'filename' should be a string, and 'file' a file object (or
- something that provides 'readline()' and 'close()' methods). It is
- recommended that you supply at least 'filename', so that TextFile
- can include it in warning messages. If 'file' is not supplied,
- TextFile creates its own using 'io.open()'.
-
- The options are all boolean, and affect the value returned by
- 'readline()':
- strip_comments [default: true]
- strip from "#" to end-of-line, as well as any whitespace
- leading up to the "#" -- unless it is escaped by a backslash
- lstrip_ws [default: false]
- strip leading whitespace from each line before returning it
- rstrip_ws [default: true]
- strip trailing whitespace (including line terminator!) from
- each line before returning it
- skip_blanks [default: true}
- skip lines that are empty *after* stripping comments and
- whitespace. (If both lstrip_ws and rstrip_ws are false,
- then some lines may consist of solely whitespace: these will
- *not* be skipped, even if 'skip_blanks' is true.)
- join_lines [default: false]
- if a backslash is the last non-newline character on a line
- after stripping comments and whitespace, join the following line
- to it to form one "logical line"; if N consecutive lines end
- with a backslash, then N+1 physical lines will be joined to
- form one logical line.
- collapse_join [default: false]
- strip leading whitespace from lines that are joined to their
- predecessor; only matters if (join_lines and not lstrip_ws)
- errors [default: 'strict']
- error handler used to decode the file content
-
- Note that since 'rstrip_ws' can strip the trailing newline, the
- semantics of 'readline()' must differ from those of the builtin file
- object's 'readline()' method! In particular, 'readline()' returns
- None for end-of-file: an empty string might just be a blank line (or
- an all-whitespace line), if 'rstrip_ws' is true but 'skip_blanks' is
- not."""
-
- default_options = { 'strip_comments': 1,
- 'skip_blanks': 1,
- 'lstrip_ws': 0,
- 'rstrip_ws': 1,
- 'join_lines': 0,
- 'collapse_join': 0,
- 'errors': 'strict',
- }
+ commonly want to do when processing a text file that has some
+ line-by-line syntax: strip comments (as long as "#" is your
+ comment character), skip blank lines, join adjacent lines by
+ escaping the newline (ie. backslash at end of line), strip
+ leading and/or trailing whitespace. All of these are optional
+ and independently controllable.
+
+ Provides a 'warn()' method so you can generate warning messages that
+ report physical line number, even if the logical line in question
+ spans multiple physical lines. Also provides 'unreadline()' for
+ implementing line-at-a-time lookahead.
+
+ Constructor is called as:
+
+ TextFile (filename=None, file=None, **options)
+
+ It bombs (RuntimeError) if both 'filename' and 'file' are None;
+ 'filename' should be a string, and 'file' a file object (or
+ something that provides 'readline()' and 'close()' methods). It is
+ recommended that you supply at least 'filename', so that TextFile
+ can include it in warning messages. If 'file' is not supplied,
+ TextFile creates its own using 'io.open()'.
+
+ The options are all boolean, and affect the value returned by
+ 'readline()':
+ strip_comments [default: true]
+ strip from "#" to end-of-line, as well as any whitespace
+ leading up to the "#" -- unless it is escaped by a backslash
+ lstrip_ws [default: false]
+ strip leading whitespace from each line before returning it
+ rstrip_ws [default: true]
+ strip trailing whitespace (including line terminator!) from
+ each line before returning it
+ skip_blanks [default: true}
+ skip lines that are empty *after* stripping comments and
+ whitespace. (If both lstrip_ws and rstrip_ws are false,
+ then some lines may consist of solely whitespace: these will
+ *not* be skipped, even if 'skip_blanks' is true.)
+ join_lines [default: false]
+ if a backslash is the last non-newline character on a line
+ after stripping comments and whitespace, join the following line
+ to it to form one "logical line"; if N consecutive lines end
+ with a backslash, then N+1 physical lines will be joined to
+ form one logical line.
+ collapse_join [default: false]
+ strip leading whitespace from lines that are joined to their
+ predecessor; only matters if (join_lines and not lstrip_ws)
+ errors [default: 'strict']
+ error handler used to decode the file content
+
+ Note that since 'rstrip_ws' can strip the trailing newline, the
+ semantics of 'readline()' must differ from those of the builtin file
+ object's 'readline()' method! In particular, 'readline()' returns
+ None for end-of-file: an empty string might just be a blank line (or
+ an all-whitespace line), if 'rstrip_ws' is true but 'skip_blanks' is
+ not."""
+
+ default_options = {
+ 'strip_comments': 1,
+ 'skip_blanks': 1,
+ 'lstrip_ws': 0,
+ 'rstrip_ws': 1,
+ 'join_lines': 0,
+ 'collapse_join': 0,
+ 'errors': 'strict',
+ }
def __init__(self, filename=None, file=None, **options):
"""Construct a new TextFile object. At least one of 'filename'
- (a string) and 'file' (a file-like object) must be supplied.
- They keyword argument options are described above and affect
- the values returned by 'readline()'."""
+ (a string) and 'file' (a file-like object) must be supplied.
+ They keyword argument options are described above and affect
+ the values returned by 'readline()'."""
if filename is None and file is None:
- raise RuntimeError("you must supply either or both of 'filename' and 'file'")
+ raise RuntimeError(
+ "you must supply either or both of 'filename' and 'file'"
+ )
# set values for all options -- either from client option hash
# or fallback to default_options
@@ -101,7 +104,7 @@ class TextFile:
else:
self.filename = filename
self.file = file
- self.current_line = 0 # assuming that file is at BOF!
+ self.current_line = 0 # assuming that file is at BOF!
# 'linebuf' is a stack of lines that will be emptied before we
# actually read from the file; it's only populated by an
@@ -110,14 +113,14 @@ class TextFile:
def open(self, filename):
"""Open a new file named 'filename'. This overrides both the
- 'filename' and 'file' arguments to the constructor."""
+ 'filename' and 'file' arguments to the constructor."""
self.filename = filename
- self.file = io.open(self.filename, 'r', errors=self.errors)
+ self.file = open(self.filename, errors=self.errors)
self.current_line = 0
def close(self):
"""Close the current file and forget everything we know about it
- (filename, current line number)."""
+ (filename, current line number)."""
file = self.file
self.file = None
self.filename = None
@@ -141,24 +144,24 @@ class TextFile:
def warn(self, msg, line=None):
"""Print (to stderr) a warning message tied to the current logical
- line in the current file. If the current logical line in the
- file spans multiple physical lines, the warning refers to the
- whole range, eg. "lines 3-5". If 'line' supplied, it overrides
- the current line number; it may be a list or tuple to indicate a
- range of physical lines, or an integer for a single physical
- line."""
+ line in the current file. If the current logical line in the
+ file spans multiple physical lines, the warning refers to the
+ whole range, eg. "lines 3-5". If 'line' supplied, it overrides
+ the current line number; it may be a list or tuple to indicate a
+ range of physical lines, or an integer for a single physical
+ line."""
sys.stderr.write("warning: " + self.gen_error(msg, line) + "\n")
- def readline(self):
+ def readline(self): # noqa: C901
"""Read and return a single logical line from the current file (or
- from an internal buffer if lines have previously been "unread"
- with 'unreadline()'). If the 'join_lines' option is true, this
- may involve reading multiple physical lines concatenated into a
- single string. Updates the current line number, so calling
- 'warn()' after 'readline()' emits a warning about the physical
- line(s) just read. Returns None on end-of-file, since the empty
- string can occur if 'rstrip_ws' is true but 'strip_blanks' is
- not."""
+ from an internal buffer if lines have previously been "unread"
+ with 'unreadline()'). If the 'join_lines' option is true, this
+ may involve reading multiple physical lines concatenated into a
+ single string. Updates the current line number, so calling
+ 'warn()' after 'readline()' emits a warning about the physical
+ line(s) just read. Returns None on end-of-file, since the empty
+ string can occur if 'rstrip_ws' is true but 'strip_blanks' is
+ not."""
# If any "unread" lines waiting in 'linebuf', return the top
# one. (We don't actually buffer read-ahead data -- lines only
# get put in 'linebuf' if the client explicitly does an
@@ -187,12 +190,12 @@ class TextFile:
# lurking in there) and otherwise leave the line alone.
pos = line.find("#")
- if pos == -1: # no "#" -- no comments
+ if pos == -1: # no "#" -- no comments
pass
# It's definitely a comment -- either "#" is the first
# character, or it's elsewhere and unescaped.
- elif pos == 0 or line[pos-1] != "\\":
+ elif pos == 0 or line[pos - 1] != "\\":
# Have to preserve the trailing newline, because it's
# the job of a later step (rstrip_ws) to remove it --
# and if rstrip_ws is false, we'd better preserve it!
@@ -211,15 +214,14 @@ class TextFile:
# result in "hello there".
if line.strip() == "":
continue
- else: # it's an escaped "#"
+ else: # it's an escaped "#"
line = line.replace("\\#", "#")
# did previous line end with a backslash? then accumulate
if self.join_lines and buildup_line:
# oops: end of file
if line is None:
- self.warn("continuation line immediately precedes "
- "end-of-file")
+ self.warn("continuation line immediately precedes " "end-of-file")
return buildup_line
if self.collapse_join:
@@ -230,11 +232,10 @@ class TextFile:
if isinstance(self.current_line, list):
self.current_line[1] = self.current_line[1] + 1
else:
- self.current_line = [self.current_line,
- self.current_line + 1]
+ self.current_line = [self.current_line, self.current_line + 1]
# just an ordinary line, read it as usual
else:
- if line is None: # eof
+ if line is None: # eof
return None
# still have to be careful about incrementing the line number!
@@ -271,7 +272,7 @@ class TextFile:
def readlines(self):
"""Read and return the list of all logical lines remaining in the
- current file."""
+ current file."""
lines = []
while True:
line = self.readline()
@@ -281,6 +282,6 @@ class TextFile:
def unreadline(self, line):
"""Push 'line' (a string) onto an internal buffer that will be
- checked by future 'readline()' calls. Handy for implementing
- a parser with line-at-a-time lookahead."""
+ checked by future 'readline()' calls. Handy for implementing
+ a parser with line-at-a-time lookahead."""
self.linebuf.append(line)
diff --git a/setuptools/_distutils/unixccompiler.py b/setuptools/_distutils/unixccompiler.py
index a07e5988..4bf2e6a6 100644
--- a/setuptools/_distutils/unixccompiler.py
+++ b/setuptools/_distutils/unixccompiler.py
@@ -13,18 +13,18 @@ the "typical" Unix-style command-line C compiler:
* link shared library handled by 'cc -shared'
"""
-import os, sys, re, shlex
-
-from distutils import sysconfig
-from distutils.dep_util import newer
-from distutils.ccompiler import \
- CCompiler, gen_preprocess_options, gen_lib_options
-from distutils.errors import \
- DistutilsExecError, CompileError, LibError, LinkError
-from distutils import log
-
-if sys.platform == 'darwin':
- import _osx_support
+import os
+import sys
+import re
+import shlex
+import itertools
+
+from . import sysconfig
+from .dep_util import newer
+from .ccompiler import CCompiler, gen_preprocess_options, gen_lib_options
+from .errors import DistutilsExecError, CompileError, LibError, LinkError
+from ._log import log
+from ._macos_compat import compiler_fixup
# XXX Things not currently handled:
# * optimization/debug/warning flags; we just use whatever's in Python's
@@ -42,6 +42,66 @@ if sys.platform == 'darwin':
# options and carry on.
+def _split_env(cmd):
+ """
+ For macOS, split command into 'env' portion (if any)
+ and the rest of the linker command.
+
+ >>> _split_env(['a', 'b', 'c'])
+ ([], ['a', 'b', 'c'])
+ >>> _split_env(['/usr/bin/env', 'A=3', 'gcc'])
+ (['/usr/bin/env', 'A=3'], ['gcc'])
+ """
+ pivot = 0
+ if os.path.basename(cmd[0]) == "env":
+ pivot = 1
+ while '=' in cmd[pivot]:
+ pivot += 1
+ return cmd[:pivot], cmd[pivot:]
+
+
+def _split_aix(cmd):
+ """
+ AIX platforms prefix the compiler with the ld_so_aix
+ script, so split that from the linker command.
+
+ >>> _split_aix(['a', 'b', 'c'])
+ ([], ['a', 'b', 'c'])
+ >>> _split_aix(['/bin/foo/ld_so_aix', 'gcc'])
+ (['/bin/foo/ld_so_aix'], ['gcc'])
+ """
+ pivot = os.path.basename(cmd[0]) == 'ld_so_aix'
+ return cmd[:pivot], cmd[pivot:]
+
+
+def _linker_params(linker_cmd, compiler_cmd):
+ """
+ The linker command usually begins with the compiler
+ command (possibly multiple elements), followed by zero or more
+ params for shared library building.
+
+ If the LDSHARED env variable overrides the linker command,
+ however, the commands may not match.
+
+ Return the best guess of the linker parameters by stripping
+ the linker command. If the compiler command does not
+ match the linker command, assume the linker command is
+ just the first element.
+
+ >>> _linker_params('gcc foo bar'.split(), ['gcc'])
+ ['foo', 'bar']
+ >>> _linker_params('gcc foo bar'.split(), ['other'])
+ ['foo', 'bar']
+ >>> _linker_params('ccache gcc foo bar'.split(), 'ccache gcc'.split())
+ ['foo', 'bar']
+ >>> _linker_params(['gcc'], ['gcc'])
+ []
+ """
+ c_len = len(compiler_cmd)
+ pivot = c_len if linker_cmd[:c_len] == compiler_cmd else 1
+ return linker_cmd[pivot:]
+
+
class UnixCCompiler(CCompiler):
compiler_type = 'unix'
@@ -52,15 +112,16 @@ class UnixCCompiler(CCompiler):
# are pretty generic; they will probably have to be set by an outsider
# (eg. using information discovered by the sysconfig about building
# Python extensions).
- executables = {'preprocessor' : None,
- 'compiler' : ["cc"],
- 'compiler_so' : ["cc"],
- 'compiler_cxx' : ["cc"],
- 'linker_so' : ["cc", "-shared"],
- 'linker_exe' : ["cc"],
- 'archiver' : ["ar", "-cr"],
- 'ranlib' : None,
- }
+ executables = {
+ 'preprocessor': None,
+ 'compiler': ["cc"],
+ 'compiler_so': ["cc"],
+ 'compiler_cxx': ["cc"],
+ 'linker_so': ["cc", "-shared"],
+ 'linker_exe': ["cc"],
+ 'archiver': ["ar", "-cr"],
+ 'ranlib': None,
+ }
if sys.platform[:6] == "darwin":
executables['ranlib'] = ["ranlib"]
@@ -71,7 +132,7 @@ class UnixCCompiler(CCompiler):
# reasonable common default here, but it's not necessarily used on all
# Unices!
- src_extensions = [".c",".C",".cc",".cxx",".cpp",".m"]
+ src_extensions = [".c", ".C", ".cc", ".cxx", ".cpp", ".m"]
obj_extension = ".o"
static_lib_extension = ".a"
shared_lib_extension = ".so"
@@ -82,8 +143,15 @@ class UnixCCompiler(CCompiler):
if sys.platform == "cygwin":
exe_extension = ".exe"
- def preprocess(self, source, output_file=None, macros=None,
- include_dirs=None, extra_preargs=None, extra_postargs=None):
+ def preprocess(
+ self,
+ source,
+ output_file=None,
+ macros=None,
+ include_dirs=None,
+ extra_preargs=None,
+ extra_postargs=None,
+ ):
fixed_args = self._fix_compile_args(None, macros, include_dirs)
ignore, macros, include_dirs = fixed_args
pp_opts = gen_preprocess_options(macros, include_dirs)
@@ -96,41 +164,39 @@ class UnixCCompiler(CCompiler):
pp_args.extend(extra_postargs)
pp_args.append(source)
- # We need to preprocess: either we're being forced to, or we're
- # generating output to stdout, or there's a target output file and
- # the source file is newer than the target (or the target doesn't
- # exist).
- if self.force or output_file is None or newer(source, output_file):
- if output_file:
- self.mkpath(os.path.dirname(output_file))
- try:
- self.spawn(pp_args)
- except DistutilsExecError as msg:
- raise CompileError(msg)
+ # reasons to preprocess:
+ # - force is indicated
+ # - output is directed to stdout
+ # - source file is newer than the target
+ preprocess = self.force or output_file is None or newer(source, output_file)
+ if not preprocess:
+ return
+
+ if output_file:
+ self.mkpath(os.path.dirname(output_file))
+
+ try:
+ self.spawn(pp_args)
+ except DistutilsExecError as msg:
+ raise CompileError(msg)
def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
- compiler_so = self.compiler_so
- if sys.platform == 'darwin':
- compiler_so = _osx_support.compiler_fixup(compiler_so,
- cc_args + extra_postargs)
+ compiler_so = compiler_fixup(self.compiler_so, cc_args + extra_postargs)
try:
- self.spawn(compiler_so + cc_args + [src, '-o', obj] +
- extra_postargs)
+ self.spawn(compiler_so + cc_args + [src, '-o', obj] + extra_postargs)
except DistutilsExecError as msg:
raise CompileError(msg)
- def create_static_lib(self, objects, output_libname,
- output_dir=None, debug=0, target_lang=None):
+ def create_static_lib(
+ self, objects, output_libname, output_dir=None, debug=0, target_lang=None
+ ):
objects, output_dir = self._fix_object_args(objects, output_dir)
- output_filename = \
- self.library_filename(output_libname, output_dir=output_dir)
+ output_filename = self.library_filename(output_libname, output_dir=output_dir)
if self._need_link(objects, output_filename):
self.mkpath(os.path.dirname(output_filename))
- self.spawn(self.archiver +
- [output_filename] +
- objects + self.objects)
+ self.spawn(self.archiver + [output_filename] + objects + self.objects)
# Not many Unices required ranlib anymore -- SunOS 4.x is, I
# think the only major Unix that does. Maybe we need some
@@ -145,26 +211,34 @@ class UnixCCompiler(CCompiler):
else:
log.debug("skipping %s (up-to-date)", output_filename)
- def link(self, target_desc, objects,
- output_filename, output_dir=None, libraries=None,
- library_dirs=None, runtime_library_dirs=None,
- export_symbols=None, debug=0, extra_preargs=None,
- extra_postargs=None, build_temp=None, target_lang=None):
+ def link(
+ self,
+ target_desc,
+ objects,
+ output_filename,
+ output_dir=None,
+ libraries=None,
+ library_dirs=None,
+ runtime_library_dirs=None,
+ export_symbols=None,
+ debug=0,
+ extra_preargs=None,
+ extra_postargs=None,
+ build_temp=None,
+ target_lang=None,
+ ):
objects, output_dir = self._fix_object_args(objects, output_dir)
- fixed_args = self._fix_lib_args(libraries, library_dirs,
- runtime_library_dirs)
+ fixed_args = self._fix_lib_args(libraries, library_dirs, runtime_library_dirs)
libraries, library_dirs, runtime_library_dirs = fixed_args
- lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs,
- libraries)
+ lib_opts = gen_lib_options(self, library_dirs, runtime_library_dirs, libraries)
if not isinstance(output_dir, (str, type(None))):
raise TypeError("'output_dir' must be a string or None")
if output_dir is not None:
output_filename = os.path.join(output_dir, output_filename)
if self._need_link(objects, output_filename):
- ld_args = (objects + self.objects +
- lib_opts + ['-o', output_filename])
+ ld_args = objects + self.objects + lib_opts + ['-o', output_filename]
if debug:
ld_args[:0] = ['-g']
if extra_preargs:
@@ -173,33 +247,22 @@ class UnixCCompiler(CCompiler):
ld_args.extend(extra_postargs)
self.mkpath(os.path.dirname(output_filename))
try:
- if target_desc == CCompiler.EXECUTABLE:
- linker = self.linker_exe[:]
- else:
- linker = self.linker_so[:]
+ # Select a linker based on context: linker_exe when
+ # building an executable or linker_so (with shared options)
+ # when building a shared library.
+ building_exe = target_desc == CCompiler.EXECUTABLE
+ linker = (self.linker_exe if building_exe else self.linker_so)[:]
+
if target_lang == "c++" and self.compiler_cxx:
- # skip over environment variable settings if /usr/bin/env
- # is used to set up the linker's environment.
- # This is needed on OSX. Note: this assumes that the
- # normal and C++ compiler have the same environment
- # settings.
- i = 0
- if os.path.basename(linker[0]) == "env":
- i = 1
- while '=' in linker[i]:
- i += 1
-
- if os.path.basename(linker[i]) == 'ld_so_aix':
- # AIX platforms prefix the compiler with the ld_so_aix
- # script, so we need to adjust our linker index
- offset = 1
- else:
- offset = 0
-
- linker[i+offset] = self.compiler_cxx[i]
-
- if sys.platform == 'darwin':
- linker = _osx_support.compiler_fixup(linker, ld_args)
+ env, linker_ne = _split_env(linker)
+ aix, linker_na = _split_aix(linker_ne)
+ _, compiler_cxx_ne = _split_env(self.compiler_cxx)
+ _, linker_exe_ne = _split_env(self.linker_exe)
+
+ params = _linker_params(linker_na, linker_exe_ne)
+ linker = env + aix + compiler_cxx_ne + params
+
+ linker = compiler_fixup(linker, ld_args)
self.spawn(linker + ld_args)
except DistutilsExecError as msg:
@@ -214,8 +277,10 @@ class UnixCCompiler(CCompiler):
def library_dir_option(self, dir):
return "-L" + dir
- def _is_gcc(self, compiler_name):
- return "gcc" in compiler_name or "g++" in compiler_name
+ def _is_gcc(self):
+ cc_var = sysconfig.get_config_var("CC")
+ compiler = os.path.basename(shlex.split(cc_var)[0])
+ return "gcc" in compiler or "g++" in compiler
def runtime_library_dir_option(self, dir):
# XXX Hackish, at the very least. See Python bug #445902:
@@ -231,20 +296,21 @@ class UnixCCompiler(CCompiler):
# this time, there's no way to determine this information from
# the configuration data stored in the Python installation, so
# we use this hack.
- compiler = os.path.basename(shlex.split(sysconfig.get_config_var("CC"))[0])
if sys.platform[:6] == "darwin":
from distutils.util import get_macosx_target_ver, split_version
+
macosx_target_ver = get_macosx_target_ver()
if macosx_target_ver and split_version(macosx_target_ver) >= [10, 5]:
return "-Wl,-rpath," + dir
- else: # no support for -rpath on earlier macOS versions
+ else: # no support for -rpath on earlier macOS versions
return "-L" + dir
elif sys.platform[:7] == "freebsd":
return "-Wl,-rpath=" + dir
elif sys.platform[:5] == "hp-ux":
- if self._is_gcc(compiler):
- return ["-Wl,+s", "-L" + dir]
- return ["+s", "-L" + dir]
+ return [
+ "-Wl,+s" if self._is_gcc() else "+s",
+ "-L" + dir,
+ ]
# For all compilers, `-Wl` is the presumed way to
# pass a compiler option to the linker and `-R` is
@@ -259,67 +325,77 @@ class UnixCCompiler(CCompiler):
def library_option(self, lib):
return "-l" + lib
+ @staticmethod
+ def _library_root(dir):
+ """
+ macOS users can specify an alternate SDK using'-isysroot'.
+ Calculate the SDK root if it is specified.
+
+ Note that, as of Xcode 7, Apple SDKs may contain textual stub
+ libraries with .tbd extensions rather than the normal .dylib
+ shared libraries installed in /. The Apple compiler tool
+ chain handles this transparently but it can cause problems
+ for programs that are being built with an SDK and searching
+ for specific libraries. Callers of find_library_file need to
+ keep in mind that the base filename of the returned SDK library
+ file might have a different extension from that of the library
+ file installed on the running system, for example:
+ /Applications/Xcode.app/Contents/Developer/Platforms/
+ MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/
+ usr/lib/libedit.tbd
+ vs
+ /usr/lib/libedit.dylib
+ """
+ cflags = sysconfig.get_config_var('CFLAGS')
+ match = re.search(r'-isysroot\s*(\S+)', cflags)
+
+ apply_root = (
+ sys.platform == 'darwin'
+ and match
+ and (
+ dir.startswith('/System/')
+ or (dir.startswith('/usr/') and not dir.startswith('/usr/local/'))
+ )
+ )
+
+ return os.path.join(match.group(1), dir[1:]) if apply_root else dir
+
def find_library_file(self, dirs, lib, debug=0):
- shared_f = self.library_filename(lib, lib_type='shared')
- dylib_f = self.library_filename(lib, lib_type='dylib')
- xcode_stub_f = self.library_filename(lib, lib_type='xcode_stub')
- static_f = self.library_filename(lib, lib_type='static')
-
- if sys.platform == 'darwin':
- # On OSX users can specify an alternate SDK using
- # '-isysroot', calculate the SDK root if it is specified
- # (and use it further on)
- #
- # Note that, as of Xcode 7, Apple SDKs may contain textual stub
- # libraries with .tbd extensions rather than the normal .dylib
- # shared libraries installed in /. The Apple compiler tool
- # chain handles this transparently but it can cause problems
- # for programs that are being built with an SDK and searching
- # for specific libraries. Callers of find_library_file need to
- # keep in mind that the base filename of the returned SDK library
- # file might have a different extension from that of the library
- # file installed on the running system, for example:
- # /Applications/Xcode.app/Contents/Developer/Platforms/
- # MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/
- # usr/lib/libedit.tbd
- # vs
- # /usr/lib/libedit.dylib
- cflags = sysconfig.get_config_var('CFLAGS')
- m = re.search(r'-isysroot\s*(\S+)', cflags)
- if m is None:
- sysroot = '/'
- else:
- sysroot = m.group(1)
-
-
-
- for dir in dirs:
- shared = os.path.join(dir, shared_f)
- dylib = os.path.join(dir, dylib_f)
- static = os.path.join(dir, static_f)
- xcode_stub = os.path.join(dir, xcode_stub_f)
-
- if sys.platform == 'darwin' and (
- dir.startswith('/System/') or (
- dir.startswith('/usr/') and not dir.startswith('/usr/local/'))):
-
- shared = os.path.join(sysroot, dir[1:], shared_f)
- dylib = os.path.join(sysroot, dir[1:], dylib_f)
- static = os.path.join(sysroot, dir[1:], static_f)
- xcode_stub = os.path.join(sysroot, dir[1:], xcode_stub_f)
-
- # We're second-guessing the linker here, with not much hard
- # data to go on: GCC seems to prefer the shared library, so I'm
- # assuming that *all* Unix C compilers do. And of course I'm
- # ignoring even GCC's "-static" option. So sue me.
- if os.path.exists(dylib):
- return dylib
- elif os.path.exists(xcode_stub):
- return xcode_stub
- elif os.path.exists(shared):
- return shared
- elif os.path.exists(static):
- return static
-
- # Oops, didn't find it in *any* of 'dirs'
- return None
+ r"""
+ Second-guess the linker with not much hard
+ data to go on: GCC seems to prefer the shared library, so
+ assume that *all* Unix C compilers do,
+ ignoring even GCC's "-static" option.
+
+ >>> compiler = UnixCCompiler()
+ >>> compiler._library_root = lambda dir: dir
+ >>> monkeypatch = getfixture('monkeypatch')
+ >>> monkeypatch.setattr(os.path, 'exists', lambda d: 'existing' in d)
+ >>> dirs = ('/foo/bar/missing', '/foo/bar/existing')
+ >>> compiler.find_library_file(dirs, 'abc').replace('\\', '/')
+ '/foo/bar/existing/libabc.dylib'
+ >>> compiler.find_library_file(reversed(dirs), 'abc').replace('\\', '/')
+ '/foo/bar/existing/libabc.dylib'
+ >>> monkeypatch.setattr(os.path, 'exists',
+ ... lambda d: 'existing' in d and '.a' in d)
+ >>> compiler.find_library_file(dirs, 'abc').replace('\\', '/')
+ '/foo/bar/existing/libabc.a'
+ >>> compiler.find_library_file(reversed(dirs), 'abc').replace('\\', '/')
+ '/foo/bar/existing/libabc.a'
+ """
+ lib_names = (
+ self.library_filename(lib, lib_type=type)
+ for type in 'dylib xcode_stub shared static'.split()
+ )
+
+ roots = map(self._library_root, dirs)
+
+ searched = (
+ os.path.join(root, lib_name)
+ for root, lib_name in itertools.product(roots, lib_names)
+ )
+
+ found = filter(os.path.exists, searched)
+
+ # Return None if it could not be found in any dir.
+ return next(found, None)
diff --git a/setuptools/_distutils/util.py b/setuptools/_distutils/util.py
index 6d506d7e..8668b436 100644
--- a/setuptools/_distutils/util.py
+++ b/setuptools/_distutils/util.py
@@ -4,30 +4,31 @@ Miscellaneous utility functions -- anything that doesn't fit into
one of the other *util.py modules.
"""
+import importlib.util
import os
import re
-import importlib.util
import string
+import subprocess
import sys
import sysconfig
-from distutils.errors import DistutilsPlatformError
-from distutils.dep_util import newer
-from distutils.spawn import spawn
-from distutils import log
-from distutils.errors import DistutilsByteCompileError
-from .py35compat import _optim_args_from_interpreter_flags
+import functools
+
+from .errors import DistutilsPlatformError, DistutilsByteCompileError
+from .dep_util import newer
+from .spawn import spawn
+from ._log import log
def get_host_platform():
- """Return a string that identifies the current platform. This is used mainly to
- distinguish platform-specific build directories and platform-specific built
- distributions.
+ """
+ Return a string that identifies the current platform. Use this
+ function to distinguish platform-specific build directories and
+ platform-specific built distributions.
"""
- # We initially exposed platforms as defined in Python 3.9
+ # This function initially exposed platforms as defined in Python 3.9
# even with older Python versions when distutils was split out.
- # Now that we delegate to stdlib sysconfig we need to restore this
- # in case anyone has started to depend on it.
+ # Now it delegates to stdlib sysconfig, but maintains compatibility.
if sys.version_info < (3, 8):
if os.name == 'nt':
@@ -41,43 +42,49 @@ def get_host_platform():
osname, host, release, version, machine = os.uname()
if osname[:3] == "aix":
from .py38compat import aix_platform
+
return aix_platform(osname, version, release)
return sysconfig.get_platform()
+
def get_platform():
if os.name == 'nt':
TARGET_TO_PLAT = {
- 'x86' : 'win32',
- 'x64' : 'win-amd64',
- 'arm' : 'win-arm32',
+ 'x86': 'win32',
+ 'x64': 'win-amd64',
+ 'arm': 'win-arm32',
'arm64': 'win-arm64',
}
- return TARGET_TO_PLAT.get(os.environ.get('VSCMD_ARG_TGT_ARCH')) or get_host_platform()
- else:
- return get_host_platform()
+ target = os.environ.get('VSCMD_ARG_TGT_ARCH')
+ return TARGET_TO_PLAT.get(target) or get_host_platform()
+ return get_host_platform()
if sys.platform == 'darwin':
- _syscfg_macosx_ver = None # cache the version pulled from sysconfig
+ _syscfg_macosx_ver = None # cache the version pulled from sysconfig
MACOSX_VERSION_VAR = 'MACOSX_DEPLOYMENT_TARGET'
+
def _clear_cached_macosx_ver():
"""For testing only. Do not call."""
global _syscfg_macosx_ver
_syscfg_macosx_ver = None
+
def get_macosx_target_ver_from_syscfg():
"""Get the version of macOS latched in the Python interpreter configuration.
Returns the version as a string or None if can't obtain one. Cached."""
global _syscfg_macosx_ver
if _syscfg_macosx_ver is None:
from distutils import sysconfig
+
ver = sysconfig.get_config_var(MACOSX_VERSION_VAR) or ''
if ver:
_syscfg_macosx_ver = ver
return _syscfg_macosx_ver
+
def get_macosx_target_ver():
"""Return the version of macOS for which we are building.
@@ -95,12 +102,16 @@ def get_macosx_target_ver():
# ensures extension modules are built with correct compatibility
# values, specifically LDSHARED which can use
# '-undefined dynamic_lookup' which only works on >= 10.3.
- if syscfg_ver and split_version(syscfg_ver) >= [10, 3] and \
- split_version(env_ver) < [10, 3]:
- my_msg = ('$' + MACOSX_VERSION_VAR + ' mismatch: '
- 'now "%s" but "%s" during configure; '
- 'must use 10.3 or later'
- % (env_ver, syscfg_ver))
+ if (
+ syscfg_ver
+ and split_version(syscfg_ver) >= [10, 3]
+ and split_version(env_ver) < [10, 3]
+ ):
+ my_msg = (
+ '$' + MACOSX_VERSION_VAR + ' mismatch: '
+ 'now "%s" but "%s" during configure; '
+ 'must use 10.3 or later' % (env_ver, syscfg_ver)
+ )
raise DistutilsPlatformError(my_msg)
return env_ver
return syscfg_ver
@@ -111,7 +122,7 @@ def split_version(s):
return [int(n) for n in s.split('.')]
-def convert_path (pathname):
+def convert_path(pathname):
"""Return 'pathname' as a name that will work on the native filesystem,
i.e. split it on '/' and put it back together again using the current
directory separator. Needed because filenames in the setup script are
@@ -136,10 +147,11 @@ def convert_path (pathname):
return os.curdir
return os.path.join(*paths)
+
# convert_path ()
-def change_root (new_root, pathname):
+def change_root(new_root, pathname):
"""Return 'pathname' with 'new_root' prepended. If 'pathname' is
relative, this is equivalent to "os.path.join(new_root,pathname)".
Otherwise, it requires making 'pathname' relative and then joining the
@@ -157,12 +169,11 @@ def change_root (new_root, pathname):
path = path[1:]
return os.path.join(new_root, path)
- else:
- raise DistutilsPlatformError("nothing known about platform '%s'" % os.name)
+ raise DistutilsPlatformError(f"nothing known about platform '{os.name}'")
-_environ_checked = 0
-def check_environ ():
+@functools.lru_cache()
+def check_environ():
"""Ensure that 'os.environ' has all the environment variables we
guarantee that users can use in config files, command-line options,
etc. Currently this includes:
@@ -170,13 +181,10 @@ def check_environ ():
PLAT - description of the current platform, including hardware
and OS (see 'get_platform()')
"""
- global _environ_checked
- if _environ_checked:
- return
-
if os.name == 'posix' and 'HOME' not in os.environ:
try:
import pwd
+
os.environ['HOME'] = pwd.getpwuid(os.getuid())[5]
except (ImportError, KeyError):
# bpo-10496: if the current user identifier doesn't exist in the
@@ -186,10 +194,8 @@ def check_environ ():
if 'PLAT' not in os.environ:
os.environ['PLAT'] = get_platform()
- _environ_checked = 1
-
-def subst_vars (s, local_vars):
+def subst_vars(s, local_vars):
"""
Perform variable substitution on 'string'.
Variables are indicated by format-style braces ("{var}").
@@ -207,19 +213,20 @@ def subst_vars (s, local_vars):
except KeyError as var:
raise ValueError(f"invalid variable {var}")
-# subst_vars ()
-
def _subst_compat(s):
"""
Replace shell/Perl-style variable substitution with
format-style. For compatibility.
"""
+
def _subst(match):
return f'{{{match.group(1)}}}'
+
repl = re.sub(r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, s)
if repl != s:
import warnings
+
warnings.warn(
"shell/Perl-style substitions are deprecated",
DeprecationWarning,
@@ -227,7 +234,7 @@ def _subst_compat(s):
return repl
-def grok_environment_error (exc, prefix="error: "):
+def grok_environment_error(exc, prefix="error: "):
# Function kept for backward compatibility.
# Used to try clever things with EnvironmentErrors,
# but nowadays str(exception) produces good messages.
@@ -236,13 +243,16 @@ def grok_environment_error (exc, prefix="error: "):
# Needed by 'split_quoted()'
_wordchars_re = _squote_re = _dquote_re = None
+
+
def _init_regex():
global _wordchars_re, _squote_re, _dquote_re
_wordchars_re = re.compile(r'[^\\\'\"%s ]*' % string.whitespace)
_squote_re = re.compile(r"'(?:[^'\\]|\\.)*'")
_dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"')
-def split_quoted (s):
+
+def split_quoted(s):
"""Split a string up according to Unix shell-like rules for quotes and
backslashes. In short: words are delimited by spaces, as long as those
spaces are not escaped by a backslash, or inside a quoted string.
@@ -256,7 +266,8 @@ def split_quoted (s):
# This is a nice algorithm for splitting up a single string, since it
# doesn't require character-by-character examination. It was a little
# bit of a brain-bender to get it working right, though...
- if _wordchars_re is None: _init_regex()
+ if _wordchars_re is None:
+ _init_regex()
s = s.strip()
words = []
@@ -269,20 +280,23 @@ def split_quoted (s):
words.append(s[:end])
break
- if s[end] in string.whitespace: # unescaped, unquoted whitespace: now
- words.append(s[:end]) # we definitely have a word delimiter
+ if s[end] in string.whitespace:
+ # unescaped, unquoted whitespace: now
+ # we definitely have a word delimiter
+ words.append(s[:end])
s = s[end:].lstrip()
pos = 0
- elif s[end] == '\\': # preserve whatever is being escaped;
- # will become part of the current word
- s = s[:end] + s[end+1:]
- pos = end+1
+ elif s[end] == '\\':
+ # preserve whatever is being escaped;
+ # will become part of the current word
+ s = s[:end] + s[end + 1 :]
+ pos = end + 1
else:
- if s[end] == "'": # slurp singly-quoted string
+ if s[end] == "'": # slurp singly-quoted string
m = _squote_re.match(s, end)
- elif s[end] == '"': # slurp doubly-quoted string
+ elif s[end] == '"': # slurp doubly-quoted string
m = _dquote_re.match(s, end)
else:
raise RuntimeError("this can't happen (bad char '%c')" % s[end])
@@ -291,7 +305,7 @@ def split_quoted (s):
raise ValueError("bad string (mismatched %s quotes?)" % s[end])
(beg, end) = m.span()
- s = s[:beg] + s[beg+1:end-1] + s[end:]
+ s = s[:beg] + s[beg + 1 : end - 1] + s[end:]
pos = m.end() - 2
if pos >= len(s):
@@ -300,10 +314,11 @@ def split_quoted (s):
return words
+
# split_quoted ()
-def execute (func, args, msg=None, verbose=0, dry_run=0):
+def execute(func, args, msg=None, verbose=0, dry_run=0):
"""Perform some action that affects the outside world (eg. by
writing to the filesystem). Such actions are special because they
are disabled by the 'dry_run' flag. This method takes care of all
@@ -313,8 +328,8 @@ def execute (func, args, msg=None, verbose=0, dry_run=0):
print.
"""
if msg is None:
- msg = "%s%r" % (func.__name__, args)
- if msg[-2:] == ',)': # correct for singleton tuple
+ msg = "{}{!r}".format(func.__name__, args)
+ if msg[-2:] == ',)': # correct for singleton tuple
msg = msg[0:-2] + ')'
log.info(msg)
@@ -322,7 +337,7 @@ def execute (func, args, msg=None, verbose=0, dry_run=0):
func(*args)
-def strtobool (val):
+def strtobool(val):
"""Convert a string representation of truth to true (1) or false (0).
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
@@ -335,14 +350,19 @@ def strtobool (val):
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
return 0
else:
- raise ValueError("invalid truth value %r" % (val,))
-
-
-def byte_compile (py_files,
- optimize=0, force=0,
- prefix=None, base_dir=None,
- verbose=1, dry_run=0,
- direct=None):
+ raise ValueError("invalid truth value {!r}".format(val))
+
+
+def byte_compile( # noqa: C901
+ py_files,
+ optimize=0,
+ force=0,
+ prefix=None,
+ base_dir=None,
+ verbose=1,
+ dry_run=0,
+ direct=None,
+):
"""Byte-compile a collection of Python source files to .pyc
files in a __pycache__ subdirectory. 'py_files' is a list
of files to compile; any files that don't end in ".py" are silently
@@ -372,10 +392,6 @@ def byte_compile (py_files,
it set to None.
"""
- # Late import to fix a bootstrap issue: _posixsubprocess is built by
- # setup.py, but setup.py uses distutils.
- import subprocess
-
# nothing is done if sys.dont_write_bytecode is True
if sys.dont_write_bytecode:
raise DistutilsByteCompileError('byte-compiling is disabled.')
@@ -391,16 +407,18 @@ def byte_compile (py_files,
# optimize mode, or if either optimization level was requested by
# the caller.
if direct is None:
- direct = (__debug__ and optimize == 0)
+ direct = __debug__ and optimize == 0
# "Indirect" byte-compilation: write a temporary script and then
# run it with the appropriate flags.
if not direct:
try:
from tempfile import mkstemp
+
(script_fd, script_name) = mkstemp(".py")
except ImportError:
from tempfile import mktemp
+
(script_fd, script_name) = None, mktemp(".py")
log.info("writing byte-compilation script '%s'", script_name)
if not dry_run:
@@ -410,10 +428,12 @@ def byte_compile (py_files,
script = open(script_name, "w")
with script:
- script.write("""\
+ script.write(
+ """\
from distutils.util import byte_compile
files = [
-""")
+"""
+ )
# XXX would be nice to write absolute filenames, just for
# safety's sake (script should be more robust in the face of
@@ -425,24 +445,22 @@ files = [
# problem is that it's really a directory, but I'm treating it
# as a dumb string, so trailing slashes and so forth matter.
- #py_files = map(os.path.abspath, py_files)
- #if prefix:
- # prefix = os.path.abspath(prefix)
-
script.write(",\n".join(map(repr, py_files)) + "]\n")
- script.write("""
+ script.write(
+ """
byte_compile(files, optimize=%r, force=%r,
prefix=%r, base_dir=%r,
verbose=%r, dry_run=0,
direct=1)
-""" % (optimize, force, prefix, base_dir, verbose))
+"""
+ % (optimize, force, prefix, base_dir, verbose)
+ )
cmd = [sys.executable]
- cmd.extend(_optim_args_from_interpreter_flags())
+ cmd.extend(subprocess._optim_args_from_interpreter_flags())
cmd.append(script_name)
spawn(cmd, dry_run=dry_run)
- execute(os.remove, (script_name,), "removing %s" % script_name,
- dry_run=dry_run)
+ execute(os.remove, (script_name,), "removing %s" % script_name, dry_run=dry_run)
# "Direct" byte-compilation: use the py_compile module to compile
# right here, right now. Note that the script generated in indirect
@@ -462,16 +480,17 @@ byte_compile(files, optimize=%r, force=%r,
# dfile - purported source filename (same as 'file' by default)
if optimize >= 0:
opt = '' if optimize == 0 else optimize
- cfile = importlib.util.cache_from_source(
- file, optimization=opt)
+ cfile = importlib.util.cache_from_source(file, optimization=opt)
else:
cfile = importlib.util.cache_from_source(file)
dfile = file
if prefix:
- if file[:len(prefix)] != prefix:
- raise ValueError("invalid prefix: filename %r doesn't start with %r"
- % (file, prefix))
- dfile = dfile[len(prefix):]
+ if file[: len(prefix)] != prefix:
+ raise ValueError(
+ "invalid prefix: filename %r doesn't start with %r"
+ % (file, prefix)
+ )
+ dfile = dfile[len(prefix) :]
if base_dir:
dfile = os.path.join(base_dir, dfile)
@@ -482,12 +501,10 @@ byte_compile(files, optimize=%r, force=%r,
if not dry_run:
compile(file, cfile, dfile)
else:
- log.debug("skipping byte-compilation of %s to %s",
- file, cfile_base)
+ log.debug("skipping byte-compilation of %s to %s", file, cfile_base)
-# byte_compile ()
-def rfc822_escape (header):
+def rfc822_escape(header):
"""Return a version of the string escaped for inclusion in an
RFC-822 header, by ensuring there are 8 spaces space after each newline.
"""
diff --git a/setuptools/_distutils/version.py b/setuptools/_distutils/version.py
index 35e181db..e29e2657 100644
--- a/setuptools/_distutils/version.py
+++ b/setuptools/_distutils/version.py
@@ -49,18 +49,18 @@ class Version:
rich comparisons to _cmp.
"""
- def __init__ (self, vstring=None):
+ def __init__(self, vstring=None):
+ if vstring:
+ self.parse(vstring)
warnings.warn(
"distutils Version classes are deprecated. "
"Use packaging.version instead.",
DeprecationWarning,
stacklevel=2,
)
- if vstring:
- self.parse(vstring)
- def __repr__ (self):
- return "%s ('%s')" % (self.__class__.__name__, str(self))
+ def __repr__(self):
+ return "{} ('{}')".format(self.__class__.__name__, str(self))
def __eq__(self, other):
c = self._cmp(other)
@@ -110,7 +110,7 @@ class Version:
# instance of your version class)
-class StrictVersion (Version):
+class StrictVersion(Version):
"""Version numbering for anal retentives and software idealists.
Implements the standard interface for version number classes as
@@ -147,17 +147,16 @@ class StrictVersion (Version):
in the distutils documentation.
"""
- version_re = re.compile(r'^(\d+) \. (\d+) (\. (\d+))? ([ab](\d+))?$',
- re.VERBOSE | re.ASCII)
-
+ version_re = re.compile(
+ r'^(\d+) \. (\d+) (\. (\d+))? ([ab](\d+))?$', re.VERBOSE | re.ASCII
+ )
- def parse (self, vstring):
+ def parse(self, vstring):
match = self.version_re.match(vstring)
if not match:
raise ValueError("invalid version number '%s'" % vstring)
- (major, minor, patch, prerelease, prerelease_num) = \
- match.group(1, 2, 4, 5, 6)
+ (major, minor, patch, prerelease, prerelease_num) = match.group(1, 2, 4, 5, 6)
if patch:
self.version = tuple(map(int, [major, minor, patch]))
@@ -169,8 +168,7 @@ class StrictVersion (Version):
else:
self.prerelease = None
-
- def __str__ (self):
+ def __str__(self):
if self.version[2] == 0:
vstring = '.'.join(map(str, self.version[0:2]))
@@ -182,8 +180,7 @@ class StrictVersion (Version):
return vstring
-
- def _cmp (self, other):
+ def _cmp(self, other): # noqa: C901
if isinstance(other, str):
with suppress_known_deprecation():
other = StrictVersion(other)
@@ -204,13 +201,13 @@ class StrictVersion (Version):
# case 3: self doesn't have prerelease, other does: self is greater
# case 4: both have prerelease: must compare them!
- if (not self.prerelease and not other.prerelease):
+ if not self.prerelease and not other.prerelease:
return 0
- elif (self.prerelease and not other.prerelease):
+ elif self.prerelease and not other.prerelease:
return -1
- elif (not self.prerelease and other.prerelease):
+ elif not self.prerelease and other.prerelease:
return 1
- elif (self.prerelease and other.prerelease):
+ elif self.prerelease and other.prerelease:
if self.prerelease == other.prerelease:
return 0
elif self.prerelease < other.prerelease:
@@ -220,6 +217,7 @@ class StrictVersion (Version):
else:
assert False, "never get here"
+
# end class StrictVersion
@@ -287,7 +285,8 @@ class StrictVersion (Version):
# the Right Thing" (ie. the code matches the conception). But I'd rather
# have a conception that matches common notions about version numbers.
-class LooseVersion (Version):
+
+class LooseVersion(Version):
"""Version numbering for anarchists and software realists.
Implements the standard interface for version number classes as
@@ -322,13 +321,12 @@ class LooseVersion (Version):
component_re = re.compile(r'(\d+ | [a-z]+ | \.)', re.VERBOSE)
- def parse (self, vstring):
+ def parse(self, vstring):
# I've given up on thinking I can reconstruct the version string
# from the parsed tuple -- so I just store the string here for
# use by __str__
self.vstring = vstring
- components = [x for x in self.component_re.split(vstring)
- if x and x != '.']
+ components = [x for x in self.component_re.split(vstring) if x and x != '.']
for i, obj in enumerate(components):
try:
components[i] = int(obj)
@@ -337,16 +335,13 @@ class LooseVersion (Version):
self.version = components
-
- def __str__ (self):
+ def __str__(self):
return self.vstring
-
- def __repr__ (self):
+ def __repr__(self):
return "LooseVersion ('%s')" % str(self)
-
- def _cmp (self, other):
+ def _cmp(self, other):
if isinstance(other, str):
other = LooseVersion(other)
elif not isinstance(other, LooseVersion):
diff --git a/setuptools/_distutils/versionpredicate.py b/setuptools/_distutils/versionpredicate.py
index 55f25d91..d6c0c007 100644
--- a/setuptools/_distutils/versionpredicate.py
+++ b/setuptools/_distutils/versionpredicate.py
@@ -1,15 +1,14 @@
"""Module for parsing and testing package version predicate strings.
"""
import re
-import distutils.version
+from . import version
import operator
-re_validPackage = re.compile(r"(?i)^\s*([a-z_]\w*(?:\.[a-z_]\w*)*)(.*)",
- re.ASCII)
+re_validPackage = re.compile(r"(?i)^\s*([a-z_]\w*(?:\.[a-z_]\w*)*)(.*)", re.ASCII)
# (package) (rest)
-re_paren = re.compile(r"^\s*\((.*)\)\s*$") # (list) inside of parentheses
+re_paren = re.compile(r"^\s*\((.*)\)\s*$") # (list) inside of parentheses
re_splitComparison = re.compile(r"^\s*(<=|>=|<|>|!=|==)\s*([^\s,]+)\s*$")
# (comp) (version)
@@ -23,12 +22,20 @@ def splitUp(pred):
if not res:
raise ValueError("bad package restriction syntax: %r" % pred)
comp, verStr = res.groups()
- with distutils.version.suppress_known_deprecation():
- other = distutils.version.StrictVersion(verStr)
+ with version.suppress_known_deprecation():
+ other = version.StrictVersion(verStr)
return (comp, other)
-compmap = {"<": operator.lt, "<=": operator.le, "==": operator.eq,
- ">": operator.gt, ">=": operator.ge, "!=": operator.ne}
+
+compmap = {
+ "<": operator.lt,
+ "<=": operator.le,
+ "==": operator.eq,
+ ">": operator.gt,
+ ">=": operator.ge,
+ "!=": operator.ne,
+}
+
class VersionPredicate:
"""Parse and test package version predicates.
@@ -96,8 +103,7 @@ class VersionPredicate:
"""
def __init__(self, versionPredicateStr):
- """Parse a version predicate string.
- """
+ """Parse a version predicate string."""
# Fields:
# name: package name
# pred: list of (comparison string, StrictVersion)
@@ -117,8 +123,7 @@ class VersionPredicate:
str = match.groups()[0]
self.pred = [splitUp(aPred) for aPred in str.split(",")]
if not self.pred:
- raise ValueError("empty parenthesized list in %r"
- % versionPredicateStr)
+ raise ValueError("empty parenthesized list in %r" % versionPredicateStr)
else:
self.pred = []
@@ -142,6 +147,7 @@ class VersionPredicate:
_provision_rx = None
+
def split_provision(value):
"""Return the name and optional version number of a provision.
@@ -156,14 +162,14 @@ def split_provision(value):
global _provision_rx
if _provision_rx is None:
_provision_rx = re.compile(
- r"([a-zA-Z_]\w*(?:\.[a-zA-Z_]\w*)*)(?:\s*\(\s*([^)\s]+)\s*\))?$",
- re.ASCII)
+ r"([a-zA-Z_]\w*(?:\.[a-zA-Z_]\w*)*)(?:\s*\(\s*([^)\s]+)\s*\))?$", re.ASCII
+ )
value = value.strip()
m = _provision_rx.match(value)
if not m:
raise ValueError("illegal provides specification: %r" % value)
ver = m.group(2) or None
if ver:
- with distutils.version.suppress_known_deprecation():
- ver = distutils.version.StrictVersion(ver)
+ with version.suppress_known_deprecation():
+ ver = version.StrictVersion(ver)
return m.group(1), ver