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

Skip to content

Commit 3faa84f

Browse files
committed
Merged revisions 61724-61725 via svnmerge from
svn+ssh://[email protected]/python/trunk ................ r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sa, 22 Mär 2008) | 49 lines Merged revisions 61602-61723 via svnmerge from svn+ssh://[email protected]/sandbox/trunk/2to3/lib2to3 ........ r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 Mär 2008) | 1 line Added fixer for implicit local imports. See #2414. ........ r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 Mär 2008) | 1 line Added a class for tests which should not run if a particular import is found. ........ r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 Mär 2008) | 1 line Two more relative import fixes in pgen2. ........ r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 Mär 2008) | 1 line Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out. ........ r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 Mär 2008) | 3 lines Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse) ........ r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 Mär 2008) | 1 line SVN is happier when you add the files you create... -_-' ........ r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 Mär 2008) | 1 line Added an explicit sort order to fixers -- fixes problems like #2427 ........ r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 Mär 2008) | 3 lines Fixes #2428 -- comments are no longer eatten by __future__ fixer. ........ r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 Mär 2008) | 1 line Added 2to3 node pretty-printer ........ r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 Mär 2008) | 1 line Made node printing a little bit prettier ........ r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 Mär 2008) | 2 lines Fix whitespace. ........ ................ r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sa, 22 Mär 2008) | 2 lines Install lib2to3. ................
1 parent 896c317 commit 3faa84f

15 files changed

Lines changed: 352 additions & 66 deletions

Lib/lib2to3/fixes/basefix.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# Local imports
1717
from ..patcomp import PatternCompiler
1818
from .. import pygram
19+
from .util import does_tree_import
1920

2021
class BaseFix(object):
2122

@@ -36,6 +37,8 @@ class BaseFix(object):
3637
used_names = set() # A set of all used NAMEs
3738
order = "post" # Does the fixer prefer pre- or post-order traversal
3839
explicit = False # Is this ignored by refactor.py -f all?
40+
run_order = 5 # Fixers will be sorted by run order before execution
41+
# Lower numbers will be run first.
3942

4043
# Shortcut for access to Python grammar symbols
4144
syms = pygram.python_symbols
@@ -163,3 +166,23 @@ def finish_tree(self, tree, filename):
163166
filename - the name of the file the tree came from.
164167
"""
165168
pass
169+
170+
171+
class ConditionalFix(BaseFix):
172+
""" Base class for fixers which not execute if an import is found. """
173+
174+
# This is the name of the import which, if found, will cause the test to be skipped
175+
skip_on = None
176+
177+
def start_tree(self, *args):
178+
super(ConditionalFix, self).start_tree(*args)
179+
self._should_skip = None
180+
181+
def should_skip(self, node):
182+
if self._should_skip is not None:
183+
return self._should_skip
184+
pkg = self.skip_on.split(".")
185+
name = pkg[-1]
186+
pkg = ".".join(pkg[:-1])
187+
self._should_skip = does_tree_import(pkg, name, node)
188+
return self._should_skip

Lib/lib2to3/fixes/fix_filter.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
# Local imports
1717
from ..pgen2 import token
1818
from . import basefix
19-
from .util import Name, Call, ListComp, does_tree_import, in_special_context
19+
from .util import Name, Call, ListComp, in_special_context
2020

21-
class FixFilter(basefix.BaseFix):
21+
class FixFilter(basefix.ConditionalFix):
2222

2323
PATTERN = """
2424
filter_lambda=power<
@@ -47,20 +47,10 @@ class FixFilter(basefix.BaseFix):
4747
>
4848
"""
4949

50-
def start_tree(self, *args):
51-
super(FixFilter, self).start_tree(*args)
52-
self._new_filter = None
53-
54-
def has_new_filter(self, node):
55-
if self._new_filter is not None:
56-
return self._new_filter
57-
self._new_filter = does_tree_import('future_builtins', 'filter', node)
58-
return self._new_filter
50+
skip_on = "future_builtins.filter"
5951

6052
def transform(self, node, results):
61-
if self.has_new_filter(node):
62-
# If filter is imported from future_builtins, we don't want to
63-
# do anything here.
53+
if self.should_skip(node):
6454
return
6555

6656
if "filter_lambda" in results:

Lib/lib2to3/fixes/fix_future.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@
1111
class FixFuture(basefix.BaseFix):
1212
PATTERN = """import_from< 'from' module_name="__future__" 'import' any >"""
1313

14+
# This should be run last -- some things check for the import
15+
run_order = 10
16+
1417
def transform(self, node, results):
15-
return BlankLine()
18+
new = BlankLine()
19+
new.prefix = node.get_prefix()
20+
return new

Lib/lib2to3/fixes/fix_import.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Fixer for import statements.
2+
If spam is being imported from the local directory, this import:
3+
from spam import eggs
4+
Becomes:
5+
from .spam import eggs
6+
7+
And this import:
8+
import spam
9+
Becomes:
10+
import .spam
11+
"""
12+
13+
# Local imports
14+
from . import basefix
15+
from os.path import dirname, join, exists, pathsep
16+
17+
class FixImport(basefix.BaseFix):
18+
19+
PATTERN = """
20+
import_from< 'from' imp=any 'import' any >
21+
|
22+
import_name< 'import' imp=any >
23+
"""
24+
25+
def transform(self, node, results):
26+
imp = results['imp']
27+
28+
if unicode(imp).startswith('.'):
29+
# Already a new-style import
30+
return
31+
32+
if not probably_a_local_import(unicode(imp), self.filename):
33+
# I guess this is a global import -- skip it!
34+
return
35+
36+
# Some imps are top-level (eg: 'import ham')
37+
# some are first level (eg: 'import ham.eggs')
38+
# some are third level (eg: 'import ham.eggs as spam')
39+
# Hence, the loop
40+
while not hasattr(imp, 'value'):
41+
imp = imp.children[0]
42+
43+
imp.value = "." + imp.value
44+
node.changed()
45+
return node
46+
47+
def probably_a_local_import(imp_name, file_path):
48+
# Must be stripped because the right space is included by the parser
49+
imp_name = imp_name.split('.', 1)[0].strip()
50+
base_path = dirname(file_path)
51+
base_path = join(base_path, imp_name)
52+
for ext in ['.py', pathsep, '.pyc', '.so', '.sl', '.pyd']:
53+
if exists(base_path + ext):
54+
return True
55+
return False

Lib/lib2to3/fixes/fix_itertools.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
""" Fixer for itertools.(imap|ifilter|izip) --> (map|filter|zip) and
22
itertools.ifilterfalse --> itertools.filterfalse (bugs 2360-2363)
33
4+
imports from itertools are fixed in fix_itertools_import.py
5+
46
If itertools is imported as something else (ie: import itertools as it;
57
it.izip(spam, eggs)) method calls will not get fixed.
68
"""
@@ -19,6 +21,9 @@ class FixItertools(basefix.BaseFix):
1921
power< func=%(it_funcs)s trailer< '(' [any] ')' > >
2022
""" %(locals())
2123

24+
# Needs to be run after fix_(map|zip|filter)
25+
run_order = 6
26+
2227
def transform(self, node, results):
2328
prefix = None
2429
func = results['func'][0]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
""" Fixer for imports of itertools.(imap|ifilter|izip|ifilterfalse) """
2+
3+
# Local imports
4+
from . import basefix
5+
from .util import BlankLine
6+
7+
class FixItertoolsImports(basefix.BaseFix):
8+
PATTERN = """
9+
import_from< 'from' 'itertools' 'import' imports=any >
10+
""" %(locals())
11+
12+
def transform(self, node, results):
13+
imports = results['imports']
14+
children = imports.children[:] or [imports]
15+
for child in children:
16+
if not hasattr(child, 'value'):
17+
# Handle 'import ... as ...'
18+
continue
19+
if child.value in ('imap', 'izip', 'ifilter'):
20+
child.remove()
21+
elif child.value == 'ifilterfalse':
22+
node.changed()
23+
child.value = 'filterfalse'
24+
25+
# Make sure the import statement is still sane
26+
children = imports.children[:] or [imports]
27+
remove_comma = True
28+
for child in children:
29+
if remove_comma and getattr(child, 'value', None) == ',':
30+
child.remove()
31+
else:
32+
remove_comma ^= True
33+
34+
if unicode(children[-1]) == ',':
35+
children[-1].remove()
36+
37+
# If there is nothing left, return a blank line
38+
if not (imports.children or getattr(imports, 'value', None)):
39+
new = BlankLine()
40+
new.prefix = node.get_prefix()
41+
else:
42+
new = node
43+
return new

Lib/lib2to3/fixes/fix_map.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
# Local imports
2323
from ..pgen2 import token
2424
from . import basefix
25-
from .util import Name, Call, ListComp, does_tree_import, in_special_context
25+
from .util import Name, Call, ListComp, in_special_context
2626
from ..pygram import python_symbols as syms
2727

28-
class FixMap(basefix.BaseFix):
28+
class FixMap(basefix.ConditionalFix):
2929

3030
PATTERN = """
3131
map_none=power<
@@ -54,20 +54,10 @@ class FixMap(basefix.BaseFix):
5454
>
5555
"""
5656

57-
def start_tree(self, *args):
58-
super(FixMap, self).start_tree(*args)
59-
self._future_map_found = None
60-
61-
def has_future_map(self, node):
62-
if self._future_map_found is not None:
63-
return self._future_map_found
64-
self._future_map_found = does_tree_import('future_builtins', 'map', node)
65-
return self._future_map_found
57+
skip_on = 'future_builtins.map'
6658

6759
def transform(self, node, results):
68-
if self.has_future_map(node):
69-
# If a future map has been imported for this file, we won't
70-
# be making any modifications
60+
if self.should_skip(node):
7161
return
7262

7363
if node.parent.type == syms.simple_stmt:

Lib/lib2to3/fixes/fix_print.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
'print ...' into 'print(...)'
99
'print ... ,' into 'print(..., end=" ")'
1010
'print >>x, ...' into 'print(..., file=x)'
11+
12+
No changes are applied if print_function is imported from __future__
13+
1114
"""
1215

1316
# Local imports
@@ -23,14 +26,20 @@
2326
)
2427

2528

26-
class FixPrint(basefix.BaseFix):
29+
class FixPrint(basefix.ConditionalFix):
2730

2831
PATTERN = """
2932
simple_stmt< bare='print' any > | print_stmt
3033
"""
3134

35+
skip_on = '__future__.print_function'
36+
3237
def transform(self, node, results):
3338
assert results
39+
40+
if self.should_skip(node):
41+
return
42+
3443
bare_print = results.get("bare")
3544

3645
if bare_print:

Lib/lib2to3/fixes/fix_zip.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,24 @@
99

1010
# Local imports
1111
from . import basefix
12-
from .util import Name, Call, does_tree_import, in_special_context
12+
from .util import Name, Call, in_special_context
1313

14-
class FixZip(basefix.BaseFix):
14+
class FixZip(basefix.ConditionalFix):
1515

1616
PATTERN = """
1717
power< 'zip' args=trailer< '(' [any] ')' >
1818
>
1919
"""
2020

21-
def start_tree(self, *args):
22-
super(FixZip, self).start_tree(*args)
23-
self._future_zip_found = None
24-
25-
def has_future_zip(self, node):
26-
if self._future_zip_found is not None:
27-
return self._future_zip_found
28-
self._future_zip_found = does_tree_import('future_builtins', 'zip', node)
29-
return self._future_zip_found
21+
skip_on = "future_builtins.zip"
3022

3123
def transform(self, node, results):
32-
if self.has_future_zip(node):
33-
# If a future zip has been imported for this file, we won't
34-
# be making any modifications
24+
if self.should_skip(node):
3525
return
3626

3727
if in_special_context(node):
3828
return None
29+
3930
new = node.clone()
4031
new.set_prefix("")
4132
new = Call(Name("list"), [new])

Lib/lib2to3/pgen2/driver.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import sys
2222

2323
# Pgen imports
24-
from . import grammar, parse, token, tokenize
24+
from . import grammar, parse, token, tokenize, pgen
2525

2626

2727
class Driver(object):
@@ -123,7 +123,6 @@ def load_grammar(gt="Grammar.txt", gp=None,
123123
gp = head + tail + ".".join(map(str, sys.version_info)) + ".pickle"
124124
if force or not _newer(gp, gt):
125125
logger.info("Generating grammar tables from %s", gt)
126-
from pgen2 import pgen
127126
g = pgen.generate_grammar(gt)
128127
if save:
129128
logger.info("Writing grammar tables to %s", gp)

0 commit comments

Comments
 (0)