|
1 """Fixer for sys.exc_{type, value, traceback} |
|
2 |
|
3 sys.exc_type -> sys.exc_info()[0] |
|
4 sys.exc_value -> sys.exc_info()[1] |
|
5 sys.exc_traceback -> sys.exc_info()[2] |
|
6 """ |
|
7 |
|
8 # By Jeff Balogh and Benjamin Peterson |
|
9 |
|
10 # Local imports |
|
11 from .. import fixer_base |
|
12 from ..fixer_util import Attr, Call, Name, Number, Subscript, Node, syms |
|
13 |
|
14 class 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()) |