summaryrefslogtreecommitdiff
path: root/Lib/ast.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-10-08 00:47:45 +0000
committerRaymond Hettinger <python@rcn.com>2010-10-08 00:47:45 +0000
commitbc95973b51abadc84960e7836ce313f12cf515cf (patch)
tree71fed836ea6caa2f4f61f2041f2b1570a94de6f5 /Lib/ast.py
parent4f3abb0f091b14e09d257ee4eb40632471177d6d (diff)
downloadcpython-git-bc95973b51abadc84960e7836ce313f12cf515cf.tar.gz
Fix handling on negative numbers in ast.literal_eval().
Diffstat (limited to 'Lib/ast.py')
-rw-r--r--Lib/ast.py20
1 files changed, 13 insertions, 7 deletions
diff --git a/Lib/ast.py b/Lib/ast.py
index aab61d7634..4b2063bd78 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -65,19 +65,25 @@ def literal_eval(node_or_string):
elif isinstance(node, Name):
if node.id in _safe_names:
return _safe_names[node.id]
+ elif isinstance(node, UnaryOp) and \
+ isinstance(node.op, (UAdd, USub)) and \
+ isinstance(node.operand, (Num, UnaryOp, BinOp)):
+ operand = _convert(node.operand)
+ if isinstance(node.op, UAdd):
+ return + operand
+ else:
+ return - operand
elif isinstance(node, BinOp) and \
isinstance(node.op, (Add, Sub)) and \
- isinstance(node.right, Num) and \
- isinstance(node.right.n, complex) and \
- isinstance(node.left, Num) and \
- isinstance(node.left.n, (int, float)):
- left = node.left.n
- right = node.right.n
+ isinstance(node.right, (Num, UnaryOp, BinOp)) and \
+ isinstance(node.left, (Num, UnaryOp, BinOp)):
+ left = _convert(node.left)
+ right = _convert(node.right)
if isinstance(node.op, Add):
return left + right
else:
return left - right
- raise ValueError('malformed string')
+ raise ValueError('malformed node or string: ' + repr(node))
return _convert(node_or_string)