[Python-checkins] r53010 - in sandbox/trunk/2to3: fix_print.py fixes/fix_print.py

guido.van.rossum python-checkins at python.org
Tue Dec 12 16:25:26 CET 2006


Author: guido.van.rossum
Date: Tue Dec 12 16:25:19 2006
New Revision: 53010
Removed:
 sandbox/trunk/2to3/fix_print.py
Modified:
 sandbox/trunk/2to3/fixes/fix_print.py
Log:
Complete the transformation -- all fixers now live in the 'fixes' package.
Deleted: /sandbox/trunk/2to3/fix_print.py
==============================================================================
--- /sandbox/trunk/2to3/fix_print.py	Tue Dec 12 16:25:19 2006
+++ (empty file)
@@ -1,154 +0,0 @@
-#!/usr/bin/env python2.5
-# Copyright 2006 Google Inc. All Rights Reserved.
-# Licensed to PSF under a Contributor Agreement.
-
-"""Refactoring tool: change print statements into function calls, ie change:
- 'print' into 'Print()'
- 'print ...'	 into 'Print(...)'
- 'print ... ,' into 'Print(..., end=" ")'
- 'print >>x, ...' into 'Print(..., file=x)'
-"""
-
-__author__ = "Guido van Rossum <guido at python.org>"
-
-# Python imports
-import os
-import sys
-import token
-import logging
-
-import pgen2
-from pgen2 import driver
-
-import pytree
-import patcomp
-
-logging.basicConfig(level=logging.DEBUG)
-
-gr = driver.load_grammar("Grammar.txt") # used by node initializers
-
-
-class Symbols(object):
-
- def __init__(self, gr):
- for name, symbol in gr.symbol2number.iteritems():
- setattr(self, name, symbol)
-
-
-syms = Symbols(gr)
-
-
-def main():
- args = sys.argv[1:] or ["example.py"]
-
- dr = driver.Driver(gr, convert=pytree.convert)
-
- for fn in args:
- print "Parsing", fn
- tree = dr.parse_file(fn)
- refactor(tree)
- diff(fn, tree)
-
-
-def refactor(tree):
- visit(tree, fix_print)
-
-
-def visit(node, func):
- func(node)
- for child in node.children:
- visit(child, func)
-
-
-pat_compile = patcomp.PatternCompiler().compile_pattern
-p_print = pat_compile("""
-'print' | print_stmt
-""")
-
-
-def fix_print(node):
- if not p_print.match(node):
- return
- if node == pytree.Leaf(token.NAME, "print"):
- if node.parent.type == syms.print_stmt:
- return # Will be covered when parent is handled
- # Special-case print all by itself
- new = pytree.Node(syms.power,
- (pytree.Leaf(token.NAME, "Print"),
- pytree.Node(syms.trailer,
- (pytree.Leaf(token.LPAR, "("),
- pytree.Leaf(token.RPAR, ")")))))
- new.set_prefix(node.get_prefix())
- node.replace(new)
- return
- if node.type != syms.print_stmt:
- return
- assert node.children[0] == pytree.Leaf(token.NAME, "print")
- args = node.children[1:]
- sep = end = file = None
- if args and args[-1] == pytree.Leaf(token.COMMA, ","):
- args = args[:-1]
- end = " "
- if args and args[0] == pytree.Leaf(token.RIGHTSHIFT, ">>"):
- assert len(args) >= 2
- file = args[1]
- args = args[3:] # Strip a possible comma after the file expression
- # Now synthesize a Print(args, sep=..., end=..., file=...) node.
- n_print = pytree.Leaf(token.NAME, "Print") # XXX -> "print"
- l_args = list(args)
- if l_args:
- l_args[0].set_prefix("")
- if sep is not None or end is not None or file is not None:
- if sep is not None:
- add_kwarg(l_args, "sep",
- pytree.Leaf(token.STRING, repr(sep)))
- if end is not None:
- add_kwarg(l_args, "end",
- pytree.Leaf(token.STRING, repr(end)))
- if file is not None:
- add_kwarg(l_args, "file", file)
- if l_args:
- for n in l_args:
- if n.parent is not None:
- n.replace(None) # Force parent to None
- n_arglist = pytree.Node(syms.arglist, l_args)
- else:
- n_arglist = None
- l_args = [pytree.Leaf(token.LPAR, "("), pytree.Leaf(token.RPAR, ")")]
- if n_arglist:
- l_args.insert(1, n_arglist)
- n_trailer = pytree.Node(syms.trailer, l_args)
- n_stmt = pytree.Node(syms.power, (n_print, n_trailer))
- n_stmt.set_prefix(node.get_prefix())
- node.replace(n_stmt)
-
-
-def add_kwarg(l_nodes, s_kwd, n_expr):
- # XXX All this prefix-setting may lose comments (though rarely)
- n_expr.set_prefix("")
- if n_expr.parent is not None:
- n_expr.replace(None) # Force parent to None
- n_argument = pytree.Node(syms.argument,
- (pytree.Leaf(token.NAME, s_kwd),
- pytree.Leaf(token.EQUAL, "="),
- n_expr))
- if l_nodes:
- l_nodes.append(pytree.Leaf(token.COMMA, ","))
- n_argument.set_prefix(" ")
- l_nodes.append(n_argument)
-
-
-def diff(fn, tree):
- f = open("@", "w")
- try:
- f.write(str(tree))
- finally:
- f.close()
- try:
- return os.system("diff -u %s @" % fn)
- finally:
- os.remove("@")
-
-
-if __name__ == "__main__":
- main()
Modified: sandbox/trunk/2to3/fixes/fix_print.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_print.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_print.py	Tue Dec 12 16:25:19 2006
@@ -1,40 +1,100 @@
 # Copyright 2006 Google, Inc. All Rights Reserved.
 # Licensed to PSF under a Contributor Agreement.
 
-"""Null fixer. Use as a template."""
+"""Fixer for print.
 
+Change:
+ 'print' into 'Print()'
+ 'print ...'	 into 'Print(...)'
+ 'print ... ,' into 'Print(..., end=" ")'
+ 'print >>x, ...' into 'Print(..., file=x)'
+"""
 
-class FixNull(object):
+# Python imports
+import token
 
- """Fixer class.
+# Local imports
+import pytree
+import patcomp
+import pygram
 
- The class name must be FixFooBar where FooBar is the result of
- removing underscores and capitalizing the words of the fix name.
- For example, the class name for a fixer named 'has_key' should be
- FixHasKey.
- """
+syms = pygram.python_symbols
+pat_compile = patcomp.PatternCompiler().compile_pattern
 
- def __init__(self, options):
- """Initializer.
+PATTERN = """
+'print' | print_stmt
+"""
+
+
+class FixPrint(object):
 
- The argument is an optparse.Values instance which can be used
- to inspect the command line options.
- """
+ def __init__(self, options):
 self.options = options
+ self.pattern = pat_compile(PATTERN)
 
 def match(self, node):
- """Matcher.
-
- Should return a true or false object (not necessarily a bool).
- It may return a non-empty dict of matching sub-nodes as
- returned by a matching pattern.
- """
- return None
+ if node.parent is not None and node.parent.type == syms.print_stmt:
+ # Avoid matching 'print' as part of a print_stmt
+ return None
+ return self.pattern.match(node)
 
 def transform(self, node):
- """Transformer.
+ results = self.match(node)
+ assert results
 
- Should return None, or a node that is a modified copy of the
- argument node. The argument should not be modified in place.
- """
- return None
+ if node == pytree.Leaf(token.NAME, "print"):
+ # Special-case print all by itself
+ new = pytree.Node(syms.power,
+ (pytree.Leaf(token.NAME, "Print"),
+ pytree.Node(syms.trailer,
+ (pytree.Leaf(token.LPAR, "("),
+ pytree.Leaf(token.RPAR, ")")))))
+ new.set_prefix(node.get_prefix())
+ return new
+ assert node.children[0] == pytree.Leaf(token.NAME, "print")
+ args = node.children[1:]
+ sep = end = file = None
+ if args and args[-1] == pytree.Leaf(token.COMMA, ","):
+ args = args[:-1]
+ end = " "
+ if args and args[0] == pytree.Leaf(token.RIGHTSHIFT, ">>"):
+ assert len(args) >= 2
+ file = args[1].clone()
+ args = args[3:] # Strip a possible comma after the file expression
+ # Now synthesize a Print(args, sep=..., end=..., file=...) node.
+ n_print = pytree.Leaf(token.NAME, "Print") # XXX -> "print"
+ l_args = [arg.clone() for arg in args]
+ if l_args:
+ l_args[0].set_prefix("")
+ if sep is not None or end is not None or file is not None:
+ if sep is not None:
+ add_kwarg(l_args, "sep",
+ pytree.Leaf(token.STRING, repr(sep)))
+ if end is not None:
+ add_kwarg(l_args, "end",
+ pytree.Leaf(token.STRING, repr(end)))
+ if file is not None:
+ add_kwarg(l_args, "file", file)
+ if l_args:
+ n_arglist = pytree.Node(syms.arglist, l_args)
+ else:
+ n_arglist = None
+ l_args = [pytree.Leaf(token.LPAR, "("), pytree.Leaf(token.RPAR, ")")]
+ if n_arglist:
+ l_args.insert(1, n_arglist)
+ n_trailer = pytree.Node(syms.trailer, l_args)
+ n_stmt = pytree.Node(syms.power, (n_print, n_trailer))
+ n_stmt.set_prefix(node.get_prefix())
+ return n_stmt
+
+def add_kwarg(l_nodes, s_kwd, n_expr):
+ # XXX All this prefix-setting may lose comments (though rarely)
+ n_expr.set_prefix("")
+ n_argument = pytree.Node(syms.argument,
+ (pytree.Leaf(token.NAME, s_kwd),
+ pytree.Leaf(token.EQUAL, "="),
+ n_expr))
+ if l_nodes:
+ l_nodes.append(pytree.Leaf(token.COMMA, ","))
+ n_argument.set_prefix(" ")
+ l_nodes.append(n_argument)


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /