summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2010-01-30 11:22:26 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2010-01-30 11:22:26 (GMT)
commit5577eaa1e30cae3c907ada8214b4c8119a83b4e0 (patch)
tree81f0a5773e6070e44d62455b96dc9cbbeda5ee27
parent34343df5438ae9b4659fe9ad757e883599190262 (diff)
downloadcpython-5577eaa1e30cae3c907ada8214b4c8119a83b4e0.zip
cpython-5577eaa1e30cae3c907ada8214b4c8119a83b4e0.tar.gz
cpython-5577eaa1e30cae3c907ada8214b4c8119a83b4e0.tar.bz2
Merged revisions 77848 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ................ r77848 | martin.v.loewis | 2010-01-30 12:05:48 +0100 (Sa, 30 Jan 2010) | 20 lines Merged revisions 77846 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ................ r77846 | martin.v.loewis | 2010-01-30 11:56:23 +0100 (Sa, 30 Jan 2010) | 13 lines Merged revisions 77419,77435 via svnmerge from svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3 ........ r77419 | benjamin.peterson | 2010-01-10 21:39:48 +0100 (So, 10 Jan 2010) | 1 line enclose path in quotes to handle paths with spaces correctly #7666 ........ r77435 | alexandre.vassalotti | 2010-01-12 01:36:54 +0100 (Di, 12 Jan 2010) | 2 lines Issue #1967: Add fixer for dictionary views. ........ ................ ................
-rw-r--r--Lib/lib2to3/fixes/fix_dict.py12
-rwxr-xr-xLib/lib2to3/tests/test_fixers.py58
-rw-r--r--Lib/lib2to3/tests/test_parser.py2
3 files changed, 68 insertions, 4 deletions
diff --git a/Lib/lib2to3/fixes/fix_dict.py b/Lib/lib2to3/fixes/fix_dict.py
index 7c37fad..d66add8 100644
--- a/Lib/lib2to3/fixes/fix_dict.py
+++ b/Lib/lib2to3/fixes/fix_dict.py
@@ -11,6 +11,10 @@ d.iterkeys() -> iter(d.keys())
d.iteritems() -> iter(d.items())
d.itervalues() -> iter(d.values())
+d.viewkeys() -> d.keys()
+d.viewitems() -> d.items()
+d.viewvalues() -> d.values()
+
Except in certain very specific contexts: the iter() can be dropped
when the context is list(), sorted(), iter() or for...in; the list()
can be dropped when the context is list() or sorted() (but not iter()
@@ -39,7 +43,8 @@ class FixDict(fixer_base.BaseFix):
PATTERN = """
power< head=any+
trailer< '.' method=('keys'|'items'|'values'|
- 'iterkeys'|'iteritems'|'itervalues') >
+ 'iterkeys'|'iteritems'|'itervalues'|
+ 'viewkeys'|'viewitems'|'viewvalues') >
parens=trailer< '(' ')' >
tail=any*
>
@@ -52,7 +57,8 @@ class FixDict(fixer_base.BaseFix):
syms = self.syms
method_name = method.value
isiter = method_name.startswith("iter")
- if isiter:
+ isview = method_name.startswith("view")
+ if isiter or isview:
method_name = method_name[4:]
assert method_name in ("keys", "items", "values"), repr(method)
head = [n.clone() for n in head]
@@ -64,7 +70,7 @@ class FixDict(fixer_base.BaseFix):
prefix=method.prefix)]),
results["parens"].clone()]
new = pytree.Node(syms.power, args)
- if not special:
+ if not (special or isview):
new.prefix = ""
new = Call(Name("iter" if isiter else "list"), [new])
if tail:
diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py
index 2e0092f..a92f14a 100755
--- a/Lib/lib2to3/tests/test_fixers.py
+++ b/Lib/lib2to3/tests/test_fixers.py
@@ -1215,6 +1215,14 @@ class Test_dict(FixerTestCase):
a = "[i for i in d. keys( ) ]"
self.check(b, a)
+ b = "if d. viewkeys ( ) : pass"
+ a = "if d. keys ( ) : pass"
+ self.check(b, a)
+
+ b = "[i for i in d. viewkeys( ) ]"
+ a = "[i for i in d. keys( ) ]"
+ self.check(b, a)
+
def test_trailing_comment(self):
b = "d.keys() # foo"
a = "list(d.keys()) # foo"
@@ -1234,6 +1242,16 @@ class Test_dict(FixerTestCase):
]"""
self.check(b, a)
+ b = """[i for i in d.iterkeys() # foo
+ ]"""
+ a = """[i for i in d.keys() # foo
+ ]"""
+ self.check(b, a)
+
+ b = "d.viewitems() # foo"
+ a = "d.items() # foo"
+ self.check(b, a)
+
def test_unchanged(self):
for wrapper in fixer_util.consuming_calls:
s = "s = %s(d.keys())" % wrapper
@@ -1367,6 +1385,46 @@ class Test_dict(FixerTestCase):
a = "for x in list(h.keys())[0]: print x"
self.check(b, a)
+ def test_25(self):
+ b = "d.viewkeys()"
+ a = "d.keys()"
+ self.check(b, a)
+
+ def test_26(self):
+ b = "d.viewitems()"
+ a = "d.items()"
+ self.check(b, a)
+
+ def test_27(self):
+ b = "d.viewvalues()"
+ a = "d.values()"
+ self.check(b, a)
+
+ def test_14(self):
+ b = "[i for i in d.viewkeys()]"
+ a = "[i for i in d.keys()]"
+ self.check(b, a)
+
+ def test_15(self):
+ b = "(i for i in d.viewkeys())"
+ a = "(i for i in d.keys())"
+ self.check(b, a)
+
+ def test_17(self):
+ b = "iter(d.viewkeys())"
+ a = "iter(d.keys())"
+ self.check(b, a)
+
+ def test_18(self):
+ b = "list(d.viewkeys())"
+ a = "list(d.keys())"
+ self.check(b, a)
+
+ def test_19(self):
+ b = "sorted(d.viewkeys())"
+ a = "sorted(d.keys())"
+ self.check(b, a)
+
class Test_xrange(FixerTestCase):
fixer = "xrange"
diff --git a/Lib/lib2to3/tests/test_parser.py b/Lib/lib2to3/tests/test_parser.py
index 7e649da..15b109e9 100644
--- a/Lib/lib2to3/tests/test_parser.py
+++ b/Lib/lib2to3/tests/test_parser.py
@@ -206,6 +206,6 @@ def diff(fn, result):
finally:
f.close()
try:
- return os.system("diff -u %s @" % fn)
+ return os.system("diff -u %r @" % fn)
finally:
os.remove("@")