[Python-checkins] r53046 - in sandbox/trunk/2to3: example.py fixes/fix_exec.py
guido.van.rossum
python-checkins at python.org
Sun Dec 17 05:41:28 CET 2006
Author: guido.van.rossum
Date: Sun Dec 17 05:41:28 2006
New Revision: 53046
Added:
sandbox/trunk/2to3/fixes/fix_exec.py (contents, props changed)
Modified:
sandbox/trunk/2to3/example.py
Log:
Add a fixer for 'exec'.
Modified: sandbox/trunk/2to3/example.py
==============================================================================
--- sandbox/trunk/2to3/example.py (original)
+++ sandbox/trunk/2to3/example.py Sun Dec 17 05:41:28 2006
@@ -93,4 +93,26 @@
#
print >> sys.stderr # spaces before sys.stderr
+def exec_examples():
+ #
+ exec code
+ #
+ exec code in ns
+ #
+ exec code in ns1, ns2
+ #
+ exec (a.b()) in ns
+ #
+ exec a.b() + c in ns
+ #
+ # These should not be touched:
+ #
+ exec(code)
+ #
+ exec (code)
+ #
+ exec(code, ns)
+ #
+ exec(code, ns1, ns2)
+
# This is the last line.
Added: sandbox/trunk/2to3/fixes/fix_exec.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_exec.py Sun Dec 17 05:41:28 2006
@@ -0,0 +1,54 @@
+# Copyright 2006 Google, Inc. All Rights Reserved.
+# Licensed to PSF under a Contributor Agreement.
+
+"""Fixer for exec."""
+
+# Python imports
+import token
+
+# Local imports
+import pytree
+import patcomp
+import pygram
+
+syms = pygram.python_symbols
+pat_compile = patcomp.PatternCompiler().compile_pattern
+
+PATTERN = """
+exec_stmt< 'exec' a=any 'in' b=any [',' c=any] >
+|
+exec_stmt< 'exec' (not atom<'(' [any] ')'>) a=any >
+"""
+
+
+class FixExec(object):
+
+ def __init__(self, options):
+ self.options = options
+ self.pattern = pat_compile(PATTERN)
+
+ def match(self, node):
+ results = {}
+ return self.pattern.match(node, results) and results
+
+ def transform(self, node):
+ results = self.match(node)
+ assert results
+ a = results["a"]
+ b = results.get("b")
+ c = results.get("c")
+ args = [a.clone()]
+ args[0].set_prefix("")
+ if b is not None:
+ args.extend([pytree.Leaf(token.COMMA, ","), b.clone()])
+ if c is not None:
+ args.extend([pytree.Leaf(token.COMMA, ","), c.clone()])
+ new = pytree.Node(syms.factor,
+ [pytree.Leaf(token.NAME, "exec"),
+ 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