Kaydet (Commit) 1ab31490 authored tarafından Benjamin Peterson's avatar Benjamin Peterson

Merged revisions 65053-65054 via svnmerge from

svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3

........
  r65053 | benjamin.peterson | 2008-07-16 21:04:12 -0500 (Wed, 16 Jul 2008) | 1 line

  massive optimizations for 2to3 (especially fix_imports) from Nick Edds
........
  r65054 | benjamin.peterson | 2008-07-16 21:05:09 -0500 (Wed, 16 Jul 2008) | 1 line

  normalize whitespace
........
üst a66bb0a7
......@@ -4,8 +4,8 @@ from . import fix_imports
MAPPING = {
'whichdb': ('dbm', ['whichdb']),
'anydbm': ('dbm', ['error', 'open']),
'whichdb': 'dbm',
'anydbm': 'dbm',
}
......
......@@ -652,20 +652,35 @@ class WildcardPattern(BasePattern):
if self.name:
r[self.name] = nodes[:count]
yield count, r
elif self.name == "bare_name":
yield self._bare_name_matches(nodes)
else:
for count, r in self._recursive_matches(nodes, 0):
if self.name:
r[self.name] = nodes[:count]
yield count, r
def _bare_name_matches(self, nodes):
"""Special optimized matcher for bare_name."""
count = 0
r = {}
done = False
max = len(nodes)
while not done and count < max:
done = True
for leaf in self.content:
if leaf[0].match(nodes[count], r):
count += 1
done = False
break
r[self.name] = nodes[:count]
return count, r
def _recursive_matches(self, nodes, count):
"""Helper to recursively yield the matches."""
assert self.content is not None
if count >= self.min:
r = {}
if self.name:
r[self.name] = nodes[:0]
yield 0, r
yield 0, {}
if count < self.max:
for alt in self.content:
for c0, r0 in generate_matches(alt, nodes):
......
......@@ -1413,7 +1413,7 @@ class Test_imports(FixerTestCase):
from ..fixes.fix_imports import MAPPING as modules
def test_import_module(self):
for old, (new, members) in self.modules.items():
for old, new in self.modules.items():
b = "import %s" % old
a = "import %s" % new
self.check(b, a)
......@@ -1423,24 +1423,17 @@ class Test_imports(FixerTestCase):
self.check(b, a)
def test_import_from(self):
for old, (new, members) in self.modules.items():
for member in members:
b = "from %s import %s" % (old, member)
a = "from %s import %s" % (new, member)
self.check(b, a)
s = "from foo import %s" % member
self.unchanged(s)
b = "from %s import %s" % (old, ", ".join(members))
a = "from %s import %s" % (new, ", ".join(members))
for old, new in self.modules.items():
b = "from %s import foo" % old
a = "from %s import foo" % new
self.check(b, a)
s = "from foo import %s" % ", ".join(members)
self.unchanged(s)
b = "from %s import foo, bar" % old
a = "from %s import foo, bar" % new
self.check(b, a)
def test_import_module_as(self):
for old, (new, members) in self.modules.items():
for old, new in self.modules.items():
b = "import %s as foo_bar" % old
a = "import %s as foo_bar" % new
self.check(b, a)
......@@ -1450,50 +1443,27 @@ class Test_imports(FixerTestCase):
self.check(b, a)
def test_import_from_as(self):
for old, (new, members) in self.modules.items():
for member in members:
b = "from %s import %s as foo_bar" % (old, member)
a = "from %s import %s as foo_bar" % (new, member)
self.check(b, a)
for old, new in self.modules.items():
b = "from %s import foo as bar" % old
a = "from %s import foo as bar" % new
self.check(b, a)
def test_star(self):
for old in self.modules:
s = "from %s import *" % old
self.warns_unchanged(s, "Cannot handle star imports")
for old, new in self.modules.items():
b = "from %s import *" % old
a = "from %s import *" % new
self.check(b, a)
def test_import_module_usage(self):
for old, (new, members) in self.modules.items():
for member in members:
b = """
import %s
foo(%s, %s.%s)
""" % (old, old, old, member)
a = """
import %s
foo(%s, %s.%s)
""" % (new, new, new, member)
self.check(b, a)
def test_from_import_usage(self):
for old, (new, members) in self.modules.items():
for member in members:
b = """
from %s import %s
foo(%s, %s())
""" % (old, member, member, member)
a = """
from %s import %s
foo(%s, %s())
""" % (new, member, member, member)
self.check(b, a)
for old, new in self.modules.items():
b = """
from %s import %s
foo(%s)
""" % (old, ", ".join(members), ", ".join(members))
import %s
foo(%s.bar)
""" % (old, old)
a = """
from %s import %s
foo(%s)
""" % (new, ", ".join(members), ", ".join(members))
import %s
foo(%s.bar)
""" % (new, new)
self.check(b, a)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment