[Python-checkins] r53733 - in sandbox/trunk/2to3: example.py fixer_tests.py fixes/fix_dict.py
guido.van.rossum
python-checkins at python.org
Sun Feb 11 08:03:45 CET 2007
Author: guido.van.rossum
Date: Sun Feb 11 08:03:43 2007
New Revision: 53733
Modified:
sandbox/trunk/2to3/example.py
sandbox/trunk/2to3/fixer_tests.py
sandbox/trunk/2to3/fixes/fix_dict.py
Log:
Make the dict fixer handle a "tail", e.g. d.keys()[0].
Modified: sandbox/trunk/2to3/example.py
==============================================================================
--- sandbox/trunk/2to3/example.py (original)
+++ sandbox/trunk/2to3/example.py Sun Feb 11 08:03:43 2007
@@ -299,6 +299,13 @@
for i in g.iterkeys(): print i
[i for i in g.iterkeys()]
(i for i in g.iterkeys())
+ #
+ # Examples with a "tail"; these are never "special"
+ #
+ print h.iterkeys().next()
+ print h.keys()[0]
+ print list(h.iterkeys().next())
+ for x in h.keys()[0]: print x
def dict_negative_examples():
#
@@ -306,10 +313,6 @@
#
print list(h.keys())
print sorted(h.keys())
- #
- # This should be left unchanged and trigger a warning:
- #
- print h.keys()[0]
def xrange_examples():
for i in xrange(100): print i
Modified: sandbox/trunk/2to3/fixer_tests.py
==============================================================================
--- sandbox/trunk/2to3/fixer_tests.py (original)
+++ sandbox/trunk/2to3/fixer_tests.py Sun Feb 11 08:03:43 2007
@@ -1020,6 +1020,27 @@
a = "foo(iter(d.keys()))"
self.check(b, a)
+ def test_21(self):
+ b = "print h.iterkeys().next()"
+ a = "print iter(h.keys()).next()"
+ self.check(b, a)
+
+ def test_22(self):
+ b = "print h.keys()[0]"
+ a = "print list(h.keys())[0]"
+ self.check(b, a)
+
+ def test_23(self):
+ b = "print list(h.iterkeys().next())"
+ a = "print list(iter(h.keys()).next())"
+ self.check(b, a)
+
+ def test_24(self):
+ b = "for x in h.keys()[0]: print x"
+ a = "for x in list(h.keys())[0]: print x"
+ self.check(b, a)
+
+
if __name__ == "__main__":
import sys
Modified: sandbox/trunk/2to3/fixes/fix_dict.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_dict.py (original)
+++ sandbox/trunk/2to3/fixes/fix_dict.py Sun Feb 11 08:03:43 2007
@@ -33,7 +33,7 @@
class FixDict(basefix.BaseFix):
PATTERN = """
- power< prefix=any+
+ power< head=any+
trailer< '.' method=('keys'|'items'|'values'|
'iterkeys'|'iteritems'|'itervalues') >
trailer< '(' ')' >
@@ -43,28 +43,29 @@
def transform(self, node):
results = self.match(node)
- prefix = results["prefix"]
+ head = results["head"]
method = results["method"][0].value # Extract method name
tail = results["tail"]
- if tail:
- return self.cannot_convert(node,
- "stuff after .[iter]keys() etc. unsupported")
syms = self.syms
isiter = method.startswith("iter")
if isiter:
method = method[4:]
assert method in ("keys", "items", "values"), repr(method)
- prefix = [n.clone() for n in prefix]
- new = pytree.Node(syms.power,
- prefix + [pytree.Node(syms.trailer,
- [pytree.Leaf(token.DOT, '.'),
- macros.Name(method)]),
- pytree.Node(syms.trailer,
- [macros.lparen_leaf.clone(),
- macros.rparen_leaf.clone()])])
- if not self.in_special_context(node, isiter):
+ head = [n.clone() for n in head]
+ tail = [n.clone() for n in tail]
+ special = not tail and self.in_special_context(node, isiter)
+ args = head + [pytree.Node(syms.trailer,
+ [pytree.Leaf(token.DOT, '.'),
+ macros.Name(method)]),
+ pytree.Node(syms.trailer,
+ [macros.lparen_leaf.clone(),
+ macros.rparen_leaf.clone()])]
+ new = pytree.Node(syms.power, args)
+ if not special:
new.set_prefix("")
new = macros.Call(macros.Name(isiter and "iter" or "list"), [new])
+ if tail:
+ new = pytree.Node(syms.power, [new] + tail)
new.set_prefix(node.get_prefix())
return new
More information about the Python-checkins
mailing list