Skip to content

Commit 81a39dc

Browse files
committed
Use "in" and "not in" instead of find()
A maintenance edit: most instances of str.find() used for membership checks like "if foo.find('bar') != -1" are replaced with "in" or "not in", or in a few cases, with the "startswith()" method. The vendored docbook utilities were not changed in this round though they had a couple of these. Signed-off-by: Mats Wichmann <mats@linux.com>
1 parent be29ebc commit 81a39dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+150
-154
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
3030
- In the SequenceTypes tuple used for dynamic type checks, include
3131
the dict contents views, exclude the dictionary view itself as it
3232
is not an interable sequence type.
33+
- Internal: where the find method on a string was used to determine
34+
if a substring is present, use the more readable "in" and "not in".
3335

3436

3537
RELEASE 4.10.0 - Thu, 02 Oct 2025 11:40:20 -0700

RELEASE.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ IMPROVEMENTS
5252
documentation: performance improvements (describe the circumstances
5353
under which they would be observed), or major code cleanups
5454

55+
- Internal: where the find method on a string was used to determine
56+
if a substring is present, use the more readable "in" and "not in".
57+
5558
PACKAGING
5659
---------
5760

SCons/ActionTests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def none(a) -> None:
312312
except SCons.Errors.UserError as e:
313313
s = str(e)
314314
m = 'Invalid command display variable'
315-
assert s.find(m) != -1, 'Unexpected string: %s' % s
315+
assert m in s, f'Unexpected string: {s}'
316316
else:
317317
raise Exception("did not catch expected UserError")
318318

@@ -540,7 +540,7 @@ def func() -> None:
540540
except SCons.Errors.UserError as e:
541541
s = str(e)
542542
m = 'Cannot have both strfunction and cmdstr args to Action()'
543-
assert s.find(m) != -1, 'Unexpected string: %s' % s
543+
assert m in s, f'Unexpected string: {s}'
544544
else:
545545
raise Exception("did not catch expected UserError")
546546

SCons/Platform/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ def platform_default():
7474
if osname == 'posix':
7575
if sys.platform == 'cygwin':
7676
return 'cygwin'
77-
elif sys.platform.find('irix') != -1:
77+
elif 'irix' in sys.platform:
7878
return 'irix'
79-
elif sys.platform.find('sunos') != -1:
79+
elif 'sunos' in sys.platform:
8080
return 'sunos'
81-
elif sys.platform.find('hp-ux') != -1:
81+
elif 'hp-ux' in sys.platform:
8282
return 'hpux'
83-
elif sys.platform.find('aix') != -1:
83+
elif 'aix' in sys.platform:
8484
return 'aix'
85-
elif sys.platform.find('darwin') != -1:
85+
elif 'darwin' in sys.platform:
8686
return 'darwin'
8787
else:
8888
return 'posix'

SCons/Platform/win32.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ def piped_spawn(sh, escape, cmd, args, env, stdout, stderr):
136136
stderrRedirected = False
137137
for arg in args:
138138
# are there more possibilities to redirect stdout ?
139-
if arg.find(">", 0, 1) != -1 or arg.find("1>", 0, 2) != -1:
139+
if arg.startswith(">")or arg.startswith("1>"):
140140
stdoutRedirected = True
141141
# are there more possibilities to redirect stderr ?
142-
if arg.find("2>", 0, 2) != -1:
142+
if arg.startswith("2>"):
143143
stderrRedirected = True
144144

145145
# redirect output of non-redirected streams to our tempfiles

SCons/Script/Main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def __init__(self, obj, interval: int=1, file=None, overwrite: bool=False) -> No
144144
self.func = obj
145145
elif SCons.Util.is_List(obj):
146146
self.func = self.spinner
147-
elif obj.find(self.target_string) != -1:
147+
elif self.target_string in obj:
148148
self.func = self.replace_string
149149
else:
150150
self.func = self.string
@@ -644,7 +644,7 @@ def find_deepest_user_frame(tb):
644644
# of SCons:
645645
for frame in tb:
646646
filename = frame[0]
647-
if filename.find(os.sep+'SCons'+os.sep) == -1:
647+
if f'{os.sep}SCons{os.sep}' not in filename:
648648
return frame
649649
return tb[0]
650650

SCons/Subst.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,8 @@ def expand(self, s, lvars, within_list):
561561
self.close_strip('$)')
562562
else:
563563
key = s[1:]
564-
if key[0] == '{' or key.find('.') >= 0:
565-
if key[0] == '{':
564+
if key.startswith('{') or '.' in key:
565+
if key.startswith('{'):
566566
key = key[1:-1]
567567

568568
# Store for error messages if we fail to expand the
@@ -962,7 +962,7 @@ def scons_subst_once(strSubst, env, key):
962962
963963
We do this with some straightforward, brute-force code here...
964964
"""
965-
if isinstance(strSubst, str) and strSubst.find('$') < 0:
965+
if isinstance(strSubst, str) and '$' not in strSubst:
966966
return strSubst
967967

968968
matchlist = ['$' + key, '${' + key + '}']

SCons/Tool/hpcxx.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
"""SCons.Tool.hpc++
2-
3-
Tool-specific initialization for c++ on HP/UX.
4-
5-
There normally shouldn't be any need to import this module directly.
6-
It will usually be imported through the generic SCons.Tool.Tool()
7-
selection method.
8-
9-
"""
10-
1+
# MIT License
112
#
12-
# __COPYRIGHT__
3+
# Copyright The SCons Foundation
134
#
145
# Permission is hereby granted, free of charge, to any person obtaining
156
# a copy of this software and associated documentation files (the
@@ -29,9 +20,15 @@
2920
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
3021
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
3122
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32-
#
3323

34-
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
24+
"""SCons.Tool.hpc++
25+
26+
Tool-specific initialization for c++ on HP/UX.
27+
28+
There normally shouldn't be any need to import this module directly.
29+
It will usually be imported through the generic SCons.Tool.Tool()
30+
selection method.
31+
"""
3532

3633
import os.path
3734

@@ -59,7 +56,7 @@
5956
acc = cc
6057
break
6158

62-
59+
6360
def generate(env) -> None:
6461
"""Add Builders and construction variables for g++ to an Environment."""
6562
cplusplus.generate(env)
@@ -70,7 +67,7 @@ def generate(env) -> None:
7067
# determine version of aCC
7168
with os.popen(acc + ' -V 2>&1') as p:
7269
line = p.readline().rstrip()
73-
if line.find('aCC: HP ANSI C++') == 0:
70+
if line.startswith('aCC: HP ANSI C++'):
7471
env['CXXVERSION'] = line.split()[-1]
7572

7673
if env['PLATFORM'] == 'cygwin':

SCons/Tool/intelc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ class ICLTopDirWarning(SCons.Warnings.SConsWarning):
569569
for ld in [envlicdir, reglicdir]:
570570
# If the string contains an '@', then assume it's a network
571571
# license (port@system) and good by definition.
572-
if ld and (ld.find('@') != -1 or os.path.exists(ld)):
572+
if ld and ('@' in ld or os.path.exists(ld)):
573573
licdir = ld
574574
break
575575
if not licdir:

SCons/Tool/midl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def midl_emitter(target, source, env):
5252

5353
midlcom = env['MIDLCOM']
5454

55-
if midlcom.find('/proxy') != -1:
55+
if '/proxy' in midlcom:
5656
proxy = base + '_p.c'
5757
targets.append(proxy)
58-
if midlcom.find('/dlldata') != -1:
58+
if '/dlldata' in midlcom:
5959
dlldata = base + '_data.c'
6060
targets.append(dlldata)
6161

0 commit comments

Comments
 (0)