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