From 2c3ac6b8757f8fc1dacf6aeaa9a9518d8729d52b Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 11 Jun 2009 23:47:38 +0000 Subject: Merged revisions 73370 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ................ r73370 | benjamin.peterson | 2009-06-11 17:06:46 -0500 (Thu, 11 Jun 2009) | 105 lines Merged revisions 72523,72950-72951,72994,73003,73033,73036-73040,73091-73093,73096,73179-73181,73192,73231,73244,73255-73256,73365 via svnmerge from svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3 ........ r72523 | benjamin.peterson | 2009-05-09 14:42:26 -0500 (Sat, 09 May 2009) | 1 line remove parenthesis ........ r72950 | benjamin.peterson | 2009-05-26 18:19:45 -0500 (Tue, 26 May 2009) | 1 line remove unused imports ........ r72951 | benjamin.peterson | 2009-05-26 18:27:00 -0500 (Tue, 26 May 2009) | 1 line this is no longer executable ........ r72994 | benjamin.peterson | 2009-05-28 15:32:54 -0500 (Thu, 28 May 2009) | 1 line fix test_all_fixers on Windows #6134 ........ r73003 | benjamin.peterson | 2009-05-28 21:57:28 -0500 (Thu, 28 May 2009) | 4 lines make 2to3 test utilities easier to use with other applications (3to2) Patch by Joe Amenta ........ r73033 | benjamin.peterson | 2009-05-29 16:58:32 -0500 (Fri, 29 May 2009) | 1 line update grammar for multi with statement ........ r73036 | benjamin.peterson | 2009-05-29 17:33:20 -0500 (Fri, 29 May 2009) | 1 line simplify fix_unicode ........ r73037 | benjamin.peterson | 2009-05-29 17:53:03 -0500 (Fri, 29 May 2009) | 1 line add custom error for pattern syntax errors ........ r73038 | benjamin.peterson | 2009-05-29 17:55:00 -0500 (Fri, 29 May 2009) | 1 line complain if details are attached to a token ........ r73039 | benjamin.peterson | 2009-05-29 18:00:28 -0500 (Fri, 29 May 2009) | 1 line add a test for whitespace ........ r73040 | benjamin.peterson | 2009-05-29 18:01:17 -0500 (Fri, 29 May 2009) | 1 line a fix for emacs highlighting ........ r73091 | benjamin.peterson | 2009-05-31 20:55:25 -0500 (Sun, 31 May 2009) | 1 line deprecate set_prefix() and get_prefix() in favor of a prefix property ........ r73092 | benjamin.peterson | 2009-05-31 21:00:51 -0500 (Sun, 31 May 2009) | 1 line change hideous java naming scheme ........ r73093 | benjamin.peterson | 2009-05-31 21:01:39 -0500 (Sun, 31 May 2009) | 1 line remove dated comment ........ r73096 | benjamin.peterson | 2009-05-31 21:40:53 -0500 (Sun, 31 May 2009) | 1 line group tests ........ r73179 | benjamin.peterson | 2009-06-03 13:09:53 -0500 (Wed, 03 Jun 2009) | 1 line handle the case where there's multiple trailers #6185 ........ r73180 | benjamin.peterson | 2009-06-03 13:18:05 -0500 (Wed, 03 Jun 2009) | 1 line scrap __main__ section ........ r73181 | benjamin.peterson | 2009-06-03 13:24:48 -0500 (Wed, 03 Jun 2009) | 1 line remove shebang lines and __main__ sections ........ r73192 | benjamin.peterson | 2009-06-03 19:16:30 -0500 (Wed, 03 Jun 2009) | 4 lines actually test something here Thanks to Joe Amenta for noticing.y ........ r73231 | benjamin.peterson | 2009-06-04 13:38:50 -0500 (Thu, 04 Jun 2009) | 1 line remove unused variable ........ r73244 | benjamin.peterson | 2009-06-05 08:39:25 -0500 (Fri, 05 Jun 2009) | 1 line allow fixers to give different options in setUp ........ r73255 | benjamin.peterson | 2009-06-06 11:23:46 -0500 (Sat, 06 Jun 2009) | 1 line fix the except fixer on one line suites #6222 ........ r73256 | benjamin.peterson | 2009-06-06 11:27:40 -0500 (Sat, 06 Jun 2009) | 1 line test one-line else and finally clauses ........ r73365 | benjamin.peterson | 2009-06-11 17:01:32 -0500 (Thu, 11 Jun 2009) | 1 line normalize whitespace ........ ................ --- Lib/lib2to3/Grammar.txt | 3 +- Lib/lib2to3/fixer_base.py | 2 +- Lib/lib2to3/fixer_util.py | 18 +-- Lib/lib2to3/fixes/fix_apply.py | 10 +- Lib/lib2to3/fixes/fix_basestring.py | 2 +- Lib/lib2to3/fixes/fix_buffer.py | 4 +- Lib/lib2to3/fixes/fix_callable.py | 2 +- Lib/lib2to3/fixes/fix_dict.py | 6 +- Lib/lib2to3/fixes/fix_except.py | 16 +-- Lib/lib2to3/fixes/fix_exec.py | 4 +- Lib/lib2to3/fixes/fix_execfile.py | 4 +- Lib/lib2to3/fixes/fix_filter.py | 4 +- Lib/lib2to3/fixes/fix_funcattrs.py | 2 +- Lib/lib2to3/fixes/fix_future.py | 2 +- Lib/lib2to3/fixes/fix_getcwdu.py | 2 +- Lib/lib2to3/fixes/fix_has_key.py | 6 +- Lib/lib2to3/fixes/fix_idioms.py | 18 +-- Lib/lib2to3/fixes/fix_import.py | 2 +- Lib/lib2to3/fixes/fix_imports.py | 4 +- Lib/lib2to3/fixes/fix_input.py | 4 +- Lib/lib2to3/fixes/fix_intern.py | 2 +- Lib/lib2to3/fixes/fix_isinstance.py | 2 +- Lib/lib2to3/fixes/fix_itertools.py | 4 +- Lib/lib2to3/fixes/fix_itertools_imports.py | 2 +- Lib/lib2to3/fixes/fix_long.py | 2 +- Lib/lib2to3/fixes/fix_map.py | 6 +- Lib/lib2to3/fixes/fix_metaclass.py | 16 +-- Lib/lib2to3/fixes/fix_methodattrs.py | 2 +- Lib/lib2to3/fixes/fix_ne.py | 3 +- Lib/lib2to3/fixes/fix_next.py | 9 +- Lib/lib2to3/fixes/fix_nonzero.py | 2 +- Lib/lib2to3/fixes/fix_numliterals.py | 2 +- Lib/lib2to3/fixes/fix_paren.py | 4 +- Lib/lib2to3/fixes/fix_print.py | 10 +- Lib/lib2to3/fixes/fix_raise.py | 12 +- Lib/lib2to3/fixes/fix_raw_input.py | 2 +- Lib/lib2to3/fixes/fix_renames.py | 2 +- Lib/lib2to3/fixes/fix_repr.py | 2 +- Lib/lib2to3/fixes/fix_set_literal.py | 6 +- Lib/lib2to3/fixes/fix_standarderror.py | 2 +- Lib/lib2to3/fixes/fix_sys_exc.py | 6 +- Lib/lib2to3/fixes/fix_throw.py | 4 +- Lib/lib2to3/fixes/fix_tuple_params.py | 14 +-- Lib/lib2to3/fixes/fix_types.py | 2 +- Lib/lib2to3/fixes/fix_unicode.py | 19 ++- Lib/lib2to3/fixes/fix_urllib.py | 6 +- Lib/lib2to3/fixes/fix_ws_comma.py | 8 +- Lib/lib2to3/fixes/fix_xrange.py | 4 +- Lib/lib2to3/fixes/fix_xreadlines.py | 2 +- Lib/lib2to3/fixes/fix_zip.py | 4 +- Lib/lib2to3/patcomp.py | 20 ++-- Lib/lib2to3/pytree.py | 69 +++++------ Lib/lib2to3/refactor.py | 7 +- Lib/lib2to3/tests/data/different_encoding.py | 3 +- .../tests/data/fixers/myfixes/fix_parrot.py | 2 +- Lib/lib2to3/tests/data/py2_test_grammar.py | 22 +++- Lib/lib2to3/tests/data/py3_test_grammar.py | 20 ++++ Lib/lib2to3/tests/pytree_idempotency.py | 2 +- Lib/lib2to3/tests/support.py | 6 +- Lib/lib2to3/tests/test_all_fixers.py | 13 +-- Lib/lib2to3/tests/test_fixers.py | 100 +++++++++++++--- Lib/lib2to3/tests/test_parser.py | 9 +- Lib/lib2to3/tests/test_pytree.py | 127 +++++++++++---------- Lib/lib2to3/tests/test_util.py | 7 -- 64 files changed, 381 insertions(+), 302 deletions(-) diff --git a/Lib/lib2to3/Grammar.txt b/Lib/lib2to3/Grammar.txt index 1f4a50f..4cf4e32 100644 --- a/Lib/lib2to3/Grammar.txt +++ b/Lib/lib2to3/Grammar.txt @@ -90,7 +90,8 @@ try_stmt: ('try' ':' suite ['else' ':' suite] ['finally' ':' suite] | 'finally' ':' suite)) -with_stmt: 'with' test [ with_var ] ':' suite +with_stmt: 'with' with_item (',' with_item)* ':' suite +with_item: test ['as' expr] with_var: 'as' expr # NB compile.c makes sure that the default except clause is last except_clause: 'except' [test [(',' | 'as') test]] diff --git a/Lib/lib2to3/fixer_base.py b/Lib/lib2to3/fixer_base.py index 66737f4..c361e61 100644 --- a/Lib/lib2to3/fixer_base.py +++ b/Lib/lib2to3/fixer_base.py @@ -120,7 +120,7 @@ class BaseFix(object): """ lineno = node.get_lineno() for_output = node.clone() - for_output.set_prefix("") + for_output.prefix = "" msg = "Line %d: could not convert: %s" self.log_message(msg % (lineno, for_output)) if reason: diff --git a/Lib/lib2to3/fixer_util.py b/Lib/lib2to3/fixer_util.py index 37343c0..5c0a088 100644 --- a/Lib/lib2to3/fixer_util.py +++ b/Lib/lib2to3/fixer_util.py @@ -27,7 +27,7 @@ def Assign(target, source): if not isinstance(target, list): target = [target] if not isinstance(source, list): - source.set_prefix(" ") + source.prefix = " " source = [source] return Node(syms.atom, @@ -60,7 +60,7 @@ def Call(func_name, args=None, prefix=None): """A function call""" node = Node(syms.power, [func_name, ArgList(args)]) if prefix is not None: - node.set_prefix(prefix) + node.prefix = prefix return node def Newline(): @@ -89,18 +89,18 @@ def ListComp(xp, fp, it, test=None): If test is None, the "if test" part is omitted. """ - xp.set_prefix("") - fp.set_prefix(" ") - it.set_prefix(" ") + xp.prefix = "" + fp.prefix = " " + it.prefix = " " for_leaf = Leaf(token.NAME, "for") - for_leaf.set_prefix(" ") + for_leaf.prefix = " " in_leaf = Leaf(token.NAME, "in") - in_leaf.set_prefix(" ") + in_leaf.prefix = " " inner_args = [for_leaf, fp, in_leaf, it] if test: - test.set_prefix(" ") + test.prefix = " " if_leaf = Leaf(token.NAME, "if") - if_leaf.set_prefix(" ") + if_leaf.prefix = " " inner_args.append(Node(syms.comp_if, [if_leaf, test])) inner = Node(syms.listmaker, [xp, Node(syms.comp_for, inner_args)]) return Node(syms.atom, diff --git a/Lib/lib2to3/fixes/fix_apply.py b/Lib/lib2to3/fixes/fix_apply.py index 5af13b7..99cd21a 100644 --- a/Lib/lib2to3/fixes/fix_apply.py +++ b/Lib/lib2to3/fixes/fix_apply.py @@ -33,25 +33,25 @@ class FixApply(fixer_base.BaseFix): func = results["func"] args = results["args"] kwds = results.get("kwds") - prefix = node.get_prefix() + prefix = node.prefix func = func.clone() if (func.type not in (token.NAME, syms.atom) and (func.type != syms.power or func.children[-2].type == token.DOUBLESTAR)): # Need to parenthesize func = parenthesize(func) - func.set_prefix("") + func.prefix = "" args = args.clone() - args.set_prefix("") + args.prefix = "" if kwds is not None: kwds = kwds.clone() - kwds.set_prefix("") + kwds.prefix = "" l_newargs = [pytree.Leaf(token.STAR, "*"), args] if kwds is not None: l_newargs.extend([Comma(), pytree.Leaf(token.DOUBLESTAR, "**"), kwds]) - l_newargs[-2].set_prefix(" ") # that's the ** token + l_newargs[-2].prefix = " " # that's the ** token # XXX Sometimes we could be cleverer, e.g. apply(f, (x, y) + t) # can be translated into f(x, y, *t) instead of f(*(x, y) + t) #new = pytree.Node(syms.power, (func, ArgList(l_newargs))) diff --git a/Lib/lib2to3/fixes/fix_basestring.py b/Lib/lib2to3/fixes/fix_basestring.py index 5d84cc7..7d11a1f 100644 --- a/Lib/lib2to3/fixes/fix_basestring.py +++ b/Lib/lib2to3/fixes/fix_basestring.py @@ -10,4 +10,4 @@ class FixBasestring(fixer_base.BaseFix): PATTERN = "'basestring'" def transform(self, node, results): - return Name("str", prefix=node.get_prefix()) + return Name("str", prefix=node.prefix) diff --git a/Lib/lib2to3/fixes/fix_buffer.py b/Lib/lib2to3/fixes/fix_buffer.py index 2f6822b..21d04ae 100644 --- a/Lib/lib2to3/fixes/fix_buffer.py +++ b/Lib/lib2to3/fixes/fix_buffer.py @@ -13,9 +13,9 @@ class FixBuffer(fixer_base.BaseFix): explicit = True # The user must ask for this fixer PATTERN = """ - power< name='buffer' trailer< '(' [any] ')' > > + power< name='buffer' trailer< '(' [any] ')' > any* > """ def transform(self, node, results): name = results["name"] - name.replace(Name("memoryview", prefix=name.get_prefix())) + name.replace(Name("memoryview", prefix=name.prefix)) diff --git a/Lib/lib2to3/fixes/fix_callable.py b/Lib/lib2to3/fixes/fix_callable.py index de93792..831b913 100644 --- a/Lib/lib2to3/fixes/fix_callable.py +++ b/Lib/lib2to3/fixes/fix_callable.py @@ -28,4 +28,4 @@ class FixCallable(fixer_base.BaseFix): func = results["func"] args = [func.clone(), String(', '), String("'__call__'")] - return Call(Name("hasattr"), args, prefix=node.get_prefix()) + return Call(Name("hasattr"), args, prefix=node.prefix) diff --git a/Lib/lib2to3/fixes/fix_dict.py b/Lib/lib2to3/fixes/fix_dict.py index dec94c5..ce973fd 100644 --- a/Lib/lib2to3/fixes/fix_dict.py +++ b/Lib/lib2to3/fixes/fix_dict.py @@ -61,15 +61,15 @@ class FixDict(fixer_base.BaseFix): args = head + [pytree.Node(syms.trailer, [Dot(), Name(method_name, - prefix=method.get_prefix())]), + prefix=method.prefix)]), results["parens"].clone()] new = pytree.Node(syms.power, args) if not special: - new.set_prefix("") + new.prefix = "" new = Call(Name(isiter and "iter" or "list"), [new]) if tail: new = pytree.Node(syms.power, [new] + tail) - new.set_prefix(node.get_prefix()) + new.prefix = node.prefix return new P1 = "power< func=NAME trailer< '(' node=any ')' > any* >" diff --git a/Lib/lib2to3/fixes/fix_except.py b/Lib/lib2to3/fixes/fix_except.py index 5554c02..ace3f84 100644 --- a/Lib/lib2to3/fixes/fix_except.py +++ b/Lib/lib2to3/fixes/fix_except.py @@ -36,11 +36,11 @@ def find_excepts(nodes): class FixExcept(fixer_base.BaseFix): PATTERN = """ - try_stmt< 'try' ':' suite - cleanup=(except_clause ':' suite)+ - tail=(['except' ':' suite] - ['else' ':' suite] - ['finally' ':' suite]) > + try_stmt< 'try' ':' (simple_stmt | suite) + cleanup=(except_clause ':' (simple_stmt | suite))+ + tail=(['except' ':' (simple_stmt | suite)] + ['else' ':' (simple_stmt | suite)] + ['finally' ':' (simple_stmt | suite)]) > """ def transform(self, node, results): @@ -58,7 +58,7 @@ class FixExcept(fixer_base.BaseFix): # Generate a new N for the except clause new_N = Name(self.new_name(), prefix=" ") target = N.clone() - target.set_prefix("") + target.prefix = "" N.replace(new_N) new_N = new_N.clone() @@ -82,10 +82,10 @@ class FixExcept(fixer_base.BaseFix): for child in reversed(suite_stmts[:i]): e_suite.insert_child(0, child) e_suite.insert_child(i, assign) - elif N.get_prefix() == "": + elif N.prefix == "": # No space after a comma is legal; no space after "as", # not so much. - N.set_prefix(" ") + N.prefix = " " #TODO(cwinter) fix this when children becomes a smart list children = [c.clone() for c in node.children[:3]] + try_cleanup + tail diff --git a/Lib/lib2to3/fixes/fix_exec.py b/Lib/lib2to3/fixes/fix_exec.py index 9b47aec..68376ba 100644 --- a/Lib/lib2to3/fixes/fix_exec.py +++ b/Lib/lib2to3/fixes/fix_exec.py @@ -30,10 +30,10 @@ class FixExec(fixer_base.BaseFix): b = results.get("b") c = results.get("c") args = [a.clone()] - args[0].set_prefix("") + args[0].prefix = "" if b is not None: args.extend([Comma(), b.clone()]) if c is not None: args.extend([Comma(), c.clone()]) - return Call(Name("exec"), args, prefix=node.get_prefix()) + return Call(Name("exec"), args, prefix=node.prefix) diff --git a/Lib/lib2to3/fixes/fix_execfile.py b/Lib/lib2to3/fixes/fix_execfile.py index f7a7a70..4336ff3 100644 --- a/Lib/lib2to3/fixes/fix_execfile.py +++ b/Lib/lib2to3/fixes/fix_execfile.py @@ -38,7 +38,7 @@ class FixExecfile(fixer_base.BaseFix): # Wrap the open call in a compile call. This is so the filename will be # preserved in the execed code. filename_arg = filename.clone() - filename_arg.set_prefix(" ") + filename_arg.prefix = " " exec_str = String("'exec'", " ") compile_args = open_expr + [Comma(), filename_arg, Comma(), exec_str] compile_call = Call(Name("compile"), compile_args, "") @@ -48,4 +48,4 @@ class FixExecfile(fixer_base.BaseFix): args.extend([Comma(), globals.clone()]) if locals is not None: args.extend([Comma(), locals.clone()]) - return Call(Name("exec"), args, prefix=node.get_prefix()) + return Call(Name("exec"), args, prefix=node.prefix) diff --git a/Lib/lib2to3/fixes/fix_filter.py b/Lib/lib2to3/fixes/fix_filter.py index 51fd02a..527dc3d 100644 --- a/Lib/lib2to3/fixes/fix_filter.py +++ b/Lib/lib2to3/fixes/fix_filter.py @@ -69,7 +69,7 @@ class FixFilter(fixer_base.ConditionalFix): if in_special_context(node): return None new = node.clone() - new.set_prefix("") + new.prefix = "" new = Call(Name("list"), [new]) - new.set_prefix(node.get_prefix()) + new.prefix = node.prefix return new diff --git a/Lib/lib2to3/fixes/fix_funcattrs.py b/Lib/lib2to3/fixes/fix_funcattrs.py index 4234993..465dbc8 100644 --- a/Lib/lib2to3/fixes/fix_funcattrs.py +++ b/Lib/lib2to3/fixes/fix_funcattrs.py @@ -16,4 +16,4 @@ class FixFuncattrs(fixer_base.BaseFix): def transform(self, node, results): attr = results["attr"][0] attr.replace(Name(("__%s__" % attr.value[5:]), - prefix=attr.get_prefix())) + prefix=attr.prefix)) diff --git a/Lib/lib2to3/fixes/fix_future.py b/Lib/lib2to3/fixes/fix_future.py index a032e88..861e13c 100644 --- a/Lib/lib2to3/fixes/fix_future.py +++ b/Lib/lib2to3/fixes/fix_future.py @@ -16,5 +16,5 @@ class FixFuture(fixer_base.BaseFix): def transform(self, node, results): new = BlankLine() - new.prefix = node.get_prefix() + new.prefix = node.prefix return new diff --git a/Lib/lib2to3/fixes/fix_getcwdu.py b/Lib/lib2to3/fixes/fix_getcwdu.py index 1175e56..8afb30d 100644 --- a/Lib/lib2to3/fixes/fix_getcwdu.py +++ b/Lib/lib2to3/fixes/fix_getcwdu.py @@ -15,4 +15,4 @@ class FixGetcwdu(fixer_base.BaseFix): def transform(self, node, results): name = results["name"] - name.replace(Name("getcwd", prefix=name.get_prefix())) + name.replace(Name("getcwd", prefix=name.prefix)) diff --git a/Lib/lib2to3/fixes/fix_has_key.py b/Lib/lib2to3/fixes/fix_has_key.py index 482f27d..b3a2572 100644 --- a/Lib/lib2to3/fixes/fix_has_key.py +++ b/Lib/lib2to3/fixes/fix_has_key.py @@ -78,7 +78,7 @@ class FixHasKey(fixer_base.BaseFix): return None negation = results.get("negation") anchor = results["anchor"] - prefix = node.get_prefix() + prefix = node.prefix before = [n.clone() for n in results["before"]] arg = results["arg"].clone() after = results.get("after") @@ -91,7 +91,7 @@ class FixHasKey(fixer_base.BaseFix): before = before[0] else: before = pytree.Node(syms.power, before) - before.set_prefix(" ") + before.prefix = " " n_op = Name("in", prefix=" ") if negation: n_not = Name("not", prefix=" ") @@ -105,5 +105,5 @@ class FixHasKey(fixer_base.BaseFix): syms.arith_expr, syms.term, syms.factor, syms.power): new = parenthesize(new) - new.set_prefix(prefix) + new.prefix = prefix return new diff --git a/Lib/lib2to3/fixes/fix_idioms.py b/Lib/lib2to3/fixes/fix_idioms.py index 8bc6397..1f68faf 100644 --- a/Lib/lib2to3/fixes/fix_idioms.py +++ b/Lib/lib2to3/fixes/fix_idioms.py @@ -101,18 +101,18 @@ class FixIdioms(fixer_base.BaseFix): def transform_isinstance(self, node, results): x = results["x"].clone() # The thing inside of type() T = results["T"].clone() # The type being compared against - x.set_prefix("") - T.set_prefix(" ") + x.prefix = "" + T.prefix = " " test = Call(Name("isinstance"), [x, Comma(), T]) if "n" in results: - test.set_prefix(" ") + test.prefix = " " test = Node(syms.not_test, [Name("not"), test]) - test.set_prefix(node.get_prefix()) + test.prefix = node.prefix return test def transform_while(self, node, results): one = results["while"] - one.replace(Name("True", prefix=one.get_prefix())) + one.replace(Name("True", prefix=one.prefix)) def transform_sort(self, node, results): sort_stmt = results["sort"] @@ -121,14 +121,14 @@ class FixIdioms(fixer_base.BaseFix): simple_expr = results.get("expr") if list_call: - list_call.replace(Name("sorted", prefix=list_call.get_prefix())) + list_call.replace(Name("sorted", prefix=list_call.prefix)) elif simple_expr: new = simple_expr.clone() - new.set_prefix("") + new.prefix = "" simple_expr.replace(Call(Name("sorted"), [new], - prefix=simple_expr.get_prefix())) + prefix=simple_expr.prefix)) else: raise RuntimeError("should not have reached here") sort_stmt.remove() if next_stmt: - next_stmt[0].set_prefix(sort_stmt.get_prefix()) + next_stmt[0].prefix = sort_stmt.prefix diff --git a/Lib/lib2to3/fixes/fix_import.py b/Lib/lib2to3/fixes/fix_import.py index 4c75133..103c3ef 100644 --- a/Lib/lib2to3/fixes/fix_import.py +++ b/Lib/lib2to3/fixes/fix_import.py @@ -73,7 +73,7 @@ class FixImport(fixer_base.BaseFix): return new = FromImport('.', [imp]) - new.set_prefix(node.get_prefix()) + new.prefix = node.prefix return new def probably_a_local_import(self, imp_name): diff --git a/Lib/lib2to3/fixes/fix_imports.py b/Lib/lib2to3/fixes/fix_imports.py index 46ba4a2..9aeca38 100644 --- a/Lib/lib2to3/fixes/fix_imports.py +++ b/Lib/lib2to3/fixes/fix_imports.py @@ -124,7 +124,7 @@ class FixImports(fixer_base.BaseFix): if import_mod: mod_name = import_mod.value new_name = self.mapping[mod_name] - import_mod.replace(Name(new_name, prefix=import_mod.get_prefix())) + import_mod.replace(Name(new_name, prefix=import_mod.prefix)) if "name_import" in results: # If it's not a "from x import x, y" or "import x as y" import, # marked its usage to be replaced. @@ -142,4 +142,4 @@ class FixImports(fixer_base.BaseFix): bare_name = results["bare_with_attr"][0] new_name = self.replace.get(bare_name.value) if new_name: - bare_name.replace(Name(new_name, prefix=bare_name.get_prefix())) + bare_name.replace(Name(new_name, prefix=bare_name.prefix)) diff --git a/Lib/lib2to3/fixes/fix_input.py b/Lib/lib2to3/fixes/fix_input.py index e0264cf..3fae491 100644 --- a/Lib/lib2to3/fixes/fix_input.py +++ b/Lib/lib2to3/fixes/fix_input.py @@ -22,5 +22,5 @@ class FixInput(fixer_base.BaseFix): return new = node.clone() - new.set_prefix("") - return Call(Name("eval"), [new], prefix=node.get_prefix()) + new.prefix = "" + return Call(Name("eval"), [new], prefix=node.prefix) diff --git a/Lib/lib2to3/fixes/fix_intern.py b/Lib/lib2to3/fixes/fix_intern.py index 66c616e..2a57ce1 100644 --- a/Lib/lib2to3/fixes/fix_intern.py +++ b/Lib/lib2to3/fixes/fix_intern.py @@ -39,6 +39,6 @@ class FixIntern(fixer_base.BaseFix): [results["lpar"].clone(), newarglist, results["rpar"].clone()])] + after) - new.set_prefix(node.get_prefix()) + new.prefix = node.prefix touch_import(None, 'sys', node) return new diff --git a/Lib/lib2to3/fixes/fix_isinstance.py b/Lib/lib2to3/fixes/fix_isinstance.py index 14da18a..4d212fc 100644 --- a/Lib/lib2to3/fixes/fix_isinstance.py +++ b/Lib/lib2to3/fixes/fix_isinstance.py @@ -45,7 +45,7 @@ class FixIsinstance(fixer_base.BaseFix): del new_args[-1] if len(new_args) == 1: atom = testlist.parent - new_args[0].set_prefix(atom.get_prefix()) + new_args[0].prefix = atom.prefix atom.replace(new_args[0]) else: args[:] = new_args diff --git a/Lib/lib2to3/fixes/fix_itertools.py b/Lib/lib2to3/fixes/fix_itertools.py index 86d6b46..a637fee 100644 --- a/Lib/lib2to3/fixes/fix_itertools.py +++ b/Lib/lib2to3/fixes/fix_itertools.py @@ -30,12 +30,12 @@ class FixItertools(fixer_base.BaseFix): if 'it' in results and func.value != 'ifilterfalse': dot, it = (results['dot'], results['it']) # Remove the 'itertools' - prefix = it.get_prefix() + prefix = it.prefix it.remove() # Replace the node wich contains ('.', 'function') with the # function (to be consistant with the second part of the pattern) dot.remove() func.parent.replace(func) - prefix = prefix or func.get_prefix() + prefix = prefix or func.prefix func.replace(Name(func.value[1:], prefix=prefix)) diff --git a/Lib/lib2to3/fixes/fix_itertools_imports.py b/Lib/lib2to3/fixes/fix_itertools_imports.py index 8a57f6d..4849dd2 100644 --- a/Lib/lib2to3/fixes/fix_itertools_imports.py +++ b/Lib/lib2to3/fixes/fix_itertools_imports.py @@ -46,7 +46,7 @@ class FixItertoolsImports(fixer_base.BaseFix): # If there are no imports left, just get rid of the entire statement if not (imports.children or getattr(imports, 'value', None)) or \ imports.parent is None: - p = node.get_prefix() + p = node.prefix node = BlankLine() node.prefix = p return node diff --git a/Lib/lib2to3/fixes/fix_long.py b/Lib/lib2to3/fixes/fix_long.py index 873ecf1..b5e0bdb 100644 --- a/Lib/lib2to3/fixes/fix_long.py +++ b/Lib/lib2to3/fixes/fix_long.py @@ -18,5 +18,5 @@ class FixLong(fixer_base.BaseFix): def transform(self, node, results): if is_probably_builtin(node): new = self.static_int.clone() - new.set_prefix(node.get_prefix()) + new.prefix = node.prefix return new diff --git a/Lib/lib2to3/fixes/fix_map.py b/Lib/lib2to3/fixes/fix_map.py index 29578e2..cf8f351 100644 --- a/Lib/lib2to3/fixes/fix_map.py +++ b/Lib/lib2to3/fixes/fix_map.py @@ -63,7 +63,7 @@ class FixMap(fixer_base.ConditionalFix): if node.parent.type == syms.simple_stmt: self.warning(node, "You should use a for loop here") new = node.clone() - new.set_prefix("") + new.prefix = "" new = Call(Name("list"), [new]) elif "map_lambda" in results: new = ListComp(results.get("xp").clone(), @@ -76,7 +76,7 @@ class FixMap(fixer_base.ConditionalFix): if in_special_context(node): return None new = node.clone() - new.set_prefix("") + new.prefix = "" new = Call(Name("list"), [new]) - new.set_prefix(node.get_prefix()) + new.prefix = node.prefix return new diff --git a/Lib/lib2to3/fixes/fix_metaclass.py b/Lib/lib2to3/fixes/fix_metaclass.py index b508f5f..f7840d4 100644 --- a/Lib/lib2to3/fixes/fix_metaclass.py +++ b/Lib/lib2to3/fixes/fix_metaclass.py @@ -89,7 +89,7 @@ def fixup_simple_stmt(parent, i, stmt_node): parent.insert_child(i, new_stmt) new_leaf1 = new_stmt.children[0].children[0] old_leaf1 = stmt_node.children[0].children[0] - new_leaf1.set_prefix(old_leaf1.get_prefix()) + new_leaf1.prefix = old_leaf1.prefix def remove_trailing_newline(node): @@ -136,7 +136,7 @@ def fixup_indent(suite): node = kids.pop() if isinstance(node, Leaf) and node.type != token.DEDENT: if node.prefix: - node.set_prefix('') + node.prefix = '' return else: kids.extend(node.children[::-1]) @@ -191,19 +191,19 @@ class FixMetaclass(fixer_base.BaseFix): # now stick the metaclass in the arglist meta_txt = last_metaclass.children[0].children[0] meta_txt.value = 'metaclass' - orig_meta_prefix = meta_txt.get_prefix() + orig_meta_prefix = meta_txt.prefix if arglist.children: arglist.append_child(Leaf(token.COMMA, ',')) - meta_txt.set_prefix(' ') + meta_txt.prefix = ' ' else: - meta_txt.set_prefix('') + meta_txt.prefix = '' # compact the expression "metaclass = Meta" -> "metaclass=Meta" expr_stmt = last_metaclass.children[0] assert expr_stmt.type == syms.expr_stmt - expr_stmt.children[1].set_prefix('') - expr_stmt.children[2].set_prefix('') + expr_stmt.children[1].prefix = '' + expr_stmt.children[2].prefix = '' arglist.append_child(last_metaclass) @@ -214,7 +214,7 @@ class FixMetaclass(fixer_base.BaseFix): # one-liner that was just __metaclass_ suite.remove() pass_leaf = Leaf(text_type, 'pass') - pass_leaf.set_prefix(orig_meta_prefix) + pass_leaf.prefix = orig_meta_prefix node.append_child(pass_leaf) node.append_child(Leaf(token.NEWLINE, '\n')) diff --git a/Lib/lib2to3/fixes/fix_methodattrs.py b/Lib/lib2to3/fixes/fix_methodattrs.py index ae4096c..f1bf78a 100644 --- a/Lib/lib2to3/fixes/fix_methodattrs.py +++ b/Lib/lib2to3/fixes/fix_methodattrs.py @@ -20,4 +20,4 @@ class FixMethodattrs(fixer_base.BaseFix): def transform(self, node, results): attr = results["attr"][0] new = MAP[attr.value] - attr.replace(Name(new, prefix=attr.get_prefix())) + attr.replace(Name(new, prefix=attr.prefix)) diff --git a/Lib/lib2to3/fixes/fix_ne.py b/Lib/lib2to3/fixes/fix_ne.py index 382f43d..1472036 100644 --- a/Lib/lib2to3/fixes/fix_ne.py +++ b/Lib/lib2to3/fixes/fix_ne.py @@ -17,6 +17,5 @@ class FixNe(fixer_base.BaseFix): return node.type == token.NOTEQUAL and node.value == "<>" def transform(self, node, results): - new = pytree.Leaf(token.NOTEQUAL, "!=") - new.set_prefix(node.get_prefix()) + new = pytree.Leaf(token.NOTEQUAL, "!=", prefix=node.prefix) return new diff --git a/Lib/lib2to3/fixes/fix_next.py b/Lib/lib2to3/fixes/fix_next.py index 492b515..c999d6f 100644 --- a/Lib/lib2to3/fixes/fix_next.py +++ b/Lib/lib2to3/fixes/fix_next.py @@ -48,17 +48,16 @@ class FixNext(fixer_base.BaseFix): base = results.get("base") attr = results.get("attr") name = results.get("name") - mod = results.get("mod") if base: if self.shadowed_next: - attr.replace(Name("__next__", prefix=attr.get_prefix())) + attr.replace(Name("__next__", prefix=attr.prefix)) else: base = [n.clone() for n in base] - base[0].set_prefix("") - node.replace(Call(Name("next", prefix=node.get_prefix()), base)) + base[0].prefix = "" + node.replace(Call(Name("next", prefix=node.prefix), base)) elif name: - n = Name("__next__", prefix=name.get_prefix()) + n = Name("__next__", prefix=name.prefix) name.replace(n) elif attr: # We don't do this transformation if we're assigning to "x.next". diff --git a/Lib/lib2to3/fixes/fix_nonzero.py b/Lib/lib2to3/fixes/fix_nonzero.py index abb1f4e..cd39dc3 100644 --- a/Lib/lib2to3/fixes/fix_nonzero.py +++ b/Lib/lib2to3/fixes/fix_nonzero.py @@ -16,5 +16,5 @@ class FixNonzero(fixer_base.BaseFix): def transform(self, node, results): name = results["name"] - new = Name("__bool__", prefix=name.get_prefix()) + new = Name("__bool__", prefix=name.prefix) name.replace(new) diff --git a/Lib/lib2to3/fixes/fix_numliterals.py b/Lib/lib2to3/fixes/fix_numliterals.py index d821e39..6552740 100644 --- a/Lib/lib2to3/fixes/fix_numliterals.py +++ b/Lib/lib2to3/fixes/fix_numliterals.py @@ -24,4 +24,4 @@ class FixNumliterals(fixer_base.BaseFix): elif val.startswith('0') and val.isdigit() and len(set(val)) > 1: val = "0o" + val[1:] - return Number(val, prefix=node.get_prefix()) + return Number(val, prefix=node.prefix) diff --git a/Lib/lib2to3/fixes/fix_paren.py b/Lib/lib2to3/fixes/fix_paren.py index 0b72bb1..71af238 100644 --- a/Lib/lib2to3/fixes/fix_paren.py +++ b/Lib/lib2to3/fixes/fix_paren.py @@ -36,7 +36,7 @@ class FixParen(fixer_base.BaseFix): target = results["target"] lparen = LParen() - lparen.set_prefix(target.get_prefix()) - target.set_prefix("") # Make it hug the parentheses + lparen.prefix = target.prefix + target.prefix = "" # Make it hug the parentheses target.insert_child(0, lparen) target.append_child(RParen()) diff --git a/Lib/lib2to3/fixes/fix_print.py b/Lib/lib2to3/fixes/fix_print.py index 134a972..abce251 100644 --- a/Lib/lib2to3/fixes/fix_print.py +++ b/Lib/lib2to3/fixes/fix_print.py @@ -45,7 +45,7 @@ class FixPrint(fixer_base.ConditionalFix): if bare_print: # Special-case print all by itself bare_print.replace(Call(Name("print"), [], - prefix=bare_print.get_prefix())) + prefix=bare_print.prefix)) return assert node.children[0] == Name("print") args = node.children[1:] @@ -65,7 +65,7 @@ class FixPrint(fixer_base.ConditionalFix): # Now synthesize a print(args, sep=..., end=..., file=...) node. l_args = [arg.clone() for arg in args] if l_args: - l_args[0].set_prefix("") + l_args[0].prefix = "" if sep is not None or end is not None or file is not None: if sep is not None: self.add_kwarg(l_args, "sep", String(repr(sep))) @@ -74,17 +74,17 @@ class FixPrint(fixer_base.ConditionalFix): if file is not None: self.add_kwarg(l_args, "file", file) n_stmt = Call(Name("print"), l_args) - n_stmt.set_prefix(node.get_prefix()) + n_stmt.prefix = node.prefix return n_stmt def add_kwarg(self, l_nodes, s_kwd, n_expr): # XXX All this prefix-setting may lose comments (though rarely) - n_expr.set_prefix("") + n_expr.prefix = "" n_argument = pytree.Node(self.syms.argument, (Name(s_kwd), pytree.Leaf(token.EQUAL, "="), n_expr)) if l_nodes: l_nodes.append(Comma()) - n_argument.set_prefix(" ") + n_argument.prefix = " " l_nodes.append(n_argument) diff --git a/Lib/lib2to3/fixes/fix_raise.py b/Lib/lib2to3/fixes/fix_raise.py index be785f5..8f17534 100644 --- a/Lib/lib2to3/fixes/fix_raise.py +++ b/Lib/lib2to3/fixes/fix_raise.py @@ -52,31 +52,31 @@ class FixRaise(fixer_base.BaseFix): # exc.children[1:-1] is the unparenthesized tuple # exc.children[1].children[0] is the first element of the tuple exc = exc.children[1].children[0].clone() - exc.set_prefix(" ") + exc.prefix = " " if "val" not in results: # One-argument raise new = pytree.Node(syms.raise_stmt, [Name("raise"), exc]) - new.set_prefix(node.get_prefix()) + new.prefix = node.prefix return new val = results["val"].clone() if is_tuple(val): args = [c.clone() for c in val.children[1:-1]] else: - val.set_prefix("") + val.prefix = "" args = [val] if "tb" in results: tb = results["tb"].clone() - tb.set_prefix("") + tb.prefix = "" e = Call(exc, args) with_tb = Attr(e, Name('with_traceback')) + [ArgList([tb])] new = pytree.Node(syms.simple_stmt, [Name("raise")] + with_tb) - new.set_prefix(node.get_prefix()) + new.prefix = node.prefix return new else: return pytree.Node(syms.raise_stmt, [Name("raise"), Call(exc, args)], - prefix=node.get_prefix()) + prefix=node.prefix) diff --git a/Lib/lib2to3/fixes/fix_raw_input.py b/Lib/lib2to3/fixes/fix_raw_input.py index 53e7a32..fca7129 100644 --- a/Lib/lib2to3/fixes/fix_raw_input.py +++ b/Lib/lib2to3/fixes/fix_raw_input.py @@ -13,4 +13,4 @@ class FixRawInput(fixer_base.BaseFix): def transform(self, node, results): name = results["name"] - name.replace(Name("input", prefix=name.get_prefix())) + name.replace(Name("input", prefix=name.prefix)) diff --git a/Lib/lib2to3/fixes/fix_renames.py b/Lib/lib2to3/fixes/fix_renames.py index 3049610..275e23f 100644 --- a/Lib/lib2to3/fixes/fix_renames.py +++ b/Lib/lib2to3/fixes/fix_renames.py @@ -66,4 +66,4 @@ class FixRenames(fixer_base.BaseFix): if mod_name and attr_name: new_attr = LOOKUP[(mod_name.value, attr_name.value)] - attr_name.replace(Name(new_attr, prefix=attr_name.get_prefix())) + attr_name.replace(Name(new_attr, prefix=attr_name.prefix)) diff --git a/Lib/lib2to3/fixes/fix_repr.py b/Lib/lib2to3/fixes/fix_repr.py index 0bc6ba6..7ddd096 100644 --- a/Lib/lib2to3/fixes/fix_repr.py +++ b/Lib/lib2to3/fixes/fix_repr.py @@ -19,4 +19,4 @@ class FixRepr(fixer_base.BaseFix): if expr.type == self.syms.testlist1: expr = parenthesize(expr) - return Call(Name("repr"), [expr], prefix=node.get_prefix()) + return Call(Name("repr"), [expr], prefix=node.prefix) diff --git a/Lib/lib2to3/fixes/fix_set_literal.py b/Lib/lib2to3/fixes/fix_set_literal.py index 35743d2..b3c267c 100644 --- a/Lib/lib2to3/fixes/fix_set_literal.py +++ b/Lib/lib2to3/fixes/fix_set_literal.py @@ -38,15 +38,15 @@ class FixSetLiteral(fixer_base.BaseFix): literal.extend(n.clone() for n in items.children) literal.append(pytree.Leaf(token.RBRACE, "}")) # Set the prefix of the right brace to that of the ')' or ']' - literal[-1].set_prefix(items.next_sibling.get_prefix()) + literal[-1].prefix = items.next_sibling.prefix maker = pytree.Node(syms.dictsetmaker, literal) - maker.set_prefix(node.get_prefix()) + maker.prefix = node.prefix # If the original was a one tuple, we need to remove the extra comma. if len(maker.children) == 4: n = maker.children[2] n.remove() - maker.children[-1].set_prefix(n.get_prefix()) + maker.children[-1].prefix = n.prefix # Finally, replace the set call with our shiny new literal. return maker diff --git a/Lib/lib2to3/fixes/fix_standarderror.py b/Lib/lib2to3/fixes/fix_standarderror.py index 4f87014..78d5a7e 100644 --- a/Lib/lib2to3/fixes/fix_standarderror.py +++ b/Lib/lib2to3/fixes/fix_standarderror.py @@ -15,4 +15,4 @@ class FixStandarderror(fixer_base.BaseFix): """ def transform(self, node, results): - return Name("Exception", prefix=node.get_prefix()) + return Name("Exception", prefix=node.prefix) diff --git a/Lib/lib2to3/fixes/fix_sys_exc.py b/Lib/lib2to3/fixes/fix_sys_exc.py index 18d9363..e041e6e 100644 --- a/Lib/lib2to3/fixes/fix_sys_exc.py +++ b/Lib/lib2to3/fixes/fix_sys_exc.py @@ -22,8 +22,8 @@ class FixSysExc(fixer_base.BaseFix): sys_attr = results["attribute"][0] index = Number(self.exc_info.index(sys_attr.value)) - call = Call(Name("exc_info"), prefix=sys_attr.get_prefix()) + call = Call(Name("exc_info"), prefix=sys_attr.prefix) attr = Attr(Name("sys"), call) - attr[1].children[0].set_prefix(results["dot"].get_prefix()) + attr[1].children[0].prefix = results["dot"].prefix attr.append(Subscript(index)) - return Node(syms.power, attr, prefix=node.get_prefix()) + return Node(syms.power, attr, prefix=node.prefix) diff --git a/Lib/lib2to3/fixes/fix_throw.py b/Lib/lib2to3/fixes/fix_throw.py index bf86d84..6096348 100644 --- a/Lib/lib2to3/fixes/fix_throw.py +++ b/Lib/lib2to3/fixes/fix_throw.py @@ -40,14 +40,14 @@ class FixThrow(fixer_base.BaseFix): if is_tuple(val): args = [c.clone() for c in val.children[1:-1]] else: - val.set_prefix("") + val.prefix = "" args = [val] throw_args = results["args"] if "tb" in results: tb = results["tb"].clone() - tb.set_prefix("") + tb.prefix = "" e = Call(exc, args) with_tb = Attr(e, Name('with_traceback')) + [ArgList([tb])] diff --git a/Lib/lib2to3/fixes/fix_tuple_params.py b/Lib/lib2to3/fixes/fix_tuple_params.py index fb29f5c..41b8dc2 100644 --- a/Lib/lib2to3/fixes/fix_tuple_params.py +++ b/Lib/lib2to3/fixes/fix_tuple_params.py @@ -63,10 +63,10 @@ class FixTupleParams(fixer_base.BaseFix): def handle_tuple(tuple_arg, add_prefix=False): n = Name(self.new_name()) arg = tuple_arg.clone() - arg.set_prefix("") + arg.prefix = "" stmt = Assign(arg, n.clone()) if add_prefix: - n.set_prefix(" ") + n.prefix = " " tuple_arg.replace(n) new_lines.append(pytree.Node(syms.simple_stmt, [stmt, end.clone()])) @@ -91,14 +91,14 @@ class FixTupleParams(fixer_base.BaseFix): # TODO(cwinter) suite-cleanup after = start if start == 0: - new_lines[0].set_prefix(" ") + new_lines[0].prefix = " " elif is_docstring(suite[0].children[start]): - new_lines[0].set_prefix(indent) + new_lines[0].prefix = indent after = start + 1 suite[0].children[after:after] = new_lines for i in range(after+1, after+len(new_lines)+1): - suite[0].children[i].set_prefix(indent) + suite[0].children[i].prefix = indent suite[0].changed() def transform_lambda(self, node, results): @@ -109,7 +109,7 @@ class FixTupleParams(fixer_base.BaseFix): # Replace lambda ((((x)))): x with lambda x: x if inner.type == token.NAME: inner = inner.clone() - inner.set_prefix(" ") + inner.prefix = " " args.replace(inner) return @@ -124,7 +124,7 @@ class FixTupleParams(fixer_base.BaseFix): subscripts = [c.clone() for c in to_index[n.value]] new = pytree.Node(syms.power, [new_param.clone()] + subscripts) - new.set_prefix(n.get_prefix()) + new.prefix = n.prefix n.replace(new) diff --git a/Lib/lib2to3/fixes/fix_types.py b/Lib/lib2to3/fixes/fix_types.py index 445f1b2..2f63cdf 100644 --- a/Lib/lib2to3/fixes/fix_types.py +++ b/Lib/lib2to3/fixes/fix_types.py @@ -58,5 +58,5 @@ class FixTypes(fixer_base.BaseFix): def transform(self, node, results): new_value = _TYPE_MAPPING.get(results["name"].value) if new_value: - return Name(new_value, prefix=node.get_prefix()) + return Name(new_value, prefix=node.prefix) return None diff --git a/Lib/lib2to3/fixes/fix_unicode.py b/Lib/lib2to3/fixes/fix_unicode.py index 7f5cc80..393396e 100644 --- a/Lib/lib2to3/fixes/fix_unicode.py +++ b/Lib/lib2to3/fixes/fix_unicode.py @@ -6,23 +6,20 @@ import re from ..pgen2 import token from .. import fixer_base +_mapping = {"unichr" : "chr", "unicode" : "str"} +_literal_re = re.compile(r"[uU][rR]?[\'\"]") + class FixUnicode(fixer_base.BaseFix): - PATTERN = "STRING | NAME<'unicode' | 'unichr'>" + PATTERN = "STRING | 'unicode' | 'unichr'" def transform(self, node, results): if node.type == token.NAME: - if node.value == "unicode": - new = node.clone() - new.value = "str" - return new - if node.value == "unichr": - new = node.clone() - new.value = "chr" - return new - # XXX Warn when __unicode__ found? + new = node.clone() + new.value = _mapping[node.value] + return new elif node.type == token.STRING: - if re.match(r"[uU][rR]?[\'\"]", node.value): + if _literal_re.match(node.value): new = node.clone() new.value = new.value[1:] return new diff --git a/Lib/lib2to3/fixes/fix_urllib.py b/Lib/lib2to3/fixes/fix_urllib.py index f262537..a89266e 100644 --- a/Lib/lib2to3/fixes/fix_urllib.py +++ b/Lib/lib2to3/fixes/fix_urllib.py @@ -78,7 +78,7 @@ class FixUrllib(FixImports): replacements. """ import_mod = results.get('module') - pref = import_mod.get_prefix() + pref = import_mod.prefix names = [] @@ -94,7 +94,7 @@ class FixUrllib(FixImports): module. """ mod_member = results.get('mod_member') - pref = mod_member.get_prefix() + pref = mod_member.prefix member = results.get('member') # Simple case with only a single member being imported @@ -162,7 +162,7 @@ class FixUrllib(FixImports): break if new_name: module_dot.replace(Name(new_name, - prefix=module_dot.get_prefix())) + prefix=module_dot.prefix)) else: self.cannot_convert(node, 'This is an invalid module element') diff --git a/Lib/lib2to3/fixes/fix_ws_comma.py b/Lib/lib2to3/fixes/fix_ws_comma.py index e493498..a54a376 100644 --- a/Lib/lib2to3/fixes/fix_ws_comma.py +++ b/Lib/lib2to3/fixes/fix_ws_comma.py @@ -26,14 +26,14 @@ class FixWsComma(fixer_base.BaseFix): comma = False for child in new.children: if child in self.SEPS: - prefix = child.get_prefix() + prefix = child.prefix if prefix.isspace() and "\n" not in prefix: - child.set_prefix("") + child.prefix = "" comma = True else: if comma: - prefix = child.get_prefix() + prefix = child.prefix if not prefix: - child.set_prefix(" ") + child.prefix = " " comma = False return new diff --git a/Lib/lib2to3/fixes/fix_xrange.py b/Lib/lib2to3/fixes/fix_xrange.py index ca8f21a..677556b 100644 --- a/Lib/lib2to3/fixes/fix_xrange.py +++ b/Lib/lib2to3/fixes/fix_xrange.py @@ -28,14 +28,14 @@ class FixXrange(fixer_base.BaseFix): def transform_xrange(self, node, results): name = results["name"] - name.replace(Name("range", prefix=name.get_prefix())) + name.replace(Name("range", prefix=name.prefix)) def transform_range(self, node, results): if not self.in_special_context(node): range_call = Call(Name("range"), [results["args"].clone()]) # Encase the range call in list(). list_call = Call(Name("list"), [range_call], - prefix=node.get_prefix()) + prefix=node.prefix) # Put things that were after the range() call after the list call. for n in results["rest"]: list_call.append_child(n) diff --git a/Lib/lib2to3/fixes/fix_xreadlines.py b/Lib/lib2to3/fixes/fix_xreadlines.py index 2d3f3c8..94f5254 100644 --- a/Lib/lib2to3/fixes/fix_xreadlines.py +++ b/Lib/lib2to3/fixes/fix_xreadlines.py @@ -19,6 +19,6 @@ class FixXreadlines(fixer_base.BaseFix): no_call = results.get("no_call") if no_call: - no_call.replace(Name("__iter__", prefix=no_call.get_prefix())) + no_call.replace(Name("__iter__", prefix=no_call.prefix)) else: node.replace([x.clone() for x in results["call"]]) diff --git a/Lib/lib2to3/fixes/fix_zip.py b/Lib/lib2to3/fixes/fix_zip.py index 08296c0..fb73c7b 100644 --- a/Lib/lib2to3/fixes/fix_zip.py +++ b/Lib/lib2to3/fixes/fix_zip.py @@ -28,7 +28,7 @@ class FixZip(fixer_base.ConditionalFix): return None new = node.clone() - new.set_prefix("") + new.prefix = "" new = Call(Name("list"), [new]) - new.set_prefix(node.get_prefix()) + new.prefix = node.prefix return new diff --git a/Lib/lib2to3/patcomp.py b/Lib/lib2to3/patcomp.py index 076fdc1..24251cb 100644 --- a/Lib/lib2to3/patcomp.py +++ b/Lib/lib2to3/patcomp.py @@ -14,10 +14,7 @@ __author__ = "Guido van Rossum " import os # Fairly local imports -from .pgen2 import driver -from .pgen2 import literals -from .pgen2 import token -from .pgen2 import tokenize +from .pgen2 import driver, literals, token, tokenize, parse # Really local imports from . import pytree @@ -28,6 +25,10 @@ _PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "PatternGrammar.txt") +class PatternSyntaxError(Exception): + pass + + def tokenize_wrapper(input): """Tokenizes a string suppressing significant whitespace.""" skip = set((token.NEWLINE, token.INDENT, token.DEDENT)) @@ -54,7 +55,10 @@ class PatternCompiler(object): def compile_pattern(self, input, debug=False): """Compiles a pattern string to a nested pytree.*Pattern object.""" tokens = tokenize_wrapper(input) - root = self.driver.parse_tokens(tokens, debug=debug) + try: + root = self.driver.parse_tokens(tokens, debug=debug) + except parse.ParseError as e: + raise PatternSyntaxError(str(e)) return self.compile_node(root) def compile_node(self, node): @@ -139,7 +143,9 @@ class PatternCompiler(object): value = node.value if value.isupper(): if value not in TOKEN_MAP: - raise SyntaxError("Invalid token: %r" % value) + raise PatternSyntaxError("Invalid token: %r" % value) + if nodes[1:]: + raise PatternSyntaxError("Can't have details for token") return pytree.LeafPattern(TOKEN_MAP[value]) else: if value == "any": @@ -147,7 +153,7 @@ class PatternCompiler(object): elif not value.startswith("_"): type = getattr(self.pysyms, value, None) if type is None: - raise SyntaxError("Invalid symbol: %r" % value) + raise PatternSyntaxError("Invalid symbol: %r" % value) if nodes[1:]: # Details present content = [self.compile_node(nodes[1].children[1])] else: diff --git a/Lib/lib2to3/pytree.py b/Lib/lib2to3/pytree.py index c60f107..f6df3a0 100644 --- a/Lib/lib2to3/pytree.py +++ b/Lib/lib2to3/pytree.py @@ -13,6 +13,7 @@ There's also a pattern matching implementation here. __author__ = "Guido van Rossum " import sys +import warnings from io import StringIO @@ -111,17 +112,21 @@ class Base(object): """ Set the prefix for the node (see Leaf class). - This must be implemented by the concrete subclass. + DEPRECATED; use the prefix property directly. """ - raise NotImplementedError + warnings.warn("set_prefix() is deprecated; use the prefix property", + DeprecationWarning, stacklevel=2) + self.prefix = prefix def get_prefix(self): """ Return the prefix for the node (see Leaf class). - This must be implemented by the concrete subclass. + DEPRECATED; use the prefix property directly. """ - raise NotImplementedError + warnings.warn("get_prefix() is deprecated; use the prefix property", + DeprecationWarning, stacklevel=2) + return self.prefix def replace(self, new): """Replace this node with a new one in the parent.""" @@ -209,12 +214,12 @@ class Base(object): def get_suffix(self): """ Return the string immediately following the invocant node. This is - effectively equivalent to node.next_sibling.get_prefix() + effectively equivalent to node.next_sibling.prefix """ next_sib = self.next_sibling if next_sib is None: return "" - return next_sib.get_prefix() + return next_sib.prefix if sys.version_info < (3, 0): def __str__(self): @@ -241,7 +246,7 @@ class Node(Base): assert ch.parent is None, repr(ch) ch.parent = self if prefix is not None: - self.set_prefix(prefix) + self.prefix = prefix def __repr__(self): """Return a canonical string representation.""" @@ -282,24 +287,19 @@ class Node(Base): for node in child.post_order(): yield node - def set_prefix(self, prefix): - """ - Set the prefix for the node. - - This passes the responsibility on to the first child. - """ - if self.children: - self.children[0].set_prefix(prefix) - - def get_prefix(self): + @property + def prefix(self): """ - Return the prefix for the node. - - This passes the call on to the first child. + The whitespace and comments preceding this node in the input. """ if not self.children: return "" - return self.children[0].get_prefix() + return self.children[0].prefix + + @prefix.setter + def prefix(self, prefix): + if self.children: + self.children[0].prefix = prefix def set_child(self, i, child): """ @@ -335,9 +335,9 @@ class Leaf(Base): """Concrete implementation for leaf nodes.""" # Default values for instance variables - prefix = "" # Whitespace and comments preceding this token in the input - lineno = 0 # Line where this token starts in the input - column = 0 # Column where this token tarts in the input + _prefix = "" # Whitespace and comments preceding this token in the input + lineno = 0 # Line where this token starts in the input + column = 0 # Column where this token tarts in the input def __init__(self, type, value, context=None, prefix=None): """ @@ -348,11 +348,11 @@ class Leaf(Base): """ assert 0 <= type < 256, type if context is not None: - self.prefix, (self.lineno, self.column) = context + self._prefix, (self.lineno, self.column) = context self.type = type self.value = value if prefix is not None: - self.prefix = prefix + self._prefix = prefix def __repr__(self): """Return a canonical string representation.""" @@ -388,14 +388,17 @@ class Leaf(Base): """Return a pre-order iterator for the tree.""" yield self - def set_prefix(self, prefix): - """Set the prefix for the node.""" - self.changed() - self.prefix = prefix + @property + def prefix(self): + """ + The whitespace and comments preceding this token in the input. + """ + return self._prefix - def get_prefix(self): - """Return the prefix for the node.""" - return self.prefix + @prefix.setter + def prefix(self, prefix): + self.changed() + self._prefix = prefix def convert(gr, raw_node): diff --git a/Lib/lib2to3/refactor.py b/Lib/lib2to3/refactor.py index 82a98d1..c5398c8 100755 --- a/Lib/lib2to3/refactor.py +++ b/Lib/lib2to3/refactor.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python2.5 # Copyright 2006 Google, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement. @@ -23,11 +22,7 @@ from itertools import chain # Local imports from .pgen2 import driver, tokenize - -from . import pytree -from . import patcomp -from . import fixes -from . import pygram +from . import pytree, pygram def get_all_fix_names(fixer_pkg, remove_prefix=True): diff --git a/Lib/lib2to3/tests/data/different_encoding.py b/Lib/lib2to3/tests/data/different_encoding.py index 4bb82bd..888f51f 100644 --- a/Lib/lib2to3/tests/data/different_encoding.py +++ b/Lib/lib2to3/tests/data/different_encoding.py @@ -1,4 +1,3 @@ #!/usr/bin/env python # -*- coding: iso-8859-1 -*- -print(u'ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ') - +print u'ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ' diff --git a/Lib/lib2to3/tests/data/fixers/myfixes/fix_parrot.py b/Lib/lib2to3/tests/data/fixers/myfixes/fix_parrot.py index 6bd2f49..6db79ad 100644 --- a/Lib/lib2to3/tests/data/fixers/myfixes/fix_parrot.py +++ b/Lib/lib2to3/tests/data/fixers/myfixes/fix_parrot.py @@ -10,4 +10,4 @@ class FixParrot(BaseFix): def transform(self, node, results): name = results["name"] - name.replace(Name("cheese", name.get_prefix())) + name.replace(Name("cheese", name.prefix)) diff --git a/Lib/lib2to3/tests/data/py2_test_grammar.py b/Lib/lib2to3/tests/data/py2_test_grammar.py index dc794e2..17f9f4c 100644 --- a/Lib/lib2to3/tests/data/py2_test_grammar.py +++ b/Lib/lib2to3/tests/data/py2_test_grammar.py @@ -1,5 +1,3 @@ -# Python 2's Lib/test/test_grammar.py (r66189) - # Python test set -- part 1, grammar. # This just tests whether the parser accepts them all. @@ -922,6 +920,26 @@ hello world self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) + def test_with_statement(self): + class manager(object): + def __enter__(self): + return (1, 2) + def __exit__(self, *args): + pass + + with manager(): + pass + with manager() as x: + pass + with manager() as (x, y): + pass + with manager(), manager(): + pass + with manager() as x, manager() as y: + pass + with manager() as x, manager(): + pass + def testIfElseExpr(self): # Test ifelse expressions in various cases def _checkeval(msg, ret): diff --git a/Lib/lib2to3/tests/data/py3_test_grammar.py b/Lib/lib2to3/tests/data/py3_test_grammar.py index eadf1db..977f0b8 100644 --- a/Lib/lib2to3/tests/data/py3_test_grammar.py +++ b/Lib/lib2to3/tests/data/py3_test_grammar.py @@ -868,6 +868,26 @@ class GrammarTests(unittest.TestCase): self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) + def test_with_statement(self): + class manager(object): + def __enter__(self): + return (1, 2) + def __exit__(self, *args): + pass + + with manager(): + pass + with manager() as x: + pass + with manager() as (x, y): + pass + with manager(), manager(): + pass + with manager() as x, manager() as y: + pass + with manager() as x, manager(): + pass + def testIfElseExpr(self): # Test ifelse expressions in various cases def _checkeval(msg, ret): diff --git a/Lib/lib2to3/tests/pytree_idempotency.py b/Lib/lib2to3/tests/pytree_idempotency.py index 3f3ab92..414eb4d 100755 --- a/Lib/lib2to3/tests/pytree_idempotency.py +++ b/Lib/lib2to3/tests/pytree_idempotency.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2.5 +#!/usr/bin/env python # Copyright 2006 Google, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement. diff --git a/Lib/lib2to3/tests/support.py b/Lib/lib2to3/tests/support.py index 8b8468c..6f2d214 100644 --- a/Lib/lib2to3/tests/support.py +++ b/Lib/lib2to3/tests/support.py @@ -30,7 +30,7 @@ def run_all_tests(test_mod=None, tests=None): def reformat(string): return dedent(string) + "\n\n" -def get_refactorer(fixers=None, options=None): +def get_refactorer(fixer_pkg="lib2to3", fixers=None, options=None): """ A convenience function for creating a RefactoringTool for tests. @@ -39,9 +39,9 @@ def get_refactorer(fixers=None, options=None): be passed to the RefactoringTool. """ if fixers is not None: - fixers = ["lib2to3.fixes.fix_" + fix for fix in fixers] + fixers = [fixer_pkg + ".fixes.fix_" + fix for fix in fixers] else: - fixers = refactor.get_fixers_from_package("lib2to3.fixes") + fixers = refactor.get_fixers_from_package(fixer_pkg + ".fixes") options = options or {} return refactor.RefactoringTool(fixers, options, explicit=True) diff --git a/Lib/lib2to3/tests/test_all_fixers.py b/Lib/lib2to3/tests/test_all_fixers.py index 1795ade..8ef222d 100644 --- a/Lib/lib2to3/tests/test_all_fixers.py +++ b/Lib/lib2to3/tests/test_all_fixers.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python2.5 """Tests that run all fixer modules over an input stream. This has been broken out into its own test module because of its @@ -6,18 +5,13 @@ running time. """ # Author: Collin Winter -# Testing imports -try: - from . import support -except ImportError: - import support - # Python imports import unittest # Local imports from .. import pytree from .. import refactor +from . import support class Test_all(support.TestCase): @@ -29,8 +23,3 @@ class Test_all(support.TestCase): for filepath in support.all_project_files(): print("Fixing %s..." % filepath) self.refactor.refactor_file(filepath) - - -if __name__ == "__main__": - import __main__ - support.run_all_tests(__main__) diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py index 5076854..2f87fb2 100755 --- a/Lib/lib2to3/tests/test_fixers.py +++ b/Lib/lib2to3/tests/test_fixers.py @@ -1,12 +1,4 @@ -#!/usr/bin/env python2.5 """ Test suite for the fixer modules """ -# Author: Collin Winter - -# Testing imports -try: - from tests import support -except ImportError: - from . import support # Python imports import os @@ -16,14 +8,19 @@ from operator import itemgetter # Local imports from lib2to3 import pygram, pytree, refactor, fixer_util +from lib2to3.tests import support class FixerTestCase(support.TestCase): - def setUp(self, fix_list=None): + + # Other test cases can subclass this class and replace "fixer_pkg" with + # their own. + def setUp(self, fix_list=None, fixer_pkg="lib2to3", options=None): if fix_list is None: fix_list = [self.fixer] - options = {"print_function" : False} - self.refactor = support.get_refactorer(fix_list, options) + if options is None: + options = {"print_function" : False} + self.refactor = support.get_refactorer(fixer_pkg, fix_list, options) self.fixer_log = [] self.filename = "" @@ -62,7 +59,7 @@ class FixerTestCase(support.TestCase): fixes = [self.fixer] fixes.extend(names) options = {"print_function" : False} - r = support.get_refactorer(fixes, options) + r = support.get_refactorer("lib2to3", fixes, options) (pre, post) = r.get_fixers() n = "fix_" + self.fixer if post and post[-1].__class__.__module__.endswith(n): @@ -419,6 +416,7 @@ class Test_print(FixerTestCase): def test_5(self): b = """print; print whatever;""" a = """print(); print(whatever);""" + self.check(b, a) def test_tuple(self): b = """print (a, b, c)""" @@ -782,6 +780,52 @@ class Test_except(FixerTestCase): pass""" self.check(b, a) + def test_one_line_suites(self): + b = """ + try: raise TypeError + except TypeError, e: + pass + """ + a = """ + try: raise TypeError + except TypeError as e: + pass + """ + self.check(b, a) + b = """ + try: + raise TypeError + except TypeError, e: pass + """ + a = """ + try: + raise TypeError + except TypeError as e: pass + """ + self.check(b, a) + b = """ + try: raise TypeError + except TypeError, e: pass + """ + a = """ + try: raise TypeError + except TypeError as e: pass + """ + self.check(b, a) + b = """ + try: raise TypeError + except TypeError, e: pass + else: function() + finally: done() + """ + a = """ + try: raise TypeError + except TypeError as e: pass + else: function() + finally: done() + """ + self.check(b, a) + # These should not be touched: def test_unchanged_1(self): @@ -2640,11 +2684,29 @@ class Test_renames(FixerTestCase): class Test_unicode(FixerTestCase): fixer = "unicode" + def test_whitespace(self): + b = """unicode( x)""" + a = """str( x)""" + self.check(b, a) + + b = """ unicode(x )""" + a = """ str(x )""" + self.check(b, a) + + b = """ u'h'""" + a = """ 'h'""" + self.check(b, a) + def test_unicode_call(self): b = """unicode(x, y, z)""" a = """str(x, y, z)""" self.check(b, a) + def test_unichr(self): + b = """unichr(u'h')""" + a = """chr('h')""" + self.check(b, a) + def test_unicode_literal_1(self): b = '''u"x"''' a = '''"x"''' @@ -2656,8 +2718,8 @@ class Test_unicode(FixerTestCase): self.check(b, a) def test_unicode_literal_3(self): - b = """UR'''x'''""" - a = """R'''x'''""" + b = """UR'''x''' """ + a = """R'''x''' """ self.check(b, a) class Test_callable(FixerTestCase): @@ -3306,6 +3368,11 @@ class Test_buffer(FixerTestCase): a = """x = memoryview(y)""" self.check(b, a) + def test_slicing(self): + b = """buffer(y)[4:5]""" + a = """memoryview(y)[4:5]""" + self.check(b, a) + class Test_future(FixerTestCase): fixer = "future" @@ -4028,8 +4095,3 @@ class Test_getcwdu(FixerTestCase): b = """os.getcwdu ( )""" a = """os.getcwd ( )""" self.check(b, a) - - -if __name__ == "__main__": - import __main__ - support.run_all_tests(__main__) diff --git a/Lib/lib2to3/tests/test_parser.py b/Lib/lib2to3/tests/test_parser.py index 2aa737c..08e7bdc 100644 --- a/Lib/lib2to3/tests/test_parser.py +++ b/Lib/lib2to3/tests/test_parser.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python2.5 """Test suite for 2to3's parser and grammar files. This is the place to add tests for changes to 2to3's grammar, such as those @@ -6,7 +5,6 @@ merging the grammars for Python 2 and 3. In addition to specific tests for parts of the grammar we've changed, we also make sure we can parse the test_grammar.py files from both Python 2 and Python 3. """ -# Author: Collin Winter # Testing imports from . import support @@ -198,7 +196,7 @@ class TestLiterals(GrammarTest): def diff(fn, result): - f = open("@", "w") + f = open("@", "wb") try: f.write(result) finally: @@ -207,8 +205,3 @@ def diff(fn, result): return os.system("diff -u %s @" % fn) finally: os.remove("@") - - -if __name__ == "__main__": - import __main__ - support.run_all_tests(__main__) diff --git a/Lib/lib2to3/tests/test_pytree.py b/Lib/lib2to3/tests/test_pytree.py index 9dc6e07..83a9b18 100755 --- a/Lib/lib2to3/tests/test_pytree.py +++ b/Lib/lib2to3/tests/test_pytree.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python2.5 # Copyright 2006 Google, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement. @@ -10,11 +9,12 @@ more helpful than printing of (the first line of) the docstring, especially when debugging a test. """ +import warnings + # Testing imports from . import support -# Local imports (XXX should become a package) -from .. import pytree +from lib2to3 import pytree try: sorted @@ -28,34 +28,48 @@ class TestNodes(support.TestCase): """Unit tests for nodes (Base, Leaf, Node).""" - def testBaseCantConstruct(self): + def test_deprecated_prefix_methods(self): + l = pytree.Leaf(100, "foo") + with warnings.catch_warnings(record=True) as w: + self.assertEqual(l.get_prefix(), "") + l.set_prefix("hi") + self.assertEqual(l.prefix, "hi") + self.assertEqual(len(w), 2) + for warning in w: + self.assertTrue(warning.category is DeprecationWarning) + self.assertEqual(str(w[0].message), "get_prefix() is deprecated; " \ + "use the prefix property") + self.assertEqual(str(w[1].message), "set_prefix() is deprecated; " \ + "use the prefix property") + + def test_instantiate_base(self): if __debug__: # Test that instantiating Base() raises an AssertionError self.assertRaises(AssertionError, pytree.Base) - def testLeaf(self): + def test_leaf(self): l1 = pytree.Leaf(100, "foo") self.assertEqual(l1.type, 100) self.assertEqual(l1.value, "foo") - def testLeafRepr(self): + def test_leaf_repr(self): l1 = pytree.Leaf(100, "foo") self.assertEqual(repr(l1), "Leaf(100, 'foo')") - def testLeafStr(self): + def test_leaf_str(self): l1 = pytree.Leaf(100, "foo") self.assertEqual(str(l1), "foo") l2 = pytree.Leaf(100, "foo", context=(" ", (10, 1))) self.assertEqual(str(l2), " foo") - def testLeafStrNumericValue(self): + def test_leaf_str_numeric_value(self): # Make sure that the Leaf's value is stringified. Failing to # do this can cause a TypeError in certain situations. l1 = pytree.Leaf(2, 5) - l1.set_prefix("foo_") + l1.prefix = "foo_" self.assertEqual(str(l1), "foo_5") - def testLeafEq(self): + def test_leaf_equality(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "foo", context=(" ", (1, 0))) self.assertEqual(l1, l2) @@ -64,67 +78,67 @@ class TestNodes(support.TestCase): self.assertNotEqual(l1, l3) self.assertNotEqual(l1, l4) - def testLeafPrefix(self): + def test_leaf_prefix(self): l1 = pytree.Leaf(100, "foo") - self.assertEqual(l1.get_prefix(), "") + self.assertEqual(l1.prefix, "") self.failIf(l1.was_changed) - l1.set_prefix(" ##\n\n") - self.assertEqual(l1.get_prefix(), " ##\n\n") + l1.prefix = " ##\n\n" + self.assertEqual(l1.prefix, " ##\n\n") self.failUnless(l1.was_changed) - def testNode(self): + def test_node(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(200, "bar") n1 = pytree.Node(1000, [l1, l2]) self.assertEqual(n1.type, 1000) self.assertEqual(n1.children, [l1, l2]) - def testNodeRepr(self): + def test_node_repr(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0))) n1 = pytree.Node(1000, [l1, l2]) self.assertEqual(repr(n1), "Node(1000, [%s, %s])" % (repr(l1), repr(l2))) - def testNodeStr(self): + def test_node_str(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0))) n1 = pytree.Node(1000, [l1, l2]) self.assertEqual(str(n1), "foo bar") - def testNodePrefix(self): + def test_node_prefix(self): l1 = pytree.Leaf(100, "foo") - self.assertEqual(l1.get_prefix(), "") + self.assertEqual(l1.prefix, "") n1 = pytree.Node(1000, [l1]) - self.assertEqual(n1.get_prefix(), "") - n1.set_prefix(" ") - self.assertEqual(n1.get_prefix(), " ") - self.assertEqual(l1.get_prefix(), " ") + self.assertEqual(n1.prefix, "") + n1.prefix = " " + self.assertEqual(n1.prefix, " ") + self.assertEqual(l1.prefix, " ") - def testGetSuffix(self): + def test_get_suffix(self): l1 = pytree.Leaf(100, "foo", prefix="a") l2 = pytree.Leaf(100, "bar", prefix="b") n1 = pytree.Node(1000, [l1, l2]) - self.assertEqual(l1.get_suffix(), l2.get_prefix()) + self.assertEqual(l1.get_suffix(), l2.prefix) self.assertEqual(l2.get_suffix(), "") self.assertEqual(n1.get_suffix(), "") l3 = pytree.Leaf(100, "bar", prefix="c") n2 = pytree.Node(1000, [n1, l3]) - self.assertEqual(n1.get_suffix(), l3.get_prefix()) + self.assertEqual(n1.get_suffix(), l3.prefix) self.assertEqual(l3.get_suffix(), "") self.assertEqual(n2.get_suffix(), "") - def testNodeEq(self): + def test_node_equality(self): n1 = pytree.Node(1000, ()) n2 = pytree.Node(1000, [], context=(" ", (1, 0))) self.assertEqual(n1, n2) n3 = pytree.Node(1001, ()) self.assertNotEqual(n1, n3) - def testNodeEqRecursive(self): + def test_node_recursive_equality(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "foo") n1 = pytree.Node(1000, [l1]) @@ -134,7 +148,7 @@ class TestNodes(support.TestCase): n3 = pytree.Node(1000, [l3]) self.assertNotEqual(n1, n3) - def testReplace(self): + def test_replace(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "+") l3 = pytree.Leaf(100, "bar") @@ -148,7 +162,7 @@ class TestNodes(support.TestCase): self.failUnless(isinstance(n1.children, list)) self.failUnless(n1.was_changed) - def testReplaceWithList(self): + def test_replace_with_list(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "+") l3 = pytree.Leaf(100, "bar") @@ -158,34 +172,30 @@ class TestNodes(support.TestCase): self.assertEqual(str(n1), "foo**bar") self.failUnless(isinstance(n1.children, list)) - def testPostOrder(self): + def test_post_order(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar") n1 = pytree.Node(1000, [l1, l2]) self.assertEqual(list(n1.post_order()), [l1, l2, n1]) - def testPreOrder(self): + def test_pre_order(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar") n1 = pytree.Node(1000, [l1, l2]) self.assertEqual(list(n1.pre_order()), [n1, l1, l2]) - def testChangedLeaf(self): + def test_changed(self): l1 = pytree.Leaf(100, "f") self.failIf(l1.was_changed) - l1.changed() self.failUnless(l1.was_changed) - def testChangedNode(self): l1 = pytree.Leaf(100, "f") n1 = pytree.Node(1000, [l1]) self.failIf(n1.was_changed) - n1.changed() self.failUnless(n1.was_changed) - def testChangedRecursive(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "+") l3 = pytree.Leaf(100, "bar") @@ -200,23 +210,23 @@ class TestNodes(support.TestCase): self.failUnless(n2.was_changed) self.failIf(l1.was_changed) - def testLeafConstructorPrefix(self): + def test_leaf_constructor_prefix(self): for prefix in ("xyz_", ""): l1 = pytree.Leaf(100, "self", prefix=prefix) self.failUnless(str(l1), prefix + "self") - self.assertEqual(l1.get_prefix(), prefix) + self.assertEqual(l1.prefix, prefix) - def testNodeConstructorPrefix(self): + def test_node_constructor_prefix(self): for prefix in ("xyz_", ""): l1 = pytree.Leaf(100, "self") l2 = pytree.Leaf(100, "foo", prefix="_") n1 = pytree.Node(1000, [l1, l2], prefix=prefix) self.failUnless(str(n1), prefix + "self_foo") - self.assertEqual(n1.get_prefix(), prefix) - self.assertEqual(l1.get_prefix(), prefix) - self.assertEqual(l2.get_prefix(), "_") + self.assertEqual(n1.prefix, prefix) + self.assertEqual(l1.prefix, prefix) + self.assertEqual(l2.prefix, "_") - def testRemove(self): + def test_remove(self): l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "foo") n1 = pytree.Node(1000, [l1, l2]) @@ -239,7 +249,7 @@ class TestNodes(support.TestCase): self.failUnless(n1.was_changed) self.failUnless(n2.was_changed) - def testRemoveParentless(self): + def test_remove_parentless(self): n1 = pytree.Node(1000, []) n1.remove() self.assertEqual(n1.parent, None) @@ -248,7 +258,7 @@ class TestNodes(support.TestCase): l1.remove() self.assertEqual(l1.parent, None) - def testNodeSetChild(self): + def test_node_set_child(self): l1 = pytree.Leaf(100, "foo") n1 = pytree.Node(1000, [l1]) @@ -269,7 +279,7 @@ class TestNodes(support.TestCase): # I don't care what it raises, so long as it's an exception self.assertRaises(Exception, n1.set_child, 0, list) - def testNodeInsertChild(self): + def test_node_insert_child(self): l1 = pytree.Leaf(100, "foo") n1 = pytree.Node(1000, [l1]) @@ -285,7 +295,7 @@ class TestNodes(support.TestCase): # I don't care what it raises, so long as it's an exception self.assertRaises(Exception, n1.insert_child, 0, list) - def testNodeAppendChild(self): + def test_node_append_child(self): n1 = pytree.Node(1000, []) l1 = pytree.Leaf(100, "foo") @@ -301,7 +311,7 @@ class TestNodes(support.TestCase): # I don't care what it raises, so long as it's an exception self.assertRaises(Exception, n1.append_child, list) - def testNodeNextSibling(self): + def test_node_next_sibling(self): n1 = pytree.Node(1000, []) n2 = pytree.Node(1000, []) p1 = pytree.Node(1000, [n1, n2]) @@ -310,7 +320,7 @@ class TestNodes(support.TestCase): self.assertEqual(n2.next_sibling, None) self.assertEqual(p1.next_sibling, None) - def testLeafNextSibling(self): + def test_leaf_next_sibling(self): l1 = pytree.Leaf(100, "a") l2 = pytree.Leaf(100, "b") p1 = pytree.Node(1000, [l1, l2]) @@ -319,7 +329,7 @@ class TestNodes(support.TestCase): self.assertEqual(l2.next_sibling, None) self.assertEqual(p1.next_sibling, None) - def testNodePrevSibling(self): + def test_node_prev_sibling(self): n1 = pytree.Node(1000, []) n2 = pytree.Node(1000, []) p1 = pytree.Node(1000, [n1, n2]) @@ -328,7 +338,7 @@ class TestNodes(support.TestCase): self.assertEqual(n1.prev_sibling, None) self.assertEqual(p1.prev_sibling, None) - def testLeafPrevSibling(self): + def test_leaf_prev_sibling(self): l1 = pytree.Leaf(100, "a") l2 = pytree.Leaf(100, "b") p1 = pytree.Node(1000, [l1, l2]) @@ -342,7 +352,7 @@ class TestPatterns(support.TestCase): """Unit tests for tree matching patterns.""" - def testBasicPatterns(self): + def test_basic_patterns(self): # Build a tree l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar") @@ -378,7 +388,7 @@ class TestPatterns(support.TestCase): self.assertFalse(pn.match(l2, results=r)) self.assertEqual(r, {}) - def testWildcardPatterns(self): + def test_wildcard(self): # Build a tree for testing l1 = pytree.Leaf(100, "foo") l2 = pytree.Leaf(100, "bar") @@ -409,7 +419,7 @@ class TestPatterns(support.TestCase): self.assert_(r["pl"] is l3) r = {} - def testGenerateMatches(self): + def test_generate_matches(self): la = pytree.Leaf(1, "a") lb = pytree.Leaf(1, "b") lc = pytree.Leaf(1, "c") @@ -439,7 +449,7 @@ class TestPatterns(support.TestCase): for c in "abcdef": self.assertEqual(r["p" + c], pytree.Leaf(1, c)) - def testHasKeyExample(self): + def test_has_key_example(self): pattern = pytree.NodePattern(331, (pytree.LeafPattern(7), pytree.WildcardPattern(name="args"), @@ -451,8 +461,3 @@ class TestPatterns(support.TestCase): r = {} self.assert_(pattern.match(node, r)) self.assertEqual(r["args"], [l2]) - - -if __name__ == "__main__": - import __main__ - support.run_all_tests(__main__) diff --git a/Lib/lib2to3/tests/test_util.py b/Lib/lib2to3/tests/test_util.py index 95b566a..1de7a83 100644 --- a/Lib/lib2to3/tests/test_util.py +++ b/Lib/lib2to3/tests/test_util.py @@ -1,6 +1,4 @@ -#!/usr/bin/env python2.5 """ Test suite for the code in fixes.util """ -# Author: Collin Winter # Testing imports from . import support @@ -552,8 +550,3 @@ class Test_touch_import(support.TestCase): node = parse('bar()') fixer_util.touch_import(None, "cgi", node) self.assertEqual(str(node), 'import cgi\nbar()\n\n') - - -if __name__ == "__main__": - import __main__ - support.run_all_tests(__main__) -- cgit v0.12