/Lib/lib2to3/fixes/fix_sys_exc.py

http://unladen-swallow.googlecode.com/ · Python · 29 lines · 15 code · 5 blank · 9 comment · 1 complexity · 2b412acd29c54b0101163bb8be2ab5c7 MD5 · raw file

  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. PATTERN = """
  14. power< 'sys' trailer< dot='.' attribute=(%s) > >
  15. """ % '|'.join("'%s'" % e for e in exc_info)
  16. def transform(self, node, results):
  17. sys_attr = results["attribute"][0]
  18. index = Number(self.exc_info.index(sys_attr.value))
  19. call = Call(Name("exc_info"), prefix=sys_attr.get_prefix())
  20. attr = Attr(Name("sys"), call)
  21. attr[1].children[0].set_prefix(results["dot"].get_prefix())
  22. attr.append(Subscript(index))
  23. return Node(syms.power, attr, prefix=node.get_prefix())