Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Minor optimizations in Python code #13478

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/ciimage/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, image_dir: Path) -> None:
data = json.loads(path.read_text(encoding='utf-8'))

assert isinstance(data, dict)
assert all([x in data for x in ['base_image', 'env']])
assert all(x in data for x in ['base_image', 'env'])
assert isinstance(data['base_image'], str)
assert isinstance(data['env'], dict)

Expand Down
2 changes: 1 addition & 1 deletion docs/jsonvalidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

def assert_has_typed_keys(path: str, data: dict, keys: T.Dict[str, T.Any]) -> dict:
assert set(data.keys()).issuperset(keys.keys()), f'{path}: DIFF: {set(data.keys()).difference(keys.keys())}'
res = dict()
res = {}
for key, val in keys.items():
cur = data.pop(key)
assert isinstance(cur, val), f'{path}: type({key}: {cur}) != {val}'
Expand Down
4 changes: 2 additions & 2 deletions docs/refman/generatormd.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(self, manual: ReferenceManual, sitemap_out: Path, sitemap_in: Path,
def _gen_filename(self, file_id: str, *, extension: str = 'md') -> str:
parts = file_id.split('.')
assert parts[0] == 'root'
assert all([x for x in parts])
assert all(x for x in parts)
parts[0] = _ROOT_BASENAME
parts = [re.sub(r'[0-9]+_', '', x) for x in parts]
return f'{"_".join(parts)}.{extension}'
Expand Down Expand Up @@ -215,7 +215,7 @@ def prepare(arg: ArgBase, link: bool = True) -> T.Tuple[str, str, str, str]:
for kwarg in self.sorted_and_filtered(list(func.kwargs.values())):
type_str, type_space, name_str, name_space = prepare(kwarg)
required = ' <i>[required]</i> ' if kwarg.required else ' '
required = required if any([x.required for x in func.kwargs.values()]) else ''
required = required if any(x.required for x in func.kwargs.values()) else ''
signature += f' {name_str}{name_space} : {type_str}{type_space} {required} # {self.brief(kwarg)}\n'

return signature + ')'
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/arglist.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def extend_preserving_lflags(self, iterable: T.Iterable[str]) -> None:
normal_flags = []
lflags = []
for i in iterable:
if i not in self.always_dedup_args and (i.startswith('-l') or i.startswith('-L')):
if i not in self.always_dedup_args and i.startswith(('-l', '-L')):
lflags.append(i)
else:
normal_flags.append(i)
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/ast/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def resolve_key(node: mparser.BaseNode) -> str:
arguments, kwargs = self.reduce_arguments(node.args, key_resolver=resolve_key)
assert not arguments
self.argument_depth += 1
for key, value in kwargs.items():
for key in kwargs:
cclauss marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(key, BaseNode):
self.evaluate_statement(key)
self.argument_depth -= 1
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/backend/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ def create_test_serialisation(self, tests: T.List['Test']) -> T.List[TestSeriali
ld_lib_path_libs.add(l)

env_build_dir = self.environment.get_build_dir()
ld_lib_path: T.Set[str] = set(os.path.join(env_build_dir, l.get_subdir()) for l in ld_lib_path_libs)
ld_lib_path: T.Set[str] = {os.path.join(env_build_dir, l.get_subdir()) for l in ld_lib_path_libs}

if ld_lib_path:
t_env.prepend('LD_LIBRARY_PATH', list(ld_lib_path), ':')
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ def generate_cs_resource_tasks(self, target):
rel_sourcefile = os.path.join(self.build_to_src, target.subdir, r)
if r.endswith('.resources'):
a = '-resource:' + rel_sourcefile
elif r.endswith('.txt') or r.endswith('.resx'):
elif r.endswith(('.txt', '.resx')):
ofilebase = os.path.splitext(os.path.basename(r))[0] + '.resources'
ofilename = os.path.join(self.get_target_private_dir(target), ofilebase)
elem = NinjaBuildElement(self.all_outputs, ofilename, "CUSTOM_COMMAND", rel_sourcefile)
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/backend/vs2010backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ def split_link_args(args):
other.append(arg)
# It's ok if we miss libraries with non-standard extensions here.
# They will go into the general link arguments.
elif arg.endswith('.lib') or arg.endswith('.a'):
elif arg.endswith(('.lib', '.a')):
# De-dup
if arg not in libs:
libs.append(arg)
Expand Down
6 changes: 3 additions & 3 deletions mesonbuild/backend/xcodebackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ def write_tree(self, objects_dict, tree_node, children_array, current_subdir) ->

def generate_project_tree(self) -> FileTreeEntry:
tree_info = FileTreeEntry()
for tname, t in self.build_targets.items():
for t in self.build_targets.values():
self.add_target_to_tree(tree_info, t)
return tree_info

Expand Down Expand Up @@ -1658,7 +1658,7 @@ def generate_single_build_target(self, objects_dict, target_name, target) -> Non
outputs = self.generator_outputs[target_name, generator_id]
generator_id += 1
for o_abs in outputs:
if o_abs.endswith('.o') or o_abs.endswith('.obj'):
if o_abs.endswith(('.o', '.obj')):
ldargs += [r'\"' + o_abs + r'\"']
else:
if isinstance(o, build.CustomTarget):
Expand Down Expand Up @@ -1750,7 +1750,7 @@ def generate_single_build_target(self, objects_dict, target_name, target) -> Non
# file. Since Xcode itself already discourages precompiled headers in favor of modules we don't try much harder here.
pchs = target.get_pch('c') + target.get_pch('cpp') + target.get_pch('objc') + target.get_pch('objcpp')
# Make sure to use headers (other backends require implementation files like *.c *.cpp, etc; these should not be used here)
pchs = [pch for pch in pchs if pch.endswith('.h') or pch.endswith('.hh') or pch.endswith('hpp')]
pchs = [pch for pch in pchs if pch.endswith(('.h', '.hh', 'hpp'))]
if pchs:
if len(pchs) > 1:
mlog.warning(f'Unsupported Xcode configuration: More than 1 precompiled header found "{pchs!s}". Target "{target.name}" might not compile correctly.')
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ def cached_compile(self, code: 'mesonlib.FileOrString', cdata: coredata.CoreData
# TODO: There's isn't really any reason for this to be a context manager

# Calculate the key
textra_args: T.Tuple[str, ...] = tuple(extra_args) if extra_args is not None else tuple()
textra_args: T.Tuple[str, ...] = tuple(extra_args) if extra_args is not None else ()
key: coredata.CompilerCheckCacheKey = (tuple(self.exelist), self.version, code, textra_args, mode)

# Check if not cached, and generate, otherwise get from the cache
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/compilers/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_
# a temporary host C++ file that includes gcc-style line directives:
# https://stackoverflow.com/a/31001220
self.warn_args = {
level: self._to_host_flags(list(f for f in flags if f != '-Wpedantic'))
level: self._to_host_flags([f for f in flags if f != '-Wpedantic'])
for level, flags in host_compiler.warn_args.items()
}
self.host_werror_args = ['-Xcompiler=' + x for x in self.host_compiler.get_werror_args()]
Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/compilers/d.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def _translate_args_to_nongnu(cls, args: T.List[str], info: MachineInfo, link_id
if suffix in link_flags_with_arg:
link_expect_arg = True

if suffix.startswith('-') or suffix.startswith('@'):
if suffix.startswith(('-', '@')):
# this is not search path
dcargs.append(arg)
continue
Expand All @@ -312,7 +312,7 @@ def _translate_args_to_nongnu(cls, args: T.List[str], info: MachineInfo, link_id
continue

# Make sure static library files are passed properly to the linker.
if arg.endswith('.a') or arg.endswith('.lib'):
if arg.endswith(('.a', '.lib')):
if len(suffix) > 0 and not suffix.startswith('-'):
dcargs.append('-L=' + suffix)
continue
Expand Down
4 changes: 1 addition & 3 deletions mesonbuild/compilers/mixins/visualstudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,7 @@ def unix_args_to_native(cls, args: T.List[str]) -> T.List[str]:
continue
# cl.exe does not allow specifying both, so remove /utf-8 that we
# added automatically in the case the user overrides it manually.
elif (i.startswith('/source-charset:')
or i.startswith('/execution-charset:')
or i == '/validate-charset-'):
elif i.startswith(('/source-charset:', '/execution-charset:')) or i == '/validate-charset-':
try:
result.remove('/utf-8')
except ValueError:
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/coredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def sanitize_prefix(self, prefix: str) -> str:
prefix = os.path.expanduser(prefix)
if not os.path.isabs(prefix):
raise MesonException(f'prefix value {prefix!r} must be an absolute path')
if prefix.endswith('/') or prefix.endswith('\\'):
if prefix.endswith(('/', '\\')):
# On Windows we need to preserve the trailing slash if the
# string is of type 'C:\' because 'C:' is not an absolute path.
if len(prefix) == 3 and prefix[1] == ':':
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/dependencies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def get_compile_args(self) -> T.List[str]:
if self.include_type == 'system':
converted = []
for i in self.compile_args:
if i.startswith('-I') or i.startswith('/I'):
if i.startswith(('-I', '/I')):
converted += ['-isystem' + i[2:]]
else:
converted += [i]
Expand Down
6 changes: 3 additions & 3 deletions mesonbuild/dependencies/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def _cached_listdir(path: str) -> T.Tuple[T.Tuple[str, str], ...]:
try:
return tuple((x, str(x).lower()) for x in os.listdir(path))
except OSError:
return tuple()
return ()

@staticmethod
@functools.lru_cache(maxsize=None)
Expand Down Expand Up @@ -300,9 +300,9 @@ def search_lib_dirs(path: str) -> bool:
# Check PATH
system_env: T.List[str] = []
for i in os.environ.get('PATH', '').split(os.pathsep):
if i.endswith('/bin') or i.endswith('\\bin'):
if i.endswith(('/bin', '\\bin')):
i = i[:-4]
if i.endswith('/sbin') or i.endswith('\\sbin'):
if i.endswith(('/sbin', '\\sbin')):
i = i[:-5]
system_env += [i]

Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/dependencies/dub.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def find_package_target(pkg: T.Dict[str, str]) -> bool:

for file in bs['sourceFiles']:
# sourceFiles may contain static libraries
if file.endswith('.lib') or file.endswith('.a'):
if file.endswith(('.lib', '.a')):
self.link_args.append(file)

for flag in bs['lflags']:
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/dependencies/pkgconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def _convert_mingw_paths(self, args: ImmutableListProtocol[str]) -> T.List[str]:
return args.copy()
converted = []
for arg in args:
pargs: T.Tuple[str, ...] = tuple()
pargs: T.Tuple[str, ...] = ()
# Library search path
if arg.startswith('-L/'):
pargs = PurePath(arg[2:]).parts
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/dependencies/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def __init__(self, name: str, env: 'Environment', kwargs: T.Dict[str, T.Any]):
debug_lib_name = self.qtpkgname + 'Core' + _get_modules_lib_suffix(self.version, self.env.machines[self.for_machine], True)
is_debug = False
for arg in self.get_link_args():
if arg == f'-l{debug_lib_name}' or arg.endswith(f'{debug_lib_name}.lib') or arg.endswith(f'{debug_lib_name}.a'):
if arg == f'-l{debug_lib_name}' or arg.endswith((f'{debug_lib_name}.lib', f'{debug_lib_name}.a')):
is_debug = True
break
libdir = self.get_variable(pkgconfig='libdir')
Expand Down
6 changes: 1 addition & 5 deletions mesonbuild/dependencies/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ def filter_args(args: T.List[str]) -> T.List[str]:
"""
result = []
for f in args:
if f.startswith('-D') \
or f.startswith('-f') \
or f.startswith('-I') \
or f == '-pthread' \
or (f.startswith('-W') and not f == '-Wall'):
if f.startswith(('-D', '-f', '-I')) or f == '-pthread' or f.startswith('-W') and not f == '-Wall':
result.append(f)
return result

Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def detect_cpu_family(compilers: CompilersDict) -> str:
elif trial.startswith('aarch64'):
# This can be `aarch64_be`
trial = 'aarch64'
elif trial.startswith('arm') or trial.startswith('earm'):
elif trial.startswith(('arm', 'earm')):
trial = 'arm'
elif trial.startswith(('powerpc64', 'ppc64')):
trial = 'ppc64'
Expand Down Expand Up @@ -409,7 +409,7 @@ def detect_cpu(compilers: CompilersDict) -> str:
# Same check as above for cpu_family
if any_compiler_has_define(compilers, '__i386__'):
trial = 'i686' # All 64 bit cpus have at least this level of x86 support.
elif trial.startswith('aarch64') or trial.startswith('arm64'):
elif trial.startswith(('aarch64', 'arm64')):
# Same check as above for cpu_family
if any_compiler_has_define(compilers, '__arm__'):
trial = 'arm'
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2963,7 +2963,7 @@ def _warn_about_builtin_args(self, args: T.List[str]) -> None:
elif arg in {'-fsanitize', '/fsanitize'} or arg.startswith(('-fsanitize=', '/fsanitize=')):
mlog.warning(f'Consider using the built-in option for sanitizers instead of using "{arg}".',
location=self.current_node)
elif arg.startswith('-std=') or arg.startswith('/std:'):
elif arg.startswith(('-std=', '/std:')):
mlog.warning(f'Consider using the built-in option for language standard version instead of using "{arg}".',
location=self.current_node)

Expand Down
4 changes: 2 additions & 2 deletions mesonbuild/minstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def check_for_stampfile(fname: str) -> str:
whose names are not known at configure time.
Check if this is the case and return the real
file instead.'''
if fname.endswith('.so') or fname.endswith('.dll'):
if fname.endswith(('.so', '.dll')):
if os.stat(fname).st_size == 0:
(base, suffix) = os.path.splitext(fname)
files = glob(base + '-*' + suffix)
Expand All @@ -276,7 +276,7 @@ def check_for_stampfile(fname: str) -> str:
sys.exit(1)
if len(files) == 1:
return files[0]
elif fname.endswith('.a') or fname.endswith('.lib'):
elif fname.endswith(('.a', '.lib')):
if os.stat(fname).st_size == 0:
(base, suffix) = os.path.splitext(fname)
files = glob(base + '-*' + '.rlib')
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/modules/simd.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def check(self, state: ModuleState, args: T.Tuple[str], kwargs: CheckKw) -> T.Li
if 'sources' in kwargs:
raise mesonlib.MesonException('SIMD module does not support the "sources" keyword')

local_kwargs = set((*ISETS, 'compiler'))
local_kwargs = {*ISETS, 'compiler'}
static_lib_kwargs = T.cast('kwtypes.StaticLibrary', {k: v for k, v in kwargs.items() if k not in local_kwargs})

prefix = args[0]
Expand Down
2 changes: 1 addition & 1 deletion mesonbuild/utils/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def perms_s_to_bits(cls, perms_s: T.Optional[str]) -> int:
See https://github.com/mesonbuild/meson/pull/8747 for the discussions."""
class File(HoldableObject):
def __init__(self, is_built: bool, subdir: str, fname: str):
if fname.endswith(".C") or fname.endswith(".H"):
if fname.endswith(('.C', '.H')):
mlog.warning(dot_C_dot_H_warning, once=True)
self.is_built = is_built
self.subdir = subdir
Expand Down
2 changes: 1 addition & 1 deletion run_format_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def check_format() -> None:
'3 editorconfig',
}
for (root, _, filenames) in os.walk('.'):
if any([x in root for x in skip_dirs]):
if any(x in root for x in skip_dirs):
continue
for fname in filenames:
file = Path(fname)
Expand Down
7 changes: 3 additions & 4 deletions run_project_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,8 +911,7 @@ def load_test_json(t: TestDef, stdout_mandatory: bool, skip_category: bool = Fal
exclude = False
opt_tuple = [(x[0], x[1]) for x in i]
for j in matrix['exclude']:
ex_list = [(k, v) for k, v in j.items()]
if all([x in opt_tuple for x in ex_list]):
if all(x in opt_tuple for x in j.items()):
exclude = True
break

Expand All @@ -924,8 +923,8 @@ def load_test_json(t: TestDef, stdout_mandatory: bool, skip_category: bool = Fal
for i in opt_list:
name = ' '.join([f'{x[0]}={x[1]}' for x in i if x[1] is not None])
opts = [f'-D{x[0]}={x[1]}' for x in i if x[1] is not None]
skip = any([x[2] for x in i])
skip_expected = any([x[3] for x in i])
skip = any(x[2] for x in i)
skip_expected = any(x[3] for x in i)
test = TestDef(t.path, name, opts, skip or t.skip, skip_category)
test.env.update(env)
test.installed_files = installed
Expand Down
2 changes: 1 addition & 1 deletion test cases/common/186 test depends/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def main():
print('Looking in:', os.getcwd())
not_found = list()
not_found = []
for f in sys.argv[1:]:
if not os.path.exists(f):
not_found.append(f)
Expand Down
2 changes: 1 addition & 1 deletion test cases/common/208 link custom/custom_stlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def generate_lib(outfile, private_dir, compiler_array):
c_file = private_dir / 'flob.c'
c_file.write_text(contents)
for i in compiler_array:
if (i.endswith('cl') or i.endswith('cl.exe')) and 'clang-cl' not in i:
if i.endswith(('cl', 'cl.exe')) and 'clang-cl' not in i:
return generate_lib_msvc(outfile, c_file, private_dir, compiler_array)
return generate_lib_gnulike(outfile, c_file, private_dir, compiler_array)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def generate_lib(outfiles, private_dir, compiler_array):

cl_found = False
for cl_arg in compiler_array:
if (cl_arg.endswith('cl') or cl_arg.endswith('cl.exe')) and 'clang-cl' not in cl_arg:
if cl_arg.endswith(('cl', 'cl.exe')) and 'clang-cl' not in cl_arg:
ret = generate_lib_msvc(outfile, c_file, private_dir, compiler_array)
if ret > 0:
return ret
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def generate_lib(outfiles, private_dir, compiler_array):

cl_found = False
for cl_arg in compiler_array:
if (cl_arg.endswith('cl') or cl_arg.endswith('cl.exe')) and 'clang-cl' not in cl_arg:
if cl_arg.endswith(('cl', 'cl.exe')) and 'clang-cl' not in cl_arg:
ret = generate_lib_msvc(outfile, c_file, private_dir, compiler_array)
if ret > 0:
return ret
Expand Down
2 changes: 1 addition & 1 deletion test cases/common/95 manygen/subdir/manygen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
if compiler_type == 'msvc':
libsuffix = '.lib'
is_vs = True
if any(['clang-cl' in c for c in compiler]):
if any('clang-cl' in c for c in compiler):
linker = 'llvm-lib'
else:
linker = 'lib'
Expand Down
2 changes: 1 addition & 1 deletion tools/regenerate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def clean_dir_arguments(text: str) -> str:
commands = get_commands(output)
commands.remove('help')

cmd_data = dict()
cmd_data = {}

for cmd in commands:
cmd_output = _get_meson_output(root_dir, [cmd, '--help'])
Expand Down
Loading
Loading