Don't use len() to test for container emptiness

I ran the numbers once before (it's in the meson history) but it's
*much* faster to *not* use len for testing if a container is empty or
not.
pull/5227/merge
Dylan Baker 6 years ago
parent 8e1670cc60
commit 5678468c2c
  1. 4
      mesonbuild/backend/xcodebackend.py
  2. 2
      mesonbuild/compilers/c.py
  3. 2
      mesonbuild/compilers/compilers.py
  4. 4
      mesonbuild/dependencies/boost.py
  5. 2
      mesonbuild/mesonmain.py
  6. 2
      mesonbuild/minit.py
  7. 4
      mesonbuild/mintro.py
  8. 2
      mesonbuild/modules/cmake.py
  9. 12
      mesonbuild/modules/qt.py
  10. 6
      mesonbuild/modules/rpm.py
  11. 2
      mesonbuild/modules/unstable_icestorm.py
  12. 2
      mesonbuild/modules/unstable_simd.py
  13. 2
      mesonbuild/scripts/dist.py
  14. 14
      mesonbuild/scripts/gtkdochelper.py
  15. 2
      mesonbuild/scripts/meson_exe.py
  16. 4
      mesonbuild/scripts/symbolextractor.py

@ -748,7 +748,7 @@ class XCodeBackend(backends.Backend):
gargs = self.build.global_args.get(lang, [])
targs = target.get_extra_args(lang)
args = pargs + gargs + targs
if len(args) > 0:
if args:
langargs[langnamemap[lang]] = args
symroot = os.path.join(self.environment.get_build_dir(), target.subdir)
self.write_line('%s /* %s */ = {' % (valid, buildtype))
@ -783,7 +783,7 @@ class XCodeBackend(backends.Backend):
self.write_line('GCC_PREFIX_HEADER = "$(PROJECT_DIR)/%s";' % relative_pch_path)
self.write_line('GCC_PREPROCESSOR_DEFINITIONS = "";')
self.write_line('GCC_SYMBOLS_PRIVATE_EXTERN = NO;')
if len(headerdirs) > 0:
if headerdirs:
quotedh = ','.join(['"\\"%s\\""' % i for i in headerdirs])
self.write_line('HEADER_SEARCH_PATHS=(%s);' % quotedh)
self.write_line('INSTALL_PATH = "%s";' % install_path)

@ -248,7 +248,7 @@ class CCompiler(Compiler):
for d in dirs:
files = [f for f in os.listdir(d) if f.endswith('.so') and os.path.isfile(os.path.join(d, f))]
# if no files, accept directory and move on
if len(files) == 0:
if not files:
retval.append(d)
continue
file_to_check = os.path.join(d, files[0])

@ -1506,7 +1506,7 @@ def gnulike_default_include_dirs(compiler, lang):
break
else:
paths.append(line[1:])
if len(paths) == 0:
if not paths:
mlog.warning('No include directory found parsing "{cmd}" output'.format(cmd=" ".join(cmd)))
return paths

@ -209,7 +209,7 @@ class BoostDependency(ExternalDependency):
for root in self.boost_roots:
globtext = os.path.join(root, 'include', 'boost-*')
incdirs = glob.glob(globtext)
if len(incdirs) > 0:
if incdirs:
return incdirs[0]
incboostdir = os.path.join(root, 'include', 'boost')
if os.path.isdir(incboostdir):
@ -427,7 +427,7 @@ class BoostDependency(ExternalDependency):
for entry in globber2_matches:
fname = os.path.basename(entry)
self.lib_modules[self.modname_from_filename(fname)] = [fname]
if len(globber2_matches) == 0:
if not globber2_matches:
# FIXME - why are we only looking for *.lib? Mingw provides *.dll.a and *.a
for entry in glob.glob(os.path.join(self.libdir, globber1 + '.lib')):
if self.static:

@ -110,7 +110,7 @@ class CommandLineParser:
# If first arg is not a known command, assume user wants to run the setup
# command.
known_commands = list(self.commands.keys()) + ['-h', '--help']
if len(args) == 0 or args[0] not in known_commands:
if not args or args[0] not in known_commands:
args = ['setup'] + args
# Hidden commands have their own parser instead of using the global one

@ -442,7 +442,7 @@ def add_arguments(parser):
parser.add_argument('--version', default='0.1')
def run(options):
if len(glob('*')) == 0:
if not glob('*'):
autodetect_options(options, sample=True)
if not options.language:
print('Defaulting to generating a C language project.')

@ -343,7 +343,7 @@ def list_projinfo_from_source(sourcedir: str, intr: IntrospectionInterpreter):
return intr.project_data
def print_results(options, results, indent):
if len(results) == 0 and not options.force_dict:
if not results and not options.force_dict:
print('No command specified')
return 1
elif len(results) == 1 and not options.force_dict:
@ -487,7 +487,7 @@ def write_meson_info_file(builddata: build.Build, errors: list, build_files_upda
'build_files_updated': build_files_updated,
}
if len(errors) > 0:
if errors:
info_data['error'] = True
info_data['error_list'] = [x if isinstance(x, str) else str(x) for x in errors]
else:

@ -158,7 +158,7 @@ class CmakeModule(ExtensionModule):
@permittedKwargs({'input', 'name', 'install_dir', 'configuration'})
def configure_package_config_file(self, interpreter, state, args, kwargs):
if len(args) > 0:
if args:
raise mesonlib.MesonException('configure_package_config_file takes only keyword arguments.')
if 'input' not in kwargs:

@ -129,13 +129,13 @@ class QtBaseModule(ExtensionModule):
self._detect_tools(state.environment, method)
err_msg = "{0} sources specified and couldn't find {1}, " \
"please check your qt{2} installation"
if len(moc_headers) + len(moc_sources) > 0 and not self.moc.found():
if (moc_headers or moc_sources) and not self.moc.found():
raise MesonException(err_msg.format('MOC', 'moc-qt{}'.format(self.qt_version), self.qt_version))
if len(rcc_files) > 0:
if rcc_files:
if not self.rcc.found():
raise MesonException(err_msg.format('RCC', 'rcc-qt{}'.format(self.qt_version), self.qt_version))
# custom output name set? -> one output file, multiple otherwise
if len(args) > 0:
if args:
qrc_deps = []
for i in rcc_files:
qrc_deps += self.parse_qrc(state, i)
@ -160,7 +160,7 @@ class QtBaseModule(ExtensionModule):
'depend_files': qrc_deps}
res_target = build.CustomTarget(name, state.subdir, state.subproject, rcc_kwargs)
sources.append(res_target)
if len(ui_files) > 0:
if ui_files:
if not self.uic.found():
raise MesonException(err_msg.format('UIC', 'uic-qt{}'.format(self.qt_version), self.qt_version))
arguments = uic_extra_arguments + ['-o', '@OUTPUT@', '@INPUT@']
@ -183,14 +183,14 @@ class QtBaseModule(ExtensionModule):
'either an external dependency (returned by find_library() or '
'dependency()) or an internal dependency (returned by '
'declare_dependency()).'.format(type(dep).__name__))
if len(moc_headers) > 0:
if moc_headers:
arguments = moc_extra_arguments + inc + compile_args + ['@INPUT@', '-o', '@OUTPUT@']
moc_kwargs = {'output': 'moc_@BASENAME@.cpp',
'arguments': arguments}
moc_gen = build.Generator([self.moc], moc_kwargs)
moc_output = moc_gen.process_files('Qt{} moc header'.format(self.qt_version), moc_headers, state)
sources.append(moc_output)
if len(moc_sources) > 0:
if moc_sources:
arguments = moc_extra_arguments + inc + compile_args + ['@INPUT@', '-o', '@OUTPUT@']
moc_kwargs = {'output': '@BASENAME@.moc',
'arguments': arguments}

@ -58,7 +58,7 @@ class RPMModule(ExtensionModule):
elif isinstance(target, TypelibTarget) and target.should_install():
files.add('%%{_libdir}/girepository-1.0/%s' % target.get_filename()[0])
for header in coredata.headers:
if len(header.get_install_subdir()) > 0:
if header.get_install_subdir():
files_devel.add('%%{_includedir}/%s/' % header.get_install_subdir())
else:
for hdr_src in header.get_sources():
@ -66,7 +66,7 @@ class RPMModule(ExtensionModule):
for man in coredata.man:
for man_file in man.get_sources():
files.add('%%{_mandir}/man%u/%s.*' % (int(man_file.split('.')[-1]), man_file))
if len(files_devel) > 0:
if files_devel:
devel_subpkg = True
filename = os.path.join(coredata.environment.get_build_dir(),
@ -122,7 +122,7 @@ class RPMModule(ExtensionModule):
fn.write('\n')
fn.write('%install\n')
fn.write('%meson_install\n')
if len(to_delete) > 0:
if to_delete:
fn.write('rm -vf %s\n' % ' '.join(to_delete))
fn.write('\n')
fn.write('%check\n')

@ -36,7 +36,7 @@ class IceStormModule(ExtensionModule):
def project(self, interpreter, state, args, kwargs):
if not self.yosys_bin:
self.detect_binaries(interpreter)
if not len(args):
if not args:
raise mesonlib.MesonException('Project requires at least one argument, which is the project name.')
proj_name = args[0]
arg_sources = args[1:]

@ -65,7 +65,7 @@ class SimdModule(ExtensionModule):
if args is None:
mlog.log('Compiler supports %s:' % iset, mlog.red('NO'))
continue
if len(args) > 0:
if args:
if not compiler.has_multi_arguments(args, state.environment):
mlog.log('Compiler supports %s:' % iset, mlog.red('NO'))
continue

@ -133,7 +133,7 @@ def create_dist_hg(dist_name, src_root, bld_root, dist_sub, dist_scripts):
tarname = os.path.join(dist_sub, dist_name + '.tar')
xzname = tarname + '.xz'
subprocess.check_call(['hg', 'archive', '-R', src_root, '-S', '-t', 'tar', tarname])
if len(dist_scripts) > 0:
if dist_scripts:
mlog.warning('dist scripts are not supported in Mercurial projects')
with lzma.open(xzname, 'wb') as xf, open(tarname, 'rb') as tf:
shutil.copyfileobj(tf, xf)

@ -175,7 +175,7 @@ def build_gtkdoc(source_root, build_root, doc_subdir, src_subdirs,
mkdb_cmd.append('--name-space=' + namespace)
if modeflag:
mkdb_cmd.append(modeflag)
if len(main_file) > 0:
if main_file:
# Yes, this is the flag even if the file is in xml.
mkdb_cmd.append('--main-sgml-file=' + main_file)
# Add user-specified arguments
@ -187,7 +187,7 @@ def build_gtkdoc(source_root, build_root, doc_subdir, src_subdirs,
'--path=' + ':'.join((doc_src, abs_out)),
module,
] + html_args
if len(main_file) > 0:
if main_file:
mkhtml_cmd.append('../' + main_file)
else:
mkhtml_cmd.append('%s-docs.xml' % module)
@ -212,23 +212,23 @@ def install_gtkdoc(build_root, doc_subdir, install_prefix, datadir, module):
def run(args):
options = parser.parse_args(args)
if len(options.htmlargs) > 0:
if options.htmlargs:
htmlargs = options.htmlargs.split('@@')
else:
htmlargs = []
if len(options.scanargs) > 0:
if options.scanargs:
scanargs = options.scanargs.split('@@')
else:
scanargs = []
if len(options.scanobjsargs) > 0:
if options.scanobjsargs:
scanobjsargs = options.scanobjsargs.split('@@')
else:
scanobjsargs = []
if len(options.fixxrefargs) > 0:
if options.fixxrefargs:
fixxrefargs = options.fixxrefargs.split('@@')
else:
fixxrefargs = []
if len(options.mkdbargs) > 0:
if options.mkdbargs:
mkdbargs = options.mkdbargs.split('@@')
else:
mkdbargs = []

@ -60,7 +60,7 @@ def run_exe(exe):
cmd = exe.fname
child_env = os.environ.copy()
child_env.update(exe.env)
if len(exe.extra_paths) > 0:
if exe.extra_paths:
child_env['PATH'] = (os.pathsep.join(exe.extra_paths + ['']) +
child_env['PATH'])
if exe.exe_runner and mesonlib.substring_is_in_list('wine', exe.exe_runner.get_command()):

@ -69,7 +69,7 @@ def linux_syms(libfilename, outfilename):
if pnm.returncode != 0:
raise RuntimeError('nm does not work.')
for line in output.split('\n'):
if len(line) == 0:
if not line:
continue
line_split = line.split()
entry = line_split[0:2]
@ -91,7 +91,7 @@ def osx_syms(libfilename, outfilename):
pnm, output = Popen_safe(['nm', '-g', '-P', libfilename])[0:2]
if pnm.returncode != 0:
raise RuntimeError('nm does not work.')
result += [' '.join(x.split()[0:2]) for x in output.split('\n') if len(x) > 0 and not x.endswith('U')]
result += [' '.join(x.split()[0:2]) for x in output.split('\n') if x and not x.endswith('U')]
write_if_changed('\n'.join(result) + '\n', outfilename)
def gen_symbols(libfilename, outfilename, cross_host):

Loading…
Cancel
Save