fix_raise.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """Fixer for 'raise E, V'
  2. From Armin Ronacher's ``python-modernize``.
  3. raise -> raise
  4. raise E -> raise E
  5. raise E, 5 -> raise E(5)
  6. raise E, 5, T -> raise E(5).with_traceback(T)
  7. raise E, None, T -> raise E.with_traceback(T)
  8. raise (((E, E'), E''), E'''), 5 -> raise E(5)
  9. raise "foo", V, T -> warns about string exceptions
  10. raise E, (V1, V2) -> raise E(V1, V2)
  11. raise E, (V1, V2), T -> raise E(V1, V2).with_traceback(T)
  12. CAVEATS:
  13. 1) "raise E, V, T" cannot be translated safely in general. If V
  14. is not a tuple or a (number, string, None) literal, then:
  15. raise E, V, T -> from future.utils import raise_
  16. raise_(E, V, T)
  17. """
  18. # Author: Collin Winter, Armin Ronacher, Mark Huang
  19. # Local imports
  20. from lib2to3 import pytree, fixer_base
  21. from lib2to3.pgen2 import token
  22. from lib2to3.fixer_util import Name, Call, is_tuple, Comma, Attr, ArgList
  23. from libfuturize.fixer_util import touch_import_top
  24. class FixRaise(fixer_base.BaseFix):
  25. BM_compatible = True
  26. PATTERN = """
  27. raise_stmt< 'raise' exc=any [',' val=any [',' tb=any]] >
  28. """
  29. def transform(self, node, results):
  30. syms = self.syms
  31. exc = results["exc"].clone()
  32. if exc.type == token.STRING:
  33. msg = "Python 3 does not support string exceptions"
  34. self.cannot_convert(node, msg)
  35. return
  36. # Python 2 supports
  37. # raise ((((E1, E2), E3), E4), E5), V
  38. # as a synonym for
  39. # raise E1, V
  40. # Since Python 3 will not support this, we recurse down any tuple
  41. # literals, always taking the first element.
  42. if is_tuple(exc):
  43. while is_tuple(exc):
  44. # exc.children[1:-1] is the unparenthesized tuple
  45. # exc.children[1].children[0] is the first element of the tuple
  46. exc = exc.children[1].children[0].clone()
  47. exc.prefix = u" "
  48. if "tb" in results:
  49. tb = results["tb"].clone()
  50. else:
  51. tb = None
  52. if "val" in results:
  53. val = results["val"].clone()
  54. if is_tuple(val):
  55. # Assume that exc is a subclass of Exception and call exc(*val).
  56. args = [c.clone() for c in val.children[1:-1]]
  57. exc = Call(exc, args)
  58. elif val.type in (token.NUMBER, token.STRING):
  59. # Handle numeric and string literals specially, e.g.
  60. # "raise Exception, 5" -> "raise Exception(5)".
  61. val.prefix = u""
  62. exc = Call(exc, [val])
  63. elif val.type == token.NAME and val.value == u"None":
  64. # Handle None specially, e.g.
  65. # "raise Exception, None" -> "raise Exception".
  66. pass
  67. else:
  68. # val is some other expression. If val evaluates to an instance
  69. # of exc, it should just be raised. If val evaluates to None,
  70. # a default instance of exc should be raised (as above). If val
  71. # evaluates to a tuple, exc(*val) should be called (as
  72. # above). Otherwise, exc(val) should be called. We can only
  73. # tell what to do at runtime, so defer to future.utils.raise_(),
  74. # which handles all of these cases.
  75. touch_import_top(u"future.utils", u"raise_", node)
  76. exc.prefix = u""
  77. args = [exc, Comma(), val]
  78. if tb is not None:
  79. args += [Comma(), tb]
  80. return Call(Name(u"raise_"), args)
  81. if tb is not None:
  82. tb.prefix = ""
  83. exc_list = Attr(exc, Name('with_traceback')) + [ArgList([tb])]
  84. else:
  85. exc_list = [exc]
  86. return pytree.Node(syms.raise_stmt,
  87. [Name(u"raise")] + exc_list,
  88. prefix=node.prefix)