diff options
Diffstat (limited to 'Lib/ast.py')
-rw-r--r-- | Lib/ast.py | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/Lib/ast.py b/Lib/ast.py index 6d92eddddc..fd5dfdba67 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -29,12 +29,12 @@ from _ast import * from _ast import __version__ -def parse(expr, filename='<unknown>', mode='exec'): +def parse(source, filename='<unknown>', mode='exec'): """ - Parse an expression into an AST node. - Equivalent to compile(expr, filename, mode, PyCF_ONLY_AST). + Parse the source into an AST node. + Equivalent to compile(source, filename, mode, PyCF_ONLY_AST). """ - return compile(expr, filename, mode, PyCF_ONLY_AST) + return compile(source, filename, mode, PyCF_ONLY_AST) def literal_eval(node_or_string): @@ -64,6 +64,18 @@ def literal_eval(node_or_string): elif isinstance(node, Name): if node.id in _safe_names: return _safe_names[node.id] + 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, long, float)): + left = node.left.n + right = node.right.n + if isinstance(node.op, Add): + return left + right + else: + return left - right raise ValueError('malformed string') return _convert(node_or_string) @@ -140,8 +152,6 @@ def increment_lineno(node, n=1): Increment the line number of each node in the tree starting at *node* by *n*. This is useful to "move code" to a different location in a file. """ - if 'lineno' in node._attributes: - node.lineno = getattr(node, 'lineno', 0) + n for child in walk(node): if 'lineno' in child._attributes: child.lineno = getattr(child, 'lineno', 0) + n @@ -192,9 +202,9 @@ def get_docstring(node, clean=True): def walk(node): """ - Recursively yield all child nodes of *node*, in no specified order. This is - useful if you only want to modify nodes in place and don't care about the - context. + Recursively yield all descendant nodes in the tree starting at *node* + (including *node* itself), in no specified order. This is useful if you + only want to modify nodes in place and don't care about the context. """ from collections import deque todo = deque([node]) |