PageRenderTime 484ms CodeModel.GetById 161ms app.highlight 104ms RepoModel.GetById 218ms app.codeStats 0ms

/Lib/lib2to3/fixes/fix_sys_exc.py

http://unladen-swallow.googlecode.com/
Python | 29 lines | 17 code | 3 blank | 9 comment | 0 complexity | 2b412acd29c54b0101163bb8be2ab5c7 MD5 | raw file
 1"""Fixer for sys.exc_{type, value, traceback}
 2
 3sys.exc_type -> sys.exc_info()[0]
 4sys.exc_value -> sys.exc_info()[1]
 5sys.exc_traceback -> sys.exc_info()[2]
 6"""
 7
 8# By Jeff Balogh and Benjamin Peterson
 9
10# Local imports
11from .. import fixer_base
12from ..fixer_util import Attr, Call, Name, Number, Subscript, Node, syms
13
14class FixSysExc(fixer_base.BaseFix):
15    # This order matches the ordering of sys.exc_info().
16    exc_info = ["exc_type", "exc_value", "exc_traceback"]
17    PATTERN = """
18              power< 'sys' trailer< dot='.' attribute=(%s) > >
19              """ % '|'.join("'%s'" % e for e in exc_info)
20
21    def transform(self, node, results):
22        sys_attr = results["attribute"][0]
23        index = Number(self.exc_info.index(sys_attr.value))
24
25        call = Call(Name("exc_info"), prefix=sys_attr.get_prefix())
26        attr = Attr(Name("sys"), call)
27        attr[1].children[0].set_prefix(results["dot"].get_prefix())
28        attr.append(Subscript(index))
29        return Node(syms.power, attr, prefix=node.get_prefix())