From ec55e86c612626e75389ed2975416f0510214164 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Wed, 25 Aug 2004 05:17:37 +0000 Subject: [PATCH] Let's catch SyntaxError and turn it into an appropriate ValueError. --- src/utils.py | 5 ++++- test/test_utils.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) 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)),