[Python-checkins] r53173 - in sandbox/trunk/2to3: example.py fixes/fix_repr.py

guido.van.rossum python-checkins at python.org
Thu Dec 28 22:31:19 CET 2006


Author: guido.van.rossum
Date: Thu Dec 28 22:31:18 2006
New Revision: 53173

Added:
   sandbox/trunk/2to3/fixes/fix_repr.py   (contents, props changed)
Modified:
   sandbox/trunk/2to3/example.py
Log:
Add a fix for `x` -> repr(x).


Modified: sandbox/trunk/2to3/example.py
==============================================================================
--- sandbox/trunk/2to3/example.py	(original)
+++ sandbox/trunk/2to3/example.py	Thu Dec 28 22:31:18 2006
@@ -146,4 +146,17 @@
     #
     exec(code, ns1, ns2)
 
+def repr_examples():
+    x = `1 + 2`
+    #
+    y = `x`
+    #
+    z = `y`.__repr__()
+    #
+    x = `1, 2, 3`
+    #
+    x = `1 + `2``
+    #
+    x = `1, 2 + `3, 4``
+
 # This is the last line.

Added: sandbox/trunk/2to3/fixes/fix_repr.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_repr.py	Thu Dec 28 22:31:18 2006
@@ -0,0 +1,33 @@
+# Copyright 2006 Google, Inc. All Rights Reserved.
+# Licensed to PSF under a Contributor Agreement.
+
+"""Fixer that transforms `xyzzy` into repr(xyzzy)."""
+
+# Python imports
+import token
+
+# Local imports
+import pytree
+from fixes import basefix
+
+
+class FixRepr(basefix.BaseFix):
+
+    PATTERN = """
+    atom < '`' expr=any '`' >
+    """
+
+    def transform(self, node):
+      results = self.match(node)
+      assert results
+      expr = results["expr"].clone()
+      if expr.type == self.syms.testlist1:
+        expr = self.parenthesize(expr)
+      new = pytree.Node(self.syms.power,
+                        (pytree.Leaf(token.NAME, "repr"),
+                         pytree.Node(self.syms.trailer,
+                                     (pytree.Leaf(token.LPAR, "("),
+                                      expr,
+                                      pytree.Leaf(token.RPAR, ")")))))
+      new.set_prefix(node.get_prefix())
+      return new


More information about the Python-checkins mailing list