• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    BM_compatible = True
18    PATTERN = """
19              power< 'sys' trailer< dot='.' attribute=(%s) > >
20              """ % '|'.join("'%s'" % e for e in exc_info)
21
22    def transform(self, node, results):
23        sys_attr = results["attribute"][0]
24        index = Number(self.exc_info.index(sys_attr.value))
25
26        call = Call(Name("exc_info"), prefix=sys_attr.prefix)
27        attr = Attr(Name("sys"), call)
28        attr[1].children[0].prefix = results["dot"].prefix
29        attr.append(Subscript(index))
30        return Node(syms.power, attr, prefix=node.prefix)
31