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

guido.van.rossum python-checkins at python.org
Fri Feb 9 05:10:51 CET 2007


Author: guido.van.rossum
Date: Fri Feb  9 05:10:50 2007
New Revision: 53682

Modified:
   sandbox/trunk/2to3/example.py
   sandbox/trunk/2to3/fixes/fix_dict.py
Log:
Refactor the context checking a bit.
Add support for special contexts 'for...in'.


Modified: sandbox/trunk/2to3/example.py
==============================================================================
--- sandbox/trunk/2to3/example.py	(original)
+++ sandbox/trunk/2to3/example.py	Fri Feb  9 05:10:50 2007
@@ -253,26 +253,35 @@
     #
     # Plain method calls in special contexts
     #
-    print list(e.keys())
-    print sorted(e.keys())
     print iter(e.keys())
     for i in e.keys(): print i
+    [i for i in e.keys()]
+    (i for i in e.keys())
     #
     # Iterator method calls
     #
-    print d.iterkeys()
-    print d.iteritems()
-    print d.itervalues()
+    print f.iterkeys()
+    print f.iteritems()
+    print f.itervalues()
     #
     # Iterator method calls in special contexts
     #
-    print list(e.iterkeys())
-    print sorted(e.iterkeys())
-    print iter(e.iterkeys())
-    for i in e.iterkeys(): print i
+    print list(g.iterkeys())
+    print sorted(g.iterkeys())
+    print iter(g.iterkeys())
+    for i in g.iterkeys(): print i
+    [i for i in g.iterkeys()]
+    (i for i in g.iterkeys())
+
+def dict_negative_examples():
+    #
+    # These should all remain unchanged:
+    #
+    print list(h.keys())
+    print sorted(h.keys())
     #
-    # This should be left unchanged but trigger a warning:
+    # This should be left unchanged and trigger a warning:
     #
-    print d.keys()[0]
+    print h.keys()[0]
 
 # This is the last line.

Modified: sandbox/trunk/2to3/fixes/fix_dict.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_dict.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_dict.py	Fri Feb  9 05:10:50 2007
@@ -68,23 +68,29 @@
     new.set_prefix(node.get_prefix())
     return new
 
-  P1 = "trailer< '(' node=any ')' >"
+  P1 = "power< func=NAME trailer< '(' node=any ')' > any* >"
   p1 = patcomp.PatternCompiler().compile_pattern(P1)
 
-  P2 = "power< func=NAME trailer< '(' node=any ')' > any* >"
+  P2 = """for_stmt< 'for' any 'in' node=any ':' any* >
+        | list_for< 'for' any 'in' node=any any* >
+        | gen_for< 'for' any 'in' node=any any* >
+       """
   p2 = patcomp.PatternCompiler().compile_pattern(P2)
 
   def in_special_context(self, node, isiter):
-    results = {}
-    if not (self.p1.match(node.parent, results) and
-            results["node"] is node):
+    if node.parent is None:
       return False
     results = {}
-    if not (self.p2.match(node.parent.parent, results) and
-            results["node"] is node):
+    if  (node.parent.parent is not None and
+         self.p1.match(node.parent.parent, results) and
+         results["node"] is node):
+      if isiter:
+        # iter(d.iterkeys()) -> iter(d.keys()), etc.
+        return results["func"].value in ("iter", "list", "sorted")
+      else:
+        # list(d.keys()) -> list(d.keys()), etc.
+        return results["func"].value in ("list", "sorted")
+    if not isiter:
       return False
-    if isiter:
-      return results["func"].value == ("iter", "list", "sorted")
-    else:
-      return results["func"].value in ("list", "sorted")
-    # XXX TODO: for...in context.
+    # for ... in d.iterkeys() -> for ... in d.keys(), etc.
+    return self.p2.match(node.parent, results) and results["node"] is node


More information about the Python-checkins mailing list