mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 05:09:23 +01:00
Let's catch SyntaxError and turn it into an appropriate ValueError.
This commit is contained in:
parent
6b72890679
commit
ec55e86c61
@ -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.
|
"""Evaluates s, safely. Useful for turning strings into tuples/lists/etc.
|
||||||
without unsafely using eval()."""
|
without unsafely using eval()."""
|
||||||
#print s, '::', stackTrace()
|
#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
|
nodes = compiler.parse(s).node.nodes
|
||||||
if not nodes:
|
if not nodes:
|
||||||
if node.__class__ is compiler.ast.Module:
|
if node.__class__ is compiler.ast.Module:
|
||||||
|
@ -341,6 +341,10 @@ class UtilsTest(SupyTestCase):
|
|||||||
for s in ['lambda: 2', 'import foo', 'foo.bar']:
|
for s in ['lambda: 2', 'import foo', 'foo.bar']:
|
||||||
self.assertRaises(ValueError, utils.safeEval, s)
|
self.assertRaises(ValueError, utils.safeEval, s)
|
||||||
|
|
||||||
|
|
||||||
|
def testSafeEvalTurnsSyntaxErrorIntoValueError(self):
|
||||||
|
self.assertRaises(ValueError, utils.safeEval, '/usr/local/')
|
||||||
|
|
||||||
def testLines(self):
|
def testLines(self):
|
||||||
L = ['foo', 'bar', '#baz', ' ', 'biff']
|
L = ['foo', 'bar', '#baz', ' ', 'biff']
|
||||||
self.assertEqual(list(utils.nonEmptyLines(L)),
|
self.assertEqual(list(utils.nonEmptyLines(L)),
|
||||||
|
Loading…
Reference in New Issue
Block a user