summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Thénault <sylvain.thenault@logilab.fr>2009-03-18 11:10:23 +0100
committerSylvain Thénault <sylvain.thenault@logilab.fr>2009-03-18 11:10:23 +0100
commita4e3901403aa98c112c268e60d1ed2029d5041d3 (patch)
tree90db9d8a8f6fbf0fa002cca296cb65e32179911e
parent8a3b6f56c772b93954a7f9dc83ec07432dab7d79 (diff)
downloadastroid-git-a4e3901403aa98c112c268e60d1ed2029d5041d3.tar.gz
If expression support
--HG-- branch : _ast_compat
-rw-r--r--_nodes_ast.py2
-rw-r--r--_nodes_compiler.py10
-rw-r--r--nodes.py1
-rw-r--r--nodes_as_string.py9
-rw-r--r--utils.py3
5 files changed, 18 insertions, 7 deletions
diff --git a/_nodes_ast.py b/_nodes_ast.py
index 180662e5..ef9a40ca 100644
--- a/_nodes_ast.py
+++ b/_nodes_ast.py
@@ -29,7 +29,7 @@ from _ast import (Assert, Assign, AugAssign,
Ellipsis, Exec,
For,
Global,
- If, Import,
+ If, IfExp, Import,
Lambda, List, ListComp,
Module,
Name,
diff --git a/_nodes_compiler.py b/_nodes_compiler.py
index c8b8d20d..e31664d0 100644
--- a/_nodes_compiler.py
+++ b/_nodes_compiler.py
@@ -56,11 +56,11 @@ except ImportError:
"""dummy Decorators node, shouldn't be used with py < 2.4"""
def __init__(self, nodes=None):
self.nodes = nodes
-
-class With:
- """dummy With node: if we are using py >= 2.5 we will use _ast;
- but we need it for the other astng modules
- """
+
+# dummy python >= 2.5 nodes: if we are using py >= 2.5 we will use _ast;
+# but we need it for the other astng modules
+class With: pass
+class IfExp: pass
# introduced in python 2.5
From.level = 0 # will be overiden by instance attribute with py>=2.5
diff --git a/nodes.py b/nodes.py
index 1a766208..476d3697 100644
--- a/nodes.py
+++ b/nodes.py
@@ -99,6 +99,7 @@ Getattr._astng_fields = ('expr',) # (former value), attr (now attrname), ctx
GenExpr._astng_fields = ('elt', 'generators')
Global._astng_fields = ()
If._astng_fields = ('test', 'body', 'orelse')
+IfExp._astng_fields = ('test', 'body', 'orelse')
Import._astng_fields = ()
Keyword._astng_fields = ('value',)
Lambda._astng_fields = ('args', 'body',)
diff --git a/nodes_as_string.py b/nodes_as_string.py
index 174a33f9..88acd2d3 100644
--- a/nodes_as_string.py
+++ b/nodes_as_string.py
@@ -232,9 +232,16 @@ class AsStringVisitor(ASTVisitor):
"""return an astng.If node as string"""
ifs = ['if %s:\n%s' % (node.test.accept(self), self._stmt_list(node.body))]
if node.orelse:
- ifs.append('else:\n%s' % self._stmt_list(node.orelse) )
+ ifs.append('else:\n%s' % self._stmt_list(node.orelse))
return '\n'.join(ifs)
+ def visit_ifexp(self, node):
+ """return an astng.IfExp node as string"""
+ ifs = ['%s if %s' % (self._stmt_list(node.body), node.test.accept(self))]
+ if node.orelse:
+ ifs.append('else %s' % self._stmt_list(node.orelse))
+ return ' '.join(ifs)
+
def visit_import(self, node):
"""return an astng.Import node as string"""
return 'import %s' % _import_string(node.names)
diff --git a/utils.py b/utils.py
index 9e0a1d01..05b0878f 100644
--- a/utils.py
+++ b/utils.py
@@ -154,6 +154,9 @@ class ASTVisitor(object):
def visit_if(self, node):
"""dummy method for visiting an If node"""
+ def visit_ifexp(self, node):
+ """dummy method for visiting an IfExp node"""
+
def visit_import(self, node):
"""dummy method for visiting an Import node"""