diff options
Diffstat (limited to 'Lib/lib2to3/fixes')
-rw-r--r-- | Lib/lib2to3/fixes/fix_import.py | 4 | ||||
-rw-r--r-- | Lib/lib2to3/fixes/fix_imports.py | 2 | ||||
-rw-r--r-- | Lib/lib2to3/fixes/fix_long.py | 11 | ||||
-rw-r--r-- | Lib/lib2to3/fixes/fix_ne.py | 4 | ||||
-rw-r--r-- | Lib/lib2to3/fixes/fix_numliterals.py | 5 | ||||
-rw-r--r-- | Lib/lib2to3/fixes/fix_operator.py | 40 | ||||
-rw-r--r-- | Lib/lib2to3/fixes/fix_print.py | 7 | ||||
-rw-r--r-- | Lib/lib2to3/fixes/fix_urllib.py | 10 |
8 files changed, 58 insertions, 25 deletions
diff --git a/Lib/lib2to3/fixes/fix_import.py b/Lib/lib2to3/fixes/fix_import.py index 103c3ef..998fe61 100644 --- a/Lib/lib2to3/fixes/fix_import.py +++ b/Lib/lib2to3/fixes/fix_import.py @@ -12,7 +12,7 @@ Becomes: # Local imports from .. import fixer_base -from os.path import dirname, join, exists, pathsep +from os.path import dirname, join, exists, sep from ..fixer_util import FromImport, syms, token @@ -84,7 +84,7 @@ class FixImport(fixer_base.BaseFix): # so can't be a relative import. if not exists(join(dirname(base_path), '__init__.py')): return False - for ext in ['.py', pathsep, '.pyc', '.so', '.sl', '.pyd']: + for ext in ['.py', sep, '.pyc', '.so', '.sl', '.pyd']: if exists(base_path + ext): return True return False diff --git a/Lib/lib2to3/fixes/fix_imports.py b/Lib/lib2to3/fixes/fix_imports.py index 9aeca38..6c41c71 100644 --- a/Lib/lib2to3/fixes/fix_imports.py +++ b/Lib/lib2to3/fixes/fix_imports.py @@ -84,8 +84,6 @@ def build_pattern(mapping=MAPPING): class FixImports(fixer_base.BaseFix): - order = "pre" # Pre-order tree traversal - # This is overridden in fix_imports2. mapping = MAPPING diff --git a/Lib/lib2to3/fixes/fix_long.py b/Lib/lib2to3/fixes/fix_long.py index b5e0bdb..9d25b74 100644 --- a/Lib/lib2to3/fixes/fix_long.py +++ b/Lib/lib2to3/fixes/fix_long.py @@ -5,18 +5,15 @@ """ # Local imports -from .. import fixer_base -from ..fixer_util import Name, Number, is_probably_builtin +from lib2to3 import fixer_base +from lib2to3.fixer_util import is_probably_builtin class FixLong(fixer_base.BaseFix): PATTERN = "'long'" - static_int = Name("int") - def transform(self, node, results): if is_probably_builtin(node): - new = self.static_int.clone() - new.prefix = node.prefix - return new + node.value = "int" + node.changed() diff --git a/Lib/lib2to3/fixes/fix_ne.py b/Lib/lib2to3/fixes/fix_ne.py index 1472036..e3ee10f 100644 --- a/Lib/lib2to3/fixes/fix_ne.py +++ b/Lib/lib2to3/fixes/fix_ne.py @@ -12,9 +12,11 @@ from .. import fixer_base class FixNe(fixer_base.BaseFix): # This is so simple that we don't need the pattern compiler. + _accept_type = token.NOTEQUAL + def match(self, node): # Override - return node.type == token.NOTEQUAL and node.value == "<>" + return node.value == "<>" def transform(self, node, results): new = pytree.Leaf(token.NOTEQUAL, "!=", prefix=node.prefix) diff --git a/Lib/lib2to3/fixes/fix_numliterals.py b/Lib/lib2to3/fixes/fix_numliterals.py index 6552740..79207d4 100644 --- a/Lib/lib2to3/fixes/fix_numliterals.py +++ b/Lib/lib2to3/fixes/fix_numliterals.py @@ -12,10 +12,11 @@ from ..fixer_util import Number class FixNumliterals(fixer_base.BaseFix): # This is so simple that we don't need the pattern compiler. + _accept_type = token.NUMBER + def match(self, node): # Override - return (node.type == token.NUMBER and - (node.value.startswith("0") or node.value[-1] in "Ll")) + return (node.value.startswith("0") or node.value[-1] in "Ll") def transform(self, node, results): val = node.value diff --git a/Lib/lib2to3/fixes/fix_operator.py b/Lib/lib2to3/fixes/fix_operator.py new file mode 100644 index 0000000..9b1089c --- /dev/null +++ b/Lib/lib2to3/fixes/fix_operator.py @@ -0,0 +1,40 @@ +"""Fixer for operator.{isCallable,sequenceIncludes} + +operator.isCallable(obj) -> hasattr(obj, '__call__') +operator.sequenceIncludes(obj) -> operator.contains(obj) +""" + +# Local imports +from .. import fixer_base +from ..fixer_util import Call, Name, String + +class FixOperator(fixer_base.BaseFix): + + methods = "method=('isCallable'|'sequenceIncludes')" + func = "'(' func=any ')'" + PATTERN = """ + power< module='operator' + trailer< '.' {methods} > trailer< {func} > > + | + power< {methods} trailer< {func} > > + """.format(methods=methods, func=func) + + def transform(self, node, results): + method = results["method"][0] + + if method.value == "sequenceIncludes": + if "module" not in results: + # operator may not be in scope, so we can't make a change. + self.warning(node, "You should use operator.contains here.") + else: + method.value = "contains" + method.changed() + elif method.value == "isCallable": + if "module" not in results: + self.warning(node, + "You should use hasattr(%s, '__call__') here." % + results["func"].value) + else: + func = results["func"] + args = [func.clone(), String(", "), String("'__call__'")] + return Call(Name("hasattr"), args, prefix=node.prefix) diff --git a/Lib/lib2to3/fixes/fix_print.py b/Lib/lib2to3/fixes/fix_print.py index abce251..cc7934a 100644 --- a/Lib/lib2to3/fixes/fix_print.py +++ b/Lib/lib2to3/fixes/fix_print.py @@ -26,20 +26,15 @@ parend_expr = patcomp.compile_pattern( ) -class FixPrint(fixer_base.ConditionalFix): +class FixPrint(fixer_base.BaseFix): PATTERN = """ simple_stmt< any* bare='print' any* > | print_stmt """ - skip_on = '__future__.print_function' - def transform(self, node, results): assert results - if self.should_skip(node): - return - bare_print = results.get("bare") if bare_print: diff --git a/Lib/lib2to3/fixes/fix_urllib.py b/Lib/lib2to3/fixes/fix_urllib.py index a89266e..d11220c 100644 --- a/Lib/lib2to3/fixes/fix_urllib.py +++ b/Lib/lib2to3/fixes/fix_urllib.py @@ -12,13 +12,13 @@ from ..fixer_util import Name, Comma, FromImport, Newline, attr_chain MAPPING = {'urllib': [ ('urllib.request', ['URLOpener', 'FancyURLOpener', 'urlretrieve', - '_urlopener', 'urlopen', 'urlcleanup']), + '_urlopener', 'urlopen', 'urlcleanup', + 'pathname2url', 'url2pathname']), ('urllib.parse', ['quote', 'quote_plus', 'unquote', 'unquote_plus', - 'urlencode', 'pathname2url', 'url2pathname', 'splitattr', - 'splithost', 'splitnport', 'splitpasswd', 'splitport', - 'splitquery', 'splittag', 'splittype', 'splituser', - 'splitvalue', ]), + 'urlencode', 'splitattr', 'splithost', 'splitnport', + 'splitpasswd', 'splitport', 'splitquery', 'splittag', + 'splittype', 'splituser', 'splitvalue', ]), ('urllib.error', ['ContentTooShortError'])], 'urllib2' : [ |