fix_sys_exc.py 1.0 KB

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