mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
merge 2to3 improvments
This commit is contained in:
parent
afcd5f36f0
commit
dd6a4edc45
18 changed files with 405 additions and 140 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
40
Lib/lib2to3/fixes/fix_operator.py
Normal file
40
Lib/lib2to3/fixes/fix_operator.py
Normal file
|
@ -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)
|
|
@ -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:
|
||||
|
|
|
@ -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' : [
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue