~pythonregexp2.7/python/issue2636-01+09-01-01

« back to all changes in this revision

Viewing changes to Lib/lib2to3/fixes/fix_sys_exc.py

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 00:02:12 UTC
  • mfrom: (39022.1.34 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922000212-7r0q4f4ugiq57jph
Merged in changes from the Atomic Grouping / Possessive Qualifiers branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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())