From f5c69f68871141018bd09f7ca499ff6724946173 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Fri, 23 Apr 2021 12:22:52 -0600 Subject: Avoid exception if tool set to empty When the reproducer in issue #1742 was run on git head, it failed in a different way than in the issue: an exception "list index out of range", caused by the test setting CC="" in the Environment call(). While this is a rather unuseful thing to do in general, the resulting call to env.WhereIs should not throw an exception because the general WhereIs function ends up indexing into something that can't be indexed. Avoid this by returning None immediately if the list of names to look for is empty. Note this does _not_ fix issue 1742, it just avoids the new problem it was failing on. Signed-off-by: Mats Wichmann --- CHANGES.txt | 1 + SCons/Environment.py | 12 +++++++----- SCons/Util.py | 12 +++++++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 22f73c8..4ea3c88 100755 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -75,6 +75,7 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER - Deprecate Python 3.5 as a supported version. - CPPDEFINES now expands construction variable references (issue 2363) - Restore behavior that Install()'d files are writable (issue 3927) + - Avoid WhereIs exception if user set a tool name to empty (from issue 1742) From Dillan Mills: - Add support for the (TARGET,SOURCE,TARGETS,SOURCES,CHANGED_TARGETS,CHANGED_SOURCES}.relpath property. diff --git a/SCons/Environment.py b/SCons/Environment.py index 92fd583..8a31b33 100644 --- a/SCons/Environment.py +++ b/SCons/Environment.py @@ -1877,9 +1877,10 @@ class Base(SubstitutionEnvironment): tool = SCons.Tool.Tool(tool, toolpath, **kw) tool(self) - def WhereIs(self, prog, path=None, pathext=None, reject=[]): - """Find prog in the path. - """ + def WhereIs(self, prog, path=None, pathext=None, reject=None): + """Find prog in the path. """ + if not prog: # nothing to search for, just give up + return None if path is None: try: path = self['ENV']['PATH'] @@ -1894,9 +1895,10 @@ class Base(SubstitutionEnvironment): pass elif is_String(pathext): pathext = self.subst(pathext) - prog = CLVar(self.subst(prog)) # support "program --with-args" + prog = CLVar(self.subst(prog)) # support "program --with-args" path = WhereIs(prog[0], path, pathext, reject) - if path: return path + if path: + return path return None ####################################################################### diff --git a/SCons/Util.py b/SCons/Util.py index 37107c9..af43220 100644 --- a/SCons/Util.py +++ b/SCons/Util.py @@ -745,7 +745,7 @@ else: if sys.platform == 'win32': - def WhereIs(file, path=None, pathext=None, reject=[]): + def WhereIs(file, path=None, pathext=None, reject=None): if path is None: try: path = os.environ['PATH'] @@ -764,6 +764,8 @@ if sys.platform == 'win32': if ext.lower() == file[-len(ext):].lower(): pathext = [''] break + if reject is None: + reject = [] if not is_List(reject) and not is_Tuple(reject): reject = [reject] for dir in path: @@ -780,7 +782,7 @@ if sys.platform == 'win32': elif os.name == 'os2': - def WhereIs(file, path=None, pathext=None, reject=[]): + def WhereIs(file, path=None, pathext=None, reject=None): if path is None: try: path = os.environ['PATH'] @@ -794,6 +796,8 @@ elif os.name == 'os2': if ext.lower() == file[-len(ext):].lower(): pathext = [''] break + if reject is None: + reject = [] if not is_List(reject) and not is_Tuple(reject): reject = [reject] for dir in path: @@ -810,7 +814,7 @@ elif os.name == 'os2': else: - def WhereIs(file, path=None, pathext=None, reject=[]): + def WhereIs(file, path=None, pathext=None, reject=None): import stat if path is None: try: @@ -819,6 +823,8 @@ else: return None if is_String(path): path = path.split(os.pathsep) + if reject is None: + reject = [] if not is_List(reject) and not is_Tuple(reject): reject = [reject] for d in path: -- cgit v0.12 From 143687c8b6c20684e50696d0431d972706178c63 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Fri, 23 Apr 2021 13:35:36 -0600 Subject: Add simple unit-test for env.Whereis('') Signed-off-by: Mats Wichmann --- SCons/EnvironmentTests.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SCons/EnvironmentTests.py b/SCons/EnvironmentTests.py index 4ea2c66..7155969 100644 --- a/SCons/EnvironmentTests.py +++ b/SCons/EnvironmentTests.py @@ -2583,6 +2583,8 @@ def generate(env): path = os.pathsep.join(pathdirs_1234) env = self.TestEnvironment(ENV = {'PATH' : path}) + wi = env.WhereIs('') + assert wi is None wi = env.WhereIs('xxx.exe') assert wi == test.workpath(sub3_xxx_exe), wi wi = env.WhereIs('xxx.exe', pathdirs_1243) -- cgit v0.12