import types
from compiler import ast

class BaseVisitor:

    def visit(self, unused_node):
        "method is really overridden by compiler.visitor.ASTVisitor"
        raise AssertionError('Unreachable')

    def visitChildren(self, n):
        for c in n.getChildNodes():
            self.visit(c)     

def try_if_exclusive(stmt_node1, stmt_node2):
    """return true if the statements are in exclusive parts of if/elif/else
    or try/finally/else"""
    
    try:
        parent = stmt_node1.parent.parent
        if parent == stmt_node2.parent.parent:
            if isinstance(parent, ast.If):
                parts = [code for test, code in parent.tests]
                parts.append(parent.else_)
                for part in parts:
                    if stmt_node1 in part.nodes:
                        return stmt_node2 not in part.nodes
            if isinstance(parent, ast.TryExcept):
                parts = []
                parts.extend(parent.body.nodes)
                parts.extend(parent.else_.nodes)
                if stmt_node1 in parts and \
                   stmt_node2 in parts:
                    return None
                return 1
    except AttributeError:
        pass
    return None

def parents(obj):
    class Parents:
        def __init__(self, start):
            self.next = start
        def __call__(self):
            retval = self.next.parent
            self.next = retval
            return retval
    return iter(Parents(obj), None)

def enclosing_scopes(scopes, node):
    result = []
    n = node
    while n:
        try:
            result.append(scopes[n])
        except KeyError:
            pass
        n = n.parent
    return result

def type_filter(seq, *classes):
    return [s for s in seq if isinstance(s, classes)]

# http://mail.python.org/pipermail/python-list/2000-December/023319.html
# Fredric Lundh
def flatten(seq):
    res = []
    for item in seq:
        if type(item) in (types.TupleType, types.ListType):
            res.extend(flatten(item))
        else:
            res.append(item)
    return res


@@ -454,29 +454,16 @@
 %22%22%0A    %0A
-    try:%0A    
     pare
@@ -496,20 +496,16 @@
 ent%0A    
-  
-  
 if paren
@@ -535,36 +535,32 @@
 parent:%0A        
-    
 if isinstance(pa
@@ -578,36 +578,32 @@
 f):%0A            
- 
-   
 parts = %5Bcode fo
@@ -632,20 +632,16 @@
 .tests%5D%0A
-    
         
@@ -679,28 +679,24 @@
             
-    
 for part in 
@@ -710,36 +710,32 @@
                 
-   
- 
 if stmt_node1 in
@@ -747,20 +747,16 @@
 .nodes:%0A
-    
         
@@ -807,28 +807,24 @@
 des%0A        
-  
-  
 if isinstanc
@@ -853,36 +853,32 @@
 t):%0A            
-    
 parts = %5B%5D%0A     
@@ -876,36 +876,32 @@
  %5B%5D%0A            
- 
-   
 parts.extend(par
@@ -908,36 +908,32 @@
 ent.body.nodes)%0A
-    
             part
@@ -965,36 +965,32 @@
 es)%0A            
-    
 if stmt_node1 in
@@ -1017,20 +1017,16 @@
         
-   
- 
 stmt_nod
@@ -1050,28 +1050,24 @@
             
-    
 return None%0A
@@ -1074,28 +1074,24 @@
             
-  
-  
 return 1%0A   
@@ -1091,48 +1091,8 @@
 n 1%0A
-    except AttributeError:%0A        pass%0A
     

remove unnecessary try/except