diff --git a/src/utils.py b/src/utils.py index e42fafb34..e1df22d67 100755 --- a/src/utils.py +++ b/src/utils.py @@ -491,7 +491,10 @@ def safeEval(s, namespace={'True': True, 'False': False, 'None': None}): """Evaluates s, safely. Useful for turning strings into tuples/lists/etc. without unsafely using eval().""" #print s, '::', stackTrace() - node = compiler.parse(s) + try: + node = compiler.parse(s) + except SyntaxError, e: + raise ValueError, 'Invalid string: %s.' % e nodes = compiler.parse(s).node.nodes if not nodes: if node.__class__ is compiler.ast.Module: diff --git a/test/test_utils.py b/test/test_utils.py index 2ded5b73f..cbebe4e79 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -341,6 +341,10 @@ class UtilsTest(SupyTestCase): for s in ['lambda: 2', 'import foo', 'foo.bar']: self.assertRaises(ValueError, utils.safeEval, s) + + def testSafeEvalTurnsSyntaxErrorIntoValueError(self): + self.assertRaises(ValueError, utils.safeEval, '/usr/local/') + def testLines(self): L = ['foo', 'bar', '#baz', ' ', 'biff'] self.assertEqual(list(utils.nonEmptyLines(L)),