home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / fixes / fix_except.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  2.9 KB  |  77 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Fixer for except statements with named exceptions.
  5.  
  6. The following cases will be converted:
  7.  
  8. - "except E, T:" where T is a name:
  9.  
  10.     except E as T:
  11.  
  12. - "except E, T:" where T is not a name, tuple or list:
  13.  
  14.         except E as t:
  15.             T = t
  16.  
  17.     This is done because the target of an "except" clause must be a
  18.     name.
  19.  
  20. - "except E, T:" where T is a tuple or list literal:
  21.  
  22.         except E as t:
  23.             T = t.args
  24. '''
  25. from  import pytree
  26. from pgen2 import token
  27. from  import fixer_base
  28. from fixer_util import Assign, Attr, Name, is_tuple, is_list
  29.  
  30. def find_excepts(nodes):
  31.     for i, n in enumerate(nodes):
  32.         if isinstance(n, pytree.Node):
  33.             if n.children[0].value == 'except':
  34.                 yield (n, nodes[i + 2])
  35.             
  36.         n.children[0].value == 'except'
  37.     
  38.  
  39.  
  40. class FixExcept(fixer_base.BaseFix):
  41.     PATTERN = "\n    try_stmt< 'try' ':' suite\n                  cleanup=(except_clause ':' suite)+\n                  tail=(['except' ':' suite]\n                        ['else' ':' suite]\n                        ['finally' ':' suite]) >\n    "
  42.     
  43.     def transform(self, node, results):
  44.         syms = self.syms
  45.         tail = [ n.clone() for n in results['tail'] ]
  46.         try_cleanup = [ ch.clone() for ch in results['cleanup'] ]
  47.         for except_clause, e_suite in find_excepts(try_cleanup):
  48.             if len(except_clause.children) == 4:
  49.                 (E, comma, N) = except_clause.children[1:4]
  50.                 comma.replace(Name('as', prefix = ' '))
  51.                 if N.type != token.NAME:
  52.                     new_N = Name(self.new_name(), prefix = ' ')
  53.                     target = N.clone()
  54.                     target.set_prefix('')
  55.                     N.replace(new_N)
  56.                     new_N = new_N.clone()
  57.                     suite_stmts = e_suite.children
  58.                     for i, stmt in enumerate(suite_stmts):
  59.                         if isinstance(stmt, pytree.Node):
  60.                             break
  61.                             continue
  62.                         []
  63.                     
  64.                     for child in reversed(suite_stmts[:i]):
  65.                         e_suite.insert_child(0, child)
  66.                     
  67.                     e_suite.insert_child(i, assign)
  68.                 elif N.get_prefix() == '':
  69.                     N.set_prefix(' ')
  70.                 
  71.             N.type != token.NAME
  72.         
  73.         children = [ c.clone() for c in node.children[:3] ] + try_cleanup + tail
  74.         return pytree.Node(node.type, children)
  75.  
  76.  
  77.