aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/build.py
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2017-05-01 15:11:01 -0700
committerJussi Pakkanen <jpakkane@gmail.com>2017-05-02 21:57:26 +0300
commita8173630eac1f35914fdc2d918a636ac268b9033 (patch)
treec971b7e0be066df5a2a77f1141eb1517dd3903ef /mesonbuild/build.py
parentae924b01a006bc1542fe7eaae1d8a933459e8a0a (diff)
downloadmeson-a8173630eac1f35914fdc2d918a636ac268b9033.zip
meson-a8173630eac1f35914fdc2d918a636ac268b9033.tar.gz
meson-a8173630eac1f35914fdc2d918a636ac268b9033.tar.bz2
Don't use len() to test emptiness vs not emptiness
Meson has a common pattern of using 'if len(foo) == 0:' or 'if len(foo) != 0:', however, this is a common anti-pattern in python. Instead tests for emptiness/non-emptiness should be done with a simple 'if foo:' or 'if not foo:' Consider the following: >>> import timeit >>> timeit.timeit('if len([]) == 0: pass') 0.10730923599840025 >>> timeit.timeit('if not []: pass') 0.030033907998586074 >>> timeit.timeit('if len(['a', 'b', 'c', 'd']) == 0: pass') 0.1154778649979562 >>> timeit.timeit("if not ['a', 'b', 'c', 'd']: pass") 0.08259823200205574 >>> timeit.timeit('if len("") == 0: pass') 0.089759664999292 >>> timeit.timeit('if not "": pass') 0.02340641999762738 >>> timeit.timeit('if len("foo") == 0: pass') 0.08848102600313723 >>> timeit.timeit('if not "foo": pass') 0.04032287199879647 And for the one additional case of 'if len(foo.strip()) == 0', which can be replaced with 'if not foo.isspace()' >>> timeit.timeit('if len(" ".strip()) == 0: pass') 0.15294511600222904 >>> timeit.timeit('if " ".isspace(): pass') 0.09413968399894657 >>> timeit.timeit('if len(" abc".strip()) == 0: pass') 0.2023209120015963 >>> timeit.timeit('if " abc".isspace(): pass') 0.09571301700270851 In other words, it's always a win to not use len(), when you don't actually want to check the length.
Diffstat (limited to 'mesonbuild/build.py')
-rw-r--r--mesonbuild/build.py16
1 files changed, 7 insertions, 9 deletions
diff --git a/mesonbuild/build.py b/mesonbuild/build.py
index a5ebc34..c8d692e 100644
--- a/mesonbuild/build.py
+++ b/mesonbuild/build.py
@@ -115,7 +115,7 @@ class Build:
self.compilers[lang] = compiler
def add_cross_compiler(self, compiler):
- if len(self.cross_compilers) == 0:
+ if not self.cross_compilers:
self.static_cross_linker = self.environment.detect_static_linker(compiler)
lang = compiler.get_language()
if lang not in self.cross_compilers:
@@ -340,9 +340,7 @@ class BuildTarget(Target):
self.process_objectlist(objects)
self.process_kwargs(kwargs, environment)
self.check_unknown_kwargs(kwargs)
- if len(self.sources) == 0 \
- and len(self.generated) == 0 \
- and len(self.objects) == 0:
+ if not self.sources and not self.generated and not self.objects:
raise InvalidArguments('Build target %s has no sources.' % name)
self.process_compilers()
self.validate_sources()
@@ -433,7 +431,7 @@ class BuildTarget(Target):
We also add compilers that were used by extracted objects to simplify
dynamic linker determination.
'''
- if len(self.sources) + len(self.generated) + len(self.objects) == 0:
+ if not self.sources and not self.generated and not self.objects:
return
# Populate list of compilers
if self.is_cross:
@@ -488,7 +486,7 @@ class BuildTarget(Target):
self.compilers['c'] = compilers['c']
def validate_sources(self):
- if len(self.sources) == 0:
+ if not self.sources:
return
for lang in ('cs', 'java'):
if lang in self.compilers:
@@ -675,7 +673,7 @@ class BuildTarget(Target):
if 'name_prefix' in kwargs:
name_prefix = kwargs['name_prefix']
if isinstance(name_prefix, list):
- if len(name_prefix) != 0:
+ if name_prefix:
raise InvalidArguments('name_prefix array must be empty to signify null.')
elif not isinstance(name_prefix, str):
raise InvalidArguments('name_prefix must be a string.')
@@ -684,7 +682,7 @@ class BuildTarget(Target):
if 'name_suffix' in kwargs:
name_suffix = kwargs['name_suffix']
if isinstance(name_suffix, list):
- if len(name_suffix) != 0:
+ if name_suffix:
raise InvalidArguments('name_suffix array must be empty to signify null.')
else:
if not isinstance(name_suffix, str):
@@ -825,7 +823,7 @@ You probably should put it in link_with instead.''')
self.link_whole_targets.append(t)
def add_pch(self, language, pchlist):
- if len(pchlist) == 0:
+ if not pchlist:
return
elif len(pchlist) == 1:
if not environment.is_header(pchlist[0]):