[Python-checkins] r71044 - sandbox/trunk/refactor_pkg/refactor/fixes/from3/fix_print.py
paul.kippes
python-checkins at python.org
Thu Apr 2 10:31:49 CEST 2009
Author: paul.kippes
Date: Thu Apr 2 10:31:49 2009
New Revision: 71044
Log:
3to2 sandbox: added fix_print.py; unittest fails but could be issue with unittest cases
Added:
sandbox/trunk/refactor_pkg/refactor/fixes/from3/fix_print.py
Added: sandbox/trunk/refactor_pkg/refactor/fixes/from3/fix_print.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/refactor_pkg/refactor/fixes/from3/fix_print.py Thu Apr 2 10:31:49 2009
@@ -0,0 +1,154 @@
+# Based on fix_xrange.py
+# 3to2 modification by Paul Kippes
+
+"""Fixer for print.
+
+Change:
+ 'print()' --> 'print'
+ 'print(..., end="")' --> 'print ... ,'
+ 'print(..., file=x)' --> 'print >>x, ...'
+"""
+
+#from ..fixer_urllib import *
+from ..fixer_common import *
+from ...pygram import python_symbols as syms
+from ...pytree import Leaf, Node
+from ...pgen2 import token
+
+from pprint import pprint
+import pdb
+
+class FixPrint(fixer_base.BaseFix):
+
+ PATTERN = """
+ power< name='print' trailer< '(' [any] ')' > >
+ """
+
+
+
+
+
+
+# 2-->3
+# print "The answer is"
+# [Leaf(3, '"The answer is"')]
+
+# print "The answer is", 3*3
+# [Leaf(3, '"The answer is"'), Leaf(12, ','), Node(term, [Leaf(2, '3'), Leaf(16, '*'), Leaf(2, '3')])]
+
+# print "The answer is", 3*3, 5*5
+# [Leaf(3, '"The answer is"'), Leaf(12, ','), Node(term, [Leaf(2, '3'), Leaf(16, '*'), Leaf(2, '3')]), Leaf(12, ','), Node(term, [Leaf(2, '5'), Leaf(16, '*'), Leaf(2, '5')])]
+
+# print "The answer is", 3*3, 5*5,
+# [Leaf(3, '"The answer is"'), Leaf(12, ','), Node(term, [Leaf(2, '3'), Leaf(16, '*'), Leaf(2, '3')]), Leaf(12, ','), Node(term, [Leaf(2, '5'), Leaf(16, '*'), Leaf(2, '5')]), Leaf(12, ',')]
+
+# print "The answer is %s" % "substring"
+# [Node(term, [Leaf(3, '"The answer is %s"'), Leaf(24, '%'), Leaf(3, '"substring"')])]
+
+
+
+
+# 3-->2
+
+# print("The answer is")
+# args=[Node(trailer, [Leaf(7, '('), Leaf(3, '"The answer is"'), Leaf(8, ')')])]
+
+# print("The answer is", 3*3)
+# args=[Node(trailer, [Leaf(7, '('), Node(arglist, [Leaf(3, '"The answer is"'), Leaf(12, ','), Node(term, [Leaf(2, '3'), Leaf(16, '*'), Leaf(2, '3')])]), Leaf(8, ')')])]
+
+# print("The answer is %s" % "substring")
+# args=[Node(trailer, [Leaf(7, '('), Node(term, [Leaf(3, '"The answer is %s"'), Leaf(24, '%'), Leaf(3, '"substring"')]), Leaf(8, ')')])]
+
+
+ # answer is"'), Leaf(8, ')')])])
+ # ---------------------------------------------------------------------
+ # print '',
+ #
+ # Node(print_stmt,[
+ # Leaf(1, 'print'),
+ # Leaf(3, "''"),
+ # Leaf(12, ',')])
+ # ---------------------------------------------------------------------
+ # print >>sys.stderr, 'test',
+ #
+ # Node(print_stmt,[
+ # Leaf(1, 'print'), Leaf(35, '>>'), # redirect
+ # Node(power, [
+ # Leaf(1, 'sys'), # FILE
+ # Node(trailer, [ # ..
+ # Leaf(23, '.'), # ..
+ # Leaf(1, 'stderr')])]), # ..
+ # Leaf(12, ','),
+ # Leaf(3, '"test"'), # 'test'
+ # Leaf(12, ',')]) # newline suppression
+ # ---------------------------------------------------------------------
+
+ def find_argument(self, nodes, arg_name):
+ """Given an argument or argument list of arguments, return a
+ copy of the argument's value.
+ """
+ for node in nodes:
+ # Node is an argument?
+ if node.type == 260:
+ if (node.children[0].value == arg_name and
+ node.children[1].type == token.EQUAL):
+ return node.children[2]
+ # Node is an argument list?
+ elif node.type == 259:
+ return self.find_argument(node.children, arg_name)
+ return None
+
+ def arg_clone_and_remove(self, node):
+ if node:
+ node_copy = node.clone()
+ node.parent.remove()
+ return node_copy
+ return None
+
+ def transform(self, node, results):
+ assert node.children[0].value == 'print'
+ # extract items between parens
+ args = node.children[1].children[1:-1]
+ # extract arguments file=value and end=value
+ arg_file = self.arg_clone_and_remove(self.find_argument(args, 'file'))
+ arg_end = self.arg_clone_and_remove(self.find_argument(args, 'end'))
+ # Reevaluate args after potential removal
+ args = node.children[1].children[1:-1]
+ # extract all string components and append to string_args
+ string_args = []
+ for outter_arg in args:
+ if outter_arg.type == token.STRING or outter_arg.type == 321:
+ # extract print("string")
+ # extract print("%s" % "string")
+ string_args.append(outter_arg.clone())
+ elif outter_arg.type == 259:
+ # extract print("string", 5)
+ # extract print("string", "")
+ for inner_arg in outter_arg.children:
+ if inner_arg.type != token.COMMA:
+ string_args.append(inner_arg.clone())
+ # combine extracted parts into print statement
+ print_stmt = Node(syms.print_stmt, [Leaf(token.NAME, 'print')])
+ if arg_file:
+ print_stmt.append_child(Leaf(token.RIGHTSHIFT, '>>', prefix=' '))
+ print_stmt.append_child(arg_file)
+ if string_args:
+ print_stmt.append_child(Leaf(token.COMMA, ','))
+ for i, string_arg in enumerate(string_args):
+ if i:
+ print_stmt.append_child(Leaf(token.COMMA, ','))
+ string_arg.set_prefix(' ')
+ print_stmt.append_child(string_arg)
+ else:
+ string_arg.set_prefix(' ')
+ print_stmt.append_child(string_arg)
+ # if 'end' argument is specified, add end character to last string
+ if arg_end and arg_end.value == "''":
+ print_stmt.append_child(Leaf(token.COMMA, ','))
+ elif arg_end:
+ if string_arg:
+ print_stmt.append_child(Leaf(token.PLUS, '+', prefix=' '))
+ arg_end.set_prefix(' ')
+ print_stmt.append_child(arg_end)
+ print_stmt.append_child(Leaf(token.COMMA, ','))
+ return print_stmt
More information about the Python-checkins
mailing list