From: stevenknight Date: Sat, 22 Feb 2003 16:17:24 +0000 (+0000) Subject: Warn when the user tries to set TARGET[S] or SOURCE[S] in an Environment. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=380163de04b3c5f0d3281c4671b7645bcb2f002e;p=scons.git Warn when the user tries to set TARGET[S] or SOURCE[S] in an Environment. git-svn-id: http://scons.tigris.org/svn/scons/trunk@597 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- diff --git a/doc/man/scons.1 b/doc/man/scons.1 index f026b726..f76bc015 100644 --- a/doc/man/scons.1 +++ b/doc/man/scons.1 @@ -2460,6 +2460,16 @@ The prefix used for shared object file names. .IP SHOBJSUFFIX The suffix used for shared object file names. +.IP SOURCE +A reserved variable name +that may not be set or used in a construction environment. +(See "Variable Substitution," below.) + +.IP SOURCES +A reserved variable name +that may not be set or used in a construction environment. +(See "Variable Substitution," below.) + .IP SPAWN A command interpreter function that will be called to execute command line strings. The function must expect 4 arguments: @@ -2501,6 +2511,16 @@ The command line used to call the tar archiver. .IP TARFLAGS General options passed to the tar archiver. +.IP TARGET +A reserved variable name +that may not be set or used in a construction environment. +(See "Variable Substitution," below.) + +.IP TARGETS +A reserved variable name +that may not be set or used in a construction environment. +(See "Variable Substitution," below.) + .IP TARSUFFIX The suffix used for tar file names. @@ -3676,6 +3696,9 @@ first source if multiple sources are being built. .IP SOURCES The file names of the sources of the build command. +(Note that the above variables are reserved +and may not be set in a construction environment.) + .LP For example, given the construction variable CC='cc', targets=['foo'], and sources=['foo.c', 'bar.c']: diff --git a/src/CHANGES.txt b/src/CHANGES.txt index 64708001..9befe249 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -28,6 +28,9 @@ RELEASE 0.12 - XXX - Make the error message the same as other build errors when there's a problem unlinking a target file in preparation for it being built. + - Make TARGET, TARGETS, SOURCE and SOURCES reserved variable names and + warn if the user tries to set them in a construction environment. + RELEASE 0.11 - Tue, 11 Feb 2003 05:24:33 -0600 diff --git a/src/engine/SCons/Environment.py b/src/engine/SCons/Environment.py index 54c91bb9..68571097 100644 --- a/src/engine/SCons/Environment.py +++ b/src/engine/SCons/Environment.py @@ -315,7 +315,13 @@ class Environment: return dlist def __setitem__(self, key, value): - if key == 'BUILDERS': + if key == 'TARGET' or \ + key == 'TARGETS' or \ + key == 'SOURCE' or \ + key == 'SOURCES': + SCons.Warnings.warn(SCons.Warnings.ReservedVariableWarning, + "Ignoring attempt to set reserved variable `%s'" % key) + elif key == 'BUILDERS': try: bd = self._dict[key] for k in bd.keys(): diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index e3139394..1163545c 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -332,6 +332,26 @@ class EnvironmentTestCase(unittest.TestCase): for tnode in tgt: assert tnode.builder == InstallBuilder + def test_ReservedVariables(self): + """Test generation of warnings when reserved variable names + are set in an environment.""" + + SCons.Warnings.enableWarningClass(SCons.Warnings.ReservedVariableWarning) + old = SCons.Warnings.warningAsException(1) + + try: + env4 = Environment() + for kw in ['TARGET', 'TARGETS', 'SOURCE', 'SOURCES']: + exc_caught = None + try: + env4[kw] = 'xyzzy' + except SCons.Warnings.ReservedVariableWarning: + exc_caught = 1 + assert exc_caught, "Did not catch ReservedVariableWarning for `%s'" % kw + assert not env4.has_key(kw), "`%s' variable was incorrectly set" % kw + finally: + SCons.Warnings.warningAsException(old) + def test_Replace(self): """Test replacing construction variables in an Environment diff --git a/src/engine/SCons/Warnings.py b/src/engine/SCons/Warnings.py index 30f1b001..6d75c062 100644 --- a/src/engine/SCons/Warnings.py +++ b/src/engine/SCons/Warnings.py @@ -43,6 +43,9 @@ class DependencyWarning(Warning): class CorruptSConsignWarning(Warning): pass +class ReservedVariableWarning(Warning): + pass + _warningAsException = 0 # The below is a list of 2-tuples. The first element is a class object. @@ -62,8 +65,11 @@ def enableWarningClass(clazz): _enabled.insert(0, (clazz, 1)) def warningAsException(flag=1): + """Turn warnings into exceptions. Returns the old value of the flag.""" global _warningAsException + old = _warningAsException _warningAsException = flag + return old def warn(clazz, *args): global _enabled, _warningAsException, _warningOut diff --git a/src/engine/SCons/WarningsTests.py b/src/engine/SCons/WarningsTests.py index e04ffc32..1016fdbe 100644 --- a/src/engine/SCons/WarningsTests.py +++ b/src/engine/SCons/WarningsTests.py @@ -57,16 +57,23 @@ class WarningsTestCase(unittest.TestCase): SCons.Warnings._warningAsException = 0 SCons.Warnings.enableWarningClass(SCons.Warnings.Warning) - SCons.Warnings.warningAsException() + old = SCons.Warnings.warningAsException() + assert old == 0, old + exc_caught = 0 try: SCons.Warnings.warn(SCons.Warnings.Warning, "Foo") except: - pass - else: - assert 0 + exc_caught = 1 + assert exc_caught == 1 - SCons.Warnings.warningAsException(0) - SCons.Warnings.warn(SCons.Warnings.Warning, "Foo") + old = SCons.Warnings.warningAsException(old) + assert old == 1, old + exc_caught = 0 + try: + SCons.Warnings.warn(SCons.Warnings.Warning, "Foo") + except: + exc_caught = 1 + assert exc_caught == 0 def test_Disable(self): """Test disabling/enabling warnings."""