[Python-checkins] r53088 - in sandbox/trunk/2to3: example.py fixes/fix_intern.py
georg.brandl
python-checkins at python.org
Tue Dec 19 22:43:54 CET 2006
Author: georg.brandl
Date: Tue Dec 19 22:43:54 2006
New Revision: 53088
Added:
sandbox/trunk/2to3/fixes/fix_intern.py
Modified:
sandbox/trunk/2to3/example.py
Log:
Add intern() fixer (patch #1619049).
Modified: sandbox/trunk/2to3/example.py
==============================================================================
--- sandbox/trunk/2to3/example.py (original)
+++ sandbox/trunk/2to3/example.py Tue Dec 19 22:43:54 2006
@@ -73,6 +73,29 @@
apply(f, args=args, kwds=kwds)
apply(f, args, kwds=kwds)
+def intern_examples():
+ #
+ # These should be refactored:
+ #
+ x = intern(a)
+ #
+ y = intern("b" # test
+ )
+ #
+ z = intern(a+b+c.d,)
+ #
+ intern("y%s" % 5).replace("y", "")
+ #
+ # These not:
+ #
+ intern(a=1)
+ #
+ intern(f, g)
+ #
+ intern(*h)
+ #
+ intern(**i)
+
def print_examples():
# plain vanilla
print 1, 1+1, 1+1+1
Added: sandbox/trunk/2to3/fixes/fix_intern.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_intern.py Tue Dec 19 22:43:54 2006
@@ -0,0 +1,61 @@
+# Copyright 2006 Georg Brandl.
+# Licensed to PSF under a Contributor Agreement.
+
+"""Fixer for intern()."""
+
+# Python imports
+import token
+
+# Local imports
+import pytree
+import patcomp
+import pygram
+
+syms = pygram.python_symbols
+pat_compile = patcomp.PatternCompiler().compile_pattern
+
+PATTERN = """
+power< 'intern'
+ trailer< lpar='('
+ ( not(arglist | argument<any '=' any>) obj=any
+ | obj=arglist<(not argument<any '=' any>) any ','> )
+ rpar=')' >
+ after=any*
+>
+"""
+
+
+class FixIntern(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
+ obj = results["obj"].clone()
+ if obj.type == syms.arglist:
+ newarglist = obj.clone()
+ else:
+ newarglist = pytree.Node(syms.arglist, [obj.clone()])
+ after = results["after"]
+ if after:
+ after = tuple(n.clone() for n in after)
+ new = pytree.Node(syms.power,
+ (pytree.Leaf(token.NAME, "sys"),
+ pytree.Node(syms.trailer,
+ [pytree.Leaf(token.DOT, "."),
+ pytree.Leaf(token.NAME, "intern")]),
+ pytree.Node(syms.trailer,
+ [results["lpar"].clone(),
+ newarglist,
+ results["rpar"].clone()]))
+ + after)
+ new.set_prefix(node.get_prefix())
+ return new
+
More information about the Python-checkins
mailing list