[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-04-13 00:00:18 +00:00
parent 72ad6dc953
commit f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions

View file

@ -18,19 +18,24 @@ Microsoft Visual C++ 14.X:
This may also support compilers shipped with compatible Visual Studio versions.
"""
from __future__ import annotations
import json
from io import open
from os import listdir, pathsep
from os.path import join, isfile, isdir, dirname
import sys
import contextlib
import platform
import itertools
import json
import platform
import subprocess
import sys
from os import listdir
from os import pathsep
from os.path import dirname
from os.path import isdir
from os.path import isfile
from os.path import join
import distutils.errors
from setuptools.extern.packaging.version import LegacyVersion
from setuptools.extern.more_itertools import unique_everseen
from setuptools.extern.packaging.version import LegacyVersion
from .monkey import get_unpatched
@ -88,17 +93,17 @@ def msvc9_find_vcvarsall(version):
key = vc_base % ('', version)
try:
# Per-user installs register the compiler path here
productdir = Reg.get_value(key, "installdir")
productdir = Reg.get_value(key, 'installdir')
except KeyError:
try:
# All-user installs on a 64-bit system register here
key = vc_base % ('Wow6432Node\\', version)
productdir = Reg.get_value(key, "installdir")
productdir = Reg.get_value(key, 'installdir')
except KeyError:
productdir = None
if productdir:
vcvarsall = join(productdir, "vcvarsall.bat")
vcvarsall = join(productdir, 'vcvarsall.bat')
if isfile(vcvarsall):
return vcvarsall
@ -148,9 +153,9 @@ def _msvc14_find_vc2015():
try:
key = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
r"Software\Microsoft\VisualStudio\SxS\VC7",
r'Software\Microsoft\VisualStudio\SxS\VC7',
0,
winreg.KEY_READ | winreg.KEY_WOW64_32KEY
winreg.KEY_READ | winreg.KEY_WOW64_32KEY,
)
except OSError:
return None, None
@ -185,25 +190,25 @@ def _msvc14_find_vc2017():
If vswhere.exe is not available, by definition, VS 2017 is not
installed.
"""
root = environ.get("ProgramFiles(x86)") or environ.get("ProgramFiles")
root = environ.get('ProgramFiles(x86)') or environ.get('ProgramFiles')
if not root:
return None, None
try:
path = subprocess.check_output([
join(root, "Microsoft Visual Studio", "Installer", "vswhere.exe"),
"-latest",
"-prerelease",
"-requiresAny",
"-requires", "Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
"-requires", "Microsoft.VisualStudio.Workload.WDExpress",
"-property", "installationPath",
"-products", "*",
]).decode(encoding="mbcs", errors="strict").strip()
join(root, 'Microsoft Visual Studio', 'Installer', 'vswhere.exe'),
'-latest',
'-prerelease',
'-requiresAny',
'-requires', 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64',
'-requires', 'Microsoft.VisualStudio.Workload.WDExpress',
'-property', 'installationPath',
'-products', '*',
]).decode(encoding='mbcs', errors='strict').strip()
except (subprocess.CalledProcessError, OSError, UnicodeDecodeError):
return None, None
path = join(path, "VC", "Auxiliary", "Build")
path = join(path, 'VC', 'Auxiliary', 'Build')
if isdir(path):
return 15, path
@ -214,7 +219,7 @@ PLAT_SPEC_TO_RUNTIME = {
'x86': 'x86',
'x86_amd64': 'x64',
'x86_arm': 'arm',
'x86_arm64': 'arm64'
'x86_arm64': 'arm64',
}
@ -229,9 +234,11 @@ def _msvc14_find_vcvarsall(plat_spec):
vcruntime_plat = 'x64' if 'amd64' in plat_spec else 'x86'
if best_dir:
vcredist = join(best_dir, "..", "..", "redist", "MSVC", "**",
vcruntime_plat, "Microsoft.VC14*.CRT",
"vcruntime140.dll")
vcredist = join(
best_dir, '..', '..', 'redist', 'MSVC', '**',
vcruntime_plat, 'Microsoft.VC14*.CRT',
'vcruntime140.dll',
)
try:
import glob
vcruntime = glob.glob(vcredist, recursive=True)[-1]
@ -241,13 +248,15 @@ def _msvc14_find_vcvarsall(plat_spec):
if not best_dir:
best_version, best_dir = _msvc14_find_vc2015()
if best_version:
vcruntime = join(best_dir, 'redist', vcruntime_plat,
"Microsoft.VC140.CRT", "vcruntime140.dll")
vcruntime = join(
best_dir, 'redist', vcruntime_plat,
'Microsoft.VC140.CRT', 'vcruntime140.dll',
)
if not best_dir:
return None, None
vcvarsall = join(best_dir, "vcvarsall.bat")
vcvarsall = join(best_dir, 'vcvarsall.bat')
if not isfile(vcvarsall):
return None, None
@ -259,7 +268,7 @@ def _msvc14_find_vcvarsall(plat_spec):
def _msvc14_get_vc_env(plat_spec):
"""Python 3.8 "distutils/_msvccompiler.py" backport"""
if "DISTUTILS_USE_SDK" in environ:
if 'DISTUTILS_USE_SDK' in environ:
return {
key.lower(): value
for key, value in environ.items()
@ -268,17 +277,17 @@ def _msvc14_get_vc_env(plat_spec):
vcvarsall, vcruntime = _msvc14_find_vcvarsall(plat_spec)
if not vcvarsall:
raise distutils.errors.DistutilsPlatformError(
"Unable to find vcvarsall.bat"
'Unable to find vcvarsall.bat',
)
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:
raise distutils.errors.DistutilsPlatformError(
"Error executing {}".format(exc.cmd)
f'Error executing {exc.cmd}',
) from exc
env = {
@ -325,7 +334,7 @@ def msvc14_gen_lib_options(*args, **kwargs):
compatibility between "numpy.distutils" and "distutils._msvccompiler"
(for Numpy < 1.11.2)
"""
if "numpy.distutils" in sys.modules:
if 'numpy.distutils' in sys.modules:
import numpy as np
if LegacyVersion(np.__version__) < LegacyVersion('1.11.2'):
return np.distutils.ccompiler.gen_lib_options(*args, **kwargs)
@ -340,7 +349,7 @@ def _augment_exception(exc, version, arch=''):
# Error if MSVC++ directory not found or environment not set
message = exc.args[0]
if "vcvarsall" in message.lower() or "visual c" in message.lower():
if 'vcvarsall' in message.lower() or 'visual c' in message.lower():
# Special error message if MSVC++ not installed
tmpl = 'Microsoft Visual C++ {version:0.1f} or greater is required.'
message = tmpl.format(**locals())
@ -362,11 +371,13 @@ def _augment_exception(exc, version, arch=''):
message += msdownload % 8279
elif version >= 14.0:
# For VC++ 14.X Redirect user to latest Visual C++ Build Tools
message += (' Get it with "Microsoft C++ Build Tools": '
r'https://visualstudio.microsoft.com'
r'/visual-cpp-build-tools/')
message += (
' Get it with "Microsoft C++ Build Tools": '
r'https://visualstudio.microsoft.com'
r'/visual-cpp-build-tools/'
)
exc.args = (message, )
exc.args = (message,)
class PlatformInfo:
@ -493,10 +504,12 @@ class RegistryInfo:
platform_info: PlatformInfo
"PlatformInfo" instance.
"""
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,
)
def __init__(self, platform_info):
self.pi = platform_info
@ -652,17 +665,17 @@ class RegistryInfo:
bkey = None
try:
bkey = openkey(hkey, ms(key), 0, key_read)
except (OSError, IOError):
except OSError:
if not self.pi.current_is_x86():
try:
bkey = openkey(hkey, ms(key, True), 0, key_read)
except (OSError, IOError):
except OSError:
continue
else:
continue
try:
return winreg.QueryValueEx(bkey, name)[0]
except (OSError, IOError):
except OSError:
pass
finally:
if bkey:
@ -695,7 +708,8 @@ class SystemInfo:
# Except for VS15+, VC version is aligned with VS version
self.vs_ver = self.vc_ver = (
vc_ver or self._find_latest_available_vs_ver())
vc_ver or self._find_latest_available_vs_ver()
)
def _find_latest_available_vs_ver(self):
"""
@ -710,7 +724,8 @@ class SystemInfo:
if not (reg_vc_vers or self.known_vs_paths):
raise distutils.errors.DistutilsPlatformError(
'No Microsoft Visual C++ version found')
'No Microsoft Visual C++ version found',
)
vc_vers = set(reg_vc_vers)
vc_vers.update(self.known_vs_paths)
@ -731,7 +746,7 @@ class SystemInfo:
for hkey, key in itertools.product(self.ri.HKEYS, vckeys):
try:
bkey = winreg.OpenKey(hkey, ms(key), 0, winreg.KEY_READ)
except (OSError, IOError):
except OSError:
continue
with bkey:
subkeys, values, _ = winreg.QueryInfoKey(bkey)
@ -764,7 +779,7 @@ class SystemInfo:
try:
hashed_names = listdir(instances_dir)
except (OSError, IOError):
except OSError:
# Directory not exists with all Visual Studio versions
return vs_versions
@ -772,7 +787,7 @@ class SystemInfo:
try:
# Get VS installation path from "state.json" file
state_path = join(instances_dir, name, 'state.json')
with open(state_path, 'rt', encoding='utf-8') as state_file:
with open(state_path, encoding='utf-8') as state_file:
state = json.load(state_file)
vs_path = state['installationPath']
@ -780,10 +795,13 @@ class SystemInfo:
listdir(join(vs_path, r'VC\Tools\MSVC'))
# Store version and path
vs_versions[self._as_float_version(
state['installationVersion'])] = vs_path
vs_versions[
self._as_float_version(
state['installationVersion'],
)
] = vs_path
except (OSError, IOError, KeyError):
except (OSError, KeyError):
# Skip if "state.json" file is missing or bad format
continue
@ -817,8 +835,10 @@ class SystemInfo:
path
"""
# Default path
default = join(self.ProgramFilesx86,
'Microsoft Visual Studio %0.1f' % self.vs_ver)
default = join(
self.ProgramFilesx86,
'Microsoft Visual Studio %0.1f' % self.vs_ver,
)
# Try to get path from registry, if fail use default path
return self.ri.lookup(self.ri.vs, '%0.1f' % self.vs_ver) or default
@ -868,7 +888,7 @@ class SystemInfo:
vc_ver = listdir(guess_vc)[-1]
self.vc_ver = self._as_float_version(vc_ver)
return join(guess_vc, vc_ver)
except (OSError, IOError, IndexError):
except (OSError, IndexError):
return ''
def _guess_vc_legacy(self):
@ -880,8 +900,10 @@ class SystemInfo:
str
path
"""
default = join(self.ProgramFilesx86,
r'Microsoft Visual Studio %0.1f\VC' % self.vs_ver)
default = join(
self.ProgramFilesx86,
r'Microsoft Visual Studio %0.1f\VC' % self.vs_ver,
)
# Try to get "VC++ for Python" path from registry as default path
reg_path = join(self.ri.vc_for_python, '%0.1f' % self.vs_ver)
@ -1030,8 +1052,10 @@ class SystemInfo:
# Find path of the more recent Kit
for ver in vers:
sdkdir = self.ri.lookup(self.ri.windows_kits_roots,
'kitsroot%s' % ver)
sdkdir = self.ri.lookup(
self.ri.windows_kits_roots,
'kitsroot%s' % ver,
)
if sdkdir:
return sdkdir or ''
@ -1058,10 +1082,12 @@ class SystemInfo:
versions
"""
# Set FxSdk versions for specified VS version
return (('4.7.2', '4.7.1', '4.7',
'4.6.2', '4.6.1', '4.6',
'4.5.2', '4.5.1', '4.5')
if self.vs_ver >= 14.0 else ())
return ((
'4.7.2', '4.7.1', '4.7',
'4.6.2', '4.6.1', '4.6',
'4.5.2', '4.5.1', '4.5',
)
if self.vs_ver >= 14.0 else ())
@property
def NetFxSdkDir(self):
@ -1279,8 +1305,10 @@ class EnvironmentInfo:
list of str
paths
"""
return [join(self.si.VCInstallDir, 'Include'),
join(self.si.VCInstallDir, r'ATLMFC\Include')]
return [
join(self.si.VCInstallDir, 'Include'),
join(self.si.VCInstallDir, r'ATLMFC\Include'),
]
@property
def VCLibraries(self):
@ -1340,14 +1368,22 @@ class EnvironmentInfo:
tools += [join(si.VCInstallDir, path)]
elif self.vs_ver >= 15.0:
host_dir = (r'bin\HostX86%s' if self.pi.current_is_x86() else
r'bin\HostX64%s')
tools += [join(
si.VCInstallDir, host_dir % self.pi.target_dir(x64=True))]
host_dir = (
r'bin\HostX86%s' if self.pi.current_is_x86() else
r'bin\HostX64%s'
)
tools += [
join(
si.VCInstallDir, host_dir % self.pi.target_dir(x64=True),
),
]
if self.pi.current_cpu != self.pi.target_cpu:
tools += [join(
si.VCInstallDir, host_dir % self.pi.current_dir(x64=True))]
tools += [
join(
si.VCInstallDir, host_dir % self.pi.current_dir(x64=True),
),
]
else:
tools += [join(si.VCInstallDir, 'Bin')]
@ -1372,7 +1408,7 @@ class EnvironmentInfo:
arch_subdir = self.pi.target_dir(x64=True)
lib = join(self.si.WindowsSdkDir, 'lib')
libver = self._sdk_subdir
return [join(lib, '%sum%s' % (libver, arch_subdir))]
return [join(lib, '{}um{}'.format(libver, arch_subdir))]
@property
def OSIncludes(self):
@ -1394,9 +1430,11 @@ class EnvironmentInfo:
sdkver = self._sdk_subdir
else:
sdkver = ''
return [join(include, '%sshared' % sdkver),
join(include, '%sum' % sdkver),
join(include, '%swinrt' % sdkver)]
return [
join(include, '%sshared' % sdkver),
join(include, '%sum' % sdkver),
join(include, '%swinrt' % sdkver),
]
@property
def OSLibpath(self):
@ -1422,15 +1460,18 @@ class EnvironmentInfo:
ref,
join(self.si.WindowsSdkDir, 'UnionMetadata'),
join(
ref, 'Windows.Foundation.UniversalApiContract', '1.0.0.0'),
ref, 'Windows.Foundation.UniversalApiContract', '1.0.0.0',
),
join(ref, 'Windows.Foundation.FoundationContract', '1.0.0.0'),
join(
ref, 'Windows.Networking.Connectivity.WwanContract',
'1.0.0.0'),
'1.0.0.0',
),
join(
self.si.WindowsSdkDir, 'ExtensionSDKs', 'Microsoft.VCLibs',
'%0.1f' % self.vs_ver, 'References', 'CommonConfiguration',
'neutral'),
'neutral',
),
]
return libpath
@ -1476,7 +1517,7 @@ class EnvironmentInfo:
path = join(self.si.WindowsSdkDir, 'Bin')
arch_subdir = self.pi.current_dir(x64=True)
sdkver = self.si.WindowsSdkLastVersion
yield join(path, '%s%s' % (sdkver, arch_subdir))
yield join(path, '{}{}'.format(sdkver, arch_subdir))
if self.si.WindowsSDKExecutablePath:
yield self.si.WindowsSDKExecutablePath
@ -1531,11 +1572,15 @@ class EnvironmentInfo:
tools = []
if include32:
tools += [join(si.FrameworkDir32, ver)
for ver in si.FrameworkVersion32]
tools += [
join(si.FrameworkDir32, ver)
for ver in si.FrameworkVersion32
]
if include64:
tools += [join(si.FrameworkDir64, ver)
for ver in si.FrameworkVersion64]
tools += [
join(si.FrameworkDir64, ver)
for ver in si.FrameworkVersion64
]
return tools
@property
@ -1600,7 +1645,7 @@ class EnvironmentInfo:
base_path = self.si.VSInstallDir
arch_subdir = ''
path = r'MSBuild\%0.1f\bin%s' % (self.vs_ver, arch_subdir)
path = r'MSBuild\{:0.1f}\bin{}'.format(self.vs_ver, arch_subdir)
build = [join(base_path, path)]
if self.vs_ver >= 15.0:
@ -1640,7 +1685,7 @@ class EnvironmentInfo:
arch_subdir = self.pi.target_dir(x64=True)
lib = join(self.si.UniversalCRTSdkDir, 'lib')
ucrtver = self._ucrt_subdir
return [join(lib, '%sucrt%s' % (ucrtver, arch_subdir))]
return [join(lib, '{}ucrt{}'.format(ucrtver, arch_subdir))]
@property
def UCRTIncludes(self):
@ -1711,9 +1756,11 @@ class EnvironmentInfo:
prefixes += [join(tools_path, 'redist')] # VS14 legacy path
# CRT directory
crt_dirs = ('Microsoft.VC%d.CRT' % (self.vc_ver * 10),
# Sometime store in directory with VS version instead of VC
'Microsoft.VC%d.CRT' % (int(self.vs_ver) * 10))
crt_dirs = (
'Microsoft.VC%d.CRT' % (self.vc_ver * 10),
# Sometime store in directory with VS version instead of VC
'Microsoft.VC%d.CRT' % (int(self.vs_ver) * 10),
)
# vcruntime path
for prefix, crt_dir in itertools.product(prefixes, crt_dirs):
@ -1736,36 +1783,52 @@ class EnvironmentInfo:
environment
"""
env = dict(
include=self._build_paths('include',
[self.VCIncludes,
self.OSIncludes,
self.UCRTIncludes,
self.NetFxSDKIncludes],
exists),
lib=self._build_paths('lib',
[self.VCLibraries,
self.OSLibraries,
self.FxTools,
self.UCRTLibraries,
self.NetFxSDKLibraries],
exists),
libpath=self._build_paths('libpath',
[self.VCLibraries,
self.FxTools,
self.VCStoreRefs,
self.OSLibpath],
exists),
path=self._build_paths('path',
[self.VCTools,
self.VSTools,
self.VsTDb,
self.SdkTools,
self.SdkSetup,
self.FxTools,
self.MSBuild,
self.HTMLHelpWorkshop,
self.FSharp],
exists),
include=self._build_paths(
'include',
[
self.VCIncludes,
self.OSIncludes,
self.UCRTIncludes,
self.NetFxSDKIncludes,
],
exists,
),
lib=self._build_paths(
'lib',
[
self.VCLibraries,
self.OSLibraries,
self.FxTools,
self.UCRTLibraries,
self.NetFxSDKLibraries,
],
exists,
),
libpath=self._build_paths(
'libpath',
[
self.VCLibraries,
self.FxTools,
self.VCStoreRefs,
self.OSLibpath,
],
exists,
),
path=self._build_paths(
'path',
[
self.VCTools,
self.VSTools,
self.VsTDb,
self.SdkTools,
self.SdkSetup,
self.FxTools,
self.MSBuild,
self.HTMLHelpWorkshop,
self.FSharp,
],
exists,
),
)
if self.vs_ver >= 14 and isfile(self.VCRuntimeRedist):
env['py_vcruntime_redist'] = self.VCRuntimeRedist
@ -1799,7 +1862,7 @@ class EnvironmentInfo:
paths = itertools.chain(spec_paths, env_paths)
extant_paths = list(filter(isdir, paths)) if exists else paths
if not extant_paths:
msg = "%s environment variable is empty" % name.upper()
msg = '%s environment variable is empty' % name.upper()
raise distutils.errors.DistutilsPlatformError(msg)
unique_paths = unique_everseen(extant_paths)
return pathsep.join(unique_paths)