Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/pythonparser
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9022fee42a33
Choose a base ref
...
head repository: m-labs/pythonparser
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 57f3b6772161
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on May 29, 2015

  1. Documentation fix.

    whitequark committed May 29, 2015
    Copy the full SHA
    f1ae99a View commit details
  2. Copy the full SHA
    57f3b67 View commit details
Showing with 31 additions and 21 deletions.
  1. +29 −19 pythonparser/algorithm.py
  2. +2 −2 pythonparser/ast.py
48 changes: 29 additions & 19 deletions pythonparser/algorithm.py
Original file line number Diff line number Diff line change
@@ -24,22 +24,24 @@ class name of the node. So a `Try` node visit function would
def generic_visit(self, node):
"""Called if no explicit visitor function exists for a node."""
for field_name in node._fields:
field_val = getattr(node, field_name)
if isinstance(field_val, list):
for field_val_elt in field_val:
self.visit(field_val_elt)
elif isinstance(field_val, ast.AST):
self.visit(field_val)

def visit(self, node):
"""Visit a node."""
self.visit(getattr(node, field_name))

def _visit_one(self, node):
visit_attr = 'visit_' + type(node).__name__
if hasattr(self, visit_attr):
return getattr(self, visit_attr)(node)
getattr(self, visit_attr)(node)
else:
return self.generic_visit(node)
self.generic_visit(node)

class Transformer(Visitor):
def visit(self, obj):
"""Visit a node or a list of nodes. Other values are ignored"""
if isinstance(obj, list):
for elt in obj:
self._visit_one(elt)
elif isinstance(obj, ast.AST):
self._visit_one(obj)

class Transformer:
"""
A node transformer base class that does a post-order traversal
of the abstract syntax tree while allowing to replace or remove
@@ -64,15 +66,23 @@ class name of the node. So a `Try` node visit function would
def generic_visit(self, node):
"""Called if no explicit visitor function exists for a node."""
for field_name in node._fields:
field_val = getattr(node, field_name)
if isinstance(field_val, list):
setattr(node, field_name,
list(filter(lambda x: x is not None, map(self.visit, field_val))))
elif isinstance(field_val, ast.AST):
setattr(node, field_name, self.visit(field_val))

setattr(node, field_name, self.visit(getattr(node, field_name)))
return node

def _visit_one(self, node):
visit_attr = 'visit_' + type(node).__name__
if hasattr(self, visit_attr):
return getattr(self, visit_attr)(node)
else:
return self.generic_visit(node)

def visit(self, obj):
"""Visit a node or a list of nodes. Other values are ignored"""
if isinstance(obj, list):
return list(filter(lambda x: x is not None, map(self._visit_one, obj)))
elif isinstance(obj, ast.AST):
return self._visit_one(obj)

def compare(left, right, compare_locs=False):
"""
An AST comparison function. Returns ``True`` if all fields in
4 changes: 2 additions & 2 deletions pythonparser/ast.py
Original file line number Diff line number Diff line change
@@ -176,7 +176,7 @@ class comprehension(AST, commonloc):
"""
A single ``for`` list comprehension clause.
:ivar target: (:class:`AST`) the variable(s) bound in comprehension body
:ivar target: (assignable :class:`AST`) the variable(s) bound in comprehension body
:ivar iter: (:class:`AST`) the expression being iterated
:ivar ifs: (list of :class:`AST`) the ``if`` clauses
:ivar for_loc: location of the ``for`` keyword
@@ -550,7 +550,7 @@ class AugAssign(stmt):
The operator-assignment statement, e.g. ``+=``.
:ivar target: (assignable :class:`AST`) left-hand side
:ivar op: (:class`) operator
:ivar op: (:class:`operator`) operator
:ivar value: (:class:`AST`) right-hand side
"""
_fields = ('target', 'op', 'value')