From b0d344aed38a3fb8a2d9e1f2cb785405ef91b3b0 Mon Sep 17 00:00:00 2001 From: stevenknight Date: Fri, 30 Jul 2004 19:07:46 +0000 Subject: [PATCH] Don't blow up if the external PATH variable is not set. git-svn-id: http://scons.tigris.org/svn/scons/trunk@1017 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- src/CHANGES.txt | 3 ++ src/engine/SCons/Util.py | 15 ++++-- src/engine/SCons/UtilTests.py | 97 +++++++++++++++++++---------------- 3 files changed, 68 insertions(+), 47 deletions(-) diff --git a/src/CHANGES.txt b/src/CHANGES.txt index 32d03c58..083d6401 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -144,6 +144,9 @@ RELEASE 0.96 - XXX arguments when creating Builder objects. Enhance Dir Nodes so that they can be created with user-specified Builder objects. + - Don't blow up with stack trace when the external $PATH environment + variable isn't set. + From Chris Murray: - Add a .win32 attribute to force file names to expand with diff --git a/src/engine/SCons/Util.py b/src/engine/SCons/Util.py index 998a01b5..261d73c8 100644 --- a/src/engine/SCons/Util.py +++ b/src/engine/SCons/Util.py @@ -1055,7 +1055,10 @@ if sys.platform == 'win32': def WhereIs(file, path=None, pathext=None, reject=[]): if path is None: - path = os.environ['PATH'] + try: + path = os.environ['PATH'] + except KeyError: + return None if is_String(path): path = string.split(path, os.pathsep) if pathext is None: @@ -1087,7 +1090,10 @@ elif os.name == 'os2': def WhereIs(file, path=None, pathext=None, reject=[]): if path is None: - path = os.environ['PATH'] + try: + path = os.environ['PATH'] + except KeyError: + return None if is_String(path): path = string.split(path, os.pathsep) if pathext is None: @@ -1114,7 +1120,10 @@ else: def WhereIs(file, path=None, pathext=None, reject=[]): if path is None: - path = os.environ['PATH'] + try: + path = os.environ['PATH'] + except KeyError: + return None if is_String(path): path = string.split(path, os.pathsep) if not is_List(reject): diff --git a/src/engine/SCons/UtilTests.py b/src/engine/SCons/UtilTests.py index 1bce1af5..fb9f14c1 100644 --- a/src/engine/SCons/UtilTests.py +++ b/src/engine/SCons/UtilTests.py @@ -1040,54 +1040,63 @@ class UtilTestCase(unittest.TestCase): env_path = os.environ['PATH'] - pathdirs_1234 = [ test.workpath('sub1'), - test.workpath('sub2'), - test.workpath('sub3'), - test.workpath('sub4'), - ] + string.split(env_path, os.pathsep) - - pathdirs_1243 = [ test.workpath('sub1'), - test.workpath('sub2'), - test.workpath('sub4'), - test.workpath('sub3'), - ] + string.split(env_path, os.pathsep) - - os.environ['PATH'] = string.join(pathdirs_1234, os.pathsep) - wi = WhereIs('xxx.exe') - assert wi == test.workpath(sub3_xxx_exe), wi - wi = WhereIs('xxx.exe', pathdirs_1243) - assert wi == test.workpath(sub4_xxx_exe), wi - wi = WhereIs('xxx.exe', string.join(pathdirs_1243, os.pathsep)) - assert wi == test.workpath(sub4_xxx_exe), wi - - wi = WhereIs('xxx.exe',reject = sub3_xxx_exe) - assert wi == test.workpath(sub4_xxx_exe), wi - wi = WhereIs('xxx.exe', pathdirs_1243, reject = sub3_xxx_exe) - assert wi == test.workpath(sub4_xxx_exe), wi - - os.environ['PATH'] = string.join(pathdirs_1243, os.pathsep) - wi = WhereIs('xxx.exe') - assert wi == test.workpath(sub4_xxx_exe), wi - wi = WhereIs('xxx.exe', pathdirs_1234) - assert wi == test.workpath(sub3_xxx_exe), wi - wi = WhereIs('xxx.exe', string.join(pathdirs_1234, os.pathsep)) - assert wi == test.workpath(sub3_xxx_exe), wi - - if sys.platform == 'win32': - wi = WhereIs('xxx', pathext = '') - assert wi is None, wi + try: + pathdirs_1234 = [ test.workpath('sub1'), + test.workpath('sub2'), + test.workpath('sub3'), + test.workpath('sub4'), + ] + string.split(env_path, os.pathsep) + + pathdirs_1243 = [ test.workpath('sub1'), + test.workpath('sub2'), + test.workpath('sub4'), + test.workpath('sub3'), + ] + string.split(env_path, os.pathsep) + + os.environ['PATH'] = string.join(pathdirs_1234, os.pathsep) + wi = WhereIs('xxx.exe') + assert wi == test.workpath(sub3_xxx_exe), wi + wi = WhereIs('xxx.exe', pathdirs_1243) + assert wi == test.workpath(sub4_xxx_exe), wi + wi = WhereIs('xxx.exe', string.join(pathdirs_1243, os.pathsep)) + assert wi == test.workpath(sub4_xxx_exe), wi + + wi = WhereIs('xxx.exe',reject = sub3_xxx_exe) + assert wi == test.workpath(sub4_xxx_exe), wi + wi = WhereIs('xxx.exe', pathdirs_1243, reject = sub3_xxx_exe) + assert wi == test.workpath(sub4_xxx_exe), wi - wi = WhereIs('xxx', pathext = '.exe') + os.environ['PATH'] = string.join(pathdirs_1243, os.pathsep) + wi = WhereIs('xxx.exe') assert wi == test.workpath(sub4_xxx_exe), wi + wi = WhereIs('xxx.exe', pathdirs_1234) + assert wi == test.workpath(sub3_xxx_exe), wi + wi = WhereIs('xxx.exe', string.join(pathdirs_1234, os.pathsep)) + assert wi == test.workpath(sub3_xxx_exe), wi + + if sys.platform == 'win32': + wi = WhereIs('xxx', pathext = '') + assert wi is None, wi + + wi = WhereIs('xxx', pathext = '.exe') + assert wi == test.workpath(sub4_xxx_exe), wi - wi = WhereIs('xxx', path = pathdirs_1234, pathext = '.BAT;.EXE') - assert string.lower(wi) == string.lower(test.workpath(sub3_xxx_exe)), wi + wi = WhereIs('xxx', path = pathdirs_1234, pathext = '.BAT;.EXE') + assert string.lower(wi) == string.lower(test.workpath(sub3_xxx_exe)), wi + + # Test that we return a normalized path even when + # the path contains forward slashes. + forward_slash = test.workpath('') + '/sub3' + wi = WhereIs('xxx', path = forward_slash, pathext = '.EXE') + assert string.lower(wi) == string.lower(test.workpath(sub3_xxx_exe)), wi + + del os.environ['PATH'] + wi = WhereIs('xxx.exe') + assert wi is None, wi - # Test that we return a normalized path even when - # the path contains forward slashes. - forward_slash = test.workpath('') + '/sub3' - wi = WhereIs('xxx', path = forward_slash, pathext = '.EXE') - assert string.lower(wi) == string.lower(test.workpath(sub3_xxx_exe)), wi + finally: + os.environ['PATH'] = env_path + def test_is_valid_construction_var(self): """Testing is_valid_construction_var()""" -- 2.26.2