Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
47 edited
1 copied

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/lib2to3/fixes/fix_apply.py

    r2 r391  
    1313
    1414class FixApply(fixer_base.BaseFix):
     15    BM_compatible = True
    1516
    1617    PATTERN = """
  • python/trunk/Lib/lib2to3/fixes/fix_basestring.py

    r2 r391  
    77
    88class FixBasestring(fixer_base.BaseFix):
     9    BM_compatible = True
    910
    1011    PATTERN = "'basestring'"
  • python/trunk/Lib/lib2to3/fixes/fix_buffer.py

    r2 r391  
    1010
    1111class FixBuffer(fixer_base.BaseFix):
     12    BM_compatible = True
    1213
    1314    explicit = True # The user must ask for this fixer
  • python/trunk/Lib/lib2to3/fixes/fix_callable.py

    r2 r391  
    1212
    1313class FixCallable(fixer_base.BaseFix):
     14    BM_compatible = True
     15
     16    order = "pre"
    1417
    1518    # Ignore callable(*args) or use of keywords.
  • python/trunk/Lib/lib2to3/fixes/fix_dict.py

    r2 r391  
    4141
    4242class FixDict(fixer_base.BaseFix):
     43    BM_compatible = True
     44
    4345    PATTERN = """
    4446    power< head=any+
  • python/trunk/Lib/lib2to3/fixes/fix_except.py

    r2 r391  
    3535
    3636class FixExcept(fixer_base.BaseFix):
     37    BM_compatible = True
    3738
    3839    PATTERN = """
  • python/trunk/Lib/lib2to3/fixes/fix_exec.py

    r2 r391  
    1717
    1818class FixExec(fixer_base.BaseFix):
     19    BM_compatible = True
    1920
    2021    PATTERN = """
  • python/trunk/Lib/lib2to3/fixes/fix_execfile.py

    r2 r391  
    1414
    1515class FixExecfile(fixer_base.BaseFix):
     16    BM_compatible = True
    1617
    1718    PATTERN = """
  • python/trunk/Lib/lib2to3/fixes/fix_filter.py

    r2 r391  
    2020
    2121class FixFilter(fixer_base.ConditionalFix):
     22    BM_compatible = True
    2223
    2324    PATTERN = """
  • python/trunk/Lib/lib2to3/fixes/fix_funcattrs.py

    r2 r391  
    88
    99class FixFuncattrs(fixer_base.BaseFix):
     10    BM_compatible = True
     11
    1012    PATTERN = """
    1113    power< any+ trailer< '.' attr=('func_closure' | 'func_doc' | 'func_globals'
  • python/trunk/Lib/lib2to3/fixes/fix_future.py

    r2 r391  
    1010
    1111class FixFuture(fixer_base.BaseFix):
     12    BM_compatible = True
     13
    1214    PATTERN = """import_from< 'from' module_name="__future__" 'import' any >"""
    1315
  • python/trunk/Lib/lib2to3/fixes/fix_getcwdu.py

    r2 r391  
    99
    1010class FixGetcwdu(fixer_base.BaseFix):
     11    BM_compatible = True
    1112
    1213    PATTERN = """
  • python/trunk/Lib/lib2to3/fixes/fix_has_key.py

    r2 r391  
    3838
    3939class FixHasKey(fixer_base.BaseFix):
     40    BM_compatible = True
    4041
    4142    PATTERN = """
  • python/trunk/Lib/lib2to3/fixes/fix_idioms.py

    r2 r391  
    3636
    3737class FixIdioms(fixer_base.BaseFix):
    38 
    3938    explicit = True # The user must ask for this fixer
    4039
  • python/trunk/Lib/lib2to3/fixes/fix_import.py

    r2 r391  
    3737
    3838class FixImport(fixer_base.BaseFix):
     39    BM_compatible = True
    3940
    4041    PATTERN = """
     
    4445    """
    4546
     47    def start_tree(self, tree, name):
     48        super(FixImport, self).start_tree(tree, name)
     49        self.skip = "absolute_import" in tree.future_features
     50
    4651    def transform(self, node, results):
     52        if self.skip:
     53            return
    4754        imp = results['imp']
    4855
     
    7279                return
    7380
    74             new = FromImport('.', [imp])
     81            new = FromImport(u".", [imp])
    7582            new.prefix = node.prefix
    7683            return new
    7784
    7885    def probably_a_local_import(self, imp_name):
    79         imp_name = imp_name.split('.', 1)[0]
     86        if imp_name.startswith(u"."):
     87            # Relative imports are certainly not local imports.
     88            return False
     89        imp_name = imp_name.split(u".", 1)[0]
    8090        base_path = dirname(self.filename)
    8191        base_path = join(base_path, imp_name)
    8292        # If there is no __init__.py next to the file its not in a package
    8393        # so can't be a relative import.
    84         if not exists(join(dirname(base_path), '__init__.py')):
     94        if not exists(join(dirname(base_path), "__init__.py")):
    8595            return False
    86         for ext in ['.py', sep, '.pyc', '.so', '.sl', '.pyd']:
     96        for ext in [".py", sep, ".pyc", ".so", ".sl", ".pyd"]:
    8797            if exists(base_path + ext):
    8898                return True
  • python/trunk/Lib/lib2to3/fixes/fix_imports.py

    r2 r391  
    8585class FixImports(fixer_base.BaseFix):
    8686
     87    BM_compatible = True
     88    keep_line_order = True
    8789    # This is overridden in fix_imports2.
    8890    mapping = MAPPING
  • python/trunk/Lib/lib2to3/fixes/fix_input.py

    r2 r391  
    1212
    1313class FixInput(fixer_base.BaseFix):
    14 
     14    BM_compatible = True
    1515    PATTERN = """
    1616              power< 'input' args=trailer< '(' [any] ')' > >
  • python/trunk/Lib/lib2to3/fixes/fix_intern.py

    r2 r391  
    1313
    1414class FixIntern(fixer_base.BaseFix):
     15    BM_compatible = True
     16    order = "pre"
    1517
    1618    PATTERN = """
  • python/trunk/Lib/lib2to3/fixes/fix_isinstance.py

    r2 r391  
    1515
    1616class FixIsinstance(fixer_base.BaseFix):
    17 
     17    BM_compatible = True
    1818    PATTERN = """
    1919    power<
  • python/trunk/Lib/lib2to3/fixes/fix_itertools.py

    r2 r391  
    1313
    1414class FixItertools(fixer_base.BaseFix):
    15     it_funcs = "('imap'|'ifilter'|'izip'|'ifilterfalse')"
     15    BM_compatible = True
     16    it_funcs = "('imap'|'ifilter'|'izip'|'izip_longest'|'ifilterfalse')"
    1617    PATTERN = """
    1718              power< it='itertools'
     
    2829        prefix = None
    2930        func = results['func'][0]
    30         if 'it' in results and func.value != u'ifilterfalse':
     31        if ('it' in results and
     32            func.value not in (u'ifilterfalse', u'izip_longest')):
    3133            dot, it = (results['dot'], results['it'])
    3234            # Remove the 'itertools'
    3335            prefix = it.prefix
    3436            it.remove()
    35             # Replace the node wich contains ('.', 'function') with the
    36             # function (to be consistant with the second part of the pattern)
     37            # Replace the node which contains ('.', 'function') with the
     38            # function (to be consistent with the second part of the pattern)
    3739            dot.remove()
    3840            func.parent.replace(func)
  • python/trunk/Lib/lib2to3/fixes/fix_itertools_imports.py

    r2 r391  
    77
    88class FixItertoolsImports(fixer_base.BaseFix):
     9    BM_compatible = True
    910    PATTERN = """
    1011              import_from< 'from' 'itertools' 'import' imports=any >
     
    2122                member = child.value
    2223                name_node = child
     24            elif child.type == token.STAR:
     25                # Just leave the import as is.
     26                return
    2327            else:
    2428                assert child.type == syms.import_as_name
     
    2832                child.value = None
    2933                child.remove()
    30             elif member_name == u'ifilterfalse':
     34            elif member_name in (u'ifilterfalse', u'izip_longest'):
    3135                node.changed()
    32                 name_node.value = u'filterfalse'
     36                name_node.value = (u'filterfalse' if member_name[1] == u'f'
     37                                   else u'zip_longest')
    3338
    3439        # Make sure the import statement is still sane
     
    4146                remove_comma ^= True
    4247
    43         if children[-1].type == token.COMMA:
    44             children[-1].remove()
     48        while children and children[-1].type == token.COMMA:
     49            children.pop().remove()
    4550
    4651        # If there are no imports left, just get rid of the entire statement
    47         if not (imports.children or getattr(imports, 'value', None)) or \
    48                 imports.parent is None:
     52        if (not (imports.children or getattr(imports, 'value', None)) or
     53            imports.parent is None):
    4954            p = node.prefix
    5055            node = BlankLine()
  • python/trunk/Lib/lib2to3/fixes/fix_long.py

    r2 r391  
    1111
    1212class FixLong(fixer_base.BaseFix):
    13 
     13    BM_compatible = True
    1414    PATTERN = "'long'"
    1515
  • python/trunk/Lib/lib2to3/fixes/fix_map.py

    r2 r391  
    2727
    2828class FixMap(fixer_base.ConditionalFix):
     29    BM_compatible = True
    2930
    3031    PATTERN = """
  • python/trunk/Lib/lib2to3/fixes/fix_metaclass.py

    r2 r391  
    4949    for node in cls_node.children:
    5050        if node.type == syms.suite:
    51             # already in the prefered format, do nothing
     51            # already in the preferred format, do nothing
    5252            return
    5353
     
    7272    """ if there is a semi-colon all the parts count as part of the same
    7373        simple_stmt.  We just want the __metaclass__ part so we move
    74         everything efter the semi-colon into its own simple_stmt node
     74        everything after the semi-colon into its own simple_stmt node
    7575    """
    7676    for semi_ind, node in enumerate(stmt_node.children):
     
    144144
    145145class FixMetaclass(fixer_base.BaseFix):
     146    BM_compatible = True
    146147
    147148    PATTERN = """
  • python/trunk/Lib/lib2to3/fixes/fix_methodattrs.py

    r2 r391  
    1414
    1515class FixMethodattrs(fixer_base.BaseFix):
     16    BM_compatible = True
    1617    PATTERN = """
    1718    power< any+ trailer< '.' attr=('im_func' | 'im_self' | 'im_class') > any* >
  • python/trunk/Lib/lib2to3/fixes/fix_next.py

    r2 r391  
    1616
    1717class FixNext(fixer_base.BaseFix):
     18    BM_compatible = True
    1819    PATTERN = """
    1920    power< base=any+ trailer< '.' attr='next' > trailer< '(' ')' > >
  • python/trunk/Lib/lib2to3/fixes/fix_nonzero.py

    r2 r391  
    77
    88class FixNonzero(fixer_base.BaseFix):
     9    BM_compatible = True
    910    PATTERN = """
    1011    classdef< 'class' any+ ':'
  • python/trunk/Lib/lib2to3/fixes/fix_operator.py

    r2 r391  
    1 """Fixer for operator.{isCallable,sequenceIncludes}
     1"""Fixer for operator functions.
    22
    3 operator.isCallable(obj) -> hasattr(obj, '__call__')
     3operator.isCallable(obj)       -> hasattr(obj, '__call__')
    44operator.sequenceIncludes(obj) -> operator.contains(obj)
     5operator.isSequenceType(obj)   -> isinstance(obj, collections.Sequence)
     6operator.isMappingType(obj)    -> isinstance(obj, collections.Mapping)
     7operator.isNumberType(obj)     -> isinstance(obj, numbers.Number)
     8operator.repeat(obj, n)        -> operator.mul(obj, n)
     9operator.irepeat(obj, n)       -> operator.imul(obj, n)
    510"""
    611
    712# Local imports
    8 from .. import fixer_base
    9 from ..fixer_util import Call, Name, String
     13from lib2to3 import fixer_base
     14from lib2to3.fixer_util import Call, Name, String, touch_import
     15
     16
     17def invocation(s):
     18    def dec(f):
     19        f.invocation = s
     20        return f
     21    return dec
     22
    1023
    1124class FixOperator(fixer_base.BaseFix):
     25    BM_compatible = True
     26    order = "pre"
    1227
    13     methods = "method=('isCallable'|'sequenceIncludes')"
    14     func = "'(' func=any ')'"
     28    methods = """
     29              method=('isCallable'|'sequenceIncludes'
     30                     |'isSequenceType'|'isMappingType'|'isNumberType'
     31                     |'repeat'|'irepeat')
     32              """
     33    obj = "'(' obj=any ')'"
    1534    PATTERN = """
    1635              power< module='operator'
    17                 trailer< '.' {methods} > trailer< {func} > >
     36                trailer< '.' %(methods)s > trailer< %(obj)s > >
    1837              |
    19               power< {methods} trailer< {func} > >
    20               """.format(methods=methods, func=func)
     38              power< %(methods)s trailer< %(obj)s > >
     39              """ % dict(methods=methods, obj=obj)
    2140
    2241    def transform(self, node, results):
     42        method = self._check_method(node, results)
     43        if method is not None:
     44            return method(node, results)
     45
     46    @invocation("operator.contains(%s)")
     47    def _sequenceIncludes(self, node, results):
     48        return self._handle_rename(node, results, u"contains")
     49
     50    @invocation("hasattr(%s, '__call__')")
     51    def _isCallable(self, node, results):
     52        obj = results["obj"]
     53        args = [obj.clone(), String(u", "), String(u"'__call__'")]
     54        return Call(Name(u"hasattr"), args, prefix=node.prefix)
     55
     56    @invocation("operator.mul(%s)")
     57    def _repeat(self, node, results):
     58        return self._handle_rename(node, results, u"mul")
     59
     60    @invocation("operator.imul(%s)")
     61    def _irepeat(self, node, results):
     62        return self._handle_rename(node, results, u"imul")
     63
     64    @invocation("isinstance(%s, collections.Sequence)")
     65    def _isSequenceType(self, node, results):
     66        return self._handle_type2abc(node, results, u"collections", u"Sequence")
     67
     68    @invocation("isinstance(%s, collections.Mapping)")
     69    def _isMappingType(self, node, results):
     70        return self._handle_type2abc(node, results, u"collections", u"Mapping")
     71
     72    @invocation("isinstance(%s, numbers.Number)")
     73    def _isNumberType(self, node, results):
     74        return self._handle_type2abc(node, results, u"numbers", u"Number")
     75
     76    def _handle_rename(self, node, results, name):
    2377        method = results["method"][0]
     78        method.value = name
     79        method.changed()
    2480
    25         if method.value == u"sequenceIncludes":
    26             if "module" not in results:
    27                 # operator may not be in scope, so we can't make a change.
    28                 self.warning(node, "You should use operator.contains here.")
     81    def _handle_type2abc(self, node, results, module, abc):
     82        touch_import(None, module, node)
     83        obj = results["obj"]
     84        args = [obj.clone(), String(u", " + u".".join([module, abc]))]
     85        return Call(Name(u"isinstance"), args, prefix=node.prefix)
     86
     87    def _check_method(self, node, results):
     88        method = getattr(self, "_" + results["method"][0].value.encode("ascii"))
     89        if callable(method):
     90            if "module" in results:
     91                return method
    2992            else:
    30                 method.value = u"contains"
    31                 method.changed()
    32         elif method.value == u"isCallable":
    33             if "module" not in results:
    34                 self.warning(node,
    35                              "You should use hasattr(%s, '__call__') here." %
    36                              results["func"].value)
    37             else:
    38                 func = results["func"]
    39                 args = [func.clone(), String(u", "), String(u"'__call__'")]
    40                 return Call(Name(u"hasattr"), args, prefix=node.prefix)
     93                sub = (unicode(results["obj"]),)
     94                invocation_str = unicode(method.invocation) % sub
     95                self.warning(node, u"You should use '%s' here." % invocation_str)
     96        return None
  • python/trunk/Lib/lib2to3/fixes/fix_paren.py

    r2 r391  
    1111# XXX This doesn't support nested for loops like [x for x in 1, 2 for x in 1, 2]
    1212class FixParen(fixer_base.BaseFix):
     13    BM_compatible = True
     14
    1315    PATTERN = """
    1416        atom< ('[' | '(')
  • python/trunk/Lib/lib2to3/fixes/fix_print.py

    r2 r391  
    2828
    2929class FixPrint(fixer_base.BaseFix):
     30
     31    BM_compatible = True
    3032
    3133    PATTERN = """
  • python/trunk/Lib/lib2to3/fixes/fix_raise.py

    r2 r391  
    55raise E, V    -> raise E(V)
    66raise E, V, T -> raise E(V).with_traceback(T)
     7raise E, None, T -> raise E.with_traceback(T)
    78
    89raise (((E, E'), E''), E'''), V -> raise E(V)
     
    3031class FixRaise(fixer_base.BaseFix):
    3132
     33    BM_compatible = True
    3234    PATTERN = """
    3335    raise_stmt< 'raise' exc=any [',' val=any [',' tb=any]] >
     
    3840
    3941        exc = results["exc"].clone()
    40         if exc.type is token.STRING:
    41             self.cannot_convert(node, "Python 3 does not support string exceptions")
     42        if exc.type == token.STRING:
     43            msg = "Python 3 does not support string exceptions"
     44            self.cannot_convert(node, msg)
    4245            return
    4346
     
    5356                # exc.children[1].children[0] is the first element of the tuple
    5457                exc = exc.children[1].children[0].clone()
    55             exc.prefix = " "
     58            exc.prefix = u" "
    5659
    5760        if "val" not in results:
     
    7275            tb.prefix = u""
    7376
    74             e = Call(exc, args)
     77            e = exc
     78            # If there's a traceback and None is passed as the value, then don't
     79            # add a call, since the user probably just wants to add a
     80            # traceback. See issue #9661.
     81            if val.type != token.NAME or val.value != u"None":
     82                e = Call(exc, args)
    7583            with_tb = Attr(e, Name(u'with_traceback')) + [ArgList([tb])]
    7684            new = pytree.Node(syms.simple_stmt, [Name(u"raise")] + with_tb)
  • python/trunk/Lib/lib2to3/fixes/fix_raw_input.py

    r2 r391  
    88class FixRawInput(fixer_base.BaseFix):
    99
     10    BM_compatible = True
    1011    PATTERN = """
    1112              power< name='raw_input' trailer< '(' [any] ')' > any* >
  • python/trunk/Lib/lib2to3/fixes/fix_reduce.py

    r2 r391  
    88"""
    99
    10 from .. import pytree
    11 from .. import fixer_base
    12 from ..fixer_util import Name, Attr, touch_import
     10from lib2to3 import fixer_base
     11from lib2to3.fixer_util import touch_import
    1312
    1413
    1514
    1615class FixReduce(fixer_base.BaseFix):
     16
     17    BM_compatible = True
     18    order = "pre"
    1719
    1820    PATTERN = """
  • python/trunk/Lib/lib2to3/fixes/fix_renames.py

    r2 r391  
    4141
    4242class FixRenames(fixer_base.BaseFix):
     43    BM_compatible = True
    4344    PATTERN = "|".join(build_pattern())
    4445
  • python/trunk/Lib/lib2to3/fixes/fix_repr.py

    r2 r391  
    1111class FixRepr(fixer_base.BaseFix):
    1212
     13    BM_compatible = True
    1314    PATTERN = """
    1415              atom < '`' expr=any '`' >
  • python/trunk/Lib/lib2to3/fixes/fix_set_literal.py

    r2 r391  
    1212class FixSetLiteral(fixer_base.BaseFix):
    1313
     14    BM_compatible = True
    1415    explicit = True
    1516
  • python/trunk/Lib/lib2to3/fixes/fix_standarderror.py

    r2 r391  
    1010
    1111class FixStandarderror(fixer_base.BaseFix):
    12 
     12    BM_compatible = True
    1313    PATTERN = """
    1414              'StandardError'
  • python/trunk/Lib/lib2to3/fixes/fix_sys_exc.py

    r2 r391  
    1414class FixSysExc(fixer_base.BaseFix):
    1515    # This order matches the ordering of sys.exc_info().
    16     exc_info = ["exc_type", "exc_value", "exc_traceback"]
     16    exc_info = [u"exc_type", u"exc_value", u"exc_traceback"]
     17    BM_compatible = True
    1718    PATTERN = """
    1819              power< 'sys' trailer< dot='.' attribute=(%s) > >
  • python/trunk/Lib/lib2to3/fixes/fix_throw.py

    r2 r391  
    1515
    1616class FixThrow(fixer_base.BaseFix):
    17 
     17    BM_compatible = True
    1818    PATTERN = """
    1919    power< any trailer< '.' 'throw' >
  • python/trunk/Lib/lib2to3/fixes/fix_tuple_params.py

    r2 r391  
    3030
    3131class FixTupleParams(fixer_base.BaseFix):
     32    run_order = 4 #use a lower order since lambda is part of other
     33                  #patterns
     34    BM_compatible = True
     35
    3236    PATTERN = """
    3337              funcdef< 'def' any parameters< '(' args=any ')' >
     
    5559        else:
    5660            start = 0
    57             indent = "; "
     61            indent = u"; "
    5862            end = pytree.Leaf(token.INDENT, u"")
    5963
     
    155159        d = {}
    156160    for i, obj in enumerate(param_list):
    157         trailer = [Subscript(Number(i))]
     161        trailer = [Subscript(Number(unicode(i)))]
    158162        if isinstance(obj, list):
    159163            map_to_index(obj, trailer, d=d)
  • python/trunk/Lib/lib2to3/fixes/fix_types.py

    r2 r391  
    5353
    5454class FixTypes(fixer_base.BaseFix):
    55 
     55    BM_compatible = True
    5656    PATTERN = '|'.join(_pats)
    5757
  • python/trunk/Lib/lib2to3/fixes/fix_unicode.py

    r2 r391  
    1 """Fixer that changes unicode to str, unichr to chr, and u"..." into "...".
     1r"""Fixer for unicode.
     2
     3* Changes unicode to str and unichr to chr.
     4
     5* If "...\u..." is not unicode literal change it into "...\\u...".
     6
     7* Change u"..." into "...".
    28
    39"""
    410
    5 import re
    611from ..pgen2 import token
    712from .. import fixer_base
    813
    914_mapping = {u"unichr" : u"chr", u"unicode" : u"str"}
    10 _literal_re = re.compile(ur"[uU][rR]?[\'\"]")
    1115
    1216class FixUnicode(fixer_base.BaseFix):
     17    BM_compatible = True
     18    PATTERN = "STRING | 'unicode' | 'unichr'"
    1319
    14     PATTERN = "STRING | 'unicode' | 'unichr'"
     20    def start_tree(self, tree, filename):
     21        super(FixUnicode, self).start_tree(tree, filename)
     22        self.unicode_literals = 'unicode_literals' in tree.future_features
    1523
    1624    def transform(self, node, results):
     
    2028            return new
    2129        elif node.type == token.STRING:
    22             if _literal_re.match(node.value):
    23                 new = node.clone()
    24                 new.value = new.value[1:]
    25                 return new
     30            val = node.value
     31            if not self.unicode_literals and val[0] in u'\'"' and u'\\' in val:
     32                val = ur'\\'.join([
     33                    v.replace(u'\\u', ur'\\u').replace(u'\\U', ur'\\U')
     34                    for v in val.split(ur'\\')
     35                ])
     36            if val[0] in u'uU':
     37                val = val[1:]
     38            if val == node.value:
     39                return node
     40            new = node.clone()
     41            new.value = val
     42            return new
  • python/trunk/Lib/lib2to3/fixes/fix_urllib.py

    r2 r391  
    66
    77# Local imports
    8 from .fix_imports import alternates, FixImports
    9 from .. import fixer_base
    10 from ..fixer_util import Name, Comma, FromImport, Newline, attr_chain
     8from lib2to3.fixes.fix_imports import alternates, FixImports
     9from lib2to3 import fixer_base
     10from lib2to3.fixer_util import (Name, Comma, FromImport, Newline,
     11                                find_indentation, Node, syms)
    1112
    12 MAPPING = {'urllib':  [
    13                 ('urllib.request',
    14                     ['URLOpener', 'FancyURLOpener', 'urlretrieve',
    15                      '_urlopener', 'urlopen', 'urlcleanup',
    16                      'pathname2url', 'url2pathname']),
    17                 ('urllib.parse',
    18                     ['quote', 'quote_plus', 'unquote', 'unquote_plus',
    19                      'urlencode', 'splitattr', 'splithost', 'splitnport',
    20                      'splitpasswd', 'splitport', 'splitquery', 'splittag',
    21                      'splittype', 'splituser', 'splitvalue', ]),
    22                 ('urllib.error',
    23                     ['ContentTooShortError'])],
    24            'urllib2' : [
    25                 ('urllib.request',
    26                     ['urlopen', 'install_opener', 'build_opener',
    27                      'Request', 'OpenerDirector', 'BaseHandler',
    28                      'HTTPDefaultErrorHandler', 'HTTPRedirectHandler',
    29                      'HTTPCookieProcessor', 'ProxyHandler',
    30                      'HTTPPasswordMgr',
    31                      'HTTPPasswordMgrWithDefaultRealm',
    32                      'AbstractBasicAuthHandler',
    33                      'HTTPBasicAuthHandler', 'ProxyBasicAuthHandler',
    34                      'AbstractDigestAuthHandler',
    35                      'HTTPDigestAuthHandler', 'ProxyDigestAuthHandler',
    36                      'HTTPHandler', 'HTTPSHandler', 'FileHandler',
    37                      'FTPHandler', 'CacheFTPHandler',
    38                      'UnknownHandler']),
    39                 ('urllib.error',
    40                     ['URLError', 'HTTPError']),
     13MAPPING = {"urllib":  [
     14                ("urllib.request",
     15                    ["URLopener", "FancyURLopener", "urlretrieve",
     16                     "_urlopener", "urlopen", "urlcleanup",
     17                     "pathname2url", "url2pathname"]),
     18                ("urllib.parse",
     19                    ["quote", "quote_plus", "unquote", "unquote_plus",
     20                     "urlencode", "splitattr", "splithost", "splitnport",
     21                     "splitpasswd", "splitport", "splitquery", "splittag",
     22                     "splittype", "splituser", "splitvalue", ]),
     23                ("urllib.error",
     24                    ["ContentTooShortError"])],
     25           "urllib2" : [
     26                ("urllib.request",
     27                    ["urlopen", "install_opener", "build_opener",
     28                     "Request", "OpenerDirector", "BaseHandler",
     29                     "HTTPDefaultErrorHandler", "HTTPRedirectHandler",
     30                     "HTTPCookieProcessor", "ProxyHandler",
     31                     "HTTPPasswordMgr",
     32                     "HTTPPasswordMgrWithDefaultRealm",
     33                     "AbstractBasicAuthHandler",
     34                     "HTTPBasicAuthHandler", "ProxyBasicAuthHandler",
     35                     "AbstractDigestAuthHandler",
     36                     "HTTPDigestAuthHandler", "ProxyDigestAuthHandler",
     37                     "HTTPHandler", "HTTPSHandler", "FileHandler",
     38                     "FTPHandler", "CacheFTPHandler",
     39                     "UnknownHandler"]),
     40                ("urllib.error",
     41                    ["URLError", "HTTPError"]),
    4142           ]
    4243}
     
    7980           replacements.
    8081        """
    81         import_mod = results.get('module')
     82        import_mod = results.get("module")
    8283        pref = import_mod.prefix
    8384
     
    9596           module.
    9697        """
    97         mod_member = results.get('mod_member')
     98        mod_member = results.get("mod_member")
    9899        pref = mod_member.prefix
    99         member = results.get('member')
     100        member = results.get("member")
    100101
    101102        # Simple case with only a single member being imported
     
    112113                mod_member.replace(Name(new_name, prefix=pref))
    113114            else:
    114                 self.cannot_convert(node,
    115                                     'This is an invalid module element')
     115                self.cannot_convert(node, "This is an invalid module element")
    116116
    117117        # Multiple members being imported
     
    120120            modules = []
    121121            mod_dict = {}
    122             members = results.get('members')
     122            members = results["members"]
    123123            for member in members:
    124                 member = member.value
    125124                # we only care about the actual members
    126                 if member != ',':
     125                if member.type == syms.import_as_name:
     126                    as_name = member.children[2].value
     127                    member_name = member.children[0].value
     128                else:
     129                    member_name = member.value
     130                    as_name = None
     131                if member_name != u",":
    127132                    for change in MAPPING[mod_member.value]:
    128                         if member in change[1]:
    129                             if change[0] in mod_dict:
    130                                 mod_dict[change[0]].append(member)
    131                             else:
    132                                 mod_dict[change[0]] = [member]
     133                        if member_name in change[1]:
     134                            if change[0] not in mod_dict:
    133135                                modules.append(change[0])
     136                            mod_dict.setdefault(change[0], []).append(member)
    134137
    135138            new_nodes = []
     139            indentation = find_indentation(node)
     140            first = True
     141            def handle_name(name, prefix):
     142                if name.type == syms.import_as_name:
     143                    kids = [Name(name.children[0].value, prefix=prefix),
     144                            name.children[1].clone(),
     145                            name.children[2].clone()]
     146                    return [Node(syms.import_as_name, kids)]
     147                return [Name(name.value, prefix=prefix)]
    136148            for module in modules:
    137149                elts = mod_dict[module]
    138150                names = []
    139151                for elt in elts[:-1]:
    140                     names.extend([Name(elt, prefix=pref), Comma()])
    141                 names.append(Name(elts[-1], prefix=pref))
    142                 new_nodes.append(FromImport(module, names))
     152                    names.extend(handle_name(elt, pref))
     153                    names.append(Comma())
     154                names.extend(handle_name(elts[-1], pref))
     155                new = FromImport(module, names)
     156                if not first or node.parent.prefix.endswith(indentation):
     157                    new.prefix = indentation
     158                new_nodes.append(new)
     159                first = False
    143160            if new_nodes:
    144161                nodes = []
     
    148165                node.replace(nodes)
    149166            else:
    150                 self.cannot_convert(node, 'All module elements are invalid')
     167                self.cannot_convert(node, "All module elements are invalid")
    151168
    152169    def transform_dot(self, node, results):
    153170        """Transform for calls to module members in code."""
    154         module_dot = results.get('bare_with_attr')
    155         member = results.get('member')
     171        module_dot = results.get("bare_with_attr")
     172        member = results.get("member")
    156173        new_name = None
    157174        if isinstance(member, list):
     
    165182                                    prefix=module_dot.prefix))
    166183        else:
    167             self.cannot_convert(node, 'This is an invalid module element')
     184            self.cannot_convert(node, "This is an invalid module element")
    168185
    169186    def transform(self, node, results):
    170         if results.get('module'):
     187        if results.get("module"):
    171188            self.transform_import(node, results)
    172         elif results.get('mod_member'):
     189        elif results.get("mod_member"):
    173190            self.transform_member(node, results)
    174         elif results.get('bare_with_attr'):
     191        elif results.get("bare_with_attr"):
    175192            self.transform_dot(node, results)
    176193        # Renaming and star imports are not supported for these modules.
    177         elif results.get('module_star'):
    178             self.cannot_convert(node, 'Cannot handle star imports.')
    179         elif results.get('module_as'):
    180             self.cannot_convert(node, 'This module is now multiple modules')
     194        elif results.get("module_star"):
     195            self.cannot_convert(node, "Cannot handle star imports.")
     196        elif results.get("module_as"):
     197            self.cannot_convert(node, "This module is now multiple modules")
  • python/trunk/Lib/lib2to3/fixes/fix_xrange.py

    r2 r391  
    1111
    1212class FixXrange(fixer_base.BaseFix):
    13 
     13    BM_compatible = True
    1414    PATTERN = """
    1515              power<
     
    1717              rest=any* >
    1818              """
     19
     20    def start_tree(self, tree, filename):
     21        super(FixXrange, self).start_tree(tree, filename)
     22        self.transformed_xranges = set()
     23
     24    def finish_tree(self, tree, filename):
     25        self.transformed_xranges = None
    1926
    2027    def transform(self, node, results):
     
    3037        name = results["name"]
    3138        name.replace(Name(u"range", prefix=name.prefix))
     39        # This prevents the new range call from being wrapped in a list later.
     40        self.transformed_xranges.add(id(node))
    3241
    3342    def transform_range(self, node, results):
    34         if not self.in_special_context(node):
     43        if (id(node) not in self.transformed_xranges and
     44            not self.in_special_context(node)):
    3545            range_call = Call(Name(u"range"), [results["args"].clone()])
    3646            # Encase the range call in list().
  • python/trunk/Lib/lib2to3/fixes/fix_xreadlines.py

    r2 r391  
    1010
    1111class FixXreadlines(fixer_base.BaseFix):
     12    BM_compatible = True
    1213    PATTERN = """
    1314    power< call=any+ trailer< '.' 'xreadlines' > trailer< '(' ')' > >
  • python/trunk/Lib/lib2to3/fixes/fix_zip.py

    r2 r391  
    1414class FixZip(fixer_base.ConditionalFix):
    1515
     16    BM_compatible = True
    1617    PATTERN = """
    1718    power< 'zip' args=trailer< '(' [any] ')' >
Note: See TracChangeset for help on using the changeset viewer.