[Python-checkins] r53386 - in sandbox/trunk/2to3: example.py fixes/fix_raise.py
guido.van.rossum
python-checkins at python.org
Thu Jan 11 21:58:28 CET 2007
Author: guido.van.rossum
Date: Thu Jan 11 21:58:27 2007
New Revision: 53386
Added:
sandbox/trunk/2to3/fixes/fix_raise.py (contents, props changed)
Modified:
sandbox/trunk/2to3/example.py
Log:
Fixer by Collin Winter to turn raise X, Y into raise X(Y).
Modified: sandbox/trunk/2to3/example.py
==============================================================================
--- sandbox/trunk/2to3/example.py (original)
+++ sandbox/trunk/2to3/example.py Thu Jan 11 21:58:27 2007
@@ -208,6 +208,25 @@
pass
except (Exception, SystemExit):
pass
+
+def raise_examples():
+ raise Exception, 5
+ #
+ raise Exception,5
+ #
+ raise Exception, (5, 6, 7)
+ #
+ # These should not be touched
+ #
+ raise Exception
+ #
+ raise Exception(5, 6)
+ #
+ # These should produce a warning
+ #
+ raise Exception, 5, 6
+ #
+ raise Exception,5,6
# This is the last line.
Added: sandbox/trunk/2to3/fixes/fix_raise.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_raise.py Thu Jan 11 21:58:27 2007
@@ -0,0 +1,52 @@
+"""Fixer for 'raise E, a1, a2, ...'"""
+
+# Python imports
+import token
+
+# Local imports
+import pytree
+from fixes import basefix
+
+could_not_convert = "At line %d: could not convert: %s"
+reason = "At line %d: Python 3's raise will not support providing a traceback"
+
+def get_lineno(node):
+ while not isinstance(node, pytree.Leaf):
+ if not node.children:
+ return
+ node = node.children[0]
+ return node.lineno
+
+class FixRaise(basefix.BaseFix):
+
+ PATTERN = """
+ raise_stmt< 'raise' exc=any ',' a1=any [',' a2=any] >
+ """
+
+ def transform(self, node):
+ syms = self.syms
+ results = self.match(node)
+ assert results
+
+ exc = results["exc"].clone()
+ args = [results["a1"].clone()]
+ args[0].set_prefix("")
+
+ arg2 = results.get("a2")
+ if arg2 is not None:
+ lineno = get_lineno(node)
+ for_output = node.clone()
+ for_output.set_prefix("")
+ self.logger.warning(could_not_convert % (lineno, for_output))
+ self.logger.warning(reason % lineno)
+ return node
+
+ new = pytree.Node(syms.raise_stmt,
+ [pytree.Leaf(token.NAME, "raise"),
+ exc,
+ pytree.Node(syms.trailer,
+ [pytree.Leaf(token.LPAR, "("),
+ pytree.Node(syms.arglist, args),
+ pytree.Leaf(token.RPAR, ")")])])
+ new.set_prefix(node.get_prefix())
+ return new
More information about the Python-checkins
mailing list