<s> """<STR_LIT>""" <EOL> from . . import pytree <EOL> from . . pgen2 import token <EOL> from . . import fixer_base <EOL> from . . fixer_util import Assign , Attr , Name , is_tuple , is_list , syms <EOL> def find_excepts ( nodes ) : <EOL> for i , n in enumerate ( nodes ) : <EOL> if n . type == syms . except_clause : <EOL> if n . children [ <NUM_LIT:0> ] . value == u'<STR_LIT>' : <EOL> yield ( n , nodes [ i + <NUM_LIT:2> ] ) <EOL> class FixExcept ( fixer_base . BaseFix ) : <EOL> BM_compatible = True <EOL> PATTERN = """<STR_LIT>""" <EOL> def transform ( self , node , results ) : <EOL> syms = self . syms <EOL> tail = [ n . clone ( ) for n in results [ "<STR_LIT>" ] ] <EOL> try_cleanup = [ ch . clone ( ) for ch in results [ "<STR_LIT>" ] ] <EOL> for except_clause , e_suite in find_excepts ( try_cleanup ) : <EOL> if len ( except_clause . children ) == <NUM_LIT:4> : <EOL> ( E , comma , N ) = except_clause . children [ <NUM_LIT:1> : <NUM_LIT:4> ] <EOL> comma . replace ( Name ( u"<STR_LIT>" , prefix = u"<STR_LIT:U+0020>" ) ) <EOL> if N . type != token . NAME : <EOL> new_N = Name ( self . new_name ( ) , prefix = u"<STR_LIT:U+0020>" ) <EOL> target = N . clone ( ) <EOL> target . prefix = u"<STR_LIT>" <EOL> N . replace ( new_N ) <EOL> new_N = new_N . clone ( ) <EOL> suite_stmts = e_suite . children <EOL> for i , stmt in enumerate ( suite_stmts ) : <EOL> if isinstance ( stmt , pytree . Node ) : <EOL> break <EOL> if is_tuple ( N ) or is_list ( N ) : <EOL> assign = Assign ( target , Attr ( new_N , Name ( u'<STR_LIT:args>' ) ) ) <EOL> else : <EOL> assign = Assign ( target , new_N ) <EOL> for child in reversed ( suite_stmts [ : i ] ) : <EOL> e_suite . insert_child ( <NUM_LIT:0> , child ) <EOL> e_suite . insert_child ( i , assign ) <EOL> elif N . prefix == u"<STR_LIT>" : <EOL> N . prefix = u"<STR_LIT:U+0020>" <EOL> children = [ c . clone ( ) for c in node . children [ : <NUM_LIT:3> ] ] + try_cleanup + tail <EOL> return pytree . Node ( node . type , children ) </s>
