fix_raise.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """Fixer for 'raise E, V, T'
  2. raise -> raise
  3. raise E -> raise E
  4. raise E, V -> raise E(V)
  5. raise E, V, T -> raise E(V).with_traceback(T)
  6. raise E, None, T -> raise E.with_traceback(T)
  7. raise (((E, E'), E''), E'''), V -> raise E(V)
  8. raise "foo", V, T -> warns about string exceptions
  9. CAVEATS:
  10. 1) "raise E, V" will be incorrectly translated if V is an exception
  11. instance. The correct Python 3 idiom is
  12. raise E from V
  13. but since we can't detect instance-hood by syntax alone and since
  14. any client code would have to be changed as well, we don't automate
  15. this.
  16. """
  17. # Author: Collin Winter
  18. # Local imports
  19. from .. import pytree
  20. from ..pgen2 import token
  21. from .. import fixer_base
  22. from ..fixer_util import Name, Call, Attr, ArgList, is_tuple
  23. class FixRaise(fixer_base.BaseFix):
  24. BM_compatible = True
  25. PATTERN = """
  26. raise_stmt< 'raise' exc=any [',' val=any [',' tb=any]] >
  27. """
  28. def transform(self, node, results):
  29. syms = self.syms
  30. exc = results["exc"].clone()
  31. if exc.type == token.STRING:
  32. msg = "Python 3 does not support string exceptions"
  33. self.cannot_convert(node, msg)
  34. return
  35. # Python 2 supports
  36. # raise ((((E1, E2), E3), E4), E5), V
  37. # as a synonym for
  38. # raise E1, V
  39. # Since Python 3 will not support this, we recurse down any tuple
  40. # literals, always taking the first element.
  41. if is_tuple(exc):
  42. while is_tuple(exc):
  43. # exc.children[1:-1] is the unparenthesized tuple
  44. # exc.children[1].children[0] is the first element of the tuple
  45. exc = exc.children[1].children[0].clone()
  46. exc.prefix = " "
  47. if "val" not in results:
  48. # One-argument raise
  49. new = pytree.Node(syms.raise_stmt, [Name("raise"), exc])
  50. new.prefix = node.prefix
  51. return new
  52. val = results["val"].clone()
  53. if is_tuple(val):
  54. args = [c.clone() for c in val.children[1:-1]]
  55. else:
  56. val.prefix = ""
  57. args = [val]
  58. if "tb" in results:
  59. tb = results["tb"].clone()
  60. tb.prefix = ""
  61. e = exc
  62. # If there's a traceback and None is passed as the value, then don't
  63. # add a call, since the user probably just wants to add a
  64. # traceback. See issue #9661.
  65. if val.type != token.NAME or val.value != "None":
  66. e = Call(exc, args)
  67. with_tb = Attr(e, Name('with_traceback')) + [ArgList([tb])]
  68. new = pytree.Node(syms.simple_stmt, [Name("raise")] + with_tb)
  69. new.prefix = node.prefix
  70. return new
  71. else:
  72. return pytree.Node(syms.raise_stmt,
  73. [Name("raise"), Call(exc, args)],
  74. prefix=node.prefix)