mirror of https://github.com/python/cpython.git
Merged revisions 76160-76161,76250,76252,76447,76506 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3 ........ r76160 | benjamin.peterson | 2009-11-08 18:53:48 -0600 (Sun, 08 Nov 2009) | 1 line undeprecate the -p option; it's useful for converting python3 sources ........ r76161 | benjamin.peterson | 2009-11-08 19:05:37 -0600 (Sun, 08 Nov 2009) | 1 line simplify condition ........ r76250 | benjamin.peterson | 2009-11-13 16:56:48 -0600 (Fri, 13 Nov 2009) | 1 line fix handling of a utf-8 bom #7313 ........ r76252 | benjamin.peterson | 2009-11-13 16:58:36 -0600 (Fri, 13 Nov 2009) | 1 line remove pdb turd ........ r76447 | benjamin.peterson | 2009-11-22 18:17:40 -0600 (Sun, 22 Nov 2009) | 1 line #7375 fix nested transformations in fix_urllib ........ r76506 | benjamin.peterson | 2009-11-24 18:34:31 -0600 (Tue, 24 Nov 2009) | 1 line use generator expressions in any() ........
This commit is contained in:
parent
2ed8813f22
commit
42d26d94cc
|
@ -108,7 +108,7 @@ def match(self, node):
|
||||||
# Module usage could be in the trailer of an attribute lookup, so we
|
# Module usage could be in the trailer of an attribute lookup, so we
|
||||||
# might have nested matches when "bare_with_attr" is present.
|
# might have nested matches when "bare_with_attr" is present.
|
||||||
if "bare_with_attr" not in results and \
|
if "bare_with_attr" not in results and \
|
||||||
any([match(obj) for obj in attr_chain(node, "parent")]):
|
any(match(obj) for obj in attr_chain(node, "parent")):
|
||||||
return False
|
return False
|
||||||
return results
|
return results
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -99,4 +99,4 @@ def find_assign(node):
|
||||||
def is_subtree(root, node):
|
def is_subtree(root, node):
|
||||||
if root == node:
|
if root == node:
|
||||||
return True
|
return True
|
||||||
return any([is_subtree(c, node) for c in root.children])
|
return any(is_subtree(c, node) for c in root.children)
|
||||||
|
|
|
@ -49,7 +49,7 @@ def match(self, node):
|
||||||
match = super(FixRenames, self).match
|
match = super(FixRenames, self).match
|
||||||
results = match(node)
|
results = match(node)
|
||||||
if results:
|
if results:
|
||||||
if any([match(obj) for obj in attr_chain(node, "parent")]):
|
if any(match(obj) for obj in attr_chain(node, "parent")):
|
||||||
return False
|
return False
|
||||||
return results
|
return results
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -63,7 +63,8 @@ def build_pattern():
|
||||||
yield """import_name< 'import'
|
yield """import_name< 'import'
|
||||||
dotted_as_name< module_as=%r 'as' any > >
|
dotted_as_name< module_as=%r 'as' any > >
|
||||||
""" % old_module
|
""" % old_module
|
||||||
yield """power< module_dot=%r trailer< '.' member=%s > any* >
|
# bare_with_attr has a special significance for FixImports.match().
|
||||||
|
yield """power< bare_with_attr=%r trailer< '.' member=%s > any* >
|
||||||
""" % (old_module, members)
|
""" % (old_module, members)
|
||||||
|
|
||||||
|
|
||||||
|
@ -150,12 +151,11 @@ def transform_member(self, node, results):
|
||||||
|
|
||||||
def transform_dot(self, node, results):
|
def transform_dot(self, node, results):
|
||||||
"""Transform for calls to module members in code."""
|
"""Transform for calls to module members in code."""
|
||||||
module_dot = results.get('module_dot')
|
module_dot = results.get('bare_with_attr')
|
||||||
member = results.get('member')
|
member = results.get('member')
|
||||||
# this may be a list of length one, or just a node
|
new_name = None
|
||||||
if isinstance(member, list):
|
if isinstance(member, list):
|
||||||
member = member[0]
|
member = member[0]
|
||||||
new_name = None
|
|
||||||
for change in MAPPING[module_dot.value]:
|
for change in MAPPING[module_dot.value]:
|
||||||
if member.value in change[1]:
|
if member.value in change[1]:
|
||||||
new_name = change[0]
|
new_name = change[0]
|
||||||
|
@ -171,7 +171,7 @@ def transform(self, node, results):
|
||||||
self.transform_import(node, results)
|
self.transform_import(node, results)
|
||||||
elif results.get('mod_member'):
|
elif results.get('mod_member'):
|
||||||
self.transform_member(node, results)
|
self.transform_member(node, results)
|
||||||
elif results.get('module_dot'):
|
elif results.get('bare_with_attr'):
|
||||||
self.transform_dot(node, results)
|
self.transform_dot(node, results)
|
||||||
# Renaming and star imports are not supported for these modules.
|
# Renaming and star imports are not supported for these modules.
|
||||||
elif results.get('module_star'):
|
elif results.get('module_star'):
|
||||||
|
|
|
@ -91,8 +91,7 @@ def main(fixer_pkg, args=None):
|
||||||
parser.add_option("-l", "--list-fixes", action="store_true",
|
parser.add_option("-l", "--list-fixes", action="store_true",
|
||||||
help="List available transformations (fixes/fix_*.py)")
|
help="List available transformations (fixes/fix_*.py)")
|
||||||
parser.add_option("-p", "--print-function", action="store_true",
|
parser.add_option("-p", "--print-function", action="store_true",
|
||||||
help="DEPRECATED Modify the grammar so that print() is "
|
help="Modify the grammar so that print() is a function")
|
||||||
"a function")
|
|
||||||
parser.add_option("-v", "--verbose", action="store_true",
|
parser.add_option("-v", "--verbose", action="store_true",
|
||||||
help="More verbose logging")
|
help="More verbose logging")
|
||||||
parser.add_option("--no-diffs", action="store_true",
|
parser.add_option("--no-diffs", action="store_true",
|
||||||
|
@ -104,12 +103,10 @@ def main(fixer_pkg, args=None):
|
||||||
|
|
||||||
# Parse command line arguments
|
# Parse command line arguments
|
||||||
refactor_stdin = False
|
refactor_stdin = False
|
||||||
|
flags = {}
|
||||||
options, args = parser.parse_args(args)
|
options, args = parser.parse_args(args)
|
||||||
if not options.write and options.no_diffs:
|
if not options.write and options.no_diffs:
|
||||||
warn("not writing files and not printing diffs; that's not very useful")
|
warn("not writing files and not printing diffs; that's not very useful")
|
||||||
if options.print_function:
|
|
||||||
warn("-p is deprecated; "
|
|
||||||
"detection of from __future__ import print_function is automatic")
|
|
||||||
if not options.write and options.nobackups:
|
if not options.write and options.nobackups:
|
||||||
parser.error("Can't use -n without -w")
|
parser.error("Can't use -n without -w")
|
||||||
if options.list_fixes:
|
if options.list_fixes:
|
||||||
|
@ -127,6 +124,8 @@ def main(fixer_pkg, args=None):
|
||||||
if options.write:
|
if options.write:
|
||||||
print >> sys.stderr, "Can't write to stdin."
|
print >> sys.stderr, "Can't write to stdin."
|
||||||
return 2
|
return 2
|
||||||
|
if options.print_function:
|
||||||
|
flags["print_function"] = True
|
||||||
|
|
||||||
# Set up logging handler
|
# Set up logging handler
|
||||||
level = logging.DEBUG if options.verbose else logging.INFO
|
level = logging.DEBUG if options.verbose else logging.INFO
|
||||||
|
@ -147,7 +146,7 @@ def main(fixer_pkg, args=None):
|
||||||
else:
|
else:
|
||||||
requested = avail_fixes.union(explicit)
|
requested = avail_fixes.union(explicit)
|
||||||
fixer_names = requested.difference(unwanted_fixes)
|
fixer_names = requested.difference(unwanted_fixes)
|
||||||
rt = StdoutRefactoringTool(sorted(fixer_names), None, sorted(explicit),
|
rt = StdoutRefactoringTool(sorted(fixer_names), flags, sorted(explicit),
|
||||||
options.nobackups, not options.no_diffs)
|
options.nobackups, not options.no_diffs)
|
||||||
|
|
||||||
# Refactor all files and directories passed as arguments
|
# Refactor all files and directories passed as arguments
|
||||||
|
|
|
@ -281,9 +281,13 @@ def find_cookie(line):
|
||||||
# This behaviour mimics the Python interpreter
|
# This behaviour mimics the Python interpreter
|
||||||
raise SyntaxError("unknown encoding: " + encoding)
|
raise SyntaxError("unknown encoding: " + encoding)
|
||||||
|
|
||||||
if bom_found and codec.name != 'utf-8':
|
if bom_found:
|
||||||
# This behaviour mimics the Python interpreter
|
if codec.name != 'utf-8':
|
||||||
raise SyntaxError('encoding problem: utf-8')
|
# This behaviour mimics the Python interpreter
|
||||||
|
raise SyntaxError('encoding problem: utf-8')
|
||||||
|
else:
|
||||||
|
# Allow it to be properly encoded and decoded.
|
||||||
|
encoding = 'utf-8-sig'
|
||||||
return encoding
|
return encoding
|
||||||
|
|
||||||
first = read_or_stop()
|
first = read_or_stop()
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
import operator
|
import operator
|
||||||
import collections
|
import collections
|
||||||
import StringIO
|
import StringIO
|
||||||
import warnings
|
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
# Local imports
|
# Local imports
|
||||||
|
@ -139,26 +138,23 @@ def advance():
|
||||||
if have_docstring:
|
if have_docstring:
|
||||||
break
|
break
|
||||||
have_docstring = True
|
have_docstring = True
|
||||||
elif tp == token.NAME:
|
elif tp == token.NAME and value == u"from":
|
||||||
if value == u"from":
|
tp, value = advance()
|
||||||
tp, value = advance()
|
if tp != token.NAME and value != u"__future__":
|
||||||
if tp != token.NAME and value != u"__future__":
|
|
||||||
break
|
|
||||||
tp, value = advance()
|
|
||||||
if tp != token.NAME and value != u"import":
|
|
||||||
break
|
|
||||||
tp, value = advance()
|
|
||||||
if tp == token.OP and value == u"(":
|
|
||||||
tp, value = advance()
|
|
||||||
while tp == token.NAME:
|
|
||||||
if value == u"print_function":
|
|
||||||
return True
|
|
||||||
tp, value = advance()
|
|
||||||
if tp != token.OP and value != u",":
|
|
||||||
break
|
|
||||||
tp, value = advance()
|
|
||||||
else:
|
|
||||||
break
|
break
|
||||||
|
tp, value = advance()
|
||||||
|
if tp != token.NAME and value != u"import":
|
||||||
|
break
|
||||||
|
tp, value = advance()
|
||||||
|
if tp == token.OP and value == u"(":
|
||||||
|
tp, value = advance()
|
||||||
|
while tp == token.NAME:
|
||||||
|
if value == u"print_function":
|
||||||
|
return True
|
||||||
|
tp, value = advance()
|
||||||
|
if tp != token.OP and value != u",":
|
||||||
|
break
|
||||||
|
tp, value = advance()
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
|
@ -172,7 +168,7 @@ class FixerError(Exception):
|
||||||
|
|
||||||
class RefactoringTool(object):
|
class RefactoringTool(object):
|
||||||
|
|
||||||
_default_options = {}
|
_default_options = {"print_function" : False}
|
||||||
|
|
||||||
CLASS_PREFIX = "Fix" # The prefix for fixer classes
|
CLASS_PREFIX = "Fix" # The prefix for fixer classes
|
||||||
FILE_PREFIX = "fix_" # The prefix for modules with a fixer within
|
FILE_PREFIX = "fix_" # The prefix for modules with a fixer within
|
||||||
|
@ -189,15 +185,16 @@ def __init__(self, fixer_names, options=None, explicit=None):
|
||||||
self.explicit = explicit or []
|
self.explicit = explicit or []
|
||||||
self.options = self._default_options.copy()
|
self.options = self._default_options.copy()
|
||||||
if options is not None:
|
if options is not None:
|
||||||
if "print_function" in options:
|
|
||||||
warnings.warn("the 'print_function' option is deprecated",
|
|
||||||
DeprecationWarning)
|
|
||||||
self.options.update(options)
|
self.options.update(options)
|
||||||
|
if self.options["print_function"]:
|
||||||
|
self.grammar = pygram.python_grammar_no_print_statement
|
||||||
|
else:
|
||||||
|
self.grammar = pygram.python_grammar
|
||||||
self.errors = []
|
self.errors = []
|
||||||
self.logger = logging.getLogger("RefactoringTool")
|
self.logger = logging.getLogger("RefactoringTool")
|
||||||
self.fixer_log = []
|
self.fixer_log = []
|
||||||
self.wrote = False
|
self.wrote = False
|
||||||
self.driver = driver.Driver(pygram.python_grammar,
|
self.driver = driver.Driver(self.grammar,
|
||||||
convert=pytree.convert,
|
convert=pytree.convert,
|
||||||
logger=self.logger)
|
logger=self.logger)
|
||||||
self.pre_order, self.post_order = self.get_fixers()
|
self.pre_order, self.post_order = self.get_fixers()
|
||||||
|
@ -353,7 +350,7 @@ def refactor_string(self, data, name):
|
||||||
name, err.__class__.__name__, err)
|
name, err.__class__.__name__, err)
|
||||||
return
|
return
|
||||||
finally:
|
finally:
|
||||||
self.driver.grammar = pygram.python_grammar
|
self.driver.grammar = self.grammar
|
||||||
self.log_debug("Refactoring %s", name)
|
self.log_debug("Refactoring %s", name)
|
||||||
self.refactor_tree(tree, name)
|
self.refactor_tree(tree, name)
|
||||||
return tree
|
return tree
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
# coding: utf-8
|
||||||
|
print "BOM BOOM!"
|
||||||
|
|
|
@ -1753,6 +1753,8 @@ def test_import_module_usage(self):
|
||||||
for old, changes in self.modules.items():
|
for old, changes in self.modules.items():
|
||||||
for new, members in changes:
|
for new, members in changes:
|
||||||
for member in members:
|
for member in members:
|
||||||
|
new_import = ", ".join([n for (n, mems)
|
||||||
|
in self.modules[old]])
|
||||||
b = """
|
b = """
|
||||||
import %s
|
import %s
|
||||||
foo(%s.%s)
|
foo(%s.%s)
|
||||||
|
@ -1760,9 +1762,16 @@ def test_import_module_usage(self):
|
||||||
a = """
|
a = """
|
||||||
import %s
|
import %s
|
||||||
foo(%s.%s)
|
foo(%s.%s)
|
||||||
""" % (", ".join([n for (n, mems)
|
""" % (new_import, new, member)
|
||||||
in self.modules[old]]),
|
self.check(b, a)
|
||||||
new, member)
|
b = """
|
||||||
|
import %s
|
||||||
|
%s.%s(%s.%s)
|
||||||
|
""" % (old, old, member, old, member)
|
||||||
|
a = """
|
||||||
|
import %s
|
||||||
|
%s.%s(%s.%s)
|
||||||
|
""" % (new_import, new, member, new, member)
|
||||||
self.check(b, a)
|
self.check(b, a)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import codecs
|
||||||
import operator
|
import operator
|
||||||
import StringIO
|
import StringIO
|
||||||
import tempfile
|
import tempfile
|
||||||
|
@ -45,12 +46,10 @@ def rt(self, options=None, fixers=_DEFAULT_FIXERS, explicit=None):
|
||||||
return refactor.RefactoringTool(fixers, options, explicit)
|
return refactor.RefactoringTool(fixers, options, explicit)
|
||||||
|
|
||||||
def test_print_function_option(self):
|
def test_print_function_option(self):
|
||||||
with warnings.catch_warnings(record=True) as w:
|
rt = self.rt({"print_function" : True})
|
||||||
warnings.simplefilter("always", DeprecationWarning)
|
self.assertTrue(rt.grammar is pygram.python_grammar_no_print_statement)
|
||||||
refactor.RefactoringTool(_DEFAULT_FIXERS, {"print_function" : True})
|
self.assertTrue(rt.driver.grammar is
|
||||||
self.assertEqual(len(w), 1)
|
pygram.python_grammar_no_print_statement)
|
||||||
msg, = w
|
|
||||||
self.assertTrue(msg.category is DeprecationWarning)
|
|
||||||
|
|
||||||
def test_fixer_loading_helpers(self):
|
def test_fixer_loading_helpers(self):
|
||||||
contents = ["explicit", "first", "last", "parrot", "preorder"]
|
contents = ["explicit", "first", "last", "parrot", "preorder"]
|
||||||
|
@ -179,10 +178,12 @@ def read_file():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
rt.refactor_file(test_file, True)
|
rt.refactor_file(test_file, True)
|
||||||
self.assertNotEqual(old_contents, read_file())
|
new_contents = read_file()
|
||||||
|
self.assertNotEqual(old_contents, new_contents)
|
||||||
finally:
|
finally:
|
||||||
with open(test_file, "wb") as fp:
|
with open(test_file, "wb") as fp:
|
||||||
fp.write(old_contents)
|
fp.write(old_contents)
|
||||||
|
return new_contents
|
||||||
|
|
||||||
def test_refactor_file(self):
|
def test_refactor_file(self):
|
||||||
test_file = os.path.join(FIXER_DIR, "parrot_example.py")
|
test_file = os.path.join(FIXER_DIR, "parrot_example.py")
|
||||||
|
@ -223,6 +224,11 @@ def test_file_encoding(self):
|
||||||
fn = os.path.join(TEST_DATA_DIR, "different_encoding.py")
|
fn = os.path.join(TEST_DATA_DIR, "different_encoding.py")
|
||||||
self.check_file_refactoring(fn)
|
self.check_file_refactoring(fn)
|
||||||
|
|
||||||
|
def test_bom(self):
|
||||||
|
fn = os.path.join(TEST_DATA_DIR, "bom.py")
|
||||||
|
data = self.check_file_refactoring(fn)
|
||||||
|
self.assertTrue(data.startswith(codecs.BOM_UTF8))
|
||||||
|
|
||||||
def test_crlf_newlines(self):
|
def test_crlf_newlines(self):
|
||||||
old_sep = os.linesep
|
old_sep = os.linesep
|
||||||
os.linesep = "\r\n"
|
os.linesep = "\r\n"
|
||||||
|
|
Loading…
Reference in New Issue