Provide a better error message when a construction variable expansion is a Python...
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Thu, 29 Jan 2004 03:35:57 +0000 (03:35 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Thu, 29 Jan 2004 03:35:57 +0000 (03:35 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@887 fdb21ef1-2011-0410-befe-b5e4ea1792b1

src/CHANGES.txt
src/engine/SCons/Util.py
src/engine/SCons/UtilTests.py
test/errors.py

index 26dd7efa7fc748a4e009bd617ba0eb6da1543406..8b14bf4b34b8838b09dd097f038a17d1b9abe2a1 100644 (file)
@@ -144,6 +144,9 @@ RELEASE 0.95 - XXX
   - Fix TestCmd.py, runtest.py and specific tests to accomodate being
     run from directories whose paths include white space.
 
+  - Provide a more useful error message if a construction variable
+    expansion contains a syntax error during evaluation.
+
   From Vincent Risi:
 
   - Add support for the bcc32, ilink32 and tlib Borland tools.
index 0af98fd3919c5acd49a8cb2866e84395ac253ee1..6d95a4e27ef51b8f58f0f1504f6af877694d4b65 100644 (file)
@@ -40,7 +40,8 @@ import sys
 import types
 import UserDict
 import UserList
-import SCons.Node
+
+import SCons.Errors
 
 try:
     from UserString import UserString
@@ -556,6 +557,8 @@ def scons_subst(strSubst, env, mode=SUBST_RAW, target=None, source=None, dict=No
                             s = eval(key, self.gvars, lvars)
                         except (IndexError, NameError, TypeError):
                             return ''
+                        except (SyntaxError):
+                            raise SCons.Errors.UserError, "Syntax error trying to evaluate `%s'" % s
                         else:
                             # Before re-expanding the result, handle
                             # recursive expansion by copying the local
@@ -690,6 +693,8 @@ def scons_subst_list(strSubst, env, mode=SUBST_RAW, target=None, source=None, di
                             s = eval(key, self.gvars, lvars)
                         except (IndexError, NameError, TypeError):
                             return
+                        except (SyntaxError):
+                            raise SCons.Errors.UserError, "Syntax error trying to evaluate `%s'" % s
                         else:
                             # Before re-expanding the result, handle
                             # recursive expansion by copying the local
index ca5f649c5da380bea64eb8f32bf9162e5c32148b..fb4ec09fdc5f89f564a369f461da80b40979df35 100644 (file)
@@ -32,6 +32,8 @@ import unittest
 from SCons.Util import *
 import TestCmd
 
+import SCons.Errors
+
 class OutBuffer:
     def __init__(self):
         self.buffer = ""
@@ -410,6 +412,14 @@ class UtilTestCase(unittest.TestCase):
                              env, target=MyNode('t'), source=MyNode('s'))
         assert newcom == "test foo baz s t", newcom
 
+        # Test that we handle syntax errors during expansion as expected.
+        try:
+            scons_subst('$foo.bar.3.0', env)
+        except SCons.Errors.UserError, e:
+            assert str(e) == "Syntax error trying to evaluate `$foo.bar.3.0'", e
+        else:
+            raise AssertionError, "did not catch expected UserError"
+
         # Test returning a function.
         #env = DummyEnv({'FUNCTION' : foo})
         #func = scons_subst("$FUNCTION", env, mode=SUBST_RAW, call=None)
@@ -732,6 +742,14 @@ class UtilTestCase(unittest.TestCase):
             del subst_list_cases[:4]
         assert failed == 0, "%d subst() mode cases failed" % failed
 
+        # Test that we handle syntax errors during expansion as expected.
+        try:
+            scons_subst_list('$foo.bar.3.0', env)
+        except SCons.Errors.UserError, e:
+            assert str(e) == "Syntax error trying to evaluate `$foo.bar.3.0'", e
+        else:
+            raise AssertionError, "did not catch expected SyntaxError"
+
     def test_splitext(self):
         assert splitext('foo') == ('foo','')
         assert splitext('foo.bar') == ('foo','.bar')
index d87b41abc193676f7bab945df1d26330c32ab176..4c493f6952a3f70a668f72095f53d73cefb34b08 100644 (file)
@@ -182,4 +182,27 @@ test.run(status=2, stderr="scons: \\*\\*\\* \\[one.out\\] Error 2\n")
 
 
 
+# Test syntax errors when trying to expand construction variables.
+test.write('SConstruct', """\
+env = Environment()
+env.subst('$foo.bar.3.0')
+""")
+
+test.run(status=2, stderr="""
+scons: \*\*\* Syntax error trying to evaluate `\$foo\.bar\.3\.0'
+File "SConstruct", line 2, in \?
+""")
+
+test.write('SConstruct', """\
+env = Environment()
+env.subst_list('$foo.3.0.x')
+""")
+
+test.run(status=2, stderr="""
+scons: \*\*\* Syntax error trying to evaluate `\$foo\.3\.0\.x'
+File "SConstruct", line 2, in \?
+""")
+
+
+
 test.pass_test()