fix_print.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright 2006 Google, Inc. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Fixer for print.
  4. Change:
  5. "print" into "print()"
  6. "print ..." into "print(...)"
  7. "print(...)" not changed
  8. "print ... ," into "print(..., end=' ')"
  9. "print >>x, ..." into "print(..., file=x)"
  10. No changes are applied if print_function is imported from __future__
  11. """
  12. # Local imports
  13. from lib2to3 import patcomp, pytree, fixer_base
  14. from lib2to3.pgen2 import token
  15. from lib2to3.fixer_util import Name, Call, Comma, String
  16. # from libmodernize import add_future
  17. parend_expr = patcomp.compile_pattern(
  18. """atom< '(' [arith_expr|atom|power|term|STRING|NAME] ')' >"""
  19. )
  20. class FixPrint(fixer_base.BaseFix):
  21. BM_compatible = True
  22. PATTERN = """
  23. simple_stmt< any* bare='print' any* > | print_stmt
  24. """
  25. def transform(self, node, results):
  26. assert results
  27. bare_print = results.get("bare")
  28. if bare_print:
  29. # Special-case print all by itself.
  30. bare_print.replace(Call(Name(u"print"), [],
  31. prefix=bare_print.prefix))
  32. # The "from __future__ import print_function"" declaration is added
  33. # by the fix_print_with_import fixer, so we skip it here.
  34. # add_future(node, u'print_function')
  35. return
  36. assert node.children[0] == Name(u"print")
  37. args = node.children[1:]
  38. if len(args) == 1 and parend_expr.match(args[0]):
  39. # We don't want to keep sticking parens around an
  40. # already-parenthesised expression.
  41. return
  42. sep = end = file = None
  43. if args and args[-1] == Comma():
  44. args = args[:-1]
  45. end = " "
  46. if args and args[0] == pytree.Leaf(token.RIGHTSHIFT, u">>"):
  47. assert len(args) >= 2
  48. file = args[1].clone()
  49. args = args[3:] # Strip a possible comma after the file expression
  50. # Now synthesize a print(args, sep=..., end=..., file=...) node.
  51. l_args = [arg.clone() for arg in args]
  52. if l_args:
  53. l_args[0].prefix = u""
  54. if sep is not None or end is not None or file is not None:
  55. if sep is not None:
  56. self.add_kwarg(l_args, u"sep", String(repr(sep)))
  57. if end is not None:
  58. self.add_kwarg(l_args, u"end", String(repr(end)))
  59. if file is not None:
  60. self.add_kwarg(l_args, u"file", file)
  61. n_stmt = Call(Name(u"print"), l_args)
  62. n_stmt.prefix = node.prefix
  63. # Note that there are corner cases where adding this future-import is
  64. # incorrect, for example when the file also has a 'print ()' statement
  65. # that was intended to print "()".
  66. # add_future(node, u'print_function')
  67. return n_stmt
  68. def add_kwarg(self, l_nodes, s_kwd, n_expr):
  69. # XXX All this prefix-setting may lose comments (though rarely)
  70. n_expr.prefix = u""
  71. n_argument = pytree.Node(self.syms.argument,
  72. (Name(s_kwd),
  73. pytree.Leaf(token.EQUAL, u"="),
  74. n_expr))
  75. if l_nodes:
  76. l_nodes.append(Comma())
  77. n_argument.prefix = u" "
  78. l_nodes.append(n_argument)