Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d8d9a5

Browse files
committed
Merged revisions 66797 via svnmerge from
svn+ssh://[email protected]/python/trunk ................ r66797 | benjamin.peterson | 2008-10-04 15:55:50 -0500 (Sat, 04 Oct 2008) | 19 lines Merged revisions 66707,66775,66782 via svnmerge from svn+ssh://[email protected]/sandbox/trunk/2to3/lib2to3 ........ r66707 | benjamin.peterson | 2008-09-30 18:27:10 -0500 (Tue, 30 Sep 2008) | 1 line fix #4001: fix_imports didn't check for __init__.py before converting to relative imports ........ r66775 | collin.winter | 2008-10-03 12:08:26 -0500 (Fri, 03 Oct 2008) | 4 lines Add an alternative iterative pattern matching system that, while slower, correctly parses files that cause the faster recursive pattern matcher to fail with a recursion error. lib2to3 falls back to the iterative matcher if the recursive one fails. Fixes http://bugs.python.org/issue2532. Thanks to Nick Edds. ........ r66782 | benjamin.peterson | 2008-10-03 17:51:36 -0500 (Fri, 03 Oct 2008) | 1 line add Victor Stinner's fixer for os.getcwdu -> os.getcwd #4023 ........ ................
1 parent b76a2b1 commit 7d8d9a5

6 files changed

Lines changed: 2833 additions & 11 deletions

File tree

Lib/lib2to3/fixes/fix_getcwdu.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Fixer that changes os.getcwdu() to os.getcwd().
3+
"""
4+
# Author: Victor Stinner
5+
6+
# Local imports
7+
from .. import fixer_base
8+
from ..fixer_util import Name
9+
10+
class FixGetcwdu(fixer_base.BaseFix):
11+
12+
PATTERN = """
13+
power< 'os' trailer< dot='.' name='getcwdu' > any* >
14+
"""
15+
16+
def transform(self, node, results):
17+
name = results["name"]
18+
name.replace(Name("getcwd", prefix=name.get_prefix()))

Lib/lib2to3/fixes/fix_import.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def probably_a_local_import(imp_name, file_path):
5454
imp_name = imp_name.split('.', 1)[0].strip()
5555
base_path = dirname(file_path)
5656
base_path = join(base_path, imp_name)
57+
# If there is no __init__.py next to the file its not in a package
58+
# so can't be a relative import.
59+
if not exists(join(dirname(base_path), '__init__.py')):
60+
return False
5761
for ext in ['.py', pathsep, '.pyc', '.so', '.sl', '.pyd']:
5862
if exists(base_path + ext):
5963
return True

Lib/lib2to3/pytree.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,10 +655,47 @@ def generate_matches(self, nodes):
655655
elif self.name == "bare_name":
656656
yield self._bare_name_matches(nodes)
657657
else:
658-
for count, r in self._recursive_matches(nodes, 0):
659-
if self.name:
660-
r[self.name] = nodes[:count]
661-
yield count, r
658+
try:
659+
for count, r in self._recursive_matches(nodes, 0):
660+
if self.name:
661+
r[self.name] = nodes[:count]
662+
yield count, r
663+
except RuntimeError:
664+
# We fall back to the iterative pattern matching scheme if the recursive
665+
# scheme hits the recursion limit.
666+
for count, r in self._iterative_matches(nodes):
667+
if self.name:
668+
r[self.name] = nodes[:count]
669+
yield count, r
670+
671+
def _iterative_matches(self, nodes):
672+
"""Helper to iteratively yield the matches."""
673+
nodelen = len(nodes)
674+
if 0 >= self.min:
675+
yield 0, {}
676+
677+
results = []
678+
# generate matches that use just one alt from self.content
679+
for alt in self.content:
680+
for c, r in generate_matches(alt, nodes):
681+
yield c, r
682+
results.append((c, r))
683+
684+
# for each match, iterate down the nodes
685+
while results:
686+
new_results = []
687+
for c0, r0 in results:
688+
# stop if the entire set of nodes has been matched
689+
if c0 < nodelen and c0 <= self.max:
690+
for alt in self.content:
691+
for c1, r1 in generate_matches(alt, nodes[c0:]):
692+
if c1 > 0:
693+
r = {}
694+
r.update(r0)
695+
r.update(r1)
696+
yield c0 + c1, r
697+
new_results.append((c0 + c1, r))
698+
results = new_results
662699

663700
def _bare_name_matches(self, nodes):
664701
"""Special optimized matcher for bare_name."""

Lib/lib2to3/tests/data/README

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Files in this directory:
2+
- py2_test_grammar.py -- test file that exercises most/all of Python 2.x's grammar.
3+
- py3_test_grammar.py -- test file that exercises most/all of Python 3.x's grammar.
4+
- infinite_recursion.py -- test file that causes lib2to3's faster recursive pattern matching
5+
scheme to fail, but passes when lib2to3 falls back to iterative pattern matching.

0 commit comments

Comments
 (0)