|
1 | 1 | """
|
2 | 2 | For the ``future`` package.
|
3 | 3 |
|
4 |
| -Adds this import line: |
| 4 | +Adds this import line:: |
5 | 5 |
|
6 |
| - from future.builtins import * |
| 6 | + from future.builtins import XYZ |
7 | 7 |
|
8 |
| -after any other imports (in an initial block of them). |
| 8 | +for each of the functions XYZ that is used in the module from those in |
| 9 | +future.builtins. |
| 10 | +
|
| 11 | +Adds these imports after any other imports (in an initial block of them). |
9 | 12 | """
|
10 | 13 |
|
11 |
| -from lib2to3.fixer_base import BaseFix |
| 14 | +from __future__ import unicode_literals |
| 15 | + |
| 16 | +from lib2to3 import fixer_base |
12 | 17 | from lib2to3.pygram import python_symbols as syms
|
13 | 18 | from libfuturize.fixer_util import touch_import_top
|
14 | 19 |
|
15 | 20 |
|
16 |
| -class FixFutureBuiltins(BaseFix): |
| 21 | +""" |
| 22 | +Fixer that changes zip(seq0, seq1, ...) into list(zip(seq0, seq1, ...) |
| 23 | +unless there exists a 'from future_builtins import zip' statement in the |
| 24 | +top-level namespace. |
| 25 | +
|
| 26 | +We avoid the transformation if the zip() call is directly contained in |
| 27 | +iter(<>), list(<>), tuple(<>), sorted(<>), ...join(<>), or for V in <>:. |
| 28 | +""" |
| 29 | + |
| 30 | +# Local imports |
| 31 | +from lib2to3.fixer_util import Name, Call, in_special_context |
| 32 | + |
| 33 | +# from future.builtins.iterators import (filter, map, zip) |
| 34 | +# from future.builtins.misc import (ascii, chr, hex, input, isinstance, oct, open) |
| 35 | +# from future.builtins.backports import (bytes, int, range, round, str, super) |
| 36 | + |
| 37 | +replaced_builtins = '''filter map zip |
| 38 | + ascii chr hex input isinstance oct open bytes int range |
| 39 | + bytes int range round str super'''.split() |
| 40 | + |
| 41 | +expression = '|'.join(["name='{}'".format(name) for name in replaced_builtins]) |
| 42 | + |
| 43 | + |
| 44 | +class FixFutureBuiltins(fixer_base.BaseFix): |
17 | 45 | BM_compatible = True
|
18 |
| - PATTERN = "file_input" |
19 |
| - run_order = 7 |
20 |
| - |
21 |
| - def match(self, node): |
22 |
| - if node.type == syms.file_input: |
23 |
| - return True |
24 |
| - return False |
25 |
| - |
| 46 | + run_order = 8 |
| 47 | + |
| 48 | + # Example: |
| 49 | + # PATTERN = """ |
| 50 | + # power< |
| 51 | + # (name='print'|name='input'|name='raw_input') trailer< '(' args=[any] ')' > |
| 52 | + # rest=any* > |
| 53 | + # """ |
| 54 | + |
| 55 | + PATTERN = """ |
| 56 | + power< |
| 57 | + ({}) trailer< '(' args=[any] ')' > |
| 58 | + rest=any* > |
| 59 | + """.format(expression) |
| 60 | + |
26 | 61 | def transform(self, node, results):
|
27 |
| - touch_import_top(u'future.builtins', u'*', node) |
| 62 | + name = results["name"] |
| 63 | + touch_import_top(u'future.builtins', name.value, node) |
| 64 | + # name.replace(Name(u"input", prefix=name.prefix)) |
| 65 | + |
| 66 | + |
28 | 67 |
|
0 commit comments